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
Return Model from log_model #5230
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8434e2f
return Model from log_model
liangz1 550e293
add ModelInfo namedtuple
liangz1 e8ca131
refactor code
liangz1 c347165
merge master
liangz1 5965f76
update all flavors return type and docstring
liangz1 7671fb2
add test to test_model.py
liangz1 cce93bb
add tests for each flavor
liangz1 5045a8b
fix test
liangz1 3a40ec8
add namedtuple docs, remove input_schema and output_schema from Model…
liangz1 00bf853
use better sphinx syntax
liangz1 2c3bd70
fix ModelSignature import issue
liangz1 83bb305
fix empty signature
liangz1 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
|
@@ -6,3 +6,5 @@ mlflow.models | |
:undoc-members: | ||
:show-inheritance: | ||
|
||
.. autoclass:: mlflow.models.model.ModelInfo | ||
:members: |
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
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
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
import os | ||
import uuid | ||
|
||
from typing import Any, Dict, Optional, Union, Callable | ||
from typing import Any, Dict, Optional, Union, Callable, NamedTuple | ||
|
||
import mlflow | ||
from mlflow.exceptions import MlflowException | ||
|
@@ -22,12 +22,60 @@ | |
"Logging model metadata to the tracking server has failed, possibly due older " | ||
"server version. The model artifacts have been logged successfully under %s. " | ||
"In addition to exporting model artifacts, MLflow clients 1.7.0 and above " | ||
"attempt to record model metadata to the tracking store. If logging to a " | ||
"mlflow server via REST, consider upgrading the server version to MLflow " | ||
"attempt to record model metadata to the tracking store. If logging to a " | ||
"mlflow server via REST, consider upgrading the server version to MLflow " | ||
"1.7.0 or above." | ||
) | ||
|
||
|
||
class ModelInfo(NamedTuple): | ||
""" | ||
The metadata of a logged MLflow Model. | ||
""" | ||
|
||
#: Run relative path identifying the logged model. | ||
artifact_path: str | ||
|
||
#: A dictionary mapping the flavor name to how to serve the model as that flavor. For example: | ||
#: | ||
#: .. code-block:: python | ||
#: | ||
#: { | ||
#: "python_function": { | ||
#: "model_path": "model.pkl", | ||
#: "loader_module": "mlflow.sklearn", | ||
#: "python_version": "3.8.10", | ||
#: "env": "conda.yaml", | ||
#: }, | ||
#: "sklearn": { | ||
#: "pickled_model": "model.pkl", | ||
#: "sklearn_version": "0.24.1", | ||
#: "serialization_format": "cloudpickle", | ||
#: }, | ||
#: } | ||
flavors: Dict[str, Any] | ||
|
||
#: The ``model_uri`` of the logged model in the format ``'runs:/<run_id>/<artifact_path>'``. | ||
model_uri: str | ||
|
||
#: The ``model_uuid`` of the logged model, e.g., ``'39ca11813cfc46b09ab83972740b80ca'``. | ||
model_uuid: str | ||
|
||
#: The ``run_id`` associated with the logged model, e.g., ``'8ede7df408dd42ed9fc39019ef7df309'`` | ||
run_id: str | ||
|
||
#: A dictionary that contains the metadata of the saved input example, e.g., | ||
#: ``{"artifact_path": "input_example.json", "type": "dataframe", "pandas_orient": "split"}``. | ||
saved_input_example_info: Optional[Dict[str, Any]] | ||
|
||
#: A dictionary that describes the model input and output generated by | ||
# :py:meth:`ModelSignature.to_dict() <mlflow.models.ModelSignature.to_dict>`. | ||
signature_dict: Optional[Dict[str, Any]] | ||
|
||
#: The UTC time that the logged model is created, e.g., ``'2022-01-12 05:17:31.634689'``. | ||
utc_time_created: str | ||
|
||
|
||
class Model: | ||
""" | ||
An MLflow Model that can support multiple model flavors. Provides APIs for implementing | ||
|
@@ -102,13 +150,33 @@ def signature(self, value): | |
|
||
@property | ||
def saved_input_example_info(self) -> Optional[Dict[str, Any]]: | ||
""" | ||
A dictionary that contains the metadata of the saved input example, e.g., | ||
``{"artifact_path": "input_example.json", "type": "dataframe", "pandas_orient": "split"}``. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve the |
||
return self._saved_input_example_info | ||
|
||
@saved_input_example_info.setter | ||
def saved_input_example_info(self, value: Dict[str, Any]): | ||
# pylint: disable=attribute-defined-outside-init | ||
self._saved_input_example_info = value | ||
|
||
def get_model_info(self): | ||
""" | ||
Create a :py:class:`ModelInfo <mlflow.models.model.ModelInfo>` instance that contains the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @harupy Also,
works while the following does not:
Thanks for helping! |
||
model metadata. | ||
""" | ||
return ModelInfo( | ||
artifact_path=self.artifact_path, | ||
flavors=self.flavors, | ||
model_uri="runs:/{}/{}".format(self.run_id, self.artifact_path), | ||
model_uuid=self.model_uuid, | ||
run_id=self.run_id, | ||
saved_input_example_info=self.saved_input_example_info, | ||
signature_dict=self.signature.to_dict() if self.signature else None, | ||
utc_time_created=self.utc_time_created, | ||
) | ||
|
||
def to_dict(self): | ||
"""Serialize the model to a dictionary.""" | ||
res = {k: v for k, v in self.__dict__.items() if not k.startswith("_")} | ||
|
@@ -203,6 +271,9 @@ def log( | |
waits for five minutes. Specify 0 or None to skip waiting. | ||
|
||
:param kwargs: Extra args passed to the model flavor. | ||
|
||
:return: A :py:class:`ModelInfo <mlflow.models.model.ModelInfo>` instance that contains the | ||
metadata of the logged model. | ||
""" | ||
with TempDir() as tmp: | ||
local_path = tmp.path("model") | ||
|
@@ -223,3 +294,4 @@ def log( | |
registered_model_name, | ||
await_registration_for=await_registration_for, | ||
) | ||
return mlflow_model.get_model_info() |
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
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
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
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
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.
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.
I added example values for some attributes.