Skip to content

Commit fcd8e44

Browse files
committed
Moved fine-tuning to the end
1 parent 41916d6 commit fcd8e44

File tree

1 file changed

+92
-93
lines changed

1 file changed

+92
-93
lines changed

workshops/fine-tuning-with-sagemakerai-and-bedrock/task_05_fmops/05.00_fmops_examples.ipynb

Lines changed: 92 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -187,96 +187,7 @@
187187
"cell_type": "markdown",
188188
"metadata": {},
189189
"source": [
190-
"### 4. Templating a Prompt"
191-
]
192-
},
193-
{
194-
"cell_type": "markdown",
195-
"metadata": {},
196-
"source": [
197-
"In this workshop we are going to fine-tune DeepSeek-R1-Distill-Llama-8B to become a medical expert. To accomplish this, we will execute a fine-tuning job using Managed MLflow on SageMaker AI. We get our data from the [FreedomIntelligence/medical-o1-reasoning-SFT](https://huggingface.co/datasets/FreedomIntelligence/medical-o1-reasoning-SFT) dataset, available on HuggingFace.\n",
198-
"\n",
199-
"We perform the full fine-tuning step in the next lab. In this lab, we show a small example of what fine-tuning looks like for a single record of the dataset."
200-
]
201-
},
202-
{
203-
"cell_type": "code",
204-
"execution_count": null,
205-
"metadata": {},
206-
"outputs": [],
207-
"source": [
208-
"FINE_TUNING_DATA_SAMPLE = {\n",
209-
" \"Question\": \"A 61-year-old woman with a long history of involuntary urine loss during activities like coughing or sneezing but no leakage at night undergoes a gynecological exam and Q-tip test. Based on these findings, what would cystometry most likely reveal about her residual volume and detrusor contractions?\", \n",
210-
" \"Complex_CoT\": \"Okay, let's think about this step by step. There's a 61-year-old woman here who's been dealing with involuntary urine leakages whenever she's doing something that ups her abdominal pressure like coughing or sneezing. This sounds a lot like stress urinary incontinence to me. Now, it's interesting that she doesn't have any issues at night; she isn't experiencing leakage while sleeping. This likely means her bladder's ability to hold urine is fine when she isn't under physical stress. Hmm, that's a clue that we're dealing with something related to pressure rather than a bladder muscle problem.\\n\\nThe fact that she underwent a Q-tip test is intriguing too. This test is usually done to assess urethral mobility. In stress incontinence, a Q-tip might move significantly, showing urethral hypermobility. This kind of movement often means there's a weakness in the support structures that should help keep the urethra closed during increases in abdominal pressure. So, that's aligning well with stress incontinence.\\n\\nNow, let's think about what would happen during cystometry. Since stress incontinence isn't usually about sudden bladder contractions, I wouldn't expect to see involuntary detrusor contractions during this test. Her bladder isn't spasming or anything; it's more about the support structure failing under stress. Plus, she likely empties her bladder completely because stress incontinence doesn't typically involve incomplete emptying. So, her residual volume should be pretty normal.\\n\\nAll in all, it seems like if they do a cystometry on her, it will likely show a normal residual volume and no involuntary contractions. Yup, I think that makes sense given her symptoms and the typical presentations of stress urinary incontinence.\",\n",
211-
" \"Response\": \"Cystometry in this case of stress urinary incontinence would most likely reveal a normal post-void residual volume, as stress incontinence typically does not involve issues with bladder emptying. Additionally, since stress urinary incontinence is primarily related to physical exertion and not an overactive bladder, you would not expect to see any involuntary detrusor contractions during the test.\"\n",
212-
"}\n",
213-
"\n",
214-
"\n",
215-
"PROMPT_TEMPLATE = \"\"\"\n",
216-
"<|begin_of_text|>\n",
217-
" <|start_header_id|>system<|end_header_id|>\n",
218-
" You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning. \n",
219-
" Below is an instruction that describes a task, paired with an input that provides further context. \n",
220-
" Write a response that appropriately completes the request.\n",
221-
" Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.\n",
222-
" <|eot_id|>\n",
223-
" <|start_header_id|>user<|end_header_id|>\n",
224-
" {{question}}\n",
225-
" <|eot_id|>\n",
226-
" <|start_header_id|>assistant<|end_header_id|>\n",
227-
" {{complex_cot}}\n",
228-
" {{answer}}\n",
229-
"<|eot_id|>\n",
230-
"\"\"\"\n",
231-
"\n",
232-
"# Template dataset to add prompt to each sample\n",
233-
"def template_dataset(sample):\n",
234-
" try:\n",
235-
" sample[\"text\"] = PROMPT_TEMPLATE.format(question=sample[\"Question\"],\n",
236-
" complex_cot=sample[\"Complex_CoT\"],\n",
237-
" answer=sample[\"Response\"])\n",
238-
" return sample\n",
239-
" except KeyError as e:\n",
240-
" print(f\"KeyError in template_dataset: {str(e)}\")\n",
241-
" # Provide default values for missing fields\n",
242-
" missing_key = str(e).strip(\"'\")\n",
243-
" if missing_key == \"Question\":\n",
244-
" sample[\"text\"] = PROMPT_TEMPLATE.format(\n",
245-
" question=\"[Missing question]\",\n",
246-
" complex_cot=sample.get(\"Complex_CoT\", \"[Missing CoT]\"),\n",
247-
" answer=sample.get(\"Response\", \"[Missing response]\")\n",
248-
" )\n",
249-
" elif missing_key == \"Complex_CoT\":\n",
250-
" sample[\"text\"] = PROMPT_TEMPLATE.format(\n",
251-
" question=sample[\"Question\"],\n",
252-
" complex_cot=\"[Missing CoT]\",\n",
253-
" answer=sample.get(\"Response\", \"[Missing response]\")\n",
254-
" )\n",
255-
" elif missing_key == \"Response\":\n",
256-
" sample[\"text\"] = PROMPT_TEMPLATE.format(\n",
257-
" question=sample[\"Question\"],\n",
258-
" complex_cot=sample.get(\"Complex_CoT\", \"[Missing CoT]\"),\n",
259-
" answer=\"[Missing response]\"\n",
260-
" )\n",
261-
" return sample\n",
262-
"\n",
263-
"PROCESSED_SAMPLE = template_dataset(FINE_TUNING_DATA_SAMPLE)\n",
264-
"print(PROCESSED_SAMPLE)"
265-
]
266-
},
267-
{
268-
"cell_type": "markdown",
269-
"metadata": {},
270-
"source": [
271-
"##### Fine-Tuning Output\n",
272-
"The above output shows the templated prompt output to be used for fine-tuning. This pre-processing happens for every record in the fine-tuning dataset before fine-tuning actually takes place. This can be time-consuming for large fine-tuning datasets. We will show in the next lab how to orchestrate this with MLflow."
273-
]
274-
},
275-
{
276-
"cell_type": "markdown",
277-
"metadata": {},
278-
"source": [
279-
"### 5. Model Deployment\n",
190+
"### 4. Model Deployment\n",
280191
"There are several approaches to deploying a model to a SageMaker AI managed endpoint. In this section, we explore the most direct option which downloads a model directly from HuggingFace to the managed endpoint via SageMaker JumpStart. We are still using DeepSeek-R1-Distill-Llama-8B, but we have not fine-tuned it. The purpose of this section is to illustrate the components required to customize a model deployment on SageMaker before fine-tuning it."
281192
]
282193
},
@@ -433,8 +344,7 @@
433344
" deserializer=JSONDeserializer()\n",
434345
")\n",
435346
"predictor.predict({\n",
436-
" # \"inputs\": \"Hi, what can you help me with?\",\n",
437-
" \"inputs\": FINE_TUNING_DATA_SAMPLE[\"Question\"],\n",
347+
" \"inputs\": \"Hi, what can you help me with?\",\n",
438348
" \"parameters\": {\n",
439349
" \"max_new_tokens\": 512,\n",
440350
" \"top_p\": 0.9,\n",
@@ -525,7 +435,7 @@
525435
"cell_type": "markdown",
526436
"metadata": {},
527437
"source": [
528-
"### 6. Qualitative Model Evaluation\n",
438+
"### 5. Qualitative Model Evaluation\n",
529439
"Let's test the default DeepSeek-R1-Distill-Llama-8B using MLFlow's LLM-as-a-Judge capability. We'll use [Anthropic's Claude 3 Haiku](https://www.anthropic.com/news/claude-3-haiku) model on [Amazon Bedrock](https://aws.amazon.com/bedrock/) as the judge. We'll also wrap our model endpoint invocation in a method making it easier to call in the evaluation. \n",
530440
"\n",
531441
"This particular endpoint is the [cross-region inference endpoint](https://docs.aws.amazon.com/bedrock/latest/userguide/cross-region-inference.html) name for Claude 3 Haiku.\n",
@@ -993,6 +903,95 @@
993903
" print(error_msg)"
994904
]
995905
},
906+
{
907+
"cell_type": "markdown",
908+
"metadata": {},
909+
"source": [
910+
"### 6. Templating a Prompt"
911+
]
912+
},
913+
{
914+
"cell_type": "markdown",
915+
"metadata": {},
916+
"source": [
917+
"In the next workshop we fine-tune DeepSeek-R1-Distill-Llama-8B to become a medical expert. To accomplish this, we execute a fine-tuning job using Managed MLflow on SageMaker AI. We get our data from the [FreedomIntelligence/medical-o1-reasoning-SFT](https://huggingface.co/datasets/FreedomIntelligence/medical-o1-reasoning-SFT) dataset, available on HuggingFace.\n",
918+
"\n",
919+
"In this lab, we show a small example of what fine-tuning looks like for a single record of the dataset."
920+
]
921+
},
922+
{
923+
"cell_type": "code",
924+
"execution_count": null,
925+
"metadata": {},
926+
"outputs": [],
927+
"source": [
928+
"FINE_TUNING_DATA_SAMPLE = {\n",
929+
" \"Question\": \"A 61-year-old woman with a long history of involuntary urine loss during activities like coughing or sneezing but no leakage at night undergoes a gynecological exam and Q-tip test. Based on these findings, what would cystometry most likely reveal about her residual volume and detrusor contractions?\", \n",
930+
" \"Complex_CoT\": \"Okay, let's think about this step by step. There's a 61-year-old woman here who's been dealing with involuntary urine leakages whenever she's doing something that ups her abdominal pressure like coughing or sneezing. This sounds a lot like stress urinary incontinence to me. Now, it's interesting that she doesn't have any issues at night; she isn't experiencing leakage while sleeping. This likely means her bladder's ability to hold urine is fine when she isn't under physical stress. Hmm, that's a clue that we're dealing with something related to pressure rather than a bladder muscle problem.\\n\\nThe fact that she underwent a Q-tip test is intriguing too. This test is usually done to assess urethral mobility. In stress incontinence, a Q-tip might move significantly, showing urethral hypermobility. This kind of movement often means there's a weakness in the support structures that should help keep the urethra closed during increases in abdominal pressure. So, that's aligning well with stress incontinence.\\n\\nNow, let's think about what would happen during cystometry. Since stress incontinence isn't usually about sudden bladder contractions, I wouldn't expect to see involuntary detrusor contractions during this test. Her bladder isn't spasming or anything; it's more about the support structure failing under stress. Plus, she likely empties her bladder completely because stress incontinence doesn't typically involve incomplete emptying. So, her residual volume should be pretty normal.\\n\\nAll in all, it seems like if they do a cystometry on her, it will likely show a normal residual volume and no involuntary contractions. Yup, I think that makes sense given her symptoms and the typical presentations of stress urinary incontinence.\",\n",
931+
" \"Response\": \"Cystometry in this case of stress urinary incontinence would most likely reveal a normal post-void residual volume, as stress incontinence typically does not involve issues with bladder emptying. Additionally, since stress urinary incontinence is primarily related to physical exertion and not an overactive bladder, you would not expect to see any involuntary detrusor contractions during the test.\"\n",
932+
"}\n",
933+
"\n",
934+
"\n",
935+
"PROMPT_TEMPLATE = \"\"\"\n",
936+
"<|begin_of_text|>\n",
937+
" <|start_header_id|>system<|end_header_id|>\n",
938+
" You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning. \n",
939+
" Below is an instruction that describes a task, paired with an input that provides further context. \n",
940+
" Write a response that appropriately completes the request.\n",
941+
" Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.\n",
942+
" <|eot_id|>\n",
943+
" <|start_header_id|>user<|end_header_id|>\n",
944+
" {{question}}\n",
945+
" <|eot_id|>\n",
946+
" <|start_header_id|>assistant<|end_header_id|>\n",
947+
" {{complex_cot}}\n",
948+
" {{answer}}\n",
949+
"<|eot_id|>\n",
950+
"\"\"\"\n",
951+
"\n",
952+
"# Template dataset to add prompt to each sample\n",
953+
"def template_dataset(sample):\n",
954+
" try:\n",
955+
" sample[\"text\"] = PROMPT_TEMPLATE.format(question=sample[\"Question\"],\n",
956+
" complex_cot=sample[\"Complex_CoT\"],\n",
957+
" answer=sample[\"Response\"])\n",
958+
" return sample\n",
959+
" except KeyError as e:\n",
960+
" print(f\"KeyError in template_dataset: {str(e)}\")\n",
961+
" # Provide default values for missing fields\n",
962+
" missing_key = str(e).strip(\"'\")\n",
963+
" if missing_key == \"Question\":\n",
964+
" sample[\"text\"] = PROMPT_TEMPLATE.format(\n",
965+
" question=\"[Missing question]\",\n",
966+
" complex_cot=sample.get(\"Complex_CoT\", \"[Missing CoT]\"),\n",
967+
" answer=sample.get(\"Response\", \"[Missing response]\")\n",
968+
" )\n",
969+
" elif missing_key == \"Complex_CoT\":\n",
970+
" sample[\"text\"] = PROMPT_TEMPLATE.format(\n",
971+
" question=sample[\"Question\"],\n",
972+
" complex_cot=\"[Missing CoT]\",\n",
973+
" answer=sample.get(\"Response\", \"[Missing response]\")\n",
974+
" )\n",
975+
" elif missing_key == \"Response\":\n",
976+
" sample[\"text\"] = PROMPT_TEMPLATE.format(\n",
977+
" question=sample[\"Question\"],\n",
978+
" complex_cot=sample.get(\"Complex_CoT\", \"[Missing CoT]\"),\n",
979+
" answer=\"[Missing response]\"\n",
980+
" )\n",
981+
" return sample\n",
982+
"\n",
983+
"PROCESSED_SAMPLE = template_dataset(FINE_TUNING_DATA_SAMPLE)\n",
984+
"print(PROCESSED_SAMPLE)"
985+
]
986+
},
987+
{
988+
"cell_type": "markdown",
989+
"metadata": {},
990+
"source": [
991+
"##### Fine-Tuning Output\n",
992+
"The above output shows the templated prompt output to be used for fine-tuning. This pre-processing happens for every record in the fine-tuning dataset before fine-tuning actually takes place. This can be time-consuming for large fine-tuning datasets. We will show in the next lab how to orchestrate this with MLflow."
993+
]
994+
},
996995
{
997996
"cell_type": "markdown",
998997
"metadata": {},

0 commit comments

Comments
 (0)