ترقية نقاط نهاية التوزيع إلى SDK v2

باستخدام SDK/CLI v1، يمكنك نشر النماذج على ACI أو AKS كخدمات ويب. ستستمر عمليات توزيع نموذج v1 الحالية وخدمات الويب في العمل كما هي، ولكن استخدام SDK/CLI v1 لنشر النماذج على ACI أو AKS كخدمات ويب يعتبر الآن قديما. بالنسبة إلى عمليات توزيع النموذج الجديدة، نوصي بالترقية إلى الإصدار 2.

في الإصدار 2، نقدم نقاط النهاية المدارة أو نقاط نهاية Kubernetes. للمقارنة بين v1 وv2، راجع نقاط النهاية والتوزيع.

هناك العديد من مسارات التوزيع مثل نقاط النهاية المدارة عبر الإنترنت ونقاط نهاية kubernetes عبر الإنترنت (بما في ذلك خدمات Azure Kubernetes وKubernetes الممكنة بواسطة Arc) في الإصدار 2، وAzure Container Instances (ACI) وخدمات Kubernetes (AKS) في v1. في هذه المقالة، سنركز على مقارنة التوزيع إلى خدمات الويب ACI (v1) ونقاط النهاية المدارة عبر الإنترنت (v2).

توضح الأمثلة الواردة في هذه المقالة كيفية:

  • توزيع النموذج الخاص بك إلى Azure
  • النتيجة باستخدام نقطة النهاية
  • حذف خدمة الويب/نقطة النهاية

إنشاء موارد الاستدلال

  • 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:

      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 (الإصدار 2)

    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 (الإصدار 2)

    # 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 (الإصدار 2)

    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 docs:

v1 docs: