|
| 1 | +# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Functions for generating ECR image URIs for pre-built SageMaker Docker images.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +import json |
| 17 | +import os |
| 18 | + |
| 19 | +from sagemaker import utils |
| 20 | + |
| 21 | +ECR_URI_TEMPLATE = "{registry}.dkr.{hostname}/{repository}:{tag}" |
| 22 | + |
| 23 | + |
| 24 | +def retrieve(framework, region, version=None, py_version=None, instance_type=None): |
| 25 | + """Retrieves the ECR URI for the Docker image matching the given arguments. |
| 26 | +
|
| 27 | + Args: |
| 28 | + framework (str): The name of the framework. |
| 29 | + region (str): The AWS region. |
| 30 | + version (str): The framework version. This is required if there is |
| 31 | + more than one supported version for the given framework. |
| 32 | + py_version (str): The Python version. This is required if there is |
| 33 | + more than one supported Python version for the given framework version. |
| 34 | + instance_type (str): The SageMaker instance type. For supported types, see |
| 35 | + https://aws.amazon.com/sagemaker/pricing/instance-types. This is required if |
| 36 | + there are different images for different processor types. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + str: the ECR URI for the corresponding SageMaker Docker image. |
| 40 | +
|
| 41 | + Raises: |
| 42 | + ValueError: If the framework version, Python version, processor type, or region is |
| 43 | + not supported given the other arguments. |
| 44 | + """ |
| 45 | + config = config_for_framework(framework) |
| 46 | + version_config = config["versions"][_version_for_config(version, config, framework)] |
| 47 | + |
| 48 | + registry = _registry_from_region(region, version_config["registries"]) |
| 49 | + hostname = utils._botocore_resolver().construct_endpoint("ecr", region)["hostname"] |
| 50 | + |
| 51 | + repo = version_config["repository"] |
| 52 | + |
| 53 | + _validate_py_version(py_version, version_config["py_versions"], framework, version) |
| 54 | + tag = "{}-{}-{}".format(version, _processor(instance_type, config["processors"]), py_version) |
| 55 | + |
| 56 | + return ECR_URI_TEMPLATE.format(registry=registry, hostname=hostname, repository=repo, tag=tag) |
| 57 | + |
| 58 | + |
| 59 | +def config_for_framework(framework): |
| 60 | + """Loads the JSON config for the given framework.""" |
| 61 | + fname = os.path.join(os.path.dirname(__file__), "image_uri_config", "{}.json".format(framework)) |
| 62 | + with open(fname) as f: |
| 63 | + return json.load(f) |
| 64 | + |
| 65 | + |
| 66 | +def _version_for_config(version, config, framework): |
| 67 | + """Returns the version string for retrieving a framework version's specific config.""" |
| 68 | + if "version_aliases" in config: |
| 69 | + if version in config["version_aliases"].keys(): |
| 70 | + return config["version_aliases"][version] |
| 71 | + |
| 72 | + available_versions = config["versions"].keys() |
| 73 | + if version in available_versions: |
| 74 | + return version |
| 75 | + |
| 76 | + raise ValueError( |
| 77 | + "Unsupported {} version: {}. " |
| 78 | + "You may need to upgrade your SDK version (pip install -U sagemaker) newer versions. " |
| 79 | + "Supported version(s): {}.".format(framework, version, ", ".join(available_versions)) |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def _registry_from_region(region, registry_dict): |
| 84 | + """Returns the ECR registry (AWS account number) for the given region.""" |
| 85 | + available_regions = registry_dict.keys() |
| 86 | + if region not in available_regions: |
| 87 | + raise ValueError( |
| 88 | + "Unsupported region: {}. " |
| 89 | + "You may need to upgrade your SDK version (pip install -U sagemaker) newer regions. " |
| 90 | + "Supported region(s): {}.".format(region, ", ".join(available_regions)) |
| 91 | + ) |
| 92 | + |
| 93 | + return registry_dict[region] |
| 94 | + |
| 95 | + |
| 96 | +def _processor(instance_type, available_processors): |
| 97 | + """Returns the processor type for the given instance type.""" |
| 98 | + if instance_type.startswith("local"): |
| 99 | + processor = "cpu" if instance_type == "local" else "gpu" |
| 100 | + elif not instance_type.startswith("ml."): |
| 101 | + raise ValueError( |
| 102 | + "Invalid SageMaker instance type: {}. See: " |
| 103 | + "https://aws.amazon.com/sagemaker/pricing/instance-types".format(instance_type) |
| 104 | + ) |
| 105 | + else: |
| 106 | + family = instance_type.split(".")[1] |
| 107 | + processor = "gpu" if family[0] in ("g", "p") else "cpu" |
| 108 | + |
| 109 | + if processor in available_processors: |
| 110 | + return processor |
| 111 | + |
| 112 | + raise ValueError( |
| 113 | + "Unsupported processor type: {} (for {}). " |
| 114 | + "Supported type(s): {}.".format(processor, instance_type, ", ".join(available_processors)) |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def _validate_py_version(py_version, available_versions, framework, fw_version): |
| 119 | + """Checks if the Python version is one of the supported versions.""" |
| 120 | + if py_version not in available_versions: |
| 121 | + raise ValueError( |
| 122 | + "Unsupported Python version for {} {}: {}. " |
| 123 | + "You may need to upgrade your SDK version (pip install -U sagemaker) newer versions. " |
| 124 | + "Supported Python version(s): {}.".format( |
| 125 | + framework, fw_version, py_version, ", ".join(available_versions) |
| 126 | + ) |
| 127 | + ) |
0 commit comments