-
Notifications
You must be signed in to change notification settings - Fork 17
Update to Model Hub integration #502
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
Open
r-sarma
wants to merge
21
commits into
main
Choose a base branch
from
modelhub-in-trainer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
1d70595
adding model hub as feature
r-sarma 80663cb
fix linting errors
r-sarma 77c8919
editing build of manifest
r-sarma 16344fa
removing unused import
r-sarma 60e8cc9
adding model hub pulling API
r-sarma a864dae
reconfiguring pulling API download
r-sarma 6cb6b5f
fix linting errors
r-sarma 38bec8e
adding tutorial, documentation and some refactoring
r-sarma dc079c1
fix linting error and mlflow integration test
r-sarma 5ad8856
fix failing integration tests
r-sarma bc7f56c
Trigger Read the Docs rebuild
r-sarma 248970d
Fix MLflow filesystem backend (#505)
matbun 1905080
Trigger Read the Docs rebuild
r-sarma 731cb03
incorporating matbun comments
r-sarma cf66202
incorporating matbun comments
r-sarma 825cfba
addressing matbun and okrochak PR comments
r-sarma 7d56d1d
fix paths
r-sarma 8b856f1
changing weights_only argument for two loaders
r-sarma 4fa8039
rolling back changes to model loader
r-sarma a5fe612
debugging integration test failure
r-sarma e9bb520
moving model-hub related functions from src cli
r-sarma 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 hidden or 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 hidden or 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,103 @@ | ||
| Accessing models from the RI-SCALE Model Hub | ||
| ============================================ | ||
|
|
||
| **Author(s)**: Rakesh Sarma (FZJ) | ||
|
|
||
| Once a ML model has been trained, it is often needed to be shared with collaborators, or to | ||
| publish it in open repositories. itwinai integrates with the `RI-SCALE Model Hub | ||
| <https://modelhub.riscale.eu>`_ to support users with this functionality which allows pushing | ||
| a trained checkpoint to the Model Hub, and pulling a checkpoint to run inference on it. | ||
|
|
||
| Pushing a model | ||
| --------------- | ||
|
|
||
| Pushing is handled automatically by :class:`~itwinai.torch.trainer.TorchTrainer` whenever | ||
| Model Hub support is enabled in its configuration. On every checkpoint save | ||
| (:meth:`~itwinai.torch.trainer.TorchTrainer.save_checkpoint`), the checkpoint directory -- | ||
| containing ``model.pt`` (the model's raw ``state_dict``), ``state.pt`` (optimizer/scheduler/ | ||
| epoch state), and ``config.yaml`` -- is handed to | ||
| :class:`~itwinai.torch.model_hub.feature.ModelHubFeature`, which: | ||
|
|
||
| 1. Writes a ``manifest.yaml`` into the checkpoint directory via | ||
| :func:`~itwinai.torch.model_hub.manifest.write_manifest`, merging user-supplied fields | ||
| with sensible defaults. At minimum, ``id`` and ``name`` must be provided. | ||
| 2. Uploads the checkpoint directory using the configured backend at the end of all epochs. | ||
| Backends implement :class:`~itwinai.torch.model_hub.backends.base.BaseBackend` and are | ||
| selected by name via :func:`~itwinai.torch.model_hub.backends.get_backend`. Currently the | ||
| only backend is :class:`~itwinai.torch.model_hub.backends.itwinai_hub.AIModelHubBackend`. | ||
| The abstraction is to enable future backends (e.g. HuggingFace). | ||
|
|
||
| The timing of the upload is controlled by a ``mode`` setting: | ||
|
|
||
| - ``online``: upload immediately, regardless of connectivity. | ||
| - ``auto``: upload if internet is available; otherwise print the checkpoint's local | ||
| location and skip the upload. | ||
| - ``deferred``: never upload automatically; the checkpoint is left ready to be pushed | ||
| manually later. | ||
|
|
||
| .. admonition:: Example Model Hub push configuration | ||
|
|
||
| .. code-block:: yaml | ||
|
|
||
| model_hub: | ||
| enabled: true | ||
| backend: ai-model-hub | ||
| mode: online | ||
| manifest: | ||
| id: checkpoint-example | ||
| name: My Model | ||
| published: true | ||
|
|
||
| The final `published: true` ensures that the pushed model is readily visible to all users | ||
| on the AI Model Hub. | ||
|
|
||
| Pulling a model | ||
| --------------- | ||
|
|
||
| Pulling is handled by :class:`~itwinai.torch.inference.ModelHubModelLoader`, an | ||
| implementation of :class:`~itwinai.serialization.ModelLoader`. Like any other | ||
| ``ModelLoader``, it can be used wherever a model loader is expected -- most commonly as the | ||
| ``model`` argument of :class:`~itwinai.torch.inference.TorchPredictor`. | ||
|
|
||
| Unlike pushing, the Model Hub's file API has no endpoint to download a whole model folder | ||
| at once: files are retrieved one at a time, by exact path | ||
| (``GET /artifacts/{model_id}/files/{file_path}``). To spare users from needing to know that | ||
| exact path, ``ModelHubModelLoader`` supports two modes: | ||
|
|
||
| - If ``file_path`` is provided explicitly, that file is downloaded directly. | ||
| - If ``file_path`` is omitted, itwinai lists the model's files | ||
| (:func:`~itwinai.torch.model_hub.download.list_files`) and locates | ||
| ``root/<checkpoint_dir_name>/model.pt`` automatically | ||
| (:func:`~itwinai.torch.model_hub.download.discover_weights_file`), matching the layout | ||
| produced by :meth:`~itwinai.torch.trainer.TorchTrainer.save_checkpoint`. | ||
| ``discover_weights_file`` only looks for a top-level ``root/`` entry; it does not | ||
| inspect or otherwise handle any other top-level entries the Hub may contain. | ||
|
|
||
| .. admonition:: Example Model Hub pull configuration | ||
|
|
||
| .. code-block:: yaml | ||
|
|
||
| predictor: | ||
| _target_: itwinai.torch.inference.TorchPredictor | ||
| config: {} | ||
| model: | ||
| _target_: itwinai.torch.inference.ModelHubModelLoader | ||
| model_id: checkpoint-example | ||
| model_class: my_module.MyModel | ||
|
|
||
| .. important:: | ||
| Model Hub checkpoints store a raw ``state_dict`` -- just tensors, with no architecture | ||
| information -- following the same convention used by | ||
| :meth:`~itwinai.torch.trainer.TorchTrainer.save_checkpoint`. This means ``model_class`` | ||
| is **always required** when pulling: it must be the exact :class:`~torch.nn.Module` | ||
| subclass used at training time. If the original training script is not available, the | ||
| downloaded ``state_dict``'s keys and tensor shapes can be inspected directly to | ||
| reconstruct a matching class by hand. | ||
|
|
||
| Connectivity | ||
| ------------ | ||
|
|
||
| Both pushing (in ``auto`` mode) and pulling rely on the same connectivity check, | ||
| :func:`~itwinai.torch.model_hub.utils.has_internet_connection`. Pulling always requires | ||
| internet access -- unlike pushing, there is no offline or deferred mode for pulling, since | ||
| there is no local fallback artifact to use in its place. |
This file contains hidden or 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 hidden or 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 @@ | ||
| Pushing and pulling models with the RI-SCALE Model Hub | ||
| ====================================================== | ||
| .. include:: ../../../tutorials/model-hub/torch-tutorial-model-hub/README.md | ||
| :parser: myst_parser.sphinx_ | ||
| :start-line: 4 | ||
|
|
||
| Run the training pipeline (trains the model and, as per ``model_hub`` in ``config.yaml``, | ||
| pushes the best checkpoint to the Model Hub): | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| itwinai exec-pipeline +pipe-key training_pipeline | ||
|
|
||
| Then run the inference pipeline (pulls that same checkpoint and runs inference on it): | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| itwinai exec-pipeline +pipe-key inference_pipeline | ||
|
|
||
| config.yaml | ||
| +++++++++++ | ||
| .. literalinclude:: ../../../tutorials/model-hub/torch-tutorial-model-hub/config.yaml | ||
| :language: yaml | ||
|
|
||
| data.py | ||
| +++++++ | ||
| .. literalinclude:: ../../../tutorials/model-hub/torch-tutorial-model-hub/data.py | ||
| :language: python | ||
|
|
||
| synthetic_data.py | ||
| +++++++++++++++++ | ||
| .. literalinclude:: ../../../tutorials/model-hub/torch-tutorial-model-hub/synthetic_data.py | ||
| :language: python |
This file contains hidden or 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 hidden or 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
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.
GitHub preview shows me a lot of text being underlined, which I think is not so good for readability.