Uppgradera distributionsslutpunkter till SDK v2

Med SDK/CLI v1 kan du distribuera modeller på ACI eller AKS som webbtjänster. Dina befintliga v1-modelldistributioner och webbtjänster fortsätter att fungera som de är, men att använda SDK/CLI v1 för att distribuera modeller på ACI eller AKS som webbtjänster betraktas nu som äldre. För nya modelldistributioner rekommenderar vi att du uppgraderar till v2.

I v2 erbjuder vi hanterade slutpunkter eller Kubernetes-slutpunkter. En jämförelse av v1 och v2 finns i Slutpunkter och distribution.

Det finns flera distributionstrattar, till exempel hanterade onlineslutpunkter, kubernetes-onlineslutpunkter (inklusive Azure Kubernetes Services och Arc-aktiverade Kubernetes) i v2 och Azure Container Instances (ACI) och Kubernetes Services-webbtjänster (AKS) i v1. I den här artikeln fokuserar vi på jämförelsen av distribution till ACI-webbtjänster (v1) och hanterade onlineslutpunkter (v2).

Exempel i den här artikeln visar hur du:

  • Distribuera din modell till Azure
  • Poängsätt med hjälp av slutpunkten
  • Ta bort webbtjänsten/slutpunkten

Skapa slutsatsdragningsresurser

  • SDK v1
    1. Konfigurera en modell, en miljö och ett bedömningsskript:

      # 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. Konfigurera och distribuera en ACI-webbtjänst:

      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)
      

Mer information om hur du registrerar modeller finns i Registrera en modell från en lokal fil.

  • SDK v2

    1. Konfigurera en modell, en miljö och ett bedömningsskript:

      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. Konfigurera och skapa en onlineslutpunkt:

      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. Konfigurera och skapa en onlinedistribution:

      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)
      

Mer information om begrepp för slutpunkter och distributioner finns i Vad är onlineslutpunkter?

Skicka en förfrågan

  • 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",
    )
    

Ta bort resurser

  • SDK v1

    service.delete()
    
  • SDK v2

    ml_client.online_endpoints.begin_delete(name=online_endpoint_name)
    

Mappning av viktiga funktioner i SDK v1 och SDK v2

Funktioner i SDK v1 Grov mappning i SDK v2
azureml.core.model.Model-klass azure.ai.ml.entities.Model-klass
azureml.core.Environment-klass azure.ai.ml.entities.Environment-klass
azureml.core.model.InferenceConfig-klass azure.ai.ml.entities.CodeConfiguration-klass
azureml.core.webservice.AciWebservice-klass azure.ai.ml.entities.OnlineDeployment-klassen (och klassen azure.ai.ml.entities.ManagedOnlineEndpoint)
Model.deploy eller Webservice.deploy ml_client.begin_create_or_update(online_deployment)
Webservice.run ml_client.online_endpoints.invoke
Webservice.delete ml_client.online_endpoints.delete

Mer information finns i

v2-dokument:

v1-dokument: