Skip to content

Commit a8b9e5e

Browse files
Merge pull request #108 from giuseppe-zappia/main
Updated fine-tuning workshop
2 parents 95cf301 + eee2a8a commit a8b9e5e

29 files changed

+3977
-1941
lines changed

workshops/fine-tuning-with-sagemakerai-and-bedrock/task_01_foundation_model_playground/01.01_search_and_deploy_huggingface_llm.ipynb

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
"id": "fcbb5d61-8a0b-47d9-a7c5-0c041c82b8bf",
1616
"metadata": {},
1717
"source": [
18-
"# 🚀 Deploy `deepseek-ai/DeepSeek-R1-Distill-Llama-8B` on Amazon SageMaker"
18+
"# 🚀 Deploy `Qwen/Qwen3-4B-Instruct-2507` on Amazon SageMaker"
1919
]
2020
},
2121
{
2222
"cell_type": "markdown",
2323
"id": "dd210e90-21e1-4f03-a08e-c3fba9aa6979",
2424
"metadata": {},
2525
"source": [
26+
"## Prerequisites\n",
27+
"\n",
2628
"To start off, let's install some packages to help us through the notebooks. **Restart the kernel after packages have been installed.**"
2729
]
2830
},
@@ -57,6 +59,14 @@
5759
"get_ipython().kernel.do_shutdown(True)"
5860
]
5961
},
62+
{
63+
"cell_type": "markdown",
64+
"id": "a947367a-bea3-498a-9548-d6e6e08f0d10",
65+
"metadata": {},
66+
"source": [
67+
"***"
68+
]
69+
},
6070
{
6171
"cell_type": "code",
6272
"execution_count": null,
@@ -66,9 +76,9 @@
6676
"source": [
6777
"import os\n",
6878
"import sagemaker\n",
69-
"from sagemaker.djl_inference import DJLModel\n",
70-
"from ipywidgets import Dropdown\n",
71-
"\n",
79+
"import boto3\n",
80+
"import shutil\n",
81+
"from sagemaker.config import load_sagemaker_config\n",
7282
"import sys\n",
7383
"sys.path.append(os.path.dirname(os.getcwd()))\n",
7484
"\n",
@@ -78,31 +88,20 @@
7888
" print_dialog,\n",
7989
" format_messages,\n",
8090
" write_eula\n",
81-
")"
82-
]
83-
},
84-
{
85-
"cell_type": "code",
86-
"execution_count": null,
87-
"id": "8b53f21c-3a65-44fc-b547-712d971cd652",
88-
"metadata": {},
89-
"outputs": [],
90-
"source": [
91-
"import boto3\n",
92-
"import shutil\n",
93-
"import sagemaker\n",
94-
"from sagemaker.config import load_sagemaker_config\n",
91+
")\n",
9592
"\n",
9693
"sagemaker_session = sagemaker.Session()\n",
9794
"s3_client = boto3.client('s3')\n",
9895
"\n",
96+
"region = sagemaker_session.boto_session.region_name\n",
9997
"bucket_name = sagemaker_session.default_bucket()\n",
10098
"default_prefix = sagemaker_session.default_bucket_prefix\n",
10199
"configs = load_sagemaker_config()\n",
102100
"\n",
103101
"session = sagemaker.Session()\n",
104102
"role = sagemaker.get_execution_role()\n",
105103
"\n",
104+
"\n",
106105
"print(f\"Execution Role: {role}\")\n",
107106
"print(f\"Default S3 Bucket: {bucket_name}\")"
108107
]
@@ -130,11 +129,14 @@
130129
"metadata": {},
131130
"outputs": [],
132131
"source": [
133-
"inference_image_uri = sagemaker.image_uris.retrieve(\n",
134-
" framework=\"djl-lmi\", \n",
135-
" region=session.boto_session.region_name, \n",
136-
" version=\"0.29.0\"\n",
137-
")\n",
132+
"# commenting until LMI 0.33.0 available via SageMaker SDK\n",
133+
"# inference_image_uri = sagemaker.image_uris.retrieve(\n",
134+
"# framework=\"djl-lmi\", \n",
135+
"# region=session.boto_session.region_name, \n",
136+
"# version=\"0.33.0\"\n",
137+
"# )\n",
138+
"\n",
139+
"inference_image_uri = f\"763104351884.dkr.ecr.{region}.amazonaws.com/djl-inference:0.33.0-lmi15.0.0-cu128\"\n",
138140
"pretty_print_html(f\"using image to host: {inference_image_uri}\")"
139141
]
140142
},
@@ -153,7 +155,7 @@
153155
"metadata": {},
154156
"outputs": [],
155157
"source": [
156-
"model_id = \"deepseek-ai/DeepSeek-R1-Distill-Llama-8B\"\n",
158+
"model_id = \"Qwen/Qwen3-4B-Instruct-2507\"\n",
157159
"model_id_filesafe = model_id.replace(\"/\",\"_\")\n",
158160
"\n",
159161
"use_local_model = True #set to false for the training job to download from HF, otherwise True will download locally"
@@ -225,7 +227,7 @@
225227
"metadata": {},
226228
"outputs": [],
227229
"source": [
228-
"model_name = \"DeepSeek-R1-Distill-Llama-8B\"\n",
230+
"model_name = \"Qwen3-4B-Instruct-2507\"\n",
229231
"\n",
230232
"lmi_model = sagemaker.Model(\n",
231233
" image_uri=inference_image_uri,\n",
@@ -242,12 +244,15 @@
242244
"metadata": {},
243245
"outputs": [],
244246
"source": [
245-
"base_endpoint_name = f\"{model_name}-endpoint\"\n",
247+
"from sagemaker.utils import name_from_base\n",
248+
"\n",
249+
"endpoint_name = f\"{model_name}-endpoint\"\n",
250+
"BASE_ENDPOINT_NAME = name_from_base(endpoint_name)\n",
246251
"\n",
247252
"predictor = lmi_model.deploy(\n",
248253
" initial_instance_count=1, \n",
249254
" instance_type=\"ml.g5.2xlarge\",\n",
250-
" endpoint_name=base_endpoint_name\n",
255+
" endpoint_name=BASE_ENDPOINT_NAME\n",
251256
")"
252257
]
253258
},
@@ -258,30 +263,19 @@
258263
"metadata": {},
259264
"outputs": [],
260265
"source": [
261-
"base_prompt = f\"\"\"\n",
262-
"<|begin_of_text|>\n",
263-
"<|start_header_id|>system<|end_header_id|>\n",
264-
"You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning. \n",
266+
"SYSTEM_PROMPT = f\"\"\"You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning. \n",
265267
"Below is an instruction that describes a task, paired with an input that provides further context. \n",
266268
"Write a response that appropriately completes the request.\n",
267-
"Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.\n",
268-
"<|eot_id|><|start_header_id|>user<|end_header_id|>\n",
269-
"{{question}}<|eot_id|>\n",
270-
"<|start_header_id|>assistant<|end_header_id|>\"\"\""
271-
]
272-
},
273-
{
274-
"cell_type": "code",
275-
"execution_count": null,
276-
"id": "6b37e7f1-730c-4b31-aa3b-55e2009f8f04",
277-
"metadata": {},
278-
"outputs": [],
279-
"source": [
280-
"prompt = base_prompt.format(\n",
281-
" question=\"A 3-week-old child has been diagnosed with late onset perinatal meningitis, and the CSF culture shows gram-positive bacilli. What characteristic of this bacterium can specifically differentiate it from other bacterial agents?\"\n",
282-
")\n",
269+
"Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.\"\"\"\n",
270+
"\n",
271+
"USER_PROMPT = \"A 3-week-old child has been diagnosed with late onset perinatal meningitis, and the CSF culture shows gram-positive bacilli. What characteristic of this bacterium can specifically differentiate it from other bacterial agents?\"\n",
283272
"\n",
284-
"print(prompt)"
273+
"messages = [\n",
274+
" {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
275+
" {\"role\": \"user\", \"content\": USER_PROMPT},\n",
276+
"]\n",
277+
"\n",
278+
"messages"
285279
]
286280
},
287281
{
@@ -292,35 +286,44 @@
292286
"outputs": [],
293287
"source": [
294288
"predictor = sagemaker.Predictor(\n",
295-
" endpoint_name=base_endpoint_name,\n",
289+
" endpoint_name=BASE_ENDPOINT_NAME,\n",
296290
" sagemaker_session=sagemaker_session,\n",
297291
" serializer=sagemaker.serializers.JSONSerializer(),\n",
298292
" deserializer=sagemaker.deserializers.JSONDeserializer(),\n",
299293
")\n",
300294
"\n",
301295
"response = predictor.predict({\n",
302-
"\t\"inputs\": prompt,\n",
296+
"\t\"messages\": messages,\n",
303297
" \"parameters\": {\n",
304298
" \"temperature\": 0.2,\n",
305299
" \"top_p\": 0.9,\n",
306300
" \"return_full_text\": False,\n",
307-
" \"max_new_tokens\": 1024,\n",
308-
" \"stop\": ['<|eot_id|>']\n",
301+
" \"max_new_tokens\": 1024\n",
309302
" }\n",
310303
"})\n",
311304
"\n",
312-
"response = response[\"generated_text\"].split(\"<|eot_id|>\")[0]\n",
305+
"response[\"choices\"][0][\"message\"][\"content\"]"
306+
]
307+
},
308+
{
309+
"cell_type": "markdown",
310+
"id": "165c8660-ee18-411f-9d8a-8032c6171d77",
311+
"metadata": {},
312+
"source": [
313+
"### Store variables\n",
313314
"\n",
314-
"response"
315+
"Save the endpoint name for use later"
315316
]
316317
},
317318
{
318319
"cell_type": "code",
319320
"execution_count": null,
320-
"id": "dbfc37bb-dc1f-4ba7-9948-6e482c1c86b0",
321+
"id": "0ed6ca9e-705c-4d01-9118-110b86caaef6",
321322
"metadata": {},
322323
"outputs": [],
323-
"source": []
324+
"source": [
325+
"%store BASE_ENDPOINT_NAME"
326+
]
324327
}
325328
],
326329
"metadata": {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
transformers==4.48.2
1+
transformers==4.53.0
22
peft==0.14.0
33
accelerate==1.3.0
44
bitsandbytes==0.45.1
55
datasets==3.2.0
66
evaluate==0.4.3
77
huggingface_hub[hf_transfer]==0.33.4
8-
mlflow
8+
mlflow==2.22.2
99
safetensors>=0.4.5
10-
sagemaker==2.239.0
10+
sagemaker==2.252.0
1111
sagemaker-mlflow==0.1.0
1212
sentencepiece==0.2.0
1313
scikit-learn==1.6.1
1414
tokenizers>=0.21.0
1515
trl==0.9.6
16-
py7zr
16+
py7zr==1.0.0

0 commit comments

Comments
 (0)