Entry personal repos utilizing the @distant decorator for Amazon SageMaker coaching workloads

As increasingly clients wish to put machine studying (ML) workloads in manufacturing, there’s a giant push in organizations to shorten the event lifecycle of ML code. Many organizations choose writing their ML code in a production-ready type within the type of Python strategies and lessons versus an exploratory type (writing code with out utilizing strategies or lessons) as a result of this helps them ship production-ready code quicker.

With Amazon SageMaker, you should utilize the @remote decorator to run a SageMaker coaching job just by annotating your Python code with an @distant decorator. The SageMaker Python SDK will mechanically translate your present workspace setting and any related information processing code and datasets right into a SageMaker coaching job that runs on the SageMaker coaching platform.

Operating a Python operate regionally usually requires a number of dependencies, which can not include the native Python runtime setting. You’ll be able to set up them by way of bundle and dependency administration instruments like pip or conda.

Nonetheless, organizations working in regulated industries like banking, insurance coverage, and healthcare function in environments which have strict information privateness and networking controls in place. These controls usually mandate having no web entry obtainable to any of their environments. The rationale for such restriction is to have full management over egress and ingress site visitors to allow them to scale back the probabilities of unscrupulous actors sending or receiving non-verified data via their community. It’s usually additionally mandated to have such community isolation as a part of the auditory and industrial compliance guidelines. Relating to ML, this restricts information scientists from downloading any bundle from public repositories like PyPI, Anaconda, or Conda-Forge.

To supply information scientists entry to the instruments of their selection whereas additionally respecting the restrictions of the setting, organizations usually arrange their very own personal bundle repository hosted in their very own setting. You’ll be able to arrange personal bundle repositories on AWS in a number of methods:

On this submit, we concentrate on the primary choice: utilizing CodeArtifact.

Answer overview

The next structure diagram reveals the answer structure.


The high-level steps to implement the answer are as follows

  • Arrange a digital personal cloud (VPC) with no web entry utilizing an AWS CloudFormation template.
  • Use a second CloudFormation template to arrange CodeArtifact as a personal PyPI repository and supply connectivity to the VPC, and arrange an Amazon SageMaker Studio setting to make use of the personal PyPI repository.
  • Prepare a classification mannequin based mostly on the MNIST dataset utilizing an @distant decorator from the open-source SageMaker Python SDK. All of the dependencies shall be downloaded from the personal PyPI repository.

Be aware that utilizing SageMaker Studio on this submit is non-compulsory. You’ll be able to select to work in any built-in improvement setting (IDE) of your selection. You simply must arrange your AWS Command Line Interface (AWS CLI) credentials accurately. For extra data, seek advice from Configure the AWS CLI.


You want an AWS account with an AWS Identity and Access Management (IAM) role with permissions to handle sources created as a part of the answer. For particulars, seek advice from Creating an AWS account.

Arrange a VPC with no web connection

Create a new CloudFormation stack utilizing the vpc.yaml template. This template creates the next sources:

  • A VPC with two personal subnets throughout two Availability Zones with no web connectivity
  • A Gateway VPC endpoint for accessing Amazon S3
  • Interface VPC endpoints for SageMaker, CodeArtifact, and some different companies to permit the sources within the VPC to connect with AWS companies by way of AWS PrivateLink

Present a stack identify, akin to No-Web, and full the stack creation course of.


Look ahead to the stack creation course of to finish.

Arrange a personal repository and SageMaker Studio utilizing the VPC

The following step is to deploy one other CloudFormation stack utilizing the sagemaker_studio_codeartifact.yaml template. This template creates the next sources:

Present a stack identify and preserve the default values or regulate the parameters for the CodeArtifact area identify, personal repository identify, person profile identify for SageMaker Studio, and identify for the upstream public PyPI repository. You additionally we have to present the VPC stack identify created within the earlier step.


When the stack creation is full, the SageMaker area must be seen on the SageMaker console.


To confirm there isn’t a web connection obtainable in SageMaker Studio, launch SageMaker Studio. Select File, New, and Terminal to launch a terminal and attempt to curl any web useful resource. It ought to fail to attach, as proven within the following screenshot.


Prepare a picture classifier utilizing an @distant decorator with the personal PyPI repository

On this part, we use the @distant decorator to run a PyTorch coaching job that produces a MNIST picture classification mannequin. To attain this, we arrange a configuration file, develop the coaching script, and run the coaching code.

Arrange a configuration file

We arrange a config.yaml file and supply the configurations wanted to do the next:

  • Run a SageMaker training job within the no-internet VPC created earlier
  • Obtain the required packages by connecting to the personal PyPI repository created earlier

The file seems to be like the next code:

SchemaVersion: '1.0'
        Dependencies: '../config/necessities.txt'
        InstanceType: 'ml.m5.xlarge'
            - 'aws codeartifact login --tool pip --domain <domain-name> --domain-owner <AWS account quantity> --repository <personal repository identify> --endpoint-url <VPC-endpoint-url-prefixed with https://>
        RoleArn: '<execution function ARN for operating coaching job>'
        S3RootUri: '<s3 bucket to retailer the job output>'
            - '<safety group id utilized by SageMaker Studio>'
            - '<VPC subnet id 1>'
            - '<VPC subnet id 2>'

The Dependencies discipline comprises the trail to necessities.txt, which comprises all of the dependencies wanted. Be aware that each one the dependencies shall be downloaded from the personal repository. The necessities.txt file comprises the next code:


The PreExecutionCommands part comprises the command to connect with the personal PyPI repository. To get the CodeArtifact VPC endpoint URL, use the next code:

response = ec2.describe_vpc_endpoints(
            'Name': 'service-name',
            'Values': [

code_artifact_api_vpc_endpoint = response['VpcEndpoints'][0]['DnsEntries'][0]['DnsName']

endpoint_url = f'https://{code_artifact_api_vpc_endpoint}'

Typically, we get two VPC endpoints for CodeArtifact, and we will use any of them within the connection instructions. For extra particulars, seek advice from Use CodeArtifact from a VPC.

Moreover, configurations like execution function, output location, and VPC configurations are offered within the config file. These configurations are wanted to run the SageMaker coaching job. To know extra about all of the configurations supported, seek advice from Configuration file.

It’s not obligatory to make use of the config.yaml file in an effort to work with the @distant decorator. That is only a cleaner strategy to provide all configurations to the @distant decorator. All of the configs may be provided straight within the decorator arguments, however that reduces readability and maintainability of adjustments in the long term. Additionally, the config file will be created by an admin and shared with all of the customers in an setting.

Develop the coaching script

Subsequent, we put together the coaching code in easy Python information. We’ve got divided the code into three information:

  • load_data.py – Incorporates the code to obtain the MNIST dataset
  • model.py – Incorporates the code for the neural community structure for the mannequin
  • train.py – Incorporates the code for coaching the mannequin through the use of load_data.py and mannequin.py

In practice.py, we have to enhance the primary coaching operate as follows:

def perform_train(train_data,
                  batch_size: int = 64,
                  test_batch_size: int = 1000,
                  epochs: int = 3,
                  lr: float = 1.0,
                  gamma: float = 0.7,
                  no_cuda: bool = True,
                  no_mps: bool = True,
                  dry_run: bool = False,
                  seed: int = 1,
                  log_interval: int = 10,
    # pytorch native coaching code........

Now we’re able to run the coaching code.

Run the coaching code with an @distant decorator

We are able to run the code from a terminal or from any executable immediate. On this submit, we use a SageMaker Studio pocket book cell to display this:

Operating the previous command triggers the coaching job. Within the logs, we will see that it’s downloading the packages from the personal PyPI repository.


This concludes the implementation of an @distant decorator working with a personal repository in an setting with no web entry.

Clear up

To wash up the sources, comply with the directions in CLEANUP.md.


On this submit, we discovered find out how to successfully use the @distant decorator’s capabilities whereas nonetheless working in restrictive environments with none web entry. We additionally discovered how can we combine CodeArtifact personal repository capabilities with the assistance of configuration file help in SageMaker. This answer makes iterative improvement a lot easier and quicker. One other added benefit is you could nonetheless proceed to write down the coaching code in a extra pure, object-oriented means and nonetheless use SageMaker capabilities to run coaching jobs on a distant cluster with minimal adjustments in your code. All of the code proven as a part of this submit is accessible within the GitHub repository.

As a subsequent step, we encourage you to take a look at the @remote decorator functionality and Python SDK API and use it in your selection of setting and IDE. Further examples can be found within the amazon-sagemaker-examples repository to get you began shortly. You can too try the submit Run your local machine learning code as Amazon SageMaker Training jobs with minimal code changes for extra particulars.

Concerning the writer

Vikesh Pandey is a Machine Studying Specialist Options Architect at AWS, serving to clients from monetary industries design and construct options on generative AI and ML. Exterior of labor, Vikesh enjoys attempting out totally different cuisines and taking part in outside sports activities.

Leave a Reply

Your email address will not be published. Required fields are marked *