-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Add support for transformers as a named flavor #8086
Conversation
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Documentation preview for 3b851a7 will be available here when this CircleCI job completes successfully. More info
|
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
7743e93
to
85ae0ad
Compare
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
mlflow/transformers.py
Outdated
""" | ||
Convert a submitted component-based model in a dictionary to a namedtuple | ||
""" | ||
ComponentModel = namedtuple("ComponentModel", model, rename=True) |
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 would pull this out from this function so that we can use isinstance(..., ComponentModel)
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.
After looking at that type validator and how it's not really possible to do the dynamic renaming in a clean way, I deleted these functions and implemented a subclass of NamedTuple. It's a bit more complex, but it gives us the ability to do explicit type validation to ensure that only properly qualified objects are able to be processed in the module.
mlflow/transformers.py
Outdated
elif _isinstance_named_tuple(model): | ||
try: | ||
return get_task(model.model.name_or_path) | ||
except RuntimeError as e: |
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.
except RuntimeError as e: | |
except Exception as e: |
Catching Exception
might be safer in case huggingface suddenly changes RunTimeError
to a different error type.
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.
excellent point. I also updated the handling in the pip requirements function (since the root of the issue is identical (but called from a different transformers function that is internal to the loading of a model instance).
mlflow/utils/docstring_utils.py
Outdated
def get_version_ranges(module_key): | ||
versions = _ML_PACKAGE_VERSIONS[module_key]["models"] | ||
min_version = versions["minimum"] | ||
max_version = versions["maximum"] | ||
return min_version, max_version |
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.
Can we move this function in the top level?
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.
Excellent idea. renamed and added usage notes
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
mlflow/ml-package-versions.yml
Outdated
pip install git+https://github.com/huggingface/transformers | ||
models: | ||
minimum: "4.25.1" | ||
maximum: "4.27.3" |
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.
maximum: "4.27.3" | |
maximum: "4.27.4" |
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.
ah, good catch.
mlflow/transformers.py
Outdated
from transformers import pipeline | ||
|
||
pipeline = "csarron/mobilebert-uncased-squad-v2" | ||
|
||
with mlflow.start_run(): | ||
mlflow.transformers.save_model( | ||
transformers_model=pipeline, | ||
path="path/to/save/model", | ||
) |
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.
This seems to fail:
Traceback (most recent call last):
File "save.py", line 6, in <module>
mlflow.transformers.save_model(
File "/Users/corey.zumar/mlflow/mlflow/utils/docstring_utils.py", line 235, in version_func
return func(*args, **kwargs)
File "/Users/corey.zumar/mlflow/mlflow/transformers.py", line 275, in save_model
_validate_transformers_model_dict(transformers_model)
File "/Users/corey.zumar/mlflow/mlflow/transformers.py", line 141, in _validate_transformers_model_dict
model = transformers_model.model
AttributeError: 'str' object has no attribute 'model'
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.
(Transformers version 4.27.3)
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.... don't even know... what I was thinking with that.... UGH. Fix incoming.
mlflow/transformers.py
Outdated
from transformers import pipeline | ||
|
||
pipeline = "csarron/mobilebert-uncased-squad-v2" | ||
|
||
with mlflow.start_run(): | ||
mlflow.transformers.log_model( | ||
transformers_model=pipeline, | ||
artifact_path="my_pipeline", | ||
) |
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.
This seems to fail (https://github.com/mlflow/mlflow/pull/8086/files#r1152507646)
mlflow/transformers.py
Outdated
"the model is not a language-based model and requires a complex input type " | ||
"that is currently not supported." | ||
) | ||
_logger.warning(f"This model is unable to be used for pyfunc prediction due to {reason}") |
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.
Can we clarify that the pyfunc flavor won't be added to the model?
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.
Added a sentence explaining this
mlflow/utils/docstring_utils.py
Outdated
required_pkg_versions = f"``{min_ver}`` - ``{max_ver}``" | ||
|
||
notice = ( | ||
f"The '{integration_name}' package is known to be compatible with the following " |
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.
f"The '{integration_name}' package is known to be compatible with the following " | |
f"The '{integration_name}' MLflow Models integration is known to be compatible with the following " |
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.
changed!
|
||
|
||
@experimental | ||
@docstring_version_compatibility_warning(integration_name=FLAVOR_NAME) |
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.
Can we also apply this decorator to load_model()
?
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.
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
mlflow/transformers.py
Outdated
"that is currently not supported." | ||
) | ||
_logger.warning( | ||
f"This model is unable to be used for pyfunc prediction due to {reason} " |
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.
Nittiest of nits (reads better):
f"This model is unable to be used for pyfunc prediction due to {reason} " | |
f"This model is unable to be used for pyfunc prediction because {reason} " |
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.
LGTM! Tested manually and it works very well for language & image use cases. Thanks @BenWilson2 !
mlflow/transformers.py
Outdated
base_reqs = ["transformers"] | ||
try: | ||
base_reqs.extend(_model_packages(model)) |
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.
base_reqs = ["transformers"] | |
try: | |
base_reqs.extend(_model_packages(model)) | |
try: | |
base_reqs = ["transformers", *_model_packages(model)] |
A minor simplification :)
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.
LGTM once https://github.com/mlflow/mlflow/pull/8086/files#r1150353739 is addressed!
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Quick question: we're currently trying to log a pytorch lightning module with transformers in it (logging transformer model and processor via callback). We are currently stuck loading these models with our lightning module wrapper - are there any best practices from the mlflow team? I cannot find anything in the docs. |
@tahesse what is the nature of the issue that you're experiencing? Would you like to file an issue so that we can have a place to discuss and you can share example code? |
@BenWilson2 I think it is pretty much #4245 except that our pretrained model is wrapped with PytorchLightning module like in https://github.com/NielsRogge/Transformers-Tutorials/blob/master/Donut/CORD/Fine_tune_Donut_on_a_custom_dataset_(CORD)_with_PyTorch_Lightning.ipynb Do you think it makes sense to open a new issue for it? I feel like there is just a best practice section missing in the docs for it. The |
Do you have an example that works properly that you'd be willing to contribute and are interested in updating documentation guidelines for this use case? If so, please feel free to file a PR and we can discuss there :) |
@BenWilson2 Unfortunately, I am not able to open source my companies code without permission. |
Related Issues/PRs
#xxxWhat changes are proposed in this pull request?
Initial PR for transformers flavor, providing support for:
How is this patch tested?
Does this PR change the documentation?
Release Notes
Is this a user-facing change?
Add support for HuggingFace transformers as a named flavor.
What component(s), interfaces, languages, and integrations does this PR affect?
Components
area/artifacts
: Artifact stores and artifact loggingarea/build
: Build and test infrastructure for MLflowarea/docs
: MLflow documentation pagesarea/examples
: Example codearea/model-registry
: Model Registry service, APIs, and the fluent client calls for Model Registryarea/models
: MLmodel format, model serialization/deserialization, flavorsarea/recipes
: Recipes, Recipe APIs, Recipe configs, Recipe Templatesarea/projects
: MLproject format, project running backendsarea/scoring
: MLflow Model server, model deployment tools, Spark UDFsarea/server-infra
: MLflow Tracking server backendarea/tracking
: Tracking Service, tracking client APIs, autologgingInterface
area/uiux
: Front-end, user experience, plotting, JavaScript, JavaScript dev serverarea/docker
: Docker use across MLflow's components, such as MLflow Projects and MLflow Modelsarea/sqlalchemy
: Use of SQLAlchemy in the Tracking Service or Model Registryarea/windows
: Windows supportLanguage
language/r
: R APIs and clientslanguage/java
: Java APIs and clientslanguage/new
: Proposals for new client languagesIntegrations
integrations/azure
: Azure and Azure ML integrationsintegrations/sagemaker
: SageMaker integrationsintegrations/databricks
: Databricks integrationsHow should the PR be classified in the release notes? Choose one:
rn/breaking-change
- The PR will be mentioned in the "Breaking Changes" sectionrn/none
- No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" sectionrn/feature
- A new user-facing feature worth mentioning in the release notesrn/bug-fix
- A user-facing bug fix worth mentioning in the release notesrn/documentation
- A user-facing documentation change worth mentioning in the release notes