Skip to content

Commit a10ee9b

Browse files
authored
Merge pull request #202720 from santiagxf/santiagxf/train-and-manage-models-mlflow
Train and manage models mlflow
2 parents 3733e05 + 5d56e90 commit a10ee9b

File tree

4 files changed

+513
-24
lines changed

4 files changed

+513
-24
lines changed

articles/machine-learning/how-to-deploy-mlflow-models-online-endpoints.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Deploy MLflow models to online endpoint (preview)
2+
title: Deploy MLflow models to online endpoint
33
titleSuffix: Azure Machine Learning
44
description: Learn to deploy your MLflow model as a web service that's automatically managed by Azure.
55
services: machine-learning
@@ -14,7 +14,7 @@ ms.custom: deploy, mlflow, devplatv2, no-code-deployment, devx-track-azurecli, c
1414
ms.devlang: azurecli
1515
---
1616

17-
# Deploy MLflow models to online endpoints (preview)
17+
# Deploy MLflow models to online endpoints
1818

1919
[!INCLUDE [cli v2](../../includes/machine-learning-cli-v2.md)]
2020

@@ -23,7 +23,7 @@ ms.devlang: azurecli
2323
> * [v1](./v1/how-to-deploy-mlflow-models.md)
2424
> * [v2 (current version)](how-to-deploy-mlflow-models-online-endpoints.md)
2525
26-
In this article, learn how to deploy your [MLflow](https://www.mlflow.org) model to an [online endpoint](concept-endpoints.md) (preview) for real-time inference. When you deploy your MLflow model to an online endpoint, it's a no-code-deployment so you don't have to provide a scoring script or an environment.
26+
In this article, learn how to deploy your [MLflow](https://www.mlflow.org) model to an [online endpoint](concept-endpoints.md) for real-time inference. When you deploy your MLflow model to an online endpoint, it's a no-code-deployment so you don't have to provide a scoring script or an environment.
2727

2828
You only provide the typical MLflow model folder contents:
2929

@@ -51,7 +51,7 @@ For no-code-deployment, Azure Machine Learning
5151

5252
[!INCLUDE [clone repo & set defaults](../../includes/machine-learning-cli-prepare.md)]
5353

54-
In this code snippets used in this article, the `ENDPOINT_NAME` environment variable contains the name of the endpoint to create and use. To set this, use the following command from the CLI. Replace `<YOUR_ENDPOINT_NAME>` with the name of your endpoint:
54+
In this code snippet used in this article, the `ENDPOINT_NAME` environment variable contains the name of the endpoint to create and use. To set this, use the following command from the CLI. Replace `<YOUR_ENDPOINT_NAME>` with the name of your endpoint:
5555

5656
:::code language="azurecli" source="~/azureml-examples-main/cli/deploy-managed-online-endpoint-mlflow.sh" ID="set_endpoint_name":::
5757

@@ -138,7 +138,7 @@ This example shows how you can deploy an MLflow model to an online endpoint usin
138138

139139
# [Endpoints page](#tab/endpoint)
140140

141-
1. From the __Endpoints__ page, Select **+Create (preview)**.
141+
1. From the __Endpoints__ page, Select **+Create**.
142142

143143
:::image type="content" source="media/how-to-deploy-mlflow-models-online-endpoints/create-from-endpoints.png" lightbox="media/how-to-deploy-mlflow-models-online-endpoints/create-from-endpoints.png" alt-text="Screenshot showing create option on the Endpoints UI page.":::
144144

@@ -155,7 +155,7 @@ This example shows how you can deploy an MLflow model to an online endpoint usin
155155

156156
# [Models page](#tab/models)
157157

158-
1. Select the MLflow model, and then select __Deploy__. When prompted, select __Deploy to real-time endpoint (preview)__.
158+
1. Select the MLflow model, and then select __Deploy__. When prompted, select __Deploy to real-time endpoint__.
159159

160160
:::image type="content" source="media/how-to-deploy-mlflow-models-online-endpoints/deploy-from-models-ui.png" lightbox="media/how-to-deploy-mlflow-models-online-endpoints/deploy-from-models-ui.png" alt-text="Screenshot showing how to deploy model from Models UI":::
161161

@@ -193,11 +193,11 @@ This section helps you understand how to deploy models to an online endpoint onc
193193

194194
To learn more, review these articles:
195195

196-
- [Deploy models with REST (preview)](how-to-deploy-with-rest.md)
197-
- [Create and use online endpoints (preview) in the studio](how-to-use-managed-online-endpoint-studio.md)
198-
- [Safe rollout for online endpoints (preview)](how-to-safely-rollout-managed-endpoints.md)
196+
- [Deploy models with REST](how-to-deploy-with-rest.md)
197+
- [Create and use online endpoints in the studio](how-to-use-managed-online-endpoint-studio.md)
198+
- [Safe rollout for online endpoints](how-to-safely-rollout-managed-endpoints.md)
199199
- [How to autoscale managed online endpoints](how-to-autoscale-endpoints.md)
200-
- [Use batch endpoints (preview) for batch scoring](how-to-use-batch-endpoint.md)
201-
- [View costs for an Azure Machine Learning managed online endpoint (preview)](how-to-view-online-endpoints-costs.md)
202-
- [Access Azure resources with an online endpoint and managed identity (preview)](how-to-access-resources-from-endpoints-managed-identities.md)
200+
- [Use batch endpoints for batch scoring](how-to-use-batch-endpoint.md)
201+
- [View costs for an Azure Machine Learning managed online endpoint](how-to-view-online-endpoints-costs.md)
202+
- [Access Azure resources with an online endpoint and managed identity](how-to-access-resources-from-endpoints-managed-identities.md)
203203
- [Troubleshoot online endpoint deployment](how-to-troubleshoot-managed-online-endpoints.md)
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
title: Manage models registries in Azure Machine Learning with MLflow
3+
titleSuffix: Azure Machine Learning
4+
description: Explains how to use MLflow for managing models in Azure Machine Learning
5+
services: machine-learning
6+
author: santiagxf
7+
ms.author: fasantia
8+
ms.service: machine-learning
9+
ms.subservice: core
10+
ms.date: 06/08/2022
11+
ms.topic: conceptual
12+
ms.custom: how-to, devx-track-python
13+
---
14+
15+
# Manage models registries in Azure Machine Learning with MLflow
16+
17+
Azure Machine Learning supports MLflow for model management. This represents a convenient way to support the entire model lifecycle for users familiar with the MLFlow client. The following article describes the different capabilities and how it compares with other options.
18+
19+
## Support matrix for managing models with MLflow
20+
21+
The MLflow client exposes several methods to retrieve and manage models. The following table shows which of those methods are currently supported in MLflow when connected to Azure ML. It also compares it with other models management capabilities in Azure ML.
22+
23+
| Feature | MLflow | Azure ML with MLflow | Azure ML CLIv2 | Azure ML Studio |
24+
| :- | :-: | :-: | :-: | :-: |
25+
| Registering models in MLflow format | **&check;** | **&check;** | **&check;** | **&check;** |
26+
| Registering models not in MLflow format | | | **&check;** | **&check;** |
27+
| Registering models from runs outputs/artifacts | **&check;** | **&check;**<sup>1</sup> | **&check;**<sup>2</sup> | **&check;** |
28+
| Listing registered models | **&check;** | **&check;** | **&check;** | **&check;** |
29+
| Retrieving details of registered model's versions | **&check;** | **&check;** | **&check;** | **&check;** |
30+
| Editing registered model's versions description | **&check;** | **&check;** | **&check;** | **&check;** |
31+
| Editing registered model's versions tags | **&check;** | **&check;** | **&check;** | **&check;** |
32+
| Renaming registered models | **&check;** | <sup>3</sup> | <sup>3</sup> | <sup>3</sup> |
33+
| Deleting a registered model (container) | **&check;** | <sup>3</sup> | <sup>3</sup> | <sup>3</sup> |
34+
| Deleting a registered model's version | **&check;** | **&check;** | **&check;** | **&check;** |
35+
| Manage MLflow model stages | **&check;** | **&check;** | | |
36+
| Search registered models by name | **&check;** | **&check;** | **&check;** | **&check;**<sup>4</sup> |
37+
| Search registered models using string comparators `LIKE` and `ILIKE` | **&check;** | | | **&check;**<sup>4</sup> |
38+
| Search registered models by tag | | | | **&check;**<sup>4</sup> |
39+
40+
> [!NOTE]
41+
> - <sup>1</sup> Use URIs with format `runs:/<ruin-id>/<path>`.
42+
> - <sup>2</sup> Use URIs with format `azureml://jobs/<job-id>/outputs/artifacts/<path>`.
43+
> - <sup>3</sup> Registered models are immutable objects in Azure ML.
44+
> - <sup>4</sup> Use search box in Azure ML Studio. Partial match supported.
45+
46+
## Registering new models in the registry
47+
48+
### Creating models from an existing run
49+
50+
If you have an MLflow model logged inside of a run and you want to register it in a registry, you can do that by using the run ID and the path where the model was logged. See [Manage experiments and runs with MLflow](how-to-track-experiments-mlflow.md) to know how to query this information if you don't have it.
51+
52+
```python
53+
mlflow.register_model(f"runs:/{run_id}/{artifact_path}", model_name)
54+
```
55+
56+
### Creating models from assets
57+
58+
If you have a folder with an MLModel MLflow model, then you can register it directly. There's no need for the model to be always in the context of a run. To do that you can use the URI schema `file://path/to/model` to register MLflow models stored in the local file system. Let's create a simple model using `Scikit-Learn` and save it in MLflow format in the local storage:
59+
60+
```python
61+
from sklearn import linear_model
62+
63+
reg = linear_model.LinearRegression()
64+
reg.fit([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
65+
66+
mlflow.sklearn.save_model(reg, "./regressor")
67+
```
68+
69+
> [!TIP]
70+
> The method `save_model` works in the same way as `log_model`. While the latter requires an MLflow run to be active so the model can be logged there, the former uses the local file system for the stage of the model's artifacts.
71+
72+
You can now register the model from the local path:
73+
74+
```python
75+
import os
76+
77+
model_local_path = os.path.abspath("./regressor")
78+
mlflow.register_model(f"file://{model_local_path}", "local-model-test")
79+
```
80+
81+
> [!NOTE]
82+
> Notice how the model URI schema `file:/` requires absolute paths.
83+
84+
## Querying models
85+
86+
### Querying all the models in the registry
87+
88+
You can query all the registered models in the registry using the MLflow client with the method `list_registered_models`. The MLflow client is required to do all these operations.
89+
90+
```python
91+
using mlflow
92+
93+
client = mlflow.tracking.MlflowClient()
94+
```
95+
96+
The following sample prints all the model's names:
97+
98+
```python
99+
for model in client.list_registered_models():
100+
print(f"{model.name}")
101+
```
102+
103+
### Getting specific versions of the model
104+
105+
The command above will retrieve the model object which contains all the model versions. However, if you want to get the last registered model version of a given model, you can use `get_registered_model`:
106+
107+
```python
108+
client.get_registered_model(model_name)
109+
```
110+
111+
If you need a specific version of the model, you can indicate so:
112+
113+
```python
114+
client.get_model_version(model_name, version=2)
115+
```
116+
117+
## Model stages
118+
119+
MLflow supports model's stages to manage model's lifecycle. Model's version can transition from one stage to another. Stages are assigned to a model's version (instead of models) which means that a given model can have multiple versions on different stages.
120+
121+
> [!IMPORTANT]
122+
> Stages can only be accessed using the MLflow SDK. They don't show up in the [Azure ML Studio portal](https://ml.azure.com) and can't be retrieved using neither Azure ML SDK, Azure ML CLI, or Azure ML REST API. Creating deployment from a given model's stage is not supported by the moment.
123+
124+
### Querying model stages
125+
126+
You can use the MLflow client to check all the possible stages a model can be:
127+
128+
```python
129+
client.get_model_version_stages(model_name, version="latest")
130+
```
131+
132+
You can see what model's version is on each stage by getting the model from the registry. The following example gets the model's version currently in the stage `Staging`.
133+
134+
> [!WARNING]
135+
> Stage names are case sensitive.
136+
137+
```python
138+
client.get_latest_versions(model_name, stages=["Staging"])
139+
```
140+
141+
> [!NOTE]
142+
> Multiple versions can be in the same stage at the same time in Mlflow, however, this method returns the latest version (greater version) among all of them.
143+
144+
### Transitioning models
145+
146+
Transitioning a model's version to a particular stage can be done using the MLflow client.
147+
148+
```python
149+
client.transition_model_version_stage(model_name, version=3, stage="Staging")
150+
```
151+
152+
By default, if there were an existing model version in that particular stage, it will remain there. Hence, it won't be replaced as multiple model's versions can be in the same stage at the same time. Alternatively, you can indicate `archive_existing_versions=True` to tell MLflow to move the existing model's version to the stage `Archived`.
153+
154+
```python
155+
client.transition_model_version_stage(
156+
model_name, version=3, stage="Staging", archive_existing_versions=True
157+
)
158+
```
159+
160+
### Loading models from stages
161+
162+
ou can load a model in a particular stage directly from Python using the `load_model` function and the following URI format. Notice that for this method to success, you need to have all the libraries and dependencies already installed in the environment you're working at.
163+
164+
```python
165+
model = mlflow.pyfunc.load_model(f"models:/{model_name}/Staging")
166+
```
167+
168+
## Editing and deleting models
169+
170+
Editing registered models is supported in both Mlflow and Azure ML, however, there are some differences between them that are important to notice:
171+
172+
> [!WARNING]
173+
> Renaming models is not supported in Azure Machine Learning as model objects are immmutable.
174+
175+
### Editing models
176+
177+
You can edit model's description and tags from a model using Mlflow:
178+
179+
```python
180+
client.update_model_version(model_name, version=1, description="My classifier description")
181+
```
182+
183+
To edit tags, you have to use the method `set_model_version_tag` and `remove_model_version_tag`:
184+
185+
```python
186+
client.set_model_version_tag(model_name, version="1", key="type", value="classification")
187+
```
188+
189+
Removing a tag:
190+
191+
```python
192+
client.delete_model_version_tag(model_name, version="1", key="type")
193+
```
194+
195+
### Deleting a model's version
196+
197+
You can delete any model version in the registry using the MLflow client, as demonstrated in the following example:
198+
199+
```python
200+
client.delete_model_version(model_name, version="2")
201+
```
202+
203+
> [!NOTE]
204+
> Azure Machine Learning doesn't support deleting the entire model container. To achieve the same thing, you will need to delete all the model versions from a given model.

0 commit comments

Comments
 (0)