Skip to content

Commit 897a059

Browse files
authored
Merge pull request #212675 from Blackmist/v2-environments
V2 environments
2 parents 2b064fb + aebfa39 commit 897a059

File tree

1 file changed

+194
-40
lines changed

1 file changed

+194
-40
lines changed

articles/machine-learning/how-to-manage-environments-v2.md

Lines changed: 194 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,107 @@
11
---
2-
title: 'Manage Azure Machine Learning environments with the CLI (v2)'
2+
title: 'Manage Azure Machine Learning environments with the CLI & SDK (v2)'
33
titleSuffix: Azure Machine Learning
4-
description: Learn how to manage Azure ML environments using Azure CLI extension for Machine Learning.
4+
description: Learn how to manage Azure ML environments using Python SDK and Azure CLI extension for Machine Learning.
55
services: machine-learning
66
ms.service: machine-learning
77
ms.subservice: core
88
ms.topic: how-to
99
author: blackmist
1010
ms.author: larryfr
11-
ms.date: 03/31/2022
11+
ms.date: 09/27/2022
1212
ms.reviewer: nibaccam
1313
ms.custom: devx-track-azurecli, devplatv2, event-tier1-build-2022
1414
---
1515

16-
# Manage Azure Machine Learning environments with the CLI (v2)
16+
# Manage Azure Machine Learning environments with the CLI & SDK (v2)
1717

18-
[!INCLUDE [cli v2](../../includes/machine-learning-cli-v2.md)]
18+
[!INCLUDE [dev v2](../../includes/machine-learning-dev-v2.md)]
1919

20-
> [!div class="op_single_selector" title1="Select the version of Azure Machine Learning CLI extension you are using:"]
20+
> [!div class="op_single_selector" title1="Select the version of Azure Machine Learning SDK or CLI extension you are using:"]
2121
> * [v1](./v1/how-to-use-environments.md)
2222
> * [v2 (current version)](how-to-manage-environments-v2.md)
2323
2424

2525

2626
Azure Machine Learning environments define the execution environments for your jobs or deployments and encapsulate the dependencies for your code. Azure ML uses the environment specification to create the Docker container that your training or scoring code runs in on the specified compute target. You can define an environment from a conda specification, Docker image, or Docker build context.
2727

28-
In this article, learn how to create and manage Azure ML environments using the CLI (v2).
28+
In this article, learn how to create and manage Azure ML environments using the SDK & CLI (v2).
2929

3030

3131
## Prerequisites
3232

33-
- To use the CLI, you must have an Azure subscription. If you don't have an Azure subscription, create a free account before you begin. Try the [free or paid version of Azure Machine Learning](https://azure.microsoft.com/free/) today.
34-
- [Install and set up the Azure CLI extension for Machine Learning](how-to-configure-cli.md)
33+
[!INCLUDE [sdk/cliv2](../../includes/machine-learning-cli-sdk-v2-prereqs.md)]
3534

3635
> [!TIP]
3736
> For a full-featured development environment, use Visual Studio Code and the [Azure Machine Learning extension](how-to-setup-vs-code.md) to [manage Azure Machine Learning resources](how-to-manage-resources-vscode.md) and [train machine learning models](tutorial-train-deploy-image-classification-model-vscode.md).
3837
3938
### Clone examples repository
4039

41-
To run the training examples, first clone the examples repository and change into the `cli` directory:
40+
To run the training examples, first clone the examples repository. For the CLI examples, change into the `cli` directory. For the SDK examples, change into the `SDK` directory:
4241

43-
:::code language="azurecli" source="~/azureml-examples-main/cli/misc.sh" id="git_clone":::
42+
```azurecli
43+
git clone --depth 1 https://github.com/Azure/azureml-examples
44+
```
45+
46+
Note that `--depth 1` clones only the latest commit to the repository, which reduces time to complete the operation.
47+
48+
### Connect to the workspace
49+
50+
> [!TIP]
51+
> Use the tabs below to select the method you want to use to work with environments. Selecting a tab will automatically switch all the tabs in this article to the same tab. You can select another tab at any time.
52+
53+
# [Azure CLI](#tab/cli)
54+
55+
When using the Azure CLI, you need identifier parameters - a subscription, resource group, and workspace name. While you can specify these parameters for each command, you can also set defaults that will be used for all the commands. Use the following commands to set default values. Replace `<subscription ID>`, `<AzureML workspace name>`, and `<resource group>` with the values for your configuration:
56+
57+
```azurecli
58+
az account set --subscription <subscription ID>
59+
az configure --defaults workspace=<AzureML workspace name> group=<resource group>
60+
```
61+
62+
# [Python SDK](#tab/python)
63+
64+
To connect to the workspace, you need identifier parameters - a subscription, resource group, and workspace name. You'll use these details in the `MLClient` from the `azure.ai.ml` namespace to get a handle to the required Azure Machine Learning workspace. To authenticate, you use the [default Azure authentication](/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python&preserve-view=true). Check this [example](https://github.com/Azure/azureml-examples/blob/v2samplesreorg/sdk/python/jobs/configuration.ipynb) for more details on how to configure credentials and connect to a workspace.
65+
66+
```python
67+
#import required libraries for workspace
68+
from azure.ai.ml import MLClient
69+
from azure.identity import DefaultAzureCredential
4470

45-
Note that `--depth 1` clones only the latest commit to the repository which reduces time to complete the operation.
71+
#import required libraries for environments examples
72+
from azure.ai.ml.entities import Environment, BuildContext
73+
74+
#Enter details of your AzureML workspace
75+
subscription_id = '<SUBSCRIPTION_ID>'
76+
resource_group = '<RESOURCE_GROUP>'
77+
workspace = '<AZUREML_WORKSPACE_NAME>'
78+
79+
#connect to the workspace
80+
ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource_group, workspace)
81+
```
82+
83+
---
4684

4785
## Curated environments
4886

4987
There are two types of environments in Azure ML: curated and custom environments. Curated environments are predefined environments containing popular ML frameworks and tooling. Custom environments are user-defined and can be created via `az ml environment create`.
5088

51-
Curated environments are provided by Azure ML and are available in your workspace by default. Azure ML routinely updates these environments with the latest framework version releases and maintains them for bug fixes and security patches. They are backed by cached Docker images, which reduces job preparation cost and model deployment time.
89+
Curated environments are provided by Azure ML and are available in your workspace by default. Azure ML routinely updates these environments with the latest framework version releases and maintains them for bug fixes and security patches. They're backed by cached Docker images, which reduce job preparation cost and model deployment time.
5290

5391
You can use these curated environments out of the box for training or deployment by referencing a specific environment using the `azureml:<curated-environment-name>:<version>` or `azureml:<curated-environment-name>@latest` syntax. You can also use them as reference for your own custom environments by modifying the Dockerfiles that back these curated environments.
5492

5593
You can see the set of available curated environments in the Azure ML studio UI, or by using the CLI (v2) via `az ml environments list`.
5694

5795
## Create an environment
5896

59-
You can define an environment from a conda specification, Docker image, or Docker build context. Configure the environment using a YAML specification file and create the environment using the following CLI command:
60-
61-
```cli
62-
az ml environment create --file my_environment.yml
63-
```
64-
65-
For the YAML reference documentation for Azure ML environments, see [CLI (v2) environment YAML schema](reference-yaml-environment.md).
97+
You can define an environment from a Docker image, a Docker build context, and a conda specification with Docker image.
6698

6799
### Create an environment from a Docker image
68100

69101
To define an environment from a Docker image, provide the image URI of the image hosted in a registry such as Docker Hub or Azure Container Registry.
70102

103+
# [Azure CLI](#tab/cli)
104+
71105
The following example is a YAML specification file for an environment defined from a Docker image. An image from the official PyTorch repository on Docker Hub is specified via the `image` property in the YAML file.
72106

73107
:::code language="yaml" source="~/azureml-examples-main/cli/assets/environment/docker-image.yml":::
@@ -78,6 +112,21 @@ To create the environment:
78112
az ml environment create --file assets/environment/docker-image.yml
79113
```
80114

115+
# [Python SDK](#tab/python)
116+
117+
The following example creates an environment from a Docker image. An image from the official PyTorch repository on Docker Hub is specified via the `image` property.
118+
119+
```python
120+
env_docker_image = Environment(
121+
image="pytorch/pytorch:latest",
122+
name="docker-image-example",
123+
description="Environment created from a Docker image.",
124+
)
125+
ml_client.environments.create_or_update(env_docker_image)
126+
```
127+
128+
---
129+
81130
> [!TIP]
82131
> Azure ML maintains a set of CPU and GPU Ubuntu Linux-based base images with common system dependencies. For example, the GPU images contain Miniconda, OpenMPI, CUDA, cuDNN, and NCCL. You can use these images for your environments, or use their corresponding Dockerfiles as reference when building your own custom images.
83132
>
@@ -87,6 +136,8 @@ az ml environment create --file assets/environment/docker-image.yml
87136

88137
Instead of defining an environment from a prebuilt image, you can also define one from a Docker [build context](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#understand-build-context). To do so, specify the directory that will serve as the build context. This directory should contain a Dockerfile and any other files needed to build the image.
89138

139+
# [Azure CLI](#tab/cli)
140+
90141
The following example is a YAML specification file for an environment defined from a build context. The local path to the build context folder is specified in the `build.path` field, and the relative path to the Dockerfile within that build context folder is specified in the `build.dockerfile_path` field. If `build.dockerfile_path` is omitted in the YAML file, Azure ML will look for a Dockerfile named `Dockerfile` at the root of the build context.
91142

92143
In this example, the build context contains a Dockerfile named `Dockerfile` and a `requirements.txt` file that is referenced within the Dockerfile for installing Python packages.
@@ -99,13 +150,30 @@ To create the environment:
99150
az ml environment create --file assets/environment/docker-context.yml
100151
```
101152

153+
# [Python SDK](#tab/python)
154+
155+
In the following example, the local path to the build context folder is specified in the `path' parameter. Azure ML will look for a Dockerfile named `Dockerfile` at the root of the build context.
156+
157+
```python
158+
env_docker_context = Environment(
159+
build=BuildContext(path="docker-contexts/python-and-pip"),
160+
name="docker-context-example",
161+
description="Environment created from a Docker context.",
162+
)
163+
ml_client.environments.create_or_update(env_docker_context)
164+
```
165+
166+
---
167+
102168
Azure ML will start building the image from the build context when the environment is created. You can monitor the status of the build and view the build logs in the studio UI.
103169

104170
### Create an environment from a conda specification
105171

106172
You can define an environment using a standard conda YAML configuration file that includes the dependencies for the conda environment. See [Creating an environment manually](https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-file-manually) for information on this standard format.
107173

108-
You must also specify a base Docker image for this environment. Azure ML will build the conda environment on top of the Docker image provided. If you install some Python dependencies in your Docker image, those packages will not exist in the execution environment thus causing runtime failures. By default, Azure ML will build a Conda environment with dependencies you specified, and will execute the job in that environment instead of using any Python libraries that you installed on the base image.
174+
You must also specify a base Docker image for this environment. Azure ML will build the conda environment on top of the Docker image provided. If you install some Python dependencies in your Docker image, those packages won't exist in the execution environment thus causing runtime failures. By default, Azure ML will build a Conda environment with dependencies you specified, and will execute the job in that environment instead of using any Python libraries that you installed on the base image.
175+
176+
## [Azure CLI](#tab/cli)
109177

110178
The following example is a YAML specification file for an environment defined from a conda specification. Here the relative path to the conda file from the Azure ML environment YAML file is specified via the `conda_file` property. You can alternatively define the conda specification inline using the `conda_file` property, rather than defining it in a separate file.
111179

@@ -117,91 +185,177 @@ To create the environment:
117185
az ml environment create --file assets/environment/docker-image-plus-conda.yml
118186
```
119187

188+
## [Python SDK](#tab/python)
189+
190+
The relative path to the conda file is specified using the `conda_file` parameter.
191+
192+
```python
193+
env_docker_conda = Environment(
194+
image="mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04",
195+
conda_file="conda-yamls/pydata.yml",
196+
name="docker-image-plus-conda-example",
197+
description="Environment created from a Docker image plus Conda environment.",
198+
)
199+
ml_client.environments.create_or_update(env_docker_conda)
200+
```
201+
202+
---
203+
120204
Azure ML will build the final Docker image from this environment specification when the environment is used in a job or deployment. You can also manually trigger a build of the environment in the studio UI.
121205

122206
## Manage environments
123207

124-
The CLI (v2) provides a set of commands under `az ml environment` for managing the lifecycle of your Azure ML environment assets.
208+
The SDK and CLI (v2) also allow you to manage the lifecycle of your Azure ML environment assets.
125209

126210
### List
127211

128212
List all the environments in your workspace:
129213

214+
# [Azure CLI](#tab/cli)
215+
130216
```cli
131217
az ml environment list
132218
```
133219

220+
# [Python SDK](#tab/python)
221+
222+
```python
223+
envs = ml_client.environments.list()
224+
for env in envs:
225+
print(env.name)
226+
```
227+
228+
---
229+
134230
List all the environment versions under a given name:
135231

232+
# [Azure CLI](#tab/cli)
233+
136234
```cli
137235
az ml environment list --name docker-image-example
138236
```
139237

238+
# [Python SDK](#tab/python)
239+
240+
```python
241+
envs = ml_client.environments.list(name="docker-image-example")
242+
for env in envs:
243+
print(env.version)
244+
```
245+
246+
---
247+
140248
### Show
141249

142250
Get the details of a specific environment:
143251

252+
# [Azure CLI](#tab/cli)
253+
144254
```cli
145255
az ml environment list --name docker-image-example --version 1
146256
```
147257

258+
# [Python SDK](#tab/python)
259+
260+
```python
261+
env = ml_client.environments.get(name="docker-image-example", version="1")
262+
print(env)
263+
```
264+
---
265+
148266
### Update
149267

150268
Update mutable properties of a specific environment:
151269

270+
# [Azure CLI](#tab/cli)
271+
152272
```cli
153273
az ml environment update --name docker-image-example --version 1 --set description="This is an updated description."
154274
```
155275

276+
# [Python SDK](#tab/python)
277+
278+
```python
279+
env.description="This is an updated description."
280+
ml_client.environments.create_or_update(environment=env)
281+
```
282+
---
283+
156284
> [!IMPORTANT]
157285
> For environments, only `description` and `tags` can be updated. All other properties are immutable; if you need to change any of those properties you should create a new version of the environment.
158286
159-
### Archive and restore
287+
### Archive
160288

161-
Archiving an environment will hide it by default from list queries (`az ml environment list`). You can still continue to reference and use an archived environment in your workflows. You can archive either an environment container or a specific environment version.
289+
Archiving an environment will hide it by default from list queries (`az ml environment list`). You can still continue to reference and use an archived environment in your workflows. You can archive either all versions of an environment or only a specific version.
162290

163-
Archiving an environment container will archive all versions of the environment under that given name. If you create a new environment version under an archived environment container, that new version will automatically be set as archived as well.
291+
If you don't specify a version, all versions of the environment under that given name will be archived. If you create a new environment version under an archived environment container, that new version will automatically be set as archived as well.
292+
293+
Archive all versions of an environment:
294+
295+
# [Azure CLI](#tab/cli)
164296

165-
Archive an environment container:
166297
```cli
167298
az ml environment archive --name docker-image-example
168-
```
299+
```
300+
301+
# [Python SDK](#tab/python)
302+
303+
```python
304+
ml_client.environments.archive(name="docker-image-example")
305+
```
306+
307+
---
169308
170309
Archive a specific environment version:
310+
311+
# [Azure CLI](#tab/cli)
312+
171313
```cli
172314
az ml environment archive --name docker-image-example --version 1
173315
```
174316

175-
You can restore an archived environment to no longer hide it from list queries.
176-
177-
If an entire environment container is archived, you can restore that archived container. You cannot restore only a specific environment version if the entire environment container is archived - you will need to restore the entire container.
317+
# [Python SDK](#tab/python)
178318

179-
Restore an environment container:
180-
```cli
181-
az ml environment restore --name docker-image-example
182-
```
319+
```python
320+
ml_client.environments.archive(name="docker-image-example", version="1")
321+
```
183322

184-
If only individual environment version(s) within an environment container are archived, you can restore those individual version(s).
323+
---
185324

186-
Restore a specific environment version:
187-
```cli
188-
az ml environment restore --name docker-image-example --version 1
189-
```
190325

191326
## Use environments for training
192327

193-
To use an environment for a training job, specify the `environment` field of the job YAML configuration. You can either reference an existing registered Azure ML environment via `environment: azureml:<environment-name>:<environment-version>` or `environment: azureml:<environment-name>@latest` (to reference the latest version of an environment), or define an environment specification inline. If defining an environment inline, do not specify the `name` and `version` fields, as these environments are treated as "unregistered" environments and are not tracked in your environment asset registry.
328+
# [Azure CLI](#tab/cli)
329+
330+
To use an environment for a training job, specify the `environment` field of the job YAML configuration. You can either reference an existing registered Azure ML environment via `environment: azureml:<environment-name>:<environment-version>` or `environment: azureml:<environment-name>@latest` (to reference the latest version of an environment), or define an environment specification inline. If defining an environment inline, don't specify the `name` and `version` fields, as these environments are treated as "unregistered" environments and aren't tracked in your environment asset registry.
194331

332+
# [Python SDK](#tab/python)
333+
334+
To use an environment for a training job, specify the `environment` property of the [command](/python/api/azure-ai-ml/azure.ai.ml#azure-ai-ml-command).
335+
336+
For examples of submitting jobs, see the examples at [https://github.com/Azure/azureml-examples/tree/main/sdk/python/jobs](https://github.com/Azure/azureml-examples/tree/main/sdk/python/jobs).
337+
338+
---
195339
When you submit a training job, the building of a new environment can take several minutes. The duration depends on the size of the required dependencies. The environments are cached by the service. So as long as the environment definition remains unchanged, you incur the full setup time only once.
196340

341+
---
342+
197343
For more information on how to use environments in jobs, see [Train models](how-to-train-model.md).
198344

199345
## Use environments for model deployments
200346

347+
# [Azure CLI](#tab/cli)
348+
201349
You can also use environments for your model deployments for both online and batch scoring. To do so, specify the `environment` field in the deployment YAML configuration.
202350

203351
For more information on how to use environments in deployments, see [Deploy and score a machine learning model by using a managed online endpoint](how-to-deploy-managed-online-endpoints.md).
204352

353+
# [Python SDK](#tab/python)
354+
355+
You can also use environments for your model deployments. For more information, see [Deploy and score a machine learning model](how-to-deploy-managed-online-endpoint-sdk-v2.md).
356+
357+
---
358+
205359
## Next steps
206360

207361
- [Train models (create jobs)](how-to-train-model.md)

0 commit comments

Comments
 (0)