You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
27
27
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).
29
29
30
30
31
31
## Prerequisites
32
32
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)
> 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).
38
37
39
38
### Clone examples repository
40
39
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:
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
44
70
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
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`.
50
88
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.
52
90
53
91
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.
54
92
55
93
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`.
56
94
57
95
## Create an environment
58
96
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.
66
98
67
99
### Create an environment from a Docker image
68
100
69
101
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.
70
102
103
+
# [Azure CLI](#tab/cli)
104
+
71
105
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.
az ml environment create --file assets/environment/docker-image.yml
79
113
```
80
114
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.",
> 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.
83
132
>
@@ -87,6 +136,8 @@ az ml environment create --file assets/environment/docker-image.yml
87
136
88
137
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.
89
138
139
+
# [Azure CLI](#tab/cli)
140
+
90
141
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.
91
142
92
143
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:
99
150
az ml environment create --file assets/environment/docker-context.yml
100
151
```
101
152
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.
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.
103
169
104
170
### Create an environment from a conda specification
105
171
106
172
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.
107
173
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)
109
177
110
178
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.
111
179
@@ -117,91 +185,177 @@ To create the environment:
117
185
az ml environment create --file assets/environment/docker-image-plus-conda.yml
118
186
```
119
187
188
+
## [Python SDK](#tab/python)
189
+
190
+
The relative path to the conda file is specified using the `conda_file` parameter.
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.
121
205
122
206
## Manage environments
123
207
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.
125
209
126
210
### List
127
211
128
212
List all the environments in your workspace:
129
213
214
+
# [Azure CLI](#tab/cli)
215
+
130
216
```cli
131
217
az ml environment list
132
218
```
133
219
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
+
134
230
List all the environment versions under a given name:
135
231
232
+
# [Azure CLI](#tab/cli)
233
+
136
234
```cli
137
235
az ml environment list --name docker-image-example
> 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.
158
286
159
-
### Archive and restore
287
+
### Archive
160
288
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.
162
290
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)
164
296
165
-
Archive an environment container:
166
297
```cli
167
298
az ml environment archive --name docker-image-example
az ml environment archive --name docker-image-example --version 1
173
315
```
174
316
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)
178
318
179
-
Restore an environment container:
180
-
```cli
181
-
az ml environment restore --name docker-image-example
If only individual environment version(s) within an environment container are archived, you can restore those individual version(s).
323
+
---
185
324
186
-
Restore a specific environment version:
187
-
```cli
188
-
az ml environment restore --name docker-image-example --version 1
189
-
```
190
325
191
326
## Use environments for training
192
327
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.
194
331
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
+
---
195
339
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.
196
340
341
+
---
342
+
197
343
For more information on how to use environments in jobs, see [Train models](how-to-train-model.md).
198
344
199
345
## Use environments for model deployments
200
346
347
+
# [Azure CLI](#tab/cli)
348
+
201
349
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.
202
350
203
351
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).
204
352
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).
0 commit comments