使用 CLI v1 创建和管理 Azure 机器学习计算实例

适用于:Azure CLI ml 扩展 v1Python SDK azureml v1

了解如何使用 CLI v1 在 Azure 机器学习工作区中创建和管理计算实例

可将计算实例用作云中的完全配置、完全托管的开发环境。 对于开发和测试,还可将该实例用作训练计算目标或用于推理目标。 计算实例可以并行运行多个作业,它有一个作业队列。 作为开发环境的计算实例不能与工作区中的其他用户共享。

计算实例可以在虚拟网络环境中安全地运行作业,无需企业打开 SSH 端口。 作业在容器化环境中执行,并将模型依赖项打包到 Docker 容器中。

在本文中,学习如何:

  • 创建计算实例
  • 管理(启动、停止、重启、删除)计算实例

注意

本文仅介绍如何使用 CLI v1 完成这些任务。 有关管理计算实例的更多最新方法,请参阅创建 Azure 机器学习计算群集

先决条件

创建

重要

下面标记了“(预览版)”的项当前为公共预览版。 该预览版在提供时没有附带服务级别协议,建议不要将其用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅适用于 Azure 预览版的补充使用条款

时间估计:大约 5 分钟。

对于工作区而言,创建计算实例是一次性过程。 可将此计算重复用作开发工作站,或者用作训练的计算目标。 可将多个计算实例附加到工作区。

对于每个区域每个虚拟机 (VM) 系列配额和创建计算实例时应用的区域总配额,专用内核数一致,且该数量与 Azure 机器学习训练计算群集配额共享。 停止计算实例不会释放配额,因此无法确保能够重启计算实例。 创建计算实例后,不能更改其虚拟机大小。

以下示例演示如何创建计算实例:

适用于:Python SDK azureml v1

import datetime
import time

from azureml.core.compute import ComputeTarget, ComputeInstance
from azureml.core.compute_target import ComputeTargetException

# Choose a name for your instance
# Compute instance name should be unique across the azure region
compute_name = "ci{}".format(ws._workspace_id)[:10]

# Verify that instance does not exist already
try:
    instance = ComputeInstance(workspace=ws, name=compute_name)
    print('Found existing instance, use it.')
except ComputeTargetException:
    compute_config = ComputeInstance.provisioning_configuration(
        vm_size='STANDARD_D3_V2',
        ssh_public_access=False,
        # vnet_resourcegroup_name='<my-resource-group>',
        # vnet_name='<my-vnet-name>',
        # subnet_name='default',
        # admin_user_ssh_public_key='<my-sshkey>'
    )
    instance = ComputeInstance.create(ws, compute_name, compute_config)
    instance.wait_for_completion(show_output=True)

有关此示例中使用的类、方法和参数的详细信息,请参阅以下参考文档:

管理

启动、停止、重启和删除计算实例。 计算实例不会自动纵向缩减,因此请确保停止该资源以免产生费用。 停止计算实例会将其解除分配。 然后在需要时重启。 虽然停止计算实例将停止按计算小时数计费,但仍会对磁盘、公共 IP 和标准负载均衡器计费。

提示

计算实例具有 120GB 的 OS 磁盘。 如果磁盘空间不足,则在停止或重启计算实例之前,使用终端可至少清空 1-2 GB 空间。 请勿通过从终端分发 sudo shutdown 来停止计算实例。 计算实例上的临时磁盘大小取决于所选 VM 大小,它装载在 /mnt 上。

适用于:Python SDK azureml v1

在下例中,计算实例的名称为“instance”。

  • 获取状态

    # get_status() gets the latest status of the ComputeInstance target
    instance.get_status()
    
  • 停止

    # stop() is used to stop the ComputeInstance
    # Stopping ComputeInstance will stop the billing meter and persist the state on the disk.
    # Available Quota will not be changed with this operation.
    instance.stop(wait_for_completion=True, show_output=True)
    
  • 开始

    # start() is used to start the ComputeInstance if it is in stopped state
    instance.start(wait_for_completion=True, show_output=True)
    
  • 重启

    # restart() is used to restart the ComputeInstance
    instance.restart(wait_for_completion=True, show_output=True)
    
  • Delete

    # delete() is used to delete the ComputeInstance target. Useful if you want to re-use the compute name
    instance.delete(wait_for_completion=True, show_output=True)
    

使用 Azure RBAC 可以对工作区中的哪些用户能够创建、删除、启动、停止、重启计算实例进行控制。 充当工作区参与者和所有者角色的所有用户可以在整个工作区中创建、删除、启动、停止和重启计算实例。 但是,只有特定计算实例的创建者或分配的用户(如果该计算实例是以其名义创建的)可访问该计算实例上的 Jupyter、JupyterLab、RStudio 和 Posit Workbench(以前称为 RStudio Workbench)。 一个计算实例专用于具有根访问权限的一个用户。 该用户有权访问实例上运行的 Jupyter/JupyterLab/RStudio/Posit Workbench。 计算实例将有单用户登录,所有操作都将使用该用户的标识,用于 Azure RBAC 和试验运行的归属。 SSH 访问是通过公钥/私钥机制控制的。

可以通过 Azure RBAC 来控制这些操作:

  • Microsoft.MachineLearningServices/workspaces/computes/read
  • Microsoft.MachineLearningServices/workspaces/computes/write
  • Microsoft.MachineLearningServices/workspaces/computes/delete
  • Microsoft.MachineLearningServices/workspaces/computes/start/action
  • Microsoft.MachineLearningServices/workspaces/computes/stop/action
  • Microsoft.MachineLearningServices/workspaces/computes/restart/action
  • Microsoft.MachineLearningServices/workspaces/computes/updateSchedules/action

若要创建计算实例,需要以下操作的权限:

  • Microsoft.MachineLearningServices/workspaces/computes/write
  • Microsoft.MachineLearningServices/workspaces/checkComputeNameAvailability/action

后续步骤