Azure Machine Learning#
RAPIDS 可以使用 Azure Machine Learning 服务 进行大规模部署,并可根据需要扩展到任意大小。
前提条件#
通过 Azure 门户、Azure ML Python SDK、Azure CLI 或 Azure Resource Manager 模板 使用现有或创建新的 Azure Machine Learning 工作区。
遵循以下高级步骤开始
1. 创建。创建您的 Azure 资源组。
2. 工作区。 在资源组内,创建 Azure Machine Learning 服务工作区。
3. 配额。检查您的订阅使用情况 + 配额,确保您所在区域有足够的配额来启动您所需的计算实例。
Azure ML 计算实例#
尽管可以在本地计算机上安装 Azure Machine Learning,但建议使用 Azure ML 计算实例,这是一种完全托管且安全的开发环境,也可以作为 ML 训练的计算目标。
计算实例提供集成的 Jupyter notebook 服务、JupyterLab、Azure ML Python SDK、CLI 和其他必备工具。
选择您的实例#
登录 Azure Machine Learning Studio 并导航到左侧菜单上的工作区。
选择 新建 > 计算实例 (创建计算实例) > 选择一个Azure RAPIDS 兼容的 GPU VM 大小(例如,Standard_NC12s_v3
)
预配 RAPIDS 安装脚本#
导航到应用程序部分。选择“使用创建脚本预配”来安装 RAPIDS 和依赖项。
将以下内容放入一个名为 rapids-azure-startup.sh
的本地文件中
sudo -u azureuser -i <<'EOF' source /anaconda/etc/profile.d/conda.sh conda create -y -n rapids \ -c rapidsai -c conda-forge -c nvidia \ -c microsoft \ rapids=25.04 python=3.12 cuda-version=12.8 \ 'azure-identity>=1.19' \ ipykernel conda activate rapids pip install 'azure-ai-ml>=1.24' python -m ipykernel install --user --name rapids echo "kernel install completed" EOF
选择 本地文件
,然后选择 浏览
,并上传该脚本。
有关如何创建安装脚本的更多详细信息,请参阅Azure ML 文档。
启动实例。
选择 RAPIDS 环境#
Notebook 实例处于 运行中
后,打开“JupyterLab”并在处理新的 notebook 时选择 rapids
内核。
Azure ML 计算集群#
在下一节中,我们将启动 Azure 的 ML 计算集群,以便将您的 RAPIDS 训练作业分发到由单个或多个 GPU 计算节点组成的集群中。
提交作业时,计算集群会自动扩容,并在容器化环境中执行,将您的模型依赖项打包在 Docker 容器中。
实例化工作区#
使用 Azure 的客户端库设置一些资源。
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
# Get a handle to the workspace.
#
# Azure ML places the workspace config at the default working
# directory for notebooks by default.
#
# If it isn't found, open a shell and look in the
# directory indicated by 'echo ${JUPYTER_SERVER_ROOT}'.
ml_client = MLClient.from_config(
credential=DefaultAzureCredential(),
path="./config.json",
)
创建 AMLCompute#
您需要使用 Azure ML 托管计算 (AmlCompute) 创建计算目标以进行远程训练。
注意
务必检查您创建计算实例的区域中的实例可用性及其限制。
这篇文章详细介绍了默认限制以及如何请求更多配额。
[size]:节点的 VM 系列。从 NC_v2、NC_v3、ND 或 ND_v2 GPU 虚拟机中指定一种(例如 Standard_NC12s_v3
)
[max_instances]:运行作业时自动扩容到的最大节点数
注意
您可以选择使用低优先级 VM 来运行您的工作负载。这些 VM 不保证可用性,但允许您利用 Azure 未使用的容量,从而显著节省成本。可用容量的数量可能因大小、区域、一天中的时间等因素而异。
from azure.ai.ml.entities import AmlCompute
gpu_compute = AmlCompute(
name="rapids-cluster",
type="amlcompute",
size="Standard_NC12s_v3", # this VM type needs to be available in your current region
max_instances=3,
idle_time_before_scale_down=300, # Seconds of idle time before scaling down
tier="low_priority", # optional
)
ml_client.begin_create_or_update(gpu_compute).result()
如果您将集群命名为 "rapids-cluster"
,您可以访问 https://ml.azure.com/compute/rapids-cluster/details 查看集群的详细信息。
访问数据存储 URI#
数据存储 URI 是对您的 Azure 账户上 blob 存储位置(路径)的引用。您可以从 AzureML Studio UI 中复制粘贴数据存储 URI。
从左侧菜单中选择 数据 > 数据存储 > 选择您的数据存储名称 > 浏览
找到包含您数据集的文件/文件夹,然后点击其旁边的省略号(…)。
从菜单中选择 复制 URI,然后选择 数据存储 URI 格式以复制到您的 notebook 中。
自定义 RAPIDS 环境#
要运行 AzureML 实验,您必须指定一个环境,其中包含在分布式节点上运行训练脚本所需的所有软件依赖项。
您可以从预构建的 docker 镜像定义环境,或从 Dockerfile 或 conda 规范文件创建自己的环境。
在 notebook 单元格中,运行以下命令将本文档中的示例代码复制到新文件夹中,并创建一个 Dockerfile 来构建镜像,该镜像基于 RAPIDS 镜像并安装工作流所需的额外软件包。
%%bash mkdir -p ./training-code repo_url='https://raw.githubusercontent.com/rapidsai/deployment/refs/heads/main/source/examples' # download training scripts wget -O ./training-code/train_rapids.py "${repo_url}/rapids-azureml-hpo/train_rapids.py" wget -O ./training-code/rapids_csp_azure.py "${repo_url}/rapids-azureml-hpo/rapids_csp_azure.py" touch ./training-code/__init__.py # create a Dockerfile defining the image the code will run in cat > ./training-code/Dockerfile <<EOF FROM nvcr.io/nvidia/rapidsai/base:25.04-cuda12.8-py3.12 RUN conda install --yes -c conda-forge 'dask-ml>=2024.4.4' \ && pip install azureml-mlflow EOF
现在创建环境,确保添加标签并提供描述
from azure.ai.ml.entities import Environment, BuildContext
# NOTE: 'path' should be a filepath pointing to a directory containing a file named 'Dockerfile'
env_docker_image = Environment(
build=BuildContext(path="./training-code/"),
name="rapids-mlflow", # label
description="RAPIDS environment with azureml-mlflow",
)
ml_client.environments.create_or_update(env_docker_image)
提交 RAPIDS 训练作业#
现在我们有了环境和自定义逻辑,我们可以配置并运行 command
类来提交训练作业。
inputs
是要传递给训练脚本的命令行参数字典。
from azure.ai.ml import command, Input
# replace this with your own dataset
datastore_name = "workspaceartifactstore"
dataset = "airline_20000000.parquet"
data_uri = f"azureml://subscriptions/{ml_client.subscription_id}/resourcegroups/{ml_client.resource_group_name}/workspaces/{ml_client.workspace_name}/datastores/{datastore_name}/paths/{dataset}"
command_job = command(
environment=f"{env_docker_image.name}:{env_docker_image.version}",
experiment_name="test_rapids_mlflow",
code="./training-code",
command="python train_rapids.py \
--data_dir ${{inputs.data_dir}} \
--n_bins ${{inputs.n_bins}} \
--cv_folds ${{inputs.cv_folds}} \
--n_estimators ${{inputs.n_estimators}} \
--max_depth ${{inputs.max_depth}} \
--max_features ${{inputs.max_features}}",
inputs={
"data_dir": Input(type="uri_file", path=data_uri),
"n_bins": 32,
"cv_folds": 5,
"n_estimators": 50,
"max_depth": 10,
"max_features": 1.0,
},
compute=gpu_compute.name,
)
# submit training job
returned_job = ml_client.jobs.create_or_update(command_job)
returned_job # displays status and details page of the experiment
创建作业后,点击 returned_job
输出中提供的详细信息页面,或转到“实验”页面查看日志、指标和输出。
注意
作为参考,此作业在使用 gpu_compute
创建中的 size="Standard_NC6s_v3"
时花费了约 7 分钟
接下来,我们可以对一组超参数执行扫描。
from azure.ai.ml.sweep import Choice, Uniform
# define hyperparameter space to sweep over
command_job_for_sweep = command_job(
n_estimators=Choice(values=range(50, 500)),
max_depth=Choice(values=range(5, 19)),
max_features=Uniform(min_value=0.2, max_value=1.0),
)
# apply hyperparameter sweep_job
sweep_job = command_job_for_sweep.sweep(
compute=gpu_compute.name,
sampling_algorithm="random",
primary_metric="Accuracy",
goal="Maximize",
)
# setting a very small limit of trials for demo purposes
sweep_job.set_limits(
max_total_trials=3, max_concurrent_trials=3, timeout=18000, trial_timeout=3600
)
# submit job
returned_sweep_job = ml_client.create_or_update(sweep_job)
returned_sweep_job
创建作业后,点击 returned_sweep_job
输出中提供的详细信息页面,或转到“实验”页面查看日志、指标和输出。在使用 size="Standard_NC6s_v3"
时,sweep_job.set_limits(...)
中设置的三次试验需要 20-40 分钟才能完成。
清理#
完成后,移除计算资源。
ml_client.compute.begin_delete(gpu_compute.name).wait()
然后访问 https://ml.azure.com/compute/list/instances 并确保您的计算实例也已停止,如果需要,可以删除。