共用方式為


將 MLflow 模型部署為 Azure Web 服務

適用於: Python SDK azureml v1 (部分機器翻譯)

MLflow 是一個開放原始碼程式庫,可用於管理機器學習實驗的生命週期。 其與 Azure Machine Learning 的 MLflow 整合可讓您將此管理功能延伸到模型定型以外的生產模型部署階段。 在本文中,您將 MLflow 模型部署為 Azure Web 服務,並應用 Azure Machine Learning 的模型管理和資料漂移偵測功能,並將其套用至您的生產模型。

下圖示範 MLflow 部署 API 如何與 Azure Machine Learning 整合以部署模型。 您可以使用 PyTorch、Tensorflow 或 scikit-learn 等熱門架構,建立模型作為 Azure Web 服務,並管理工作區中的服務:

示範 MLflow 部署 API 如何與 Azure Machine Learning 整合以部署模型的圖表。

提示

本文件供想要將其 MLflow 模型部署到 Azure Machine Learning Web 服務端點的資料科學家和開發人員使用。 如果您是想要從 Azure Machine Learning 監視資源使用量和事件 (例如配額、已完成的定型回合或已完成的模型部署) 的系統管理員,請參閱監視 Azure Machine Learning

必要條件

部署選項

Azure Machine Learning 提供下列項目的部署組態選項:

  • Azure 容器執行個體:適用於快速開發測試部署。
  • Azure Kubernetes Service (AKS):建議用於可調整的生產環境部署。

注意

Azure Machine Learning 端點 (v2) 提供經過改良且更簡單的部署體驗。 端點同時支援即時和 Batch 推斷案例。 端點會提供整合介面,以叫用和管理跨計算類型的模型部署。 請參閱什麼是 Azure Machine Learning 端點?

關於其他 MLflow 和 Azure Machine Learning 功能整合,請參閱使用 v2 SDK 的 MLflow 和 Azure Machine Learning (v2)

部署至 Azure 容器執行個體

若要將 MLflow 模型部署到 Azure Machine Learning Web 服務,您必須使用 MLflow 追蹤 URI 以連接 Azure Machine Learning,來設定您的模型。

針對部署至 Azure 容器執行個體,您不需要定義任何部署組態。 未提供設定時,服務預設為 Azure 容器執行個體部署。 您可使用 MLflow 適用於 Azure Machine Learning 的部署方法,在一個步驟中註冊和部署模型。

from mlflow.deployments import get_deploy_client

# Set the tracking URI as the deployment client
client = get_deploy_client(mlflow.get_tracking_uri())

# Set the model path 
model_path = "model"

# Define the model path and the name as the service name
# The model is registered automatically and a name is autogenerated by using the "name" parameter 
client.create_deployment(name="mlflow-test-aci", model_uri='runs:/{}/{}'.format(run.id, model_path))

自定義部署組態 json 檔案

如果您不想使用預設值,您可以設定部署,其中包含使用 deploy_configuration() 方法中的參數作為參考的部署組態 json 檔案。

定義部署設定參數

針對您的部署組態 json 檔案,以字典的形式定義每個部署組態參數。 下列程式碼片段提供範例。 如需部署組態 json 檔案可包含之內容的詳細資訊,請參閱 Azure Machine Learning Azure CLI 參考中的 Azure 容器執行個體部署組態架構

{"computeType": "aci",
 "containerResourceRequirements": {"cpu": 1, "memoryInGB": 1},
 "location": "eastus2"
}

然後,您可以使用設定 json 檔案來建立您的部署:

# Set the deployment config json file
deploy_path = "deployment_config.json"
test_config = {'deploy-config-file': deploy_path}

client.create_deployment(model_uri='runs:/{}/{}'.format(run.id, model_path), config=test_config, name="mlflow-test-aci")    

部署到 Azure Kubernetes Service (AKS)

若要將 MLflow 模型部署到 Azure Machine Learning Web 服務,您必須使用 MLflow 追蹤 URI 以連接 Azure Machine Learning,來設定您的模型。

若要部署至 AKS,您必須先使用 ComputeTarget.create() 方法來建立 AKS 叢集。 建立新叢集的程序可能需要 20-25 分鐘的時間。

from azureml.core.compute import AksCompute, ComputeTarget

# Use the default configuration (can also provide parameters to customize)
prov_config = AksCompute.provisioning_configuration()

aks_name = 'aks-mlflow'

# Create the cluster
aks_target = ComputeTarget.create(workspace=ws, name=aks_name, provisioning_configuration=prov_config)

aks_target.wait_for_completion(show_output = True)

print(aks_target.provisioning_state)
print(aks_target.provisioning_errors)

藉由使用 deploy_configuration() 方法值作為參考,建立部署組態 json。 將每個部署設定參數定義為字典,如下列範例所示:

{"computeType": "aks", "computeTargetName": "aks-mlflow"}

然後,使用 MLflow 的部署用戶端,在單一個步驟中註冊和部署模型:

from mlflow.deployments import get_deploy_client

# Set the tracking URI as the deployment client
client = get_deploy_client(mlflow.get_tracking_uri())

# Set the model path 
model_path = "model"

# Set the deployment config json file
deploy_path = "deployment_config.json"
test_config = {'deploy-config-file': deploy_path}

# Define the model path and the name as the service name
# The model is registered automatically and a name is autogenerated by using the "name" parameter 
client.create_deployment(model_uri='runs:/{}/{}'.format(run.id, model_path), config=test_config, name="mlflow-test-aci")

服務部署可能需要數分鐘的時間。

清除資源

如果您不打算使用已部署的 Web 服務,請使用 service.delete() 方法從您的筆記本刪除服務。 如需詳細資訊,請參閱 Python SDK 文件中 WebService 類別的 delete() 方法

探索筆記本範例

使用搭配 Azure Machine Learning 筆記本的 MLflow 示範和擴充本文所述的概念。

注意

如需使用 MLflow 的範例社群驅動存放庫,請參閱 GitHub 上的 Azure Machine Learning 範例存放庫