Skip to content

Commit ed6a83b

Browse files
authored
Update inference example notebooks (#5340)
1 parent 44f560e commit ed6a83b

8 files changed

+109
-40
lines changed

v3-examples/inference-examples/huggingface-example.ipynb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
"This notebook demonstrates how to deploy HuggingFace models using SageMaker V3 ModelBuilder for text generation tasks."
1010
]
1111
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"### Prerequisites\n",
17+
"Note: Ensure you have sagemaker and ipywidgets installed in your environment. The ipywidgets package is required to monitor endpoint deployment progress in Jupyter notebooks."
18+
]
19+
},
1220
{
1321
"cell_type": "code",
1422
"execution_count": null,
@@ -292,7 +300,7 @@
292300
],
293301
"metadata": {
294302
"kernelspec": {
295-
"display_name": "Python 3",
303+
"display_name": "venv-test",
296304
"language": "python",
297305
"name": "python3"
298306
},
@@ -306,9 +314,9 @@
306314
"name": "python",
307315
"nbconvert_exporter": "python",
308316
"pygments_lexer": "ipython3",
309-
"version": "3.12.2"
317+
"version": "3.12.11"
310318
}
311319
},
312320
"nbformat": 4,
313321
"nbformat_minor": 4
314-
}
322+
}

v3-examples/inference-examples/in-process-mode-example.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@
313313
],
314314
"metadata": {
315315
"kernelspec": {
316-
"display_name": "Python 3",
316+
"display_name": "venv-test",
317317
"language": "python",
318318
"name": "python3"
319319
},
@@ -327,9 +327,9 @@
327327
"name": "python",
328328
"nbconvert_exporter": "python",
329329
"pygments_lexer": "ipython3",
330-
"version": "3.12.2"
330+
"version": "3.12.11"
331331
}
332332
},
333333
"nbformat": 4,
334334
"nbformat_minor": 4
335-
}
335+
}

v3-examples/inference-examples/inference-spec-example.ipynb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
"This notebook demonstrates how to create and deploy custom models using InferenceSpec with SageMaker V3 ModelBuilder."
1010
]
1111
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"### Prerequisites\n",
17+
"Note: Ensure you have sagemaker and ipywidgets installed in your environment. The ipywidgets package is required to monitor endpoint deployment progress in Jupyter notebooks.\n"
18+
]
19+
},
1220
{
1321
"cell_type": "code",
1422
"execution_count": null,
@@ -311,7 +319,7 @@
311319
],
312320
"metadata": {
313321
"kernelspec": {
314-
"display_name": "Python 3",
322+
"display_name": "venv-test",
315323
"language": "python",
316324
"name": "python3"
317325
},
@@ -325,9 +333,9 @@
325333
"name": "python",
326334
"nbconvert_exporter": "python",
327335
"pygments_lexer": "ipython3",
328-
"version": "3.12.2"
336+
"version": "3.12.11"
329337
}
330338
},
331339
"nbformat": 4,
332340
"nbformat_minor": 4
333-
}
341+
}

v3-examples/inference-examples/jumpstart-e2e-training-example.ipynb

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
"This notebook demonstrates how to use SageMaker V3 to train a JumpStart model from scratch and deploy it for inference."
1010
]
1111
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"### Prerequisites\n",
17+
"Note: Ensure you have sagemaker and ipywidgets installed in your environment. The ipywidgets package is required to monitor endpoint deployment progress in Jupyter notebooks.\n"
18+
]
19+
},
1220
{
1321
"cell_type": "code",
1422
"execution_count": null,
@@ -41,7 +49,7 @@
4149
"outputs": [],
4250
"source": [
4351
"# Configuration\n",
44-
"MODEL_ID = \"huggingface-llm-falcon-7b-bf16\"\n",
52+
"MODEL_ID = \"huggingface-spc-bert-base-cased\"\n",
4553
"MODEL_NAME_PREFIX = \"js-e2e-example-model\"\n",
4654
"ENDPOINT_NAME_PREFIX = \"js-e2e-example-endpoint\"\n",
4755
"\n",
@@ -62,7 +70,7 @@
6270
"source": [
6371
"## Step 2: Train the Model\n",
6472
"\n",
65-
"Use ModelTrainer to train the JumpStart model. This step will take significant time."
73+
"Use ModelTrainer to train the JumpStart model. The training job may take 30+ minutes to complete."
6674
]
6775
},
6876
{
@@ -75,11 +83,15 @@
7583
"jumpstart_config = JumpStartConfig(model_id=MODEL_ID)\n",
7684
"\n",
7785
"# Initialize ModelTrainer from JumpStart config\n",
78-
"model_trainer = ModelTrainer.from_jumpstart_config(jumpstart_config=jumpstart_config)\n",
86+
"model_trainer = ModelTrainer.from_jumpstart_config(\n",
87+
" jumpstart_config=jumpstart_config, \n",
88+
" base_job_name=training_job_name, \n",
89+
" hyperparameters={\"epochs\": 1}\n",
90+
")\n",
7991
"\n",
8092
"# Train the model\n",
8193
"print(\"Starting model training...\")\n",
82-
"model_trainer.train(training_job_name=training_job_name)\n",
94+
"model_trainer.train()\n",
8395
"print(f\"Training completed: {training_job_name}\")"
8496
]
8597
},
@@ -99,9 +111,10 @@
99111
"outputs": [],
100112
"source": [
101113
"# Build ModelBuilder from trained model\n",
102-
"print(\"Building model from training artifacts...\")\n",
103-
"model_builder = model_trainer.build()\n",
104-
"print(\"ModelBuilder created successfully from training artifacts!\")"
114+
"model_builder = ModelBuilder(\n",
115+
" model=model_trainer,\n",
116+
" dependencies={\"auto\": False}\n",
117+
")"
105118
]
106119
},
107120
{
@@ -150,7 +163,9 @@
150163
"source": [
151164
"## Step 6: Test the Endpoint\n",
152165
"\n",
153-
"Send a test request to the deployed endpoint."
166+
"This endpoint performs text entailment classification, determining the logical relationship between pairs of sentences. The returned scores indicate the model's confidence for different entailment categories (e.g., entailment, contradiction, neutral) - higher scores indicate stronger predictions for each relationship type. \n",
167+
"\n",
168+
"This sends a test request to the deployed endpoint."
154169
]
155170
},
156171
{
@@ -160,16 +175,15 @@
160175
"outputs": [],
161176
"source": [
162177
"# Test with a sample query for the trained model\n",
163-
"test_data = {\"inputs\": \"What are falcons?\", \"parameters\": {\"max_new_tokens\": 32}}\n",
178+
"test_data = [\"The weather is sunny today\", \"It is not raining\"]\n",
164179
"\n",
165180
"result = core_endpoint.invoke(\n",
166181
" body=json.dumps(test_data),\n",
167-
" content_type=\"application/json\"\n",
182+
" content_type=\"application/list-text\"\n",
168183
")\n",
169184
"\n",
170-
"# Decode the output of the invocation and print the result\n",
171-
"prediction = json.loads(result.body.read().decode('utf-8'))\n",
172-
"print(f\"Result of invoking trained endpoint: {prediction}\")"
185+
"entailment_scores = json.loads(result.body.read().decode('utf-8'))\n",
186+
"print(f\"Result of invoking trained endpoint: {entailment_scores}\")\n"
173187
]
174188
},
175189
{
@@ -218,7 +232,7 @@
218232
],
219233
"metadata": {
220234
"kernelspec": {
221-
"display_name": "Python 3",
235+
"display_name": "venv-test",
222236
"language": "python",
223237
"name": "python3"
224238
},
@@ -232,7 +246,7 @@
232246
"name": "python",
233247
"nbconvert_exporter": "python",
234248
"pygments_lexer": "ipython3",
235-
"version": "3.12.2"
249+
"version": "3.12.11"
236250
}
237251
},
238252
"nbformat": 4,

v3-examples/inference-examples/jumpstart-example.ipynb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
"This notebook demonstrates how to use SageMaker V3 ModelBuilder with JumpStart models for easy model deployment and inference."
1010
]
1111
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"### Prerequisites\n",
17+
"Note: Ensure you have sagemaker and ipywidgets installed in your environment. The ipywidgets package is required to monitor endpoint deployment progress in Jupyter notebooks."
18+
]
19+
},
1220
{
1321
"cell_type": "code",
1422
"execution_count": null,
@@ -21,7 +29,8 @@
2129
"\n",
2230
"from sagemaker.serve.model_builder import ModelBuilder\n",
2331
"from sagemaker.core.jumpstart.configs import JumpStartConfig\n",
24-
"from sagemaker.core.resources import EndpointConfig"
32+
"from sagemaker.core.resources import EndpointConfig\n",
33+
"from sagemaker.train.configs import Compute"
2534
]
2635
},
2736
{
@@ -69,8 +78,9 @@
6978
"outputs": [],
7079
"source": [
7180
"# Initialize model_builder object with JumpStart configuration\n",
81+
"compute = Compute(instance_type=\"ml.g5.2xlarge\")\n",
7282
"jumpstart_config = JumpStartConfig(model_id=MODEL_ID)\n",
73-
"model_builder = ModelBuilder.from_jumpstart_config(jumpstart_config=jumpstart_config)\n",
83+
"model_builder = ModelBuilder.from_jumpstart_config(jumpstart_config=jumpstart_config, compute=compute)\n",
7484
"\n",
7585
"print(\"ModelBuilder created successfully from JumpStart config!\")"
7686
]
@@ -188,7 +198,7 @@
188198
],
189199
"metadata": {
190200
"kernelspec": {
191-
"display_name": "Python 3",
201+
"display_name": "venv-test",
192202
"language": "python",
193203
"name": "python3"
194204
},
@@ -202,7 +212,7 @@
202212
"name": "python",
203213
"nbconvert_exporter": "python",
204214
"pygments_lexer": "ipython3",
205-
"version": "3.12.2"
215+
"version": "3.12.11"
206216
}
207217
},
208218
"nbformat": 4,

v3-examples/inference-examples/local-mode-example.ipynb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@
3131
"from sagemaker.serve.mode.function_pointers import Mode"
3232
]
3333
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"# NOTE: Local mode requires Docker to be installed and running. \n",
41+
"# If Docker is not in your system PATH, you may need to define the Docker path in one of the top cells.\n",
42+
"# Here is an example:\n",
43+
"import os\n",
44+
"os.environ['PATH'] = '/usr/local/bin:/Applications/Docker.app/Contents/Resources/bin:' + os.environ['PATH']"
45+
]
46+
},
3447
{
3548
"cell_type": "markdown",
3649
"metadata": {},
@@ -217,7 +230,7 @@
217230
"source": [
218231
"## Step 6: Deploy in Local Container\n",
219232
"\n",
220-
"Deploy the model in a local Docker container. This may take a few minutes to pull the container image."
233+
"Deploy the model in a local Docker container. This may take a few minutes to pull the container image and ping the container until it is live. This is a normal part of the deployment process."
221234
]
222235
},
223236
{
@@ -395,7 +408,7 @@
395408
],
396409
"metadata": {
397410
"kernelspec": {
398-
"display_name": "Python 3",
411+
"display_name": "venv-test",
399412
"language": "python",
400413
"name": "python3"
401414
},
@@ -409,9 +422,9 @@
409422
"name": "python",
410423
"nbconvert_exporter": "python",
411424
"pygments_lexer": "ipython3",
412-
"version": "3.12.2"
425+
"version": "3.12.11"
413426
}
414427
},
415428
"nbformat": 4,
416429
"nbformat_minor": 4
417-
}
430+
}

v3-examples/inference-examples/optimize-example.ipynb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
"This notebook demonstrates how to use SageMaker V3 ModelBuilder to optimize a JumpStart model for improved inference performance."
1010
]
1111
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"### Prerequisites\n",
17+
"Note: Ensure you have sagemaker and ipywidgets installed in your environment. The ipywidgets package is required to monitor endpoint deployment progress in Jupyter notebooks."
18+
]
19+
},
1220
{
1321
"cell_type": "code",
1422
"execution_count": null,
@@ -124,7 +132,6 @@
124132
" model=MODEL_ID,\n",
125133
" schema_builder=schema_builder,\n",
126134
" sagemaker_session=sagemaker_session,\n",
127-
" model_name=model_name,\n",
128135
")\n",
129136
"print(\"ModelBuilder created successfully!\")"
130137
]
@@ -135,7 +142,7 @@
135142
"source": [
136143
"## Step 5: Optimize the Model\n",
137144
"\n",
138-
"Optimize the model using AWQ quantization for improved inference performance."
145+
"Optimize the model using AWQ quantization for improved inference performance. This step may take up to 30 minutes to complete!"
139146
]
140147
},
141148
{
@@ -152,7 +159,8 @@
152159
" output_path=f\"s3://sagemaker-us-east-2-593793038179/optimize-output/jumpstart-{unique_id}/\",\n",
153160
" quantization_config={\"OverrideEnvironment\": {\"OPTION_QUANTIZE\": \"awq\"}},\n",
154161
" accept_eula=True,\n",
155-
" job_name=job_name\n",
162+
" job_name=job_name,\n",
163+
" model_name=model_name,\n",
156164
")\n",
157165
"print(f\"Model Successfully Optimized: {optimized_model.model_name}\")"
158166
]
@@ -257,7 +265,7 @@
257265
],
258266
"metadata": {
259267
"kernelspec": {
260-
"display_name": "Python 3",
268+
"display_name": "venv-test",
261269
"language": "python",
262270
"name": "python3"
263271
},
@@ -271,7 +279,7 @@
271279
"name": "python",
272280
"nbconvert_exporter": "python",
273281
"pygments_lexer": "ipython3",
274-
"version": "3.12.2"
282+
"version": "3.12.11"
275283
}
276284
},
277285
"nbformat": 4,

0 commit comments

Comments
 (0)