Skip to content
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 11 commits into from
Nov 3, 2023
Merged

Model archiver api #2751

merged 11 commits into from
Nov 3, 2023

Conversation

GeeCastro
Copy link
Collaborator

Description

Adds a model archiver interface to archive models programmatically with a config class ModelArchiverConfig and an archiver class ModelArchiver. Simply wraps the existing CLI feature.

Fixes #2695

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Feature/Issue validation/testing

As it's only wrapping the actual feature there's only a unit test very similar to the existing packaging test.

  • Test Model archiver
model_archiver/tests/unit_tests/test_model_archiver.py::TestModelArchiver::test_gen_model_archive PASSED                                                     [  3%]
  • Test Model archiver config from argparse
model_archiver/tests/unit_tests/test_model_archiver.py::TestModelArchiver::test_model_archiver_config_from_args PASSED                                       [  6%]

Checklist:

  • Did you have fun?
  • Have you added tests that prove your fix is effective or that this feature works?
  • Has code been commented, particularly in hard-to-understand areas? Not yet
  • Have you made corresponding changes to the documentation? Not yet

@msaroufim
Copy link
Member

Very nice change, wanna create a new page something like documentation/python_api.md so I can evangelize this change to others. I'd be excited about doing this as well for torchserve --start and eventually all our REST APIs as well

@codecov
Copy link

codecov bot commented Oct 30, 2023

Codecov Report

Merging #2751 (5d702e2) into master (ada7856) will increase coverage by 0.22%.
Report is 1 commits behind head on master.
The diff coverage is 100.00%.

❗ Current head 5d702e2 differs from pull request most recent head 3e3609c. Consider uploading reports for the commit 3e3609c to get more accurate results

@@            Coverage Diff             @@
##           master    #2751      +/-   ##
==========================================
+ Coverage   72.44%   72.66%   +0.22%     
==========================================
  Files          85       87       +2     
  Lines        3963     3995      +32     
  Branches       58       60       +2     
==========================================
+ Hits         2871     2903      +32     
  Misses       1088     1088              
  Partials        4        4              
Files Coverage Δ
model-archiver/model_archiver/model_archiver.py 100.00% <100.00%> (ø)
...l-archiver/model_archiver/model_archiver_config.py 100.00% <100.00%> (ø)
model-archiver/model_archiver/model_packaging.py 100.00% <100.00%> (ø)
...l-archiver/model_archiver/model_packaging_utils.py 92.35% <100.00%> (+0.04%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

patches.export_utils.validate_inputs.assert_called()
patches.export_utils.archive.assert_called()

def test_export_model_method_tar(self, patches):
self.args.update(archive_format="tar")
self.config.archive_format = "tar"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value "tar" doesn't align with the list of choices originally accepted in the CLI https://github.com/pytorch/serve/blob/master/model-archiver/model_archiver/arg_parser.py#L102. Should we add "tar" to the literal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes let's add

Comment on lines -38 to -40
convert=False,
version=version,
source_vocab=source_vocab,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neither convert nor source_vocab are unsed in model archiver. Not here anyway, could this break a behaviour?

Copy link
Member

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

Copy link
Collaborator

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.

(serve) ubuntu@ip-172-31-55-226:~/serve$ grep -r convert model-archiver/
model-archiver/model_archiver/model_packaging_utils.py:            logging.error("Failed to convert %s to the model-archive.", model_name)
Binary file model-archiver/model_archiver/tests/unit_tests/__pycache__/test_model_packaging.cpython-310-pytest-7.3.1.pyc matches
model-archiver/model_archiver/tests/unit_tests/test_model_packaging.py:        convert=False,
model-archiver/build/lib/model_archiver/model_packaging_utils.py:            logging.error("Failed to convert %s to the model-archive.", model_name)
(serve) ubuntu@ip-172-31-55-226:~/serve$ grep -r source_vocab model-archiver/
Binary file model-archiver/model_archiver/tests/integ_tests/__pycache__/test_integration_model_archiver.cpython-310-pytest-7.3.1.pyc matches
model-archiver/model_archiver/tests/integ_tests/test_integration_model_archiver.py:        assert os.path.join(prefix, "source_vocab.pt") in file_list
model-archiver/model_archiver/tests/integ_tests/default_handler_configuration.json:    "extra-files": "model_archiver/tests/integ_tests/resources/regular_model/test_index_to_name.json,model_archiver/tests/integ_tests/resources/regular_model/source_vocab.pt",
Binary file model-archiver/model_archiver/tests/unit_tests/__pycache__/test_model_packaging.cpython-310-pytest-7.3.1.pyc matches
model-archiver/model_archiver/tests/unit_tests/test_model_packaging.py:    source_vocab = None
model-archiver/model_archiver/tests/unit_tests/test_model_packaging.py:        source_vocab=source_vocab,

@msaroufim msaroufim marked this pull request as ready for review November 1, 2023 00:20
@msaroufim
Copy link
Member

Just missing docs but nice to see CI green

Copy link
Collaborator

@mreso mreso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some minor comments. LGTM otherwise

@staticmethod
def from_args(args: Namespace) -> "ModelArchiverConfig":
config = ModelArchiverConfig(
model_name=args.model_name,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameters and fields between args and config should always be aligned. Would be good to make the argument transfer automatic to remove one point that we need to touch in case we alter the parameters. Something like this:

from dataclasses import fields
# ...
     @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

)
config = ModelArchiverConfig.from_args(args)

config == self.config
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to ass assert here so the test fails if unequal

Comment on lines -38 to -40
convert=False,
version=version,
source_vocab=source_vocab,
Copy link
Collaborator

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.

(serve) ubuntu@ip-172-31-55-226:~/serve$ grep -r convert model-archiver/
model-archiver/model_archiver/model_packaging_utils.py:            logging.error("Failed to convert %s to the model-archive.", model_name)
Binary file model-archiver/model_archiver/tests/unit_tests/__pycache__/test_model_packaging.cpython-310-pytest-7.3.1.pyc matches
model-archiver/model_archiver/tests/unit_tests/test_model_packaging.py:        convert=False,
model-archiver/build/lib/model_archiver/model_packaging_utils.py:            logging.error("Failed to convert %s to the model-archive.", model_name)
(serve) ubuntu@ip-172-31-55-226:~/serve$ grep -r source_vocab model-archiver/
Binary file model-archiver/model_archiver/tests/integ_tests/__pycache__/test_integration_model_archiver.cpython-310-pytest-7.3.1.pyc matches
model-archiver/model_archiver/tests/integ_tests/test_integration_model_archiver.py:        assert os.path.join(prefix, "source_vocab.pt") in file_list
model-archiver/model_archiver/tests/integ_tests/default_handler_configuration.json:    "extra-files": "model_archiver/tests/integ_tests/resources/regular_model/test_index_to_name.json,model_archiver/tests/integ_tests/resources/regular_model/source_vocab.pt",
Binary file model-archiver/model_archiver/tests/unit_tests/__pycache__/test_model_packaging.cpython-310-pytest-7.3.1.pyc matches
model-archiver/model_archiver/tests/unit_tests/test_model_packaging.py:    source_vocab = None
model-archiver/model_archiver/tests/unit_tests/test_model_packaging.py:        source_vocab=source_vocab,

Copy link
Collaborator

@mreso mreso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quickly addressed the comments myself as I want to use this feature in master :-) ; thanks for contributing @Chichilele!

@msaroufim msaroufim self-requested a review November 3, 2023 19:14
@msaroufim msaroufim added this pull request to the merge queue Nov 3, 2023
Merged via the queue into pytorch:master with commit 3193748 Nov 3, 2023
13 checks passed
@GeeCastro
Copy link
Collaborator Author

Happy this is useful. I’m on holiday with limited access to a laptop so wouldn’t have been able to address this until next week.

@GeeCastro GeeCastro deleted the model-archiver-api branch November 4, 2023 06:50
@mreso
Copy link
Collaborator

mreso commented Nov 4, 2023 via email

@chauhang chauhang added this to the v0.10.0 milestone Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Model archiver API
4 participants