-
Notifications
You must be signed in to change notification settings - Fork 863
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
Model archiver api #2751
Merged
Merged
Model archiver api #2751
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
322b821
add archiver config
GeeCastro dc44ed4
add model archiver
GeeCastro f79b4d4
add to init
GeeCastro b60b0a2
integrated ModelArchiverConfig in existing scripts
GeeCastro d45b3b1
remove sys.exit mock
GeeCastro 182621d
add ModelArchiver unit test
GeeCastro 4db275c
fix tests
GeeCastro 2d4ac04
ModelArchiverConfig to dataclass
GeeCastro 3e3609c
fix regression tests
GeeCastro 27866ac
Simplify args->config conversion; clean up test
mreso 22eb9c8
Merge branch 'master' into model-archiver-api
mreso 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
|
||
|
||
""" | ||
This module does the following: | ||
Exports the model folder to generate a Model Archive file out of it in .mar format | ||
""" | ||
from . import version | ||
|
||
__version__ = version.__version__ | ||
|
||
from .model_archiver import ModelArchiver | ||
from .model_archiver_config import ModelArchiverConfig |
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,17 @@ | ||
""" | ||
Helper class to generate a model archive file | ||
""" | ||
|
||
from model_archiver.model_archiver_config import ModelArchiverConfig | ||
from model_archiver.model_packaging import generate_model_archive | ||
|
||
|
||
class ModelArchiver: | ||
@staticmethod | ||
def generate_model_archive(config: ModelArchiverConfig) -> None: | ||
""" | ||
Generate a model archive file | ||
:param config: Model Archiver Config object | ||
:return: | ||
""" | ||
generate_model_archive(config) |
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,28 @@ | ||
import os | ||
from argparse import Namespace | ||
from dataclasses import dataclass, fields | ||
from typing import Literal, Optional | ||
|
||
from model_archiver.manifest_components.manifest import RuntimeType | ||
|
||
|
||
@dataclass | ||
class ModelArchiverConfig: | ||
model_name: str | ||
handler: str | ||
version: str | ||
serialized_file: Optional[str] = None | ||
model_file: Optional[str] = None | ||
extra_files: Optional[str] = None | ||
runtime: str = RuntimeType.PYTHON.value | ||
export_path: str = os.getcwd() | ||
archive_format: Literal["default", "tgz", "no-archive"] = "default" | ||
force: bool = False | ||
requirements_file: Optional[str] = None | ||
config_file: Optional[str] = None | ||
|
||
@classmethod | ||
def from_args(cls, args: Namespace) -> "ModelArchiverConfig": | ||
params = {field.name: getattr(args, field.name) for field in fields(cls)} | ||
config = cls(**params) | ||
return config |
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
68 changes: 68 additions & 0 deletions
68
model-archiver/model_archiver/tests/unit_tests/test_model_archiver.py
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,68 @@ | ||
from argparse import Namespace | ||
from collections import namedtuple | ||
|
||
import pytest | ||
from model_archiver import ModelArchiver, ModelArchiverConfig | ||
from model_archiver.manifest_components.manifest import RuntimeType | ||
|
||
|
||
# noinspection PyClassHasNoInit | ||
class TestModelArchiver: | ||
model_name = "my-model" | ||
model_file = "my-model/" | ||
serialized_file = "my-model/" | ||
handler = "a.py::my-awesome-func" | ||
export_path = "/Users/dummyUser/" | ||
version = "1.0" | ||
requirements_file = "requirements.txt" | ||
config_file = None | ||
|
||
config = ModelArchiverConfig( | ||
model_name=model_name, | ||
handler=handler, | ||
runtime=RuntimeType.PYTHON.value, | ||
model_file=model_file, | ||
serialized_file=serialized_file, | ||
extra_files=None, | ||
export_path=export_path, | ||
force=False, | ||
archive_format="default", | ||
version=version, | ||
requirements_file=requirements_file, | ||
config_file=None, | ||
) | ||
|
||
@pytest.fixture() | ||
def patches(self, mocker): | ||
Patches = namedtuple("Patches", ["arg_parse", "export_utils", "export_method"]) | ||
patches = Patches( | ||
mocker.patch("model_archiver.arg_parser.ArgParser"), | ||
mocker.patch("model_archiver.model_packaging.ModelExportUtils"), | ||
mocker.patch("model_archiver.model_packaging.package_model"), | ||
) | ||
mocker.patch("shutil.rmtree") | ||
|
||
return patches | ||
|
||
def test_gen_model_archive(self, patches): | ||
ModelArchiver.generate_model_archive(self.config) | ||
patches.export_method.assert_called() | ||
|
||
def test_model_archiver_config_from_args(self): | ||
args = Namespace( | ||
model_name=self.model_name, | ||
handler=self.handler, | ||
runtime=RuntimeType.PYTHON.value, | ||
model_file=self.model_file, | ||
serialized_file=self.serialized_file, | ||
extra_files=None, | ||
export_path=self.export_path, | ||
force=False, | ||
archive_format="default", | ||
version=self.version, | ||
requirements_file=self.requirements_file, | ||
config_file=None, | ||
) | ||
config = ModelArchiverConfig.from_args(args) | ||
|
||
assert config == self.config |
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
Oops, something went wrong.
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.
neither
convert
norsource_vocab
are unsed in model archiver. Not here anyway, could this break a behaviour?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.
Let's keep them for now, this might be breaking something we're not testing
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.
I think its okay to remove them. They are not part of the namespace created in argparse and only show up in this test. Presumably a relicts from before extra_files.