MLflow モデルを Azure Web サービスとしてデプロイする

適用対象: Python SDK azureml v1

MLflow は、機械学習の実験のライフ サイクルを管理するためのオープンソース ライブラリです。 MLflow と Azure Machine Learning の統合により、モデル トレーニング以外の管理機能を運用モデルのデプロイ フェーズに拡張できます。 この記事では、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 Container Instances: 迅速な開発テスト デプロイに適しています。
  • Azure Kubernetes Service (AKS): スケーラブルな運用環境デプロイに推奨されます。

Note

Azure Machine Learning のエンドポイント (v2) では、デプロイが改善され、シンプルになります。 エンドポイントは、リアルタイム シナリオとバッチ推論の両方をサポートします。 エンドポイントは、複数のコンピューティングの種類にわたってモデル デプロイを起動および管理するための統一インターフェイスを提供します。 「Azure Machine Learning エンドポイントとは」を参照してください。

MLflow と Azure Machine Learning の機能統合の詳細については、v2 SDK を使用する MLflow と Azure Machine Learning (v2) に関する記事をご覧ください。

Azure Container Instances へのデプロイ

MLflow モデルを Azure Machine Learning Web サービスにデプロイするには、Azure Machine Learning に接続するための MLflow 追跡 URI を使用してモデルを設定する必要があります。

Azure Container Instances へのデプロイでは、デプロイ構成を定義する必要はありません。 構成が指定されていない場合、サービスは既定で Azure Container Instances デプロイに設定されます。 Azure Machine Learning 用の MLflow の deploy メソッドを使用して、1 つの手順でモデルを登録し、デプロイできます。

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 Container Instance デプロイ構成スキーマ」を参照してください。

{"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 サービスにデプロイするには、Azure Machine Learning に接続するための MLflow 追跡 URI を使用してモデルを設定する必要があります。

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 のデプロイ クライアントを使用して、1 つの手順でモデルを登録し、デプロイします。

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 は、この記事で提示した概念を示し、さらに詳しく説明します。

Note

MLflow を使用する例のコミュニティ主導のリポジトリについては、GitHub の「Azure Machine Learning サンプル リポジトリ」を参照してください。