-
Notifications
You must be signed in to change notification settings - Fork 705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KEP-2170: Create model and dataset initializers #2303
Merged
google-oss-prow
merged 6 commits into
kubeflow:master
from
andreyvelich:issue-2210-storage-initializer
Oct 27, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe481b7
KEP-2170: Create model and dataset initializers
andreyvelich 59d3224
Add abstract classes
andreyvelich 45c860c
Add storage URI to config
andreyvelich 468500d
Update .gitignore
andreyvelich 5c39812
Fix the misspelling for initializer
andreyvelich 2e8a518
Add .pt and .pth to ignore_patterns
andreyvelich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM python:3.11-alpine | ||
|
||
WORKDIR /workspace | ||
|
||
# Copy the required Python modules. | ||
COPY cmd/initializer_v2/dataset/requirements.txt . | ||
COPY sdk/python/kubeflow sdk/python/kubeflow | ||
COPY pkg/initializer_v2 pkg/initializer_v2 | ||
|
||
# Install the needed packages. | ||
RUN pip install -r requirements.txt | ||
|
||
ENTRYPOINT ["python", "-m", "pkg.initializer_v2.dataset"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
huggingface_hub==0.23.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM python:3.11-alpine | ||
|
||
WORKDIR /workspace | ||
|
||
# Copy the required Python modules. | ||
COPY cmd/initializer_v2/model/requirements.txt . | ||
COPY sdk/python/kubeflow sdk/python/kubeflow | ||
COPY pkg/initializer_v2 pkg/initializer_v2 | ||
|
||
# Install the needed packages. | ||
RUN pip install -r requirements.txt | ||
|
||
ENTRYPOINT ["python", "-m", "pkg.initializer_v2.model"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
huggingface_hub==0.23.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import logging | ||
import os | ||
from urllib.parse import urlparse | ||
|
||
import pkg.initializer_v2.utils.utils as utils | ||
from pkg.initializer_v2.dataset.huggingface import HuggingFace | ||
|
||
logging.basicConfig( | ||
format="%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s", | ||
datefmt="%Y-%m-%dT%H:%M:%SZ", | ||
level=logging.INFO, | ||
) | ||
|
||
if __name__ == "__main__": | ||
logging.info("Starting dataset initialization") | ||
|
||
try: | ||
storage_uri = os.environ[utils.STORAGE_URI_ENV] | ||
except Exception as e: | ||
logging.error("STORAGE_URI env variable must be set.") | ||
raise e | ||
|
||
match urlparse(storage_uri).scheme: | ||
# TODO (andreyvelich): Implement more dataset providers. | ||
case utils.HF_SCHEME: | ||
hf = HuggingFace() | ||
hf.load_config() | ||
hf.download_dataset() | ||
case _: | ||
logging.error("STORAGE_URI must have the valid dataset provider") | ||
raise Exception |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
|
||
# TODO (andreyvelich): This should be moved under Training V2 SDK. | ||
@dataclass | ||
class HuggingFaceDatasetConfig: | ||
storage_uri: str | ||
access_token: Optional[str] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import logging | ||
from urllib.parse import urlparse | ||
|
||
import huggingface_hub | ||
|
||
import pkg.initializer_v2.utils.utils as utils | ||
|
||
# TODO (andreyvelich): This should be moved to SDK V2 constants. | ||
import sdk.python.kubeflow.storage_initializer.constants as constants | ||
from pkg.initializer_v2.dataset.config import HuggingFaceDatasetConfig | ||
|
||
logging.basicConfig( | ||
format="%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s", | ||
datefmt="%Y-%m-%dT%H:%M:%SZ", | ||
level=logging.INFO, | ||
) | ||
|
||
|
||
class HuggingFace(utils.DatasetProvider): | ||
|
||
def load_config(self): | ||
config_dict = utils.get_config_from_env(HuggingFaceDatasetConfig) | ||
logging.info(f"Config for HuggingFace dataset initializer: {config_dict}") | ||
self.config = HuggingFaceDatasetConfig(**config_dict) | ||
|
||
def download_dataset(self): | ||
storage_uri_parsed = urlparse(self.config.storage_uri) | ||
dataset_uri = storage_uri_parsed.netloc + storage_uri_parsed.path | ||
|
||
logging.info(f"Downloading dataset: {dataset_uri}") | ||
logging.info("-" * 40) | ||
|
||
if self.config.access_token: | ||
huggingface_hub.login(self.config.access_token) | ||
|
||
huggingface_hub.snapshot_download( | ||
repo_id=dataset_uri, | ||
repo_type="dataset", | ||
local_dir=constants.VOLUME_PATH_DATASET, | ||
) | ||
|
||
logging.info("Dataset has been downloaded") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import logging | ||
import os | ||
from urllib.parse import urlparse | ||
|
||
import pkg.initializer_v2.utils.utils as utils | ||
from pkg.initializer_v2.model.huggingface import HuggingFace | ||
|
||
logging.basicConfig( | ||
format="%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s", | ||
datefmt="%Y-%m-%dT%H:%M:%SZ", | ||
level=logging.INFO, | ||
) | ||
|
||
if __name__ == "__main__": | ||
logging.info("Starting pre-trained model initialization") | ||
|
||
try: | ||
storage_uri = os.environ[utils.STORAGE_URI_ENV] | ||
except Exception as e: | ||
logging.error("STORAGE_URI env variable must be set.") | ||
raise e | ||
|
||
match urlparse(storage_uri).scheme: | ||
# TODO (andreyvelich): Implement more model providers. | ||
case utils.HF_SCHEME: | ||
hf = HuggingFace() | ||
hf.load_config() | ||
hf.download_model() | ||
case _: | ||
logging.error( | ||
f"STORAGE_URI must have the valid model provider. STORAGE_URI: {storage_uri}" | ||
) | ||
raise Exception |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
|
||
# TODO (andreyvelich): This should be moved under Training V2 SDK. | ||
@dataclass | ||
class HuggingFaceModelInputConfig: | ||
storage_uri: str | ||
access_token: Optional[str] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import logging | ||
from urllib.parse import urlparse | ||
|
||
import huggingface_hub | ||
|
||
import pkg.initializer_v2.utils.utils as utils | ||
|
||
# TODO (andreyvelich): This should be moved to SDK V2 constants. | ||
import sdk.python.kubeflow.storage_initializer.constants as constants | ||
from pkg.initializer_v2.model.config import HuggingFaceModelInputConfig | ||
|
||
logging.basicConfig( | ||
format="%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s", | ||
datefmt="%Y-%m-%dT%H:%M:%SZ", | ||
level=logging.INFO, | ||
) | ||
|
||
|
||
class HuggingFace(utils.ModelProvider): | ||
|
||
def load_config(self): | ||
config_dict = utils.get_config_from_env(HuggingFaceModelInputConfig) | ||
logging.info(f"Config for HuggingFace model initializer: {config_dict}") | ||
self.config = HuggingFaceModelInputConfig(**config_dict) | ||
|
||
def download_model(self): | ||
storage_uri_parsed = urlparse(self.config.storage_uri) | ||
model_uri = storage_uri_parsed.netloc + storage_uri_parsed.path | ||
|
||
logging.info(f"Downloading model: {model_uri}") | ||
logging.info("-" * 40) | ||
|
||
if self.config.access_token: | ||
huggingface_hub.login(self.config.access_token) | ||
|
||
# TODO (andreyvelich): We should consider to follow vLLM approach with allow patterns. | ||
# Ref: https://github.com/kubeflow/training-operator/pull/2303#discussion_r1815913663 | ||
# TODO (andreyvelich): We should update patterns for Mistral model | ||
# Ref: https://github.com/kubeflow/training-operator/pull/2303#discussion_r1815914270 | ||
huggingface_hub.snapshot_download( | ||
repo_id=model_uri, | ||
local_dir=constants.VOLUME_PATH_MODEL, | ||
allow_patterns=["*.json", "*.safetensors", "*.model"], | ||
ignore_patterns=["*.msgpack", "*.h5", "*.bin", ".pt", ".pth"], | ||
) | ||
|
||
logging.info("Model has been downloaded") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
from abc import ABC, abstractmethod | ||
from dataclasses import fields | ||
from typing import Dict | ||
|
||
STORAGE_URI_ENV = "STORAGE_URI" | ||
HF_SCHEME = "hf" | ||
|
||
|
||
class ModelProvider(ABC): | ||
@abstractmethod | ||
def load_config(self): | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def download_model(self): | ||
raise NotImplementedError() | ||
|
||
|
||
class DatasetProvider(ABC): | ||
@abstractmethod | ||
def load_config(self): | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def download_dataset(self): | ||
raise NotImplementedError() | ||
|
||
|
||
# Get DataClass config from the environment variables. | ||
# Env names must be equal to the DataClass parameters. | ||
def get_config_from_env(config) -> Dict[str, str]: | ||
config_from_env = {} | ||
for field in fields(config): | ||
config_from_env[field.name] = os.getenv(field.name.upper()) | ||
|
||
return config_from_env |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andreyvelich Could you use the Debian image since the Alpine has a performance penalty due to with musl libc?
Python still depends on the C codes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, let me create an issue