将模型部署到 Azure 容器实例Deploy a model to Azure Container Instances

适用于:是基本版是企业版               (升级到企业版APPLIES TO: yesBasic edition yesEnterprise edition                    (Upgrade to Enterprise edition)

了解如何使用 Azure 机器学习将模型部署为 Azure 容器实例 (ACI) 上的 Web 服务。Learn how to use Azure Machine Learning to deploy a model as a web service on Azure Container Instances (ACI). 如果满足下列任一条件,请使用 Azure 容器实例:Use Azure Container Instances if one of the following conditions is true:

  • 你需要快速部署并验证你的模型。You need to quickly deploy and validate your model. 不需要提前创建 ACI 容器。You do not need to create ACI containers ahead of time. 将在部署过程中创建它们。They are created as part of the deployment process.
  • 正在测试一个开发中的模型。You are testing a model that is under development.

有关 ACI 的配额和区域可用性的信息,请参阅 Azure 容器实例的配额和区域可用性文章。For information on quota and region availability for ACI, see Quotas and region availability for Azure Container Instances article.

重要

强烈建议在部署到 Web 服务之前先进行本地调试。有关详细信息,请参阅本地调试It is highly advised to debug locally before deploying to the web service, for more information see Debug Locally

还可参阅 Azure 机器学习 - 部署到本地笔记本You can also refer to Azure Machine Learning - Deploy to Local Notebook

先决条件Prerequisites

部署到 ACIDeploy to ACI

要将模型部署到 Azure 容器实例,请创建一个描述所需计算资源的部署配置。To deploy a model to Azure Container Instances, create a deployment configuration that describes the compute resources needed. 例如,核心数和内存。For example, number of cores and memory. 此外,还需要一个推理配置,描述托管模型和 Web 服务所需的环境。You also need an inference configuration, which describes the environment needed to host the model and web service. 有关如何创建推理配置的详细信息,请参阅部署模型的方式和位置For more information on creating the inference configuration, see How and where to deploy models.

备注

  • ACI 仅适用于大小 <1GB 的小模型。ACI is suitable only for small models <1GB in size.
  • 建议使用单节点 AKS 对较大的模型进行开发测试。We recommend to use single node AKS for dev-test of larger models.

使用 SDKUsing the SDK

from azureml.core.webservice import AciWebservice, Webservice
from azureml.core.model import Model

deployment_config = AciWebservice.deploy_configuration(cpu_cores = 1, memory_gb = 1)
service = Model.deploy(ws, "aciservice", [model], inference_config, deployment_config)
service.wait_for_deployment(show_output = True)
print(service.state)

有关此示例中使用的类、方法和参数的详细信息,请参阅以下参考文档:For more information on the classes, methods, and parameters used in this example, see the following reference documents:

使用 CLIUsing the CLI

要使用 CLI 进行部署,请使用以下命令。To deploy using the CLI, use the following command. mymodel:1 替换为注册的模型的名称和版本。Replace mymodel:1 with the name and version of the registered model. myservice 替换为要赋予此服务的名称:Replace myservice with the name to give this service:

az ml model deploy -m mymodel:1 -n myservice -ic inferenceconfig.json -dc deploymentconfig.json

deploymentconfig.json 文档中的条目映射到 AciWebservice.deploy_configuration 的参数。The entries in the deploymentconfig.json document map to the parameters for AciWebservice.deploy_configuration. 下表描述了 JSON 文档中的实体与方法参数之间的映射:The following table describes the mapping between the entities in the JSON document and the parameters for the method:

JSON 实体JSON entity 方法参数Method parameter 说明Description
computeType 不可用NA 计算目标。The compute target. 对于 ACI,此值必须为 ACIFor ACI, the value must be ACI.
containerResourceRequirements 不可用NA CPU 和内存实体的容器。Container for the CPU and memory entities.
  cpu cpu_cores 要分配的 CPU 核心数。The number of CPU cores to allocate. 默认值为 0.1Defaults, 0.1
  memoryInGB memory_gb 为此 Web 服务分配的内存量 (GB)。The amount of memory (in GB) to allocate for this web service. 默认值为 0.5Default, 0.5
location location 要将此 Webservice 部署到的 Azure 区域。The Azure region to deploy this Webservice to. 如果未指定,将使用工作区位置。If not specified the Workspace location will be used. 有关可用区域的详细信息,请参阅:ACI 区域More details on available regions can be found here: ACI Regions
authEnabled auth_enabled 是否为此 Webservice 启用身份验证。Whether to enable auth for this Webservice. 默认为 FalseDefaults to False
sslEnabled ssl_enabled 是否为此 Webservice 启用 SSL。Whether to enable SSL for this Webservice. 默认为 False。Defaults to False.
appInsightsEnabled enable_app_insights 是否为此 Webservice 启用 AppInsights。Whether to enable AppInsights for this Webservice. 默认为 FalseDefaults to False
sslCertificate ssl_cert_pem_file 如果启用 SSL,则需要证书文件The cert file needed if SSL is enabled
sslKey ssl_key_pem_file 如果启用 SSL,则需要密钥文件The key file needed if SSL is enabled
cname ssl_cname 如果启用 SSL,则需要 CNAMEThe cname for if SSL is enabled
dnsNameLabel dns_name_label 评分终结点的 DNS 名称标签。The dns name label for the scoring endpoint. 如果未指定,则将为评分终结点生成唯一的 DNS 名称标签。If not specified a unique dns name label will be generated for the scoring endpoint.

以下 JSON 是用于 CLI 的部署配置示例:The following JSON is an example deployment configuration for use with the CLI:

{
    "computeType": "aci",
    "containerResourceRequirements":
    {
        "cpu": 0.5,
        "memoryInGB": 1.0
    },
    "authEnabled": true,
    "sslEnabled": false,
    "appInsightsEnabled": false
}

有关详细信息,请参阅 az ml model deploy 参考文档。For more information, see the az ml model deploy reference.

使用 VS CodeUsing VS Code

请参阅使用 VS Code 部署模型See deploy your models with VS Code.

重要

无需事先创建 ACI 容器即可进行测试。You don't need to create an ACI container to test in advance. 将据需要创建 ACI 容器。ACI containers are created as needed.

后续步骤Next steps