forked from kubeflow/training-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEP-2170: Create model and dataset initializers
Signed-off-by: Andrey Velichkevich <[email protected]>
- Loading branch information
1 parent
fd4d102
commit fe481b7
Showing
14 changed files
with
225 additions
and
2 deletions.
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/initiailizer_v2/dataset/requirements.txt . | ||
COPY sdk/python/kubeflow sdk/python/kubeflow | ||
COPY pkg/initiailizer_v2 pkg/initiailizer_v2 | ||
|
||
# Install the needed packages. | ||
RUN pip install -r requirements.txt | ||
|
||
ENTRYPOINT ["python", "-m", "pkg.initiailizer_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/initiailizer_v2/model/requirements.txt . | ||
COPY sdk/python/kubeflow sdk/python/kubeflow | ||
COPY pkg/initiailizer_v2 pkg/initiailizer_v2 | ||
|
||
# Install the needed packages. | ||
RUN pip install -r requirements.txt | ||
|
||
ENTRYPOINT ["python", "-m", "pkg.initiailizer_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,35 @@ | ||
import logging | ||
import os | ||
from urllib.parse import urlparse | ||
|
||
import pkg.initiailizer_v2.utils.utils as utils | ||
from pkg.initiailizer_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 | ||
|
||
logging.info(f"Storage URI: {storage_uri}") | ||
|
||
storage_uri_parsed = urlparse(storage_uri) | ||
|
||
match storage_uri_parsed.scheme: | ||
# TODO (andreyvelich): Implement more dataset providers. | ||
case utils.HF_SCHEME: | ||
hf = HuggingFace() | ||
hf.load_config() | ||
hf.download_dataset(storage_uri_parsed) | ||
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,8 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
|
||
# TODO (andreyvelich): This should be moved under Training V2 SDK. | ||
@dataclass | ||
class HuggingFaceDatasetConfig: | ||
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,40 @@ | ||
import logging | ||
from urllib.parse import ParseResult | ||
|
||
import huggingface_hub | ||
|
||
import pkg.initiailizer_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.initiailizer_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: | ||
|
||
def load_config(self): | ||
config_dict = utils.get_config_from_env(HuggingFaceDatasetConfig) | ||
logging.info(f"Config for HuggingFace dataset initiailizer: {config_dict}") | ||
self.config = HuggingFaceDatasetConfig(**config_dict) | ||
|
||
def download_dataset(self, storage_uri_parsed: ParseResult): | ||
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,37 @@ | ||
import logging | ||
import os | ||
from urllib.parse import urlparse | ||
|
||
import pkg.initiailizer_v2.utils.utils as utils | ||
from pkg.initiailizer_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 | ||
|
||
logging.info(f"Storage URI: {storage_uri}") | ||
|
||
storage_uri_parsed = urlparse(storage_uri) | ||
|
||
match storage_uri_parsed.scheme: | ||
# TODO (andreyvelich): Implement more model providers. | ||
case utils.HF_SCHEME: | ||
hf = HuggingFace() | ||
hf.load_config() | ||
hf.download_model(storage_uri_parsed) | ||
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: | ||
invalid: 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 ParseResult | ||
|
||
import huggingface_hub | ||
|
||
import pkg.initiailizer_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.initiailizer_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: | ||
|
||
def load_config(self): | ||
config_dict = utils.get_config_from_env(HuggingFaceModelInputConfig) | ||
logging.info(f"Config for HuggingFace model initiailizer: {config_dict}") | ||
self.config = HuggingFaceModelInputConfig(**config_dict) | ||
|
||
def download_model(self, storage_uri_parsed: ParseResult): | ||
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 verify these patterns for different models. | ||
huggingface_hub.snapshot_download( | ||
repo_id=model_uri, | ||
local_dir=constants.VOLUME_PATH_MODEL, | ||
allow_patterns=["*.json", "*.safetensors", "*.model"], | ||
ignore_patterns=["*.msgpack", "*.h5", "*.bin"], | ||
) | ||
|
||
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,16 @@ | ||
import os | ||
from dataclasses import fields | ||
from typing import Dict | ||
|
||
STORAGE_URI_ENV = "STORAGE_URI" | ||
HF_SCHEME = "hf" | ||
|
||
|
||
# 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 |