Skip to content

Commit 6925931

Browse files
authored
Merge branch 'master' into pccornel-tensorflow_mnist
2 parents 09c2338 + bc70715 commit 6925931

File tree

37 files changed

+3528
-3230
lines changed

37 files changed

+3528
-3230
lines changed

advanced_functionality/multi_model_bring_your_own/multi_model_endpoint_bring_your_own.ipynb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
"\n",
1212
"For the inference container to serve multiple models in a multi-model endpoint, it must implement [additional APIs](https://docs.aws.amazon.com/sagemaker/latest/dg/build-multi-model-build-container.html) in order to load, list, get, unload and invoke specific models. This notebook demonstrates how to build your own inference container that implements these APIs.\n",
1313
"\n",
14+
"**Note**: Because this notebook builds a Docker container, it does not run in Amazon SageMaker Studio.\n",
15+
"\n",
16+
"This notebook was tested with the `conda_mxnet_p36` kernel running SageMaker Python SDK version 2.15.3 on an Amazon SageMaker notebook instance.\n",
17+
"\n",
1418
"---\n",
1519
"\n",
1620
"### Contents\n",
@@ -553,7 +557,7 @@
553557
"name": "python",
554558
"nbconvert_exporter": "python",
555559
"pygments_lexer": "ipython3",
556-
"version": "3.6.5"
560+
"version": "3.7.6"
557561
}
558562
},
559563
"nbformat": 4,

autopilot/autopilot_customer_churn_high_level_with_evaluation.ipynb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"\n",
2323
"---\n",
2424
"\n",
25-
"This notebook works with sagemaker python sdk >= 1.65.1.\n",
25+
"This notebook works with sagemaker python sdk 2.x\n",
2626
"\n",
2727
"## Contents\n",
2828
"\n",
@@ -794,17 +794,19 @@
794794
"metadata": {},
795795
"outputs": [],
796796
"source": [
797-
"from sagemaker.predictor import RealTimePredictor\n",
798-
"from sagemaker.content_types import CONTENT_TYPE_CSV\n",
797+
"from sagemaker.predictor import Predictor\n",
798+
"from sagemaker.serializers import CSVSerializer\n",
799+
"from sagemaker.deserializers import CSVDeserializer\n",
799800
"\n",
800801
"predictor = automl.deploy(initial_instance_count=1,\n",
801802
" instance_type='ml.m5.2xlarge',\n",
802803
" candidate=candidates[best_candidate_idx],\n",
803804
" inference_response_keys=inference_response_keys,\n",
804-
" predictor_cls=RealTimePredictor)\n",
805-
"predictor.content_type = CONTENT_TYPE_CSV\n",
805+
" predictor_cls=Predictor,\n",
806+
" serializer=CSVSerializer(),\n",
807+
" deserializer=CSVDeserializer())\n",
806808
"\n",
807-
"print(\"Created endpoint: {}\".format(predictor.endpoint))\n"
809+
"print(\"Created endpoint: {}\".format(predictor.endpoint_name))\n"
808810
]
809811
},
810812
{
@@ -829,11 +831,9 @@
829831
"metadata": {},
830832
"outputs": [],
831833
"source": [
832-
"from io import StringIO\n",
833-
"\n",
834-
"prediction = predictor.predict(test_data_no_target.to_csv(sep=',', header=False, index=False)).decode('utf-8')\n",
835-
"prediction_df = pd.read_csv(StringIO(prediction), header=None, names=inference_response_keys)\n",
836-
"custom_predicted_labels = prediction_df.iloc[:,1].values >= best_candidate_threshold\n",
834+
"prediction = predictor.predict(test_data_no_target.to_csv(sep=',', header=False, index=False))\n",
835+
"prediction_df = pd.DataFrame(prediction, columns=inference_response_keys)\n",
836+
"custom_predicted_labels = prediction_df.iloc[:,1].astype(float).values >= best_candidate_threshold\n",
837837
"prediction_df['custom_predicted_label'] = custom_predicted_labels\n",
838838
"prediction_df['custom_predicted_label'] = prediction_df['custom_predicted_label'].map({False: target_attribute_values[0], True: target_attribute_values[1]})\n",
839839
"prediction_df"
@@ -906,4 +906,4 @@
906906
},
907907
"nbformat": 4,
908908
"nbformat_minor": 4
909-
}
909+
}

aws_sagemaker_studio/sagemaker_neo_compilation_jobs/deploy_tensorflow_model_on_Inf1_instance/tensorflow_distributed_mnist_neo_inf1_studio.ipynb

Lines changed: 84 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66
"source": [
77
"# Compile and Deploy a TensorFlow model on Inf1 instances\n",
88
"\n",
9+
"Amazon SageMaker supports Inf1 instances for high performance and cost-effective inferences. Inf1 instances are ideal for large scale machine learning inference applications like image recognition, speech recognition, natural language processing, personalization, and fraud detection. In this example, train a classification model on the MNIST dataset using TensorFlow, compile it using Amazon SageMaker Neo, deploy the model on Inf1 instances on a SageMaker endpoint, and use the Neo Deep Learning Runtime to make inferences in real-time and with low latency. \n",
910
"\n",
10-
"Amazon SageMaker now supports Inf1 instances for high performance and cost-effective inferences. Inf1 instances are ideal for large scale machine learning inference applications like image recognition, speech recognition, natural language processing, personalization, and fraud detection. In this example, we train a classification model on the MNIST dataset using TensorFlow, compile it using Amazon SageMaker Neo, and deploy the model on Inf1 instances on a SageMaker endpoint and use the Neo Deep Learning Runtime to make inferences in real-time and with low latency. \n",
11+
"## Inf 1 instances \n",
12+
"Inf1 instances are built from the ground up to support machine learning inference applications and feature up to 16 AWS Inferentia chips, which are high-performance machine learning inference chips designed and built by AWS. The Inferentia chips are coupled with the latest custom 2nd generation Intel® Xeon® Scalable processors and up to 100 Gbps networking to enable high throughput inference. With 1 to 16 AWS Inferentia chips per instance, Inf1 instances can scale in performance to up to 2000 Tera Operations per Second (TOPS) and deliver extremely low latency for real-time inference applications. The large on-chip memory on AWS Inferentia chips used in Inf1 instances allows caching of machine learning models directly on the chip. This eliminates the need to access outside memory resources during inference, enabling low latency without impacting bandwidth. \n",
1113
"\n",
12-
"### Inf 1 instances \n",
13-
"Inf1 instances are built from the ground up to support machine learning inference applications and feature up to 16 AWS Inferentia chips, high-performance machine learning inference chips designed and built by AWS. The Inferentia chips are coupled with the latest custom 2nd generation Intel® Xeon® Scalable processors and up to 100 Gbps networking to enable high throughput inference. With 1 to 16 AWS Inferentia chips per instance, Inf1 instances can scale in performance to up to 2000 Tera Operations per Second (TOPS) and deliver extremely low latency for real-time inference applications. The large on-chip memory on AWS Inferentia chips used in Inf1 instances allows caching of machine learning models directly on the chip. This eliminates the need to access outside memory resources during inference, enabling low latency without impacting bandwidth. \n"
14+
"## Prerequisites\n",
15+
"\n",
16+
"* SageMaker Studio with Python 3 (Data Science) kernel\n",
17+
"* SageMaker SDK version 1.x"
1418
]
1519
},
1620
{
1721
"cell_type": "markdown",
1822
"metadata": {},
1923
"source": [
20-
"Make sure you selected `Python 3 (Data Science)` kernel."
24+
"## Setup\n",
25+
"\n",
26+
"Install the required version of SageMaker and TensorFlow."
2127
]
2228
},
2329
{
@@ -26,7 +32,16 @@
2632
"metadata": {},
2733
"outputs": [],
2834
"source": [
29-
"%cd /root/amazon-sagemaker-examples/aws_sagemaker_studio/sagemaker_neo_compilation_jobs/deploy_tensorflow_model_on_Inf1_instance"
35+
"import sagemaker\n",
36+
"if sagemaker.__version__ >= '2':\n",
37+
" orig_sm_version = sagemaker.__version__\n",
38+
" with open('orig_sm_version.txt', \"w\") as f:\n",
39+
" f.write(orig_sm_version)\n",
40+
" %pip install \"sagemaker>=1.14.2,<2\"\n",
41+
"\n",
42+
"if sagemaker.__version__ >= '2':\n",
43+
" print(f\"WARNING: The current running version of the SageMaker SDK is {sagemaker.__version__}, which will cause this notebook to fail. \"\n",
44+
" f\"Restart the kernel to run the required version of the SDK.\")"
3045
]
3146
},
3247
{
@@ -35,15 +50,14 @@
3550
"metadata": {},
3651
"outputs": [],
3752
"source": [
38-
"import sys\n",
39-
"!{sys.executable} -m pip install tensorflow==1.13.1"
53+
"%pip install tensorflow==1.15.4"
4054
]
4155
},
4256
{
4357
"cell_type": "markdown",
4458
"metadata": {},
4559
"source": [
46-
"### Set up the environment"
60+
"Start a SageMaker session and get the excecution role."
4761
]
4862
},
4963
{
@@ -90,8 +104,8 @@
90104
"cell_type": "markdown",
91105
"metadata": {},
92106
"source": [
93-
"### Upload the data\n",
94-
"We use the ```sagemaker.Session.upload_data``` function to upload our datasets to an S3 location. The return value inputs identifies the location -- we will use this later when we start the training job."
107+
"### Upload the data to Amazon Simple Storage Service (Amazon S3)\n",
108+
"Use the `sagemaker.Session.upload_data` function to upload datasets to an S3 location. The return value is the location, which is used when the training job is started."
95109
]
96110
},
97111
{
@@ -107,8 +121,9 @@
107121
"cell_type": "markdown",
108122
"metadata": {},
109123
"source": [
110-
"# Construct a script for distributed training \n",
111-
"Here is the full code for the network model:"
124+
"## Construct a script for distributed training \n",
125+
"\n",
126+
"To see the code for the network model, either browse to `mnist.py` in the File Browser or run the following command to show it here."
112127
]
113128
},
114129
{
@@ -124,12 +139,12 @@
124139
"cell_type": "markdown",
125140
"metadata": {},
126141
"source": [
127-
"The script here is and adaptation of the [TensorFlow MNIST example](https://github.com/tensorflow/models/tree/master/official/vision/image_classification). It provides a ```model_fn(features, labels, mode)```, which is used for training, evaluation and inference. See [TensorFlow MNIST distributed training notebook](The script here is and adaptation of the [TensorFlow MNIST example](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/tutorials/mnist). It provides a ```model_fn(features, labels, mode)```, which is used for training, evaluation and inference. See [TensorFlow MNIST distributed training notebook](https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/tensorflow_script_mode_training_and_serving/tensorflow_script_mode_training_and_serving.ipynb) for more details about the training script.\n",
142+
"This script is an adaptation of the [TensorFlow MNIST example](https://github.com/tensorflow/models/tree/master/official/vision/image_classification). It provides a `model_fn(features, labels, mode)` function that is used for training, evaluation and inference. For more details, see [TensorFlow MNIST distributed training notebook](https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/tensorflow_script_mode_training_and_serving/tensorflow_script_mode_training_and_serving.ipynb).\n",
128143
"\n",
144+
"At the end of the training script, there are two additional functions that are used with Neo Deep Learning Runtime:\n",
129145
"\n",
130-
"At the end of the training script, there are two additional functions, to be used with Neo Deep Learning Runtime:\n",
131-
"* `neo_preprocess(payload, content_type)`: Function that takes in the payload and Content-Type of each incoming request and returns a NumPy array\n",
132-
"* `neo_postprocess(result)`: Function that takes the prediction results produced by Deep Learining Runtime and returns the response body\n",
146+
"* `neo_preprocess(payload, content_type)`: takes the payload and Content-Type of each incoming request and returns a NumPy array.\n",
147+
"* `neo_postprocess(result)`: takes the prediction results produced by Deep Learning Runtime and returns the response body.\n",
133148
"\n",
134149
"LeCun, Y., Cortes, C., & Burges, C. (2010). MNIST handwritten digit databaseATT Labs [Online]. Available: http://yann.lecun.com/exdb/mnist, 2."
135150
]
@@ -138,7 +153,9 @@
138153
"cell_type": "markdown",
139154
"metadata": {},
140155
"source": [
141-
"## Create a training job using the sagemaker.TensorFlow estimator"
156+
"## Create a training job\n",
157+
"\n",
158+
"Use the `sagemaker.TensorFlow` estimator to create a training job."
142159
]
143160
},
144161
{
@@ -167,22 +184,24 @@
167184
"cell_type": "markdown",
168185
"metadata": {},
169186
"source": [
170-
"The **```fit```** method will create a training job in two **ml.c5.xlarge** instances. The logs above will show the instances doing training, evaluation, and incrementing the number of **training steps**. \n",
187+
"The `fit` method creates a training job in two **ml.c5.xlarge** instances. The logs from `fit` show the instances training, evaluating, and incrementing the number of **training steps**. \n",
171188
"\n",
172-
"In the end of the training, the training job will generate a saved model for compilation."
189+
"At the end of the training, the training job generates a saved model for compilation."
173190
]
174191
},
175192
{
176193
"cell_type": "markdown",
177194
"metadata": {},
178195
"source": [
179-
"# Deploy the trained model on Inf1 instance for real-time inferences\n",
196+
"## Deploy the trained model\n",
197+
"\n",
198+
"Deploy the model to an Inf1 instance for real-time inferences. Once the training is complete, compile the model using SageMaker Neo to optimize performance for the desired deployment target. SageMaker Neo enables you to train machine learning models once and run them anywhere in the cloud and at the edge. To compile the trained model for deployment to Inf1 instances, use the `TensorFlowEstimator.compile_model` method and select `ml_inf1` as the deployment target. The compiled model is deployed on an endpoint that uses Inf1 instances in SageMaker.\n",
180199
"\n",
181-
"Once the training is complete, we compile the model using Amazon SageMaker Neo to optize performance for our desired deployment target. Amazon SageMaker Neo enables you to train machine learning models once and run them anywhere in the cloud and at the edge. To compile our trained model for deploying on Inf1 instances, we are using the ``TensorFlowEstimator.compile_model`` method and select ``'ml_inf1'`` as our deployment target. The compiled model will then be deployed on an endpoint using Inf1 instances in Amazon SageMaker. \n",
200+
"### Compile the model \n",
182201
"\n",
183-
"## Compile the model \n",
202+
"The `input_shape` is the definition for the model's input tensor and `output_path` is where the compiled model is stored in S3.\n",
184203
"\n",
185-
"The ``input_shape`` is the definition for the model's input tensor and ``output_path`` is where the compiled model will be stored in S3. **Important. If the following command result in a permission error, scroll up and locate the value of execution role returned by `get_execution_role()`. The role must have access to the S3 bucket specified in ``output_path``.**"
204+
"> Note: If `compile_model` results in a permission error, verify that the execution role returned previously by `get_execution_role()` has access to the Amazon S3 bucket specified in `output_path`."
186205
]
187206
},
188207
{
@@ -204,9 +223,9 @@
204223
"cell_type": "markdown",
205224
"metadata": {},
206225
"source": [
207-
"## Deploy the compiled model on a SageMaker endpoint\n",
226+
"### Deploy to a SageMaker endpoint\n",
208227
"\n",
209-
"Now that we have the compiled model, we will deploy it on an Amazon SageMaker endpoint. Inf1 instances in Amazon SageMaker are available in four sizes: ml.inf1.xlarge, ml.inf1.2xlarge, ml.inf1.6xlarge, and ml.inf1.24xlarge. In this example, we are using ``'ml.inf1.xlarge'`` for deploying our model.\n"
228+
"Deploy the compiled model to an Amazon SageMaker endpoint. This example uses the Inf1 `ml.inf1.xlarge` instance type."
210229
]
211230
},
212231
{
@@ -219,6 +238,13 @@
219238
" instance_type = 'ml.inf1.xlarge')"
220239
]
221240
},
241+
{
242+
"cell_type": "markdown",
243+
"metadata": {},
244+
"source": [
245+
"Configure a serializer for `application/vnd+python.numpy+binary` Content-Type."
246+
]
247+
},
222248
{
223249
"cell_type": "code",
224250
"execution_count": null,
@@ -242,7 +268,7 @@
242268
"source": [
243269
"## Invoking the endpoint\n",
244270
"\n",
245-
"Once the endpoint is ready, you can send requests to it and receive inference results in real-time with low latency. "
271+
"When the endpoint is ready, send requests to it and receive inference results in real time with low latency. "
246272
]
247273
},
248274
{
@@ -277,9 +303,9 @@
277303
"cell_type": "markdown",
278304
"metadata": {},
279305
"source": [
280-
"## Deleting endpoint\n",
306+
"## Cleanup\n",
281307
"\n",
282-
"Delete the endpoint if you no longer need it. "
308+
"Delete the endpoint. "
283309
]
284310
},
285311
{
@@ -290,6 +316,36 @@
290316
"source": [
291317
"sagemaker_session.delete_endpoint(optimized_predictor.endpoint)"
292318
]
319+
},
320+
{
321+
"cell_type": "markdown",
322+
"metadata": {},
323+
"source": [
324+
"Rollback the SageMaker Python SDK version"
325+
]
326+
},
327+
{
328+
"cell_type": "code",
329+
"execution_count": null,
330+
"metadata": {},
331+
"outputs": [],
332+
"source": [
333+
"# rollback the SageMaker Python SDK to the kernel's original version\n",
334+
"if os.path.exists('orig_sm_version.txt'):\n",
335+
" with open('orig_sm_version.txt', 'r') as f:\n",
336+
" orig_sm_version = f.read()\n",
337+
" print(f\"Original version: {orig_sm_version}\")\n",
338+
" print(f\"Current version: {sagemaker.__version__}\")\n",
339+
" %pip install sagemaker=={orig_sm_version}\n",
340+
" os.remove('orig_sm_version.txt')"
341+
]
342+
},
343+
{
344+
"cell_type": "markdown",
345+
"metadata": {},
346+
"source": [
347+
"Restart the kernel to run the updated version of the SDK."
348+
]
293349
}
294350
],
295351
"metadata": {

buildspec.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
version: 0.2
2+
3+
env:
4+
variables:
5+
INSTANCE_TYPE: 'ml.c4.8xlarge'
6+
REGION: 'us-west-2'
7+
8+
phases:
9+
pre_build:
10+
commands:
11+
- PR_NUM=$(echo $CODEBUILD_SOURCE_VERSION | grep -o '[0-9]\+')
12+
- NOTEBOOKS="$(pr-notebook-filenames --pr $PR_NUM)"
13+
14+
build:
15+
commands:
16+
- |-
17+
if [ -z "$NOTEBOOKS" ]; then
18+
echo "No notebooks to test in this pull request."
19+
else
20+
echo "Testing $NOTEBOOKS"
21+
aws s3 cp s3://sagemaker-mead-cli/mead-nb-test.tar.gz mead-nb-test.tar.gz
22+
tar -xzf mead-nb-test.tar.gz
23+
export JAVA_HOME=$(get-java-home)
24+
echo "set JAVA_HOME=$JAVA_HOME"
25+
export SAGEMAKER_ROLE_ARN=$(aws iam list-roles --output text --query "Roles[?RoleName == 'SageMakerRole'].Arn")
26+
echo "set SAGEMAKER_ROLE_ARN=$SAGEMAKER_ROLE_ARN"
27+
28+
./runtime/bin/mead-run-nb-test \
29+
--instance-type $INSTANCE_TYPE \
30+
--region $REGION \
31+
--notebook-instance-role-arn $SAGEMAKER_ROLE_ARN \
32+
$NOTEBOOKS
33+
fi

0 commit comments

Comments
 (0)