Skip to content

Commit e3e6119

Browse files
authored
Merge pull request #105883 from rastala/master
explain environment caching
2 parents 6cc9872 + a695ef9 commit e3e6119

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

articles/machine-learning/concept-environments.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ms.date: 01/06/2020
1414
# What are Azure Machine Learning environments?
1515
[!INCLUDE [applies-to-skus](../../includes/aml-applies-to-basic-enterprise-sku.md)]
1616

17-
Azure Machine Learning environments specify the Python packages, environment variables, and software settings around your training and scoring scripts. They also specify run times (Python, Spark, or Docker). They are managed and versioned entities within your Machine Learning workspace that enable reproducible, auditable, and portable machine learning workflows across a variety of compute targets.
17+
Azure Machine Learning environments specify the Python packages, environment variables, and software settings around your training and scoring scripts. They also specify run times (Python, Spark, or Docker). The environments are managed and versioned entities within your Machine Learning workspace that enable reproducible, auditable, and portable machine learning workflows across a variety of compute targets.
1818

1919
You can use an `Environment` object on your local compute to:
2020
* Develop your training script.
@@ -53,6 +53,45 @@ For specific code samples, see the "Create an environment" section of [Reuse env
5353

5454
For code samples, see the "Manage environments" section of [Reuse environments for training and deployment](how-to-use-environments.md#manage-environments).
5555

56+
## Environment building, caching, and reuse
57+
58+
The Azure Machine Learning service builds environment definitions into Docker images and conda environments. It also caches the environments so they can be reused in subsequent training runs and service endpoint deployments.
59+
60+
### Building environments as Docker images
61+
62+
Typically, when you first submit a run using an environment, the Azure Machine Learning service invokes an [ACR Build Task](https://docs.microsoft.com/azure/container-registry/container-registry-tasks-overview) on the Azure Container Registry (ACR) associated with the Workspace. The built Docker image is then cached on the Workspace ACR. At the start of the run execution, the image is retrieved by the compute target.
63+
64+
The image build consists of two steps:
65+
66+
1. Downloading a base image, and executing any Docker steps
67+
2. Building a conda environment according to conda dependencies specified in the environment definition.
68+
69+
The second step is omitted if you specify [user-managed dependencies](https://docs.microsoft.com/python/api/azureml-core/azureml.core.environment.pythonsection?view=azure-ml-py). In this case you're responsible for installing any Python packages, by including them in your base image, or specifying custom Docker steps within the first step. You're also responsible for specifying the correct location for the Python executable.
70+
71+
### Image caching and reuse
72+
73+
If you use the same environment definition for another run, the Azure Machine Learning service reuses the cached image from the Workspace ACR.
74+
75+
To view the details of a cached image, use [Environment.get_image_details](https://docs.microsoft.com/python/api/azureml-core/azureml.core.environment.environment?view=azure-ml-py#get-image-details-workspace-) method.
76+
77+
To determine whether to reuse a cached image or build a new one, the service computes [a hash value](https://en.wikipedia.org/wiki/Hash_table) from the environment definition and compares it to the hashes of existing environments. The hash is based on:
78+
79+
* Base image property value
80+
* Custom docker steps property value
81+
* List of Python packages in Conda definition
82+
* List of packages in Spark definition
83+
84+
The hash doesn't depend on environment name or version. Environment definition changes, such as adding or removing a Python package or changing the package version, causes the hash value to change and triggers an image rebuild. However, if you simply rename your environment or create a new environment with the exact properties and packages of an existing one, then the hash value remains the same and the cached image is used.
85+
86+
See the following diagram that shows three environment definitions. Two of them have different name and version, but identical base image and Python packages. They have the same hash and therefore correspond to the same cached image. The third environment has different Python packages and versions, and therefore corresponds to a different cached image.
87+
88+
![Diagram of environment caching as Docker images](./media/concept-environments/environment-caching.png)
89+
90+
If you create an environment with unpinned package dependency, for example ```numpy```, that environment will keep using the package version installed at the time of environment creation. Also, any future environment with matching definition will keep using the old version. To update the package, specify a version number to force image rebuild, for example ```numpy==1.18.1```. Note that new dependencies, including nested ones will be installed that might break a previously working scenario
91+
92+
> [!WARNING]
93+
> The [Environment.build](https://docs.microsoft.com/python/api/azureml-core/azureml.core.environment.environment?view=azure-ml-py#build-workspace-) method will rebuild the cached image, with possible side-effect of updating unpinned packages and breaking reproducibility for all environment definitions corresponding to that cached image.
94+
5695
## Next steps
5796

5897
* Learn how to [create and use environments](how-to-use-environments.md) in Azure Machine Learning.
15.6 KB
Loading

0 commit comments

Comments
 (0)