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

Fix regression in downloading single files from models artifact store #10362

Merged
merged 5 commits into from
Nov 14, 2023

Conversation

BenWilson2
Copy link
Member

@BenWilson2 BenWilson2 commented Nov 11, 2023

🛠 DevTools 🛠

Open in GitHub Codespaces

Install mlflow from this PR

pip install git+https://github.com/mlflow/mlflow.git@refs/pull/10362/merge

Checkout with GitHub CLI

gh pr checkout 10362

Related Issues/PRs

Resolve #10230

What changes are proposed in this pull request?

Fixed the regression introduced in #9402 wherein no handling logic is provided for non-directory source file resolution, which would attempt to create a registration metadata file using a destination file as a directory path (which raises an Exception).

How is this PR tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests

Validated the basic repro is broken in 2.8.0 and is fixed in this PR (test added to validate as well):

import mlflow
from mlflow.store.artifact.artifact_repository_registry import get_artifact_repository

mlflow.set_tracking_uri("sqlite:///:memory:")

with mlflow.start_run():
    mlflow.sklearn.log_model("I am a model", "model", registered_model_name="foo")

model_uri = f"models:/foo/1"

artifact_repository = get_artifact_repository(model_uri) 
artifact_repository.download_artifacts("requirements.txt")

Does this PR require documentation update?

  • No. You can skip the rest of this section.
  • Yes. I've updated:
    • Examples
    • API references
    • Instructions

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/artifacts: Artifact stores and artifact logging
  • area/build: Build and test infrastructure for MLflow
  • area/docs: MLflow documentation pages
  • area/examples: Example code
  • area/gateway: AI Gateway service, Gateway client APIs, third-party Gateway integrations
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/recipes: Recipes, Recipe APIs, Recipe configs, Recipe Templates
  • area/projects: MLproject format, project running backends
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs
  • area/server-infra: MLflow Tracking server backend
  • area/tracking: Tracking Service, tracking client APIs, autologging

Interface

  • area/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev server
  • area/docker: Docker use across MLflow's components, such as MLflow Projects and MLflow Models
  • area/sqlalchemy: Use of SQLAlchemy in the Tracking Service or Model Registry
  • area/windows: Windows support

Language

  • language/r: R APIs and clients
  • language/java: Java APIs and clients
  • language/new: Proposals for new client languages

Integrations

  • integrations/azure: Azure and Azure ML integrations
  • integrations/sagemaker: SageMaker integrations
  • integrations/databricks: Databricks integrations

How should the PR be classified in the release notes? Choose one:

  • rn/none - No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" section
  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

@github-actions github-actions bot added area/artifacts Artifact stores and artifact logging rn/bug-fix Mention under Bug Fixes in Changelogs. labels Nov 11, 2023
Copy link

github-actions bot commented Nov 11, 2023

Documentation preview for 0e72186 will be available here when this CircleCI job completes successfully.

More info

model_name = "MyModel"
model_version = "12"

with mock.patch.object(
MlflowClient, "get_model_version_download_uri", return_value=artifact_location
), mock.patch("mlflow.store.artifact.models_artifact_repo.write_yaml") as write_yaml_mock:
), mock.patch("os.path.isdir", return_value=True), mock.patch(
Copy link
Member

@harupy harupy Nov 11, 2023

Choose a reason for hiding this comment

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

Do we really need to patch os.path.isdir? Patching the built-in functions can lead to bugs. I think we can download a file instead of a directory.

Copy link
Member Author

Choose a reason for hiding this comment

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

refactored tests to use tmp_path location

Comment on lines 159 to 162
_logger.warning(
"Registered Model Metadata file not able to be written. Destination path "
f"'{model_path}' is not a directory."
)
Copy link
Member

@harupy harupy Nov 11, 2023

Choose a reason for hiding this comment

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

This warning might scare users or make them wonder what Registered Model Metadata file unnecessarily.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed. Removed this. It's really only valid for deploying a root model directory and only if deploying to MLserver anyway. I'll add a note in the code about why the file might not be created.

Comment on lines 179 to 181
) as write_yaml_mock, mock.patch(
"os.path.isdir", return_value=False
), mock.patch(
Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines 162 to 166
# Calling the download_artifacts method on local FileStore will create an ./mlruns directory
# which is a test side effect. Clean this up.
mlruns_dir = "./mlruns"
if os.path.exists(mlruns_dir):
shutil.rmtree(mlruns_dir)
Copy link
Member

Choose a reason for hiding this comment

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

Instead, can we set the artifact location to a temporary directory? If we hit an error before this block, ./mlruns won't be cleaned up.

Copy link
Member Author

Choose a reason for hiding this comment

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

yep! refactored

overwrite=True,
ensure_yaml_extension=False,
)
if os.path.isdir(model_path):
Copy link
Member

Choose a reason for hiding this comment

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

Is this file created when we download a subdirectory in the model directory?

- model
    - subdir <- this directory
    - MLmodel
    - ...

Copy link
Collaborator

@serena-ruan serena-ruan Nov 13, 2023

Choose a reason for hiding this comment

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

Could we update 'download_artifacts' function to only call this method when 'model_path' is a directory instead? Then we also don't need below warning as it doesn't make sense for non-directory cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

@harupy good call. Adjusted the logic to only create this registered metadata file iff the model_path resolution after download contains the MLmodel file. Add a test to verify this behavior.

@serena-ruan great idea - updated!

@@ -169,8 +173,14 @@ def download_artifacts(self, artifact_path, dst_path=None):
:return: Absolute path of the local filesystem location containing the desired artifacts.
"""

from mlflow.models.model import MLMODEL_FILE_NAME
Copy link
Member Author

Choose a reason for hiding this comment

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

Cannot do a non-local import due to circular reference. We really should migrate constants like these to a separate 'neutral zero-dependency' location, but that is out of scope for this PR.


models_repo.download_artifacts(artifact_location, str(tmp_path))

add_meta_mock.assert_called_once()
Copy link
Member

Choose a reason for hiding this comment

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

Instead, can we check that the metadata file exists?

dummy_dir = tmp_path / artifact_path
dummy_dir.mkdir()
dummy_file = dummy_dir / "dummy_file.txt"
dummy_file.write_text("dummy content")
Copy link
Member

@harupy harupy Nov 13, 2023

Choose a reason for hiding this comment

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

Suggested change
dummy_file.write_text("dummy content")
dummy_file.touch()

We can use touch if the content is not important.

Copy link
Member

@harupy harupy left a comment

Choose a reason for hiding this comment

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

LGTM

model_dir = temp_remote_storage / "model_dir"
model_dir.mkdir(parents=True)
mlmodel_path = model_dir / "MLmodel"
mlmodel_path.write_text("dummy content")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
mlmodel_path.write_text("dummy content")
mlmodel_path.touch()

we can use more touch.

artifact_dst_path = f"{dst_path}/{artifact_path}"

dummy_file = tmp_path / artifact_path
dummy_file.write_text("dummy content")
Copy link
Member

Choose a reason for hiding this comment

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

same here

"""
artifact_location = "s3://blah_bucket/"
dummy_file = tmp_path / "dummy_file.txt"
dummy_file.write_text("dummy content")
Copy link
Member

Choose a reason for hiding this comment

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

same here

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>
@BenWilson2 BenWilson2 merged commit d668245 into mlflow:master Nov 14, 2023
45 checks passed
@BenWilson2 BenWilson2 deleted the fix-metadata-write branch November 14, 2023 16:00
BenWilson2 added a commit that referenced this pull request Nov 14, 2023
…#10362)

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
KonakanchiSwathi pushed a commit to KonakanchiSwathi/mlflow that referenced this pull request Nov 29, 2023
…mlflow#10362)

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: swathi <konakanchi.swathi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/artifacts Artifact stores and artifact logging rn/bug-fix Mention under Bug Fixes in Changelogs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Model lineage tracking - missing "MLmodel/registered_model_meta" file
3 participants