Amazon SageMaker

This page is based on this notion page and multiple videos.

Videos

Real time endpoints

Batch transformations

Overview

AWS SageMaker is a platform on Amazon Web Services meant to create Machine Learning models and serve them as a paid service over an API.

In this short tutorial I will describe how you can call the SageMaker API that hosts a Typing Tool we created to receive segment assignment predictions. Additionally, I will show the process behind the implementation and all the wires than needed to get connected to make this work.

The work included in this page is based on the first video, even though solutions provided there were not reproducible.
We managed to create a reproducible example on our own. We stored all the code in tools/sagemaker

Pricing

Based on Amazon SageMaker Pricing

  • On Demand SageMaker Notebook Instance used to develop an example - ml.t2.medium
    • $0.0582/h → $1.3968/day → $9.7776/week
    • could be on for 2 weeks! (~ $20)
  • SageMaker Training used to train and store the model - ml.m4.xlarge
    • $0.28/h
    • all the test training shouldn’t take longer (in total) than 3 hours
  • SageMaker Hosting - Real Time Inference - Endpoints used to host the API - ml.m4.xlarge
    • $0.28/h → $6.72/day → $47.04/week
    • we had 3 endpoints that could run for 12 days = 3*12*$6.72 = $241

How much we spend for the POC = ~$270

How can we save $? We can try the Amazon SageMaker Batch Transform that only charges you for the instances used while your jobs are executing. If your data is already in Amazon S3, then there is no cost for reading input data from S3 and writing output data to S3.

Quick Usage

  • Open AWS Console
  • Select SageMaker service
  • On the left panel select Notebook Instances
  • Click OpenJupyterLab next to MarcinTest notebook ****instance
  • Start new Jupyter Notebook File → New → Notebook
    pick python_conda3 a a baseline kernel for the Jupyter Notebook
  • Upload a file with cases to be scored
    You can use this file that contains 3 rows
    Column names are verified after in they match one of: t1, t2, t3, t4, t5, t6, t7, t8
    Values are verified after if they match any of: Agree or Disagree
  • Use this Python code in a first chunk
      import boto3
      import re
      import sagemaker
      import json
      from sagemaker import get_execution_role
      import pandas as pd
      role = get_execution_role()
      bucket = sagemaker.Session().default_bucket()
      prefix = 'sagemaker/test'
      r_endpoint = 'MARCIN-r-endpoint-202010020932'
      runtime = boto3.Session().client('runtime.sagemaker')
      tt = pd.read_csv('typing_tool_test_3_cases.csv')
      tt_file = tt.to_csv(index=False)
      response = runtime.invoke_endpoint(EndpointName=r_endpoint,
                                         ContentType='text/csv',
                                         Body=tt_file)
      
      result = json.loads(response['Body'].read().decode())
      result
      
  • Hit run to get predictions
  • Result should look like below

What is happening under the hood?

Going top-bottom

  1. An AWS SageMaker Enpoint is called
  2. To have the endpoint to be created you needed to create an Endpoint Configuration
  3. The endpoint configuration can be created only if you have a SageMaker Machine Learning Model that was created during the SageMaker Machine Learning Training Job
  4. SageMaker Machine Learning Jobs (Training and Modelling) can be invoked through SageMaker Notebook Instances
  5. To run any code in Notebook Instances you need to pack your code in a Docker image that can:
    1. for a training channel: create and store ML model
    2. for a serving channel: serve a API in AWS SageMaker fashion
  6. This Docker image needs to be hosted on AWS ECR (Elastic Container Registry) platform. To have this Docker build you need to launch AWS EC2 and
    1. pull g6tr repo
    2. switch to proper branch (in this case sagemaker)
    3. build the image with sudo docker built -t NAME .
    4. connect the image with the registry sudo docker tag NAME [895465320602.dkr.ecr.us-east-1.amazonaws.com/docker-containers:latest](http://895465320602.dkr.ecr.us-east-1.amazonaws.com/docker-containers:latest)
    5. authenticate the registry aws ecr get-login-password --region us-east-1 | sudo docker login --username AWS --password-stdin [895465320602.dkr.ecr.us-east-1.amazonaws.com](http://895465320602.dkr.ecr.us-east-1.amazonaws.com/)
    6. push to the registry sudo docker push [895465320602.dkr.ecr.us-east-1.amazonaws.com/docker-containers](http://895465320602.dkr.ecr.us-east-1.amazonaws.com/docker-containers)
    7. make sure you have aws client installed pip3 install --upgrade --user awscli
    8. and that you are authenticated with aws aws2 configure set aws_access_key_id YOURKEY aws2 configure set aws_secret_access_key YOURSECRET
  7. The Docker image needs to contain the initialization function with the plumber R api

Going bottom-top

  1. Create an R script to score new data based on a model
  2. Pack it into a plumber API
  3. Pack the plumber API into a Docker image
  4. Put the image into AWS ECR
  5. Use the image in AWS SageMaker Notebook Instances
    1. to create a training job
    2. to create a ml model
  6. Create AWS Endpoint Configuration
  7. Create AWS Endpoint

Notes

  1. All the logs can be checked at AWS CloudWatch . This enables you to see results of any execution and any call so that you can troubleshoot your R code inside a Docker container.

  2. The workflow storage is orchestrated over AWS S3 default bucket for SageMaker for our account which is https://s3.console.aws.amazon.com/s3/buckets/sagemaker-us-east-1-895465320602/?region=us-east-1&tab=overview