デプロイ エンドポイントを SDK v2 にアップグレードする

SDK/CLI v1 を使用すると、ACI または AKS に Web サービスとしてモデルをデプロイできます。 既存の v1 モデル デプロイと Web サービスはそのまま機能し続けますが、SDK/CLI v1 を使用して ACI または AKS に Web サービスとしてモデルをデプロイすることは、従来の機能とみなされるようになりました。 新しいモデル デプロイの場合、v2 にアップグレードすることをお勧めします。

v2 では、マネージド エンドポイントまたは Kubernetes エンドポイントが用意されています。 v1 と v2 の比較については、エンドポイントとデプロイに関するページを参照してください。

マネージド オンライン エンドポイント、v2 の kubernetes オンライン エンドポイント (Azure Kubernetes Services と Arc 対応 Kubernetes を含む)、v1 の Azure Container Instances (ACI) と Kubernetes Services (AKS) Web サービスなど、いくつかのデプロイ ファネルがあります。 この記事では、ACI Web サービス (v1) とマネージド オンライン エンドポイント (v2) へのデプロイの比較に焦点を当てます。

この記事の例は、以下の方法を示します。

  • Azure にモデルをデプロイする
  • エンドポイントを使用してスコア付けする
  • Web サービス/エンドポイントを削除する

推論リソースを作成する

  • SDK v1
    1. モデル、環境、スコアリング スクリプトを構成します。

      # configure a model. example for registering a model 
      from azureml.core.model import Model
      model = Model.register(ws, model_name="bidaf_onnx", model_path="./model.onnx")
      
      # configure an environment
      from azureml.core import Environment
      env = Environment(name='myenv')
      python_packages = ['nltk', 'numpy', 'onnxruntime']
      for package in python_packages:
          env.python.conda_dependencies.add_pip_package(package)
      
      # configure an inference configuration with a scoring script
      from azureml.core.model import InferenceConfig
      inference_config = InferenceConfig(
          environment=env,
          source_directory="./source_dir",
          entry_script="./score.py",
      )
      
    2. ACI Web サービスを構成してデプロイします。

      from azureml.core.webservice import AciWebservice
      
      # defince compute resources for ACI
      deployment_config = AciWebservice.deploy_configuration(
          cpu_cores=0.5, memory_gb=1, auth_enabled=True
      )
      
      # define an ACI webservice
      service = Model.deploy(
          ws,
          "myservice",
          [model],
          inference_config,
          deployment_config,
          overwrite=True,
      )
      
      # create the service 
      service.wait_for_deployment(show_output=True)
      

モデルの登録の詳細については、「ローカル ファイルからモデルを登録する」を参照してください。

  • SDK v2

    1. モデル、環境、スコアリング スクリプトを構成します。

      from azure.ai.ml.entities import Model
      # configure a model
      model = Model(path="../model-1/model/sklearn_regression_model.pkl")
      
      # configure an environment
      from azure.ai.ml.entities import Environment
      env = Environment(
          conda_file="../model-1/environment/conda.yml",
          image="mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04:20210727.v1",
      )
      
      # configure an inference configuration with a scoring script
      from azure.ai.ml.entities import CodeConfiguration
      code_config = CodeConfiguration(
              code="../model-1/onlinescoring", scoring_script="score.py"
          )
      
    2. オンライン エンドポイントを構成して作成します。

      import datetime
      from azure.ai.ml.entities import ManagedOnlineEndpoint
      
      # create a unique endpoint name with current datetime to avoid conflicts
      online_endpoint_name = "endpoint-" + datetime.datetime.now().strftime("%m%d%H%M%f")
      
      # define an online endpoint
      endpoint = ManagedOnlineEndpoint(
          name=online_endpoint_name,
          description="this is a sample online endpoint",
          auth_mode="key",
          tags={"foo": "bar"},
      )
      
      # create the endpoint:
      ml_client.begin_create_or_update(endpoint)
      
    3. オンライン デプロイを構成して作成します。

      from azure.ai.ml.entities import ManagedOnlineDeployment
      
      # define a deployment
      blue_deployment = ManagedOnlineDeployment(
          name="blue",
          endpoint_name=online_endpoint_name,
          model=model,
          environment=env,
          code_configuration=code_config,
          instance_type="Standard_F2s_v2",
          instance_count=1,
      )
      
      # create the deployment:
      ml_client.begin_create_or_update(blue_deployment)
      
      # blue deployment takes 100 traffic
      endpoint.traffic = {"blue": 100}
      ml_client.begin_create_or_update(endpoint)
      

エンドポイントとデプロイの概念について詳しくは、「オンライン エンドポイントとは」をご覧ください。

要求を送信する

  • SDK v1

    import json
    data = {
        "query": "What color is the fox",
        "context": "The quick brown fox jumped over the lazy dog.",
    }
    data = json.dumps(data)
    predictions = service.run(input_data=data)
    print(predictions)
    
  • SDK v2

    # test the endpoint (the request will route to blue deployment as set above)
    ml_client.online_endpoints.invoke(
        endpoint_name=online_endpoint_name,
        request_file="../model-1/sample-request.json",
    )
    
    # test the specific (blue) deployment
    ml_client.online_endpoints.invoke(
        endpoint_name=online_endpoint_name,
        deployment_name="blue",
        request_file="../model-1/sample-request.json",
    )
    

リソースを削除する

  • SDK v1

    service.delete()
    
  • SDK v2

    ml_client.online_endpoints.begin_delete(name=online_endpoint_name)
    

SDK v1 と SDK v2 の主要機能のマッピング

SDK v1 の機能 SDK v2 での大まかなマッピング
azureml.core.model.Model クラス azure.ai.ml.entities.Model クラス
azureml.core.Environment クラス azure.ai.ml.entities.Environment クラス
azureml.core.model.InferenceConfig クラス azure.ai.ml.entities.CodeConfiguration クラス
azureml.core.webservice.AciWebservice クラス azure.ai.ml.entities.OnlineDeployment クラス (および azure.ai.ml.entities.ManagedOnlineEndpoint クラス)
Model.deploy または Webservice.deploy ml_client.begin_create_or_update(online_deployment)
Webservice.run ml_client.online_endpoints.invoke
Webservice.delete ml_client.online_endpoints.delete

詳細については、「

v2 ドキュメント:

v1 ドキュメント: