Skip to content

Add a register_model_with_run_id api to MLflowLogger #2967

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

Merged
merged 8 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions composer/loggers/mlflow_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,53 @@ def log_model(self, flavor: Literal['transformers'], **kwargs):
else:
raise NotImplementedError(f'flavor {flavor} not supported.')

def register_model_with_run_id(
self,
model_uri: str,
name: str,
await_creation_for: int = 300,
tags: Optional[Dict[str, Any]] = None,
):
"""Similar to ``register_model``, but uses a different MLflow API to allow passing in the run id.

Args:
model_uri (str): The URI of the model to register.
name (str): The name of the model to register. Will be appended to ``model_registry_prefix``.
await_creation_for (int, optional): The number of seconds to wait for the model to be registered. Defaults to 300.
tags (Optional[Dict[str, Any]], optional): A dictionary of tags to add to the model. Defaults to None.
"""
if self._enabled:
from mlflow.exceptions import MlflowException
from mlflow.protos.databricks_pb2 import ALREADY_EXISTS, RESOURCE_ALREADY_EXISTS, ErrorCode

full_name = f'{self.model_registry_prefix}.{name}' if len(self.model_registry_prefix) > 0 else name

# This try/catch code is copied from
# https://github.com/mlflow/mlflow/blob/3ba1e50e90a38be19920cb9118593a43d7cfa90e/mlflow/tracking/_model_registry/fluent.py#L90-L103
try:
create_model_response = self._mlflow_client.create_registered_model(full_name)
log.info(f'Successfully registered model {name} with {create_model_response.name}')
except MlflowException as e:
if e.error_code in (
ErrorCode.Name(RESOURCE_ALREADY_EXISTS),
ErrorCode.Name(ALREADY_EXISTS),
):
log.info(f'Registered model {name} already exists. Creating a new version of this model...')
else:
raise e

create_version_response = self._mlflow_client.create_model_version(
name=full_name,
source=model_uri,
run_id=self._run_id,
await_creation_for=await_creation_for,
tags=tags,
)

log.info(
f'Successfully created model version {create_version_response.version} for model {create_version_response.name}'
)

def log_images(
self,
images: Union[np.ndarray, torch.Tensor, Sequence[Union[np.ndarray, torch.Tensor]]],
Expand Down
53 changes: 48 additions & 5 deletions tests/loggers/test_mlflow_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,54 @@ def test_mlflow_register_model(tmp_path, monkeypatch):
name='my_model',
)

assert mlflow.register_model.called_with(model_uri=local_mlflow_save_path,
name='my_catalog.my_schema.my_model',
await_registration_for=300,
tags=None,
registry_uri='databricks-uc')
mlflow.register_model.assert_called_with(
model_uri=local_mlflow_save_path,
name='my_catalog.my_schema.my_model',
await_registration_for=300,
tags=None,
)
assert mlflow.get_registry_uri() == 'databricks-uc'

test_mlflow_logger.post_close()


@pytest.mark.filterwarnings('ignore:.*Setuptools is replacing distutils.*:UserWarning')
@pytest.mark.filterwarnings("ignore:.*The 'transformers' MLflow Models integration.*:FutureWarning")
def test_mlflow_register_model_with_run_id(tmp_path, monkeypatch):
mlflow = pytest.importorskip('mlflow')

mlflow_uri = tmp_path / Path('my-test-mlflow-uri')
mlflow_exp_name = 'test-log-model-exp-name'
test_mlflow_logger = MLFlowLogger(
tracking_uri=mlflow_uri,
experiment_name=mlflow_exp_name,
model_registry_prefix='my_catalog.my_schema',
model_registry_uri='databricks-uc',
)

monkeypatch.setattr(test_mlflow_logger._mlflow_client, 'create_model_version', MagicMock())
monkeypatch.setattr(test_mlflow_logger._mlflow_client, 'create_registered_model',
MagicMock(return_value=type('MockResponse', (), {'name': 'my_catalog.my_schema.my_model'})))

mock_state = MagicMock()
mock_state.run_name = 'dummy-run-name' # this run name should be unused.
mock_logger = MagicMock()

local_mlflow_save_path = str(tmp_path / Path('my_model_local'))
test_mlflow_logger.init(state=mock_state, logger=mock_logger)

test_mlflow_logger.register_model_with_run_id(
model_uri=local_mlflow_save_path,
name='my_model',
)

test_mlflow_logger._mlflow_client.create_model_version.assert_called_with(
name='my_catalog.my_schema.my_model',
source=local_mlflow_save_path,
run_id=test_mlflow_logger._run_id,
await_creation_for=300,
tags=None,
)
assert mlflow.get_registry_uri() == 'databricks-uc'

test_mlflow_logger.post_close()
Expand Down