Skip to content

Commit 3ba7b96

Browse files
committed
add SageMaker Async post
1 parent 334c6de commit 3ba7b96

1 file changed

Lines changed: 285 additions & 0 deletions

File tree

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 24,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import pandas as pd\n",
10+
"from sklearn.datasets import fetch_california_housing\n",
11+
"import sagemaker\n",
12+
"from sagemaker.inputs import TrainingInput\n",
13+
"from sagemaker.serializers import CSVSerializer\n",
14+
"import boto3\n",
15+
"import os\n",
16+
"\n",
17+
"# IAM role with permissions to create an endpoint, S3 bucket with a preferred prefix\n",
18+
"role = \"<YOUR_IAM_ROLE>\"\n",
19+
"bucket = \"<YOUR_BUCKET_NAME>\"\n",
20+
"prefix = \"<YOUR_FOLDER_NAME>\""
21+
]
22+
},
23+
{
24+
"cell_type": "markdown",
25+
"metadata": {},
26+
"source": [
27+
"## Download the dataset and upload it to S3"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 21,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"# Download California Housing Dataset\n",
37+
"data = fetch_california_housing()\n",
38+
"df = pd.DataFrame(data['data'], columns=data['feature_names'])\n",
39+
"df['Target'] = data['target']\n",
40+
"\n",
41+
"# SageMaker XGboost expect the target column to be the first one\n",
42+
"df = df.loc[:,['Target'] + data['feature_names']]\n",
43+
"\n",
44+
"# save as csv with no header row and index column\n",
45+
"df.to_csv(\"train.csv\", header=None, index=False)"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 22,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"boto3.Session().resource(\"s3\").Bucket(bucket).Object(os.path.join(prefix, \"train.csv\")).upload_file(\"train.csv\")\n",
55+
"s3_input_train = TrainingInput(s3_data=\"s3://{}/{}\".format(bucket, prefix), content_type=\"csv\")"
56+
]
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"metadata": {},
61+
"source": [
62+
"## Train the XGBoost model"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": null,
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"container = sagemaker.image_uris.retrieve(\"xgboost\", boto3.Session().region_name, \"latest\")\n",
72+
"\n",
73+
"sess = sagemaker.Session()\n",
74+
"\n",
75+
"xgb = sagemaker.estimator.Estimator(\n",
76+
" container,\n",
77+
" role,\n",
78+
" instance_count=1,\n",
79+
" instance_type=\"ml.m4.xlarge\",\n",
80+
" output_path=\"s3://{}/{}/output\".format(bucket, prefix),\n",
81+
" sagemaker_session=sess,\n",
82+
")\n",
83+
"xgb.set_hyperparameters(\n",
84+
" max_depth=5,\n",
85+
" eta=0.2,\n",
86+
" gamma=4,\n",
87+
" min_child_weight=6,\n",
88+
" subsample=0.8,\n",
89+
" silent=0,\n",
90+
" num_round=100,\n",
91+
")\n",
92+
"\n",
93+
"xgb.fit({\"train\": s3_input_train})"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"## Create Real-Time Endpoint"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 25,
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"name": "stdout",
110+
"output_type": "stream",
111+
"text": [
112+
"------!"
113+
]
114+
}
115+
],
116+
"source": [
117+
"xgb_predictor = xgb.deploy(initial_instance_count=1, instance_type=\"ml.m4.xlarge\", serializer=CSVSerializer())"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": 26,
123+
"metadata": {},
124+
"outputs": [
125+
{
126+
"data": {
127+
"text/plain": [
128+
"b'4.154237747192383'"
129+
]
130+
},
131+
"execution_count": 26,
132+
"metadata": {},
133+
"output_type": "execute_result"
134+
}
135+
],
136+
"source": [
137+
"xgb_predictor.predict(\"8.3252,41.0,6.984126984126984,1.0238095238095237,322.0,2.5555555555555554,37.88,-122.23\")"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 54,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"xgb_predictor.delete_endpoint()"
147+
]
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"metadata": {},
152+
"source": [
153+
"## Create Async Endpoint"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 35,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"name": "stdout",
163+
"output_type": "stream",
164+
"text": [
165+
"------!"
166+
]
167+
}
168+
],
169+
"source": [
170+
"from sagemaker.async_inference import AsyncInferenceConfig\n",
171+
"\n",
172+
"# Create an empty AsyncInferenceConfig object to use default values\n",
173+
"async_config = AsyncInferenceConfig(output_path=f\"s3://{bucket}/{prefix}/output\")\n",
174+
"\n",
175+
"# deploy model to SageMaker Inference\n",
176+
"xgb_async_predictor = xgb.deploy(\n",
177+
" async_inference_config=async_config,\n",
178+
" initial_instance_count=1, # number of instances\n",
179+
" instance_type='ml.m4.xlarge', # instance type\n",
180+
" serializer=CSVSerializer(), # define serializer to convert bytes to CSV\n",
181+
")"
182+
]
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": 50,
187+
"metadata": {},
188+
"outputs": [],
189+
"source": [
190+
"# Alternatively, you can provide the input_path parameter for predict_async with the s3 path for the input data\n",
191+
"response = xgb_async_predictor.predict_async(\"8.0,41.0,6.9,1.0,322.0,2.5,37.8,-122.2\")\n",
192+
"output_location = response.output_path"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": 51,
198+
"metadata": {},
199+
"outputs": [],
200+
"source": [
201+
"import time\n",
202+
"from botocore.exceptions import ClientError\n",
203+
"import boto3\n",
204+
"\n",
205+
"def get_output(s3_client, output_path):\n",
206+
" output_bucket = output_path.split('/')[2]\n",
207+
" output_key = \"/\".join(output_path.split('/')[3:])\n",
208+
" while True:\n",
209+
" try:\n",
210+
" obj = s3_client.Object(output_bucket, output_key)\n",
211+
" output = obj.get()['Body'].read().decode('utf-8')\n",
212+
" return output\n",
213+
" except ClientError as e:\n",
214+
" if e.response[\"Error\"][\"Code\"] == \"NoSuchKey\":\n",
215+
" print(\"waiting for output...\")\n",
216+
" time.sleep(2)\n",
217+
" continue\n",
218+
" raise"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": 52,
224+
"metadata": {},
225+
"outputs": [
226+
{
227+
"name": "stdout",
228+
"output_type": "stream",
229+
"text": [
230+
"Output: 4.112793445587158\n"
231+
]
232+
}
233+
],
234+
"source": [
235+
"s3 = boto3.resource('s3')\n",
236+
"output = get_output(s3, output_location)\n",
237+
"print(f\"Output: {output}\")"
238+
]
239+
},
240+
{
241+
"cell_type": "code",
242+
"execution_count": 53,
243+
"metadata": {},
244+
"outputs": [],
245+
"source": [
246+
"xgb_async_predictor.delete_endpoint()"
247+
]
248+
},
249+
{
250+
"cell_type": "markdown",
251+
"metadata": {},
252+
"source": [
253+
"## Example Log"
254+
]
255+
},
256+
{
257+
"cell_type": "markdown",
258+
"metadata": {},
259+
"source": [
260+
"ModelLatency: 3047 us, RequestDownloadLatency: 18701 us, ResponseUploadLatency: 59713 us, TimeInBacklog: 5 ms, TotalProcessingTime: 94 ms"
261+
]
262+
}
263+
],
264+
"metadata": {
265+
"kernelspec": {
266+
"display_name": "Python 3",
267+
"language": "python",
268+
"name": "python3"
269+
},
270+
"language_info": {
271+
"codemirror_mode": {
272+
"name": "ipython",
273+
"version": 3
274+
},
275+
"file_extension": ".py",
276+
"mimetype": "text/x-python",
277+
"name": "python",
278+
"nbconvert_exporter": "python",
279+
"pygments_lexer": "ipython3",
280+
"version": "3.7.3"
281+
}
282+
},
283+
"nbformat": 4,
284+
"nbformat_minor": 4
285+
}

0 commit comments

Comments
 (0)