Upload llama-3-2-3b-reasoning-vi-medical-lora-training.ipynb
Browse files
    	
        notebooks/llama-3-2-3b-reasoning-vi-medical-lora-training.ipynb
    ADDED
    
    | @@ -0,0 +1,994 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            {
         | 
| 2 | 
            +
             "cells": [
         | 
| 3 | 
            +
              {
         | 
| 4 | 
            +
               "cell_type": "markdown",
         | 
| 5 | 
            +
               "metadata": {},
         | 
| 6 | 
            +
               "source": [
         | 
| 7 | 
            +
                "# Download Dependencies"
         | 
| 8 | 
            +
               ]
         | 
| 9 | 
            +
              },
         | 
| 10 | 
            +
              {
         | 
| 11 | 
            +
               "cell_type": "code",
         | 
| 12 | 
            +
               "execution_count": 5,
         | 
| 13 | 
            +
               "metadata": {
         | 
| 14 | 
            +
                "execution": {
         | 
| 15 | 
            +
                 "iopub.execute_input": "2025-07-20T11:12:17.715970Z",
         | 
| 16 | 
            +
                 "iopub.status.busy": "2025-07-20T11:12:17.715559Z",
         | 
| 17 | 
            +
                 "iopub.status.idle": "2025-07-20T11:12:21.426930Z",
         | 
| 18 | 
            +
                 "shell.execute_reply": "2025-07-20T11:12:21.425827Z",
         | 
| 19 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:12:17.715941Z"
         | 
| 20 | 
            +
                },
         | 
| 21 | 
            +
                "trusted": true
         | 
| 22 | 
            +
               },
         | 
| 23 | 
            +
               "outputs": [],
         | 
| 24 | 
            +
               "source": [
         | 
| 25 | 
            +
                "!pip install -q unsloth==2025.6.8"
         | 
| 26 | 
            +
               ]
         | 
| 27 | 
            +
              },
         | 
| 28 | 
            +
              {
         | 
| 29 | 
            +
               "cell_type": "markdown",
         | 
| 30 | 
            +
               "metadata": {},
         | 
| 31 | 
            +
               "source": [
         | 
| 32 | 
            +
                "# Download Datasets and Models"
         | 
| 33 | 
            +
               ]
         | 
| 34 | 
            +
              },
         | 
| 35 | 
            +
              {
         | 
| 36 | 
            +
               "cell_type": "code",
         | 
| 37 | 
            +
               "execution_count": 6,
         | 
| 38 | 
            +
               "metadata": {
         | 
| 39 | 
            +
                "execution": {
         | 
| 40 | 
            +
                 "iopub.execute_input": "2025-07-20T11:12:21.429056Z",
         | 
| 41 | 
            +
                 "iopub.status.busy": "2025-07-20T11:12:21.428734Z",
         | 
| 42 | 
            +
                 "iopub.status.idle": "2025-07-20T11:12:21.435207Z",
         | 
| 43 | 
            +
                 "shell.execute_reply": "2025-07-20T11:12:21.434516Z",
         | 
| 44 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:12:21.429023Z"
         | 
| 45 | 
            +
                },
         | 
| 46 | 
            +
                "trusted": true
         | 
| 47 | 
            +
               },
         | 
| 48 | 
            +
               "outputs": [
         | 
| 49 | 
            +
                {
         | 
| 50 | 
            +
                 "name": "stdout",
         | 
| 51 | 
            +
                 "output_type": "stream",
         | 
| 52 | 
            +
                 "text": [
         | 
| 53 | 
            +
                  "datasets.__version__ 3.6.0\n",
         | 
| 54 | 
            +
                  "numpy.__version__ 1.26.4\n",
         | 
| 55 | 
            +
                  "unsloth.__version__ 2025.6.8\n",
         | 
| 56 | 
            +
                  "torch.__version__ 2.7.0+cu126\n",
         | 
| 57 | 
            +
                  "transformers.__version__ 4.52.4\n",
         | 
| 58 | 
            +
                  "trl.__version__ 0.19.1\n"
         | 
| 59 | 
            +
                 ]
         | 
| 60 | 
            +
                }
         | 
| 61 | 
            +
               ],
         | 
| 62 | 
            +
               "source": [
         | 
| 63 | 
            +
                "import unsloth\n",
         | 
| 64 | 
            +
                "\n",
         | 
| 65 | 
            +
                "import datasets\n",
         | 
| 66 | 
            +
                "import numpy as np\n",
         | 
| 67 | 
            +
                "\n",
         | 
| 68 | 
            +
                "import torch\n",
         | 
| 69 | 
            +
                "import trl \n",
         | 
| 70 | 
            +
                "import transformers\n",
         | 
| 71 | 
            +
                "\n",
         | 
| 72 | 
            +
                "print(\"datasets.__version__\", datasets.__version__)\n",
         | 
| 73 | 
            +
                "print(\"numpy.__version__\", np.__version__)\n",
         | 
| 74 | 
            +
                "print(\"unsloth.__version__\", unsloth.__version__)\n",
         | 
| 75 | 
            +
                "print(\"torch.__version__\", torch.__version__)\n",
         | 
| 76 | 
            +
                "print(\"transformers.__version__\", transformers.__version__)\n",
         | 
| 77 | 
            +
                "print(\"trl.__version__\", trl.__version__)\n",
         | 
| 78 | 
            +
                "\n",
         | 
| 79 | 
            +
                "# datasets.__version__ 3.6.0\n",
         | 
| 80 | 
            +
                "# numpy.__version__ 1.26.4\n",
         | 
| 81 | 
            +
                "# unsloth.__version__ 2025.6.3\n",
         | 
| 82 | 
            +
                "# torch.__version__ 2.7.0+cu126\n",
         | 
| 83 | 
            +
                "# transformers.__version__ 4.51.3\n",
         | 
| 84 | 
            +
                "# trl.__version__ 0.19.0"
         | 
| 85 | 
            +
               ]
         | 
| 86 | 
            +
              },
         | 
| 87 | 
            +
              {
         | 
| 88 | 
            +
               "cell_type": "code",
         | 
| 89 | 
            +
               "execution_count": null,
         | 
| 90 | 
            +
               "metadata": {
         | 
| 91 | 
            +
                "execution": {
         | 
| 92 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:12.985746Z",
         | 
| 93 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:12.985464Z",
         | 
| 94 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:13.292103Z",
         | 
| 95 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:13.291221Z",
         | 
| 96 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:12.985725Z"
         | 
| 97 | 
            +
                },
         | 
| 98 | 
            +
                "trusted": true
         | 
| 99 | 
            +
               },
         | 
| 100 | 
            +
               "outputs": [],
         | 
| 101 | 
            +
               "source": [
         | 
| 102 | 
            +
                "from huggingface_hub import HfApi\n",
         | 
| 103 | 
            +
                "from huggingface_hub import snapshot_download\n",
         | 
| 104 | 
            +
                "import os\n",
         | 
| 105 | 
            +
                "api = HfApi()\n",
         | 
| 106 | 
            +
                "\n",
         | 
| 107 | 
            +
                "repo_id = \"danhtran2mind/Llama-3.2-3B-Reasoning-Vi-Medical-LoRA\"\n",
         | 
| 108 | 
            +
                "save_path = \"./Llama-3.2-3B-Reasoning-Vi-Medical-LoRA\"\n",
         | 
| 109 | 
            +
                "\n",
         | 
| 110 | 
            +
                "# Create the directory if it doesn't exist\n",
         | 
| 111 | 
            +
                "os.makedirs(save_path, exist_ok=True)\n",
         | 
| 112 | 
            +
                "\n",
         | 
| 113 | 
            +
                "# Download the dataset\n",
         | 
| 114 | 
            +
                "snapshot_download(repo_id=repo_id, repo_type=\"model\", local_dir=save_path)"
         | 
| 115 | 
            +
               ]
         | 
| 116 | 
            +
              },
         | 
| 117 | 
            +
              {
         | 
| 118 | 
            +
               "cell_type": "code",
         | 
| 119 | 
            +
               "execution_count": null,
         | 
| 120 | 
            +
               "metadata": {
         | 
| 121 | 
            +
                "execution": {
         | 
| 122 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:15.936427Z",
         | 
| 123 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:15.935684Z",
         | 
| 124 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:15.989125Z",
         | 
| 125 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:15.988356Z",
         | 
| 126 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:15.936392Z"
         | 
| 127 | 
            +
                },
         | 
| 128 | 
            +
                "trusted": true
         | 
| 129 | 
            +
               },
         | 
| 130 | 
            +
               "outputs": [],
         | 
| 131 | 
            +
               "source": [
         | 
| 132 | 
            +
                "# To temporary Model hub\n",
         | 
| 133 | 
            +
                "from huggingface_hub import HfApi\n",
         | 
| 134 | 
            +
                "from huggingface_hub import login\n",
         | 
| 135 | 
            +
                "# Initialize API\n",
         | 
| 136 | 
            +
                "login(\"<your_huggingface_token>\")  # Replace with your Hugging Face token\n",
         | 
| 137 | 
            +
                "api = HfApi()"
         | 
| 138 | 
            +
               ]
         | 
| 139 | 
            +
              },
         | 
| 140 | 
            +
              {
         | 
| 141 | 
            +
               "cell_type": "code",
         | 
| 142 | 
            +
               "execution_count": 13,
         | 
| 143 | 
            +
               "metadata": {
         | 
| 144 | 
            +
                "execution": {
         | 
| 145 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:17.831047Z",
         | 
| 146 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:17.830296Z",
         | 
| 147 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:17.835080Z",
         | 
| 148 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:17.834392Z",
         | 
| 149 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:17.831021Z"
         | 
| 150 | 
            +
                },
         | 
| 151 | 
            +
                "trusted": true
         | 
| 152 | 
            +
               },
         | 
| 153 | 
            +
               "outputs": [],
         | 
| 154 | 
            +
               "source": [
         | 
| 155 | 
            +
                "import numpy as np\n",
         | 
| 156 | 
            +
                "from datasets import concatenate_datasets, load_dataset\n",
         | 
| 157 | 
            +
                "\n",
         | 
| 158 | 
            +
                "from unsloth import FastLanguageModel\n",
         | 
| 159 | 
            +
                "from unsloth import is_bfloat16_supported\n",
         | 
| 160 | 
            +
                "from unsloth.chat_templates import train_on_responses_only  \n",
         | 
| 161 | 
            +
                "import torch\n",
         | 
| 162 | 
            +
                "\n",
         | 
| 163 | 
            +
                "from trl import SFTTrainer\n",
         | 
| 164 | 
            +
                "from transformers import TrainingArguments, DataCollatorForSeq2Seq\n",
         | 
| 165 | 
            +
                "\n",
         | 
| 166 | 
            +
                "from unsloth import FastModel\n",
         | 
| 167 | 
            +
                "from unsloth.chat_templates import get_chat_template"
         | 
| 168 | 
            +
               ]
         | 
| 169 | 
            +
              },
         | 
| 170 | 
            +
              {
         | 
| 171 | 
            +
               "cell_type": "code",
         | 
| 172 | 
            +
               "execution_count": null,
         | 
| 173 | 
            +
               "metadata": {
         | 
| 174 | 
            +
                "execution": {
         | 
| 175 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:19.050548Z",
         | 
| 176 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:19.050275Z",
         | 
| 177 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:41.489209Z",
         | 
| 178 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:41.488392Z",
         | 
| 179 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:19.050529Z"
         | 
| 180 | 
            +
                },
         | 
| 181 | 
            +
                "trusted": true
         | 
| 182 | 
            +
               },
         | 
| 183 | 
            +
               "outputs": [
         | 
| 184 | 
            +
                {
         | 
| 185 | 
            +
                 "name": "stdout",
         | 
| 186 | 
            +
                 "output_type": "stream",
         | 
| 187 | 
            +
                 "text": [
         | 
| 188 | 
            +
                  "==((====))==  Unsloth 2025.6.8: Fast Llama patching. Transformers: 4.52.4.\n",
         | 
| 189 | 
            +
                  "   \\\\   /|    Tesla P100-PCIE-16GB. Num GPUs = 1. Max memory: 15.888 GB. Platform: Linux.\n",
         | 
| 190 | 
            +
                  "O^O/ \\_/ \\    Torch: 2.7.0+cu126. CUDA: 6.0. CUDA Toolkit: 12.6. Triton: 3.3.0\n",
         | 
| 191 | 
            +
                  "\\        /    Bfloat16 = FALSE. FA [Xformers = 0.0.30. FA2 = False]\n",
         | 
| 192 | 
            +
                  " \"-____-\"     Free license: http://github.com/unslothai/unsloth\n",
         | 
| 193 | 
            +
                  "Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!\n"
         | 
| 194 | 
            +
                 ]
         | 
| 195 | 
            +
                },
         | 
| 196 | 
            +
                {
         | 
| 197 | 
            +
                 "data": {
         | 
| 198 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 199 | 
            +
                   "model_id": "a9f78c2a5daa4a9ba498798ede73cfd4",
         | 
| 200 | 
            +
                   "version_major": 2,
         | 
| 201 | 
            +
                   "version_minor": 0
         | 
| 202 | 
            +
                  },
         | 
| 203 | 
            +
                  "text/plain": [
         | 
| 204 | 
            +
                   "model.safetensors:   0%|          | 0.00/2.35G [00:00<?, ?B/s]"
         | 
| 205 | 
            +
                  ]
         | 
| 206 | 
            +
                 },
         | 
| 207 | 
            +
                 "metadata": {},
         | 
| 208 | 
            +
                 "output_type": "display_data"
         | 
| 209 | 
            +
                },
         | 
| 210 | 
            +
                {
         | 
| 211 | 
            +
                 "data": {
         | 
| 212 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 213 | 
            +
                   "model_id": "bae4399e5c644daf8b1739caac8d550d",
         | 
| 214 | 
            +
                   "version_major": 2,
         | 
| 215 | 
            +
                   "version_minor": 0
         | 
| 216 | 
            +
                  },
         | 
| 217 | 
            +
                  "text/plain": [
         | 
| 218 | 
            +
                   "generation_config.json:   0%|          | 0.00/234 [00:00<?, ?B/s]"
         | 
| 219 | 
            +
                  ]
         | 
| 220 | 
            +
                 },
         | 
| 221 | 
            +
                 "metadata": {},
         | 
| 222 | 
            +
                 "output_type": "display_data"
         | 
| 223 | 
            +
                },
         | 
| 224 | 
            +
                {
         | 
| 225 | 
            +
                 "data": {
         | 
| 226 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 227 | 
            +
                   "model_id": "1f9ee0aec34644fa87799e4eeec599de",
         | 
| 228 | 
            +
                   "version_major": 2,
         | 
| 229 | 
            +
                   "version_minor": 0
         | 
| 230 | 
            +
                  },
         | 
| 231 | 
            +
                  "text/plain": [
         | 
| 232 | 
            +
                   "tokenizer_config.json: 0.00B [00:00, ?B/s]"
         | 
| 233 | 
            +
                  ]
         | 
| 234 | 
            +
                 },
         | 
| 235 | 
            +
                 "metadata": {},
         | 
| 236 | 
            +
                 "output_type": "display_data"
         | 
| 237 | 
            +
                },
         | 
| 238 | 
            +
                {
         | 
| 239 | 
            +
                 "data": {
         | 
| 240 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 241 | 
            +
                   "model_id": "e280268830b84c158b2a301c481fc65d",
         | 
| 242 | 
            +
                   "version_major": 2,
         | 
| 243 | 
            +
                   "version_minor": 0
         | 
| 244 | 
            +
                  },
         | 
| 245 | 
            +
                  "text/plain": [
         | 
| 246 | 
            +
                   "special_tokens_map.json:   0%|          | 0.00/454 [00:00<?, ?B/s]"
         | 
| 247 | 
            +
                  ]
         | 
| 248 | 
            +
                 },
         | 
| 249 | 
            +
                 "metadata": {},
         | 
| 250 | 
            +
                 "output_type": "display_data"
         | 
| 251 | 
            +
                },
         | 
| 252 | 
            +
                {
         | 
| 253 | 
            +
                 "data": {
         | 
| 254 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 255 | 
            +
                   "model_id": "32bddcd8a99449d7a67dfbb140f9534b",
         | 
| 256 | 
            +
                   "version_major": 2,
         | 
| 257 | 
            +
                   "version_minor": 0
         | 
| 258 | 
            +
                  },
         | 
| 259 | 
            +
                  "text/plain": [
         | 
| 260 | 
            +
                   "tokenizer.json:   0%|          | 0.00/17.2M [00:00<?, ?B/s]"
         | 
| 261 | 
            +
                  ]
         | 
| 262 | 
            +
                 },
         | 
| 263 | 
            +
                 "metadata": {},
         | 
| 264 | 
            +
                 "output_type": "display_data"
         | 
| 265 | 
            +
                },
         | 
| 266 | 
            +
                {
         | 
| 267 | 
            +
                 "data": {
         | 
| 268 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 269 | 
            +
                   "model_id": "1d5292e81a7648969dacf9c8b031a8bd",
         | 
| 270 | 
            +
                   "version_major": 2,
         | 
| 271 | 
            +
                   "version_minor": 0
         | 
| 272 | 
            +
                  },
         | 
| 273 | 
            +
                  "text/plain": [
         | 
| 274 | 
            +
                   "chat_template.jinja: 0.00B [00:00, ?B/s]"
         | 
| 275 | 
            +
                  ]
         | 
| 276 | 
            +
                 },
         | 
| 277 | 
            +
                 "metadata": {},
         | 
| 278 | 
            +
                 "output_type": "display_data"
         | 
| 279 | 
            +
                },
         | 
| 280 | 
            +
                {
         | 
| 281 | 
            +
                 "name": "stderr",
         | 
| 282 | 
            +
                 "output_type": "stream",
         | 
| 283 | 
            +
                 "text": [
         | 
| 284 | 
            +
                  "Unsloth 2025.6.8 patched 28 layers with 28 QKV layers, 28 O layers and 28 MLP layers.\n"
         | 
| 285 | 
            +
                 ]
         | 
| 286 | 
            +
                }
         | 
| 287 | 
            +
               ],
         | 
| 288 | 
            +
               "source": [
         | 
| 289 | 
            +
                "model_id = \"meta-llama/Llama-3.2-3B-Instruct\"\n",
         | 
| 290 | 
            +
                "\n",
         | 
| 291 | 
            +
                "lora_rank = 16\n",
         | 
| 292 | 
            +
                "model, tokenizer = FastLanguageModel.from_pretrained(\n",
         | 
| 293 | 
            +
                "    model_name = model_id,\n",
         | 
| 294 | 
            +
                "    max_seq_length = 2048,   # Context length - can be longer, but uses more memory\n",
         | 
| 295 | 
            +
                "    load_in_4bit = True,     # 4bit uses much less memory\n",
         | 
| 296 | 
            +
                "    load_in_8bit = False,    # A bit more accurate, uses 2x memory\n",
         | 
| 297 | 
            +
                "    full_finetuning = False, # We have full finetuning now!\n",
         | 
| 298 | 
            +
                ")\n",
         | 
| 299 | 
            +
                "\n",
         | 
| 300 | 
            +
                "model = FastLanguageModel.get_peft_model(  \n",
         | 
| 301 | 
            +
                "    model,  \n",
         | 
| 302 | 
            +
                "    r=lora_rank,  \n",
         | 
| 303 | 
            +
                "    target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\",  \n",
         | 
| 304 | 
            +
                "                    \"gate_proj\", \"up_proj\", \"down_proj\"],  \n",
         | 
| 305 | 
            +
                "    lora_alpha=lora_rank,  \n",
         | 
| 306 | 
            +
                "    lora_dropout=0,  \n",
         | 
| 307 | 
            +
                "    bias=\"none\",  \n",
         | 
| 308 | 
            +
                "    use_gradient_checkpointing=\"unsloth\",  \n",
         | 
| 309 | 
            +
                "    random_state=42,  \n",
         | 
| 310 | 
            +
                "    use_rslora=False,  \n",
         | 
| 311 | 
            +
                "    loftq_config=None\n",
         | 
| 312 | 
            +
                ")"
         | 
| 313 | 
            +
               ]
         | 
| 314 | 
            +
              },
         | 
| 315 | 
            +
              {
         | 
| 316 | 
            +
               "cell_type": "markdown",
         | 
| 317 | 
            +
               "metadata": {},
         | 
| 318 | 
            +
               "source": [
         | 
| 319 | 
            +
                "# Data Preparetation"
         | 
| 320 | 
            +
               ]
         | 
| 321 | 
            +
              },
         | 
| 322 | 
            +
              {
         | 
| 323 | 
            +
               "cell_type": "code",
         | 
| 324 | 
            +
               "execution_count": 15,
         | 
| 325 | 
            +
               "metadata": {
         | 
| 326 | 
            +
                "execution": {
         | 
| 327 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:41.490660Z",
         | 
| 328 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:41.490415Z",
         | 
| 329 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:41.494655Z",
         | 
| 330 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:41.493917Z",
         | 
| 331 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:41.490644Z"
         | 
| 332 | 
            +
                },
         | 
| 333 | 
            +
                "trusted": true
         | 
| 334 | 
            +
               },
         | 
| 335 | 
            +
               "outputs": [],
         | 
| 336 | 
            +
               "source": [
         | 
| 337 | 
            +
                "train_prompt_style = \"\"\"Bên dưới là một hướng dẫn mô tả nhiệm vụ, đi kèm với thông tin đầu vào để cung cấp thêm ngữ cảnh.\n",
         | 
| 338 | 
            +
                "Hãy viết một phản hồi hoàn thành yêu cầu một cách phù hợp.\n",
         | 
| 339 | 
            +
                "Trước khi trả lời, hãy suy nghĩ cẩn thận về câu hỏi và tạo ra một chuỗi suy nghĩ từng bước để đảm bảo một phản hồi logic và chính xác.\n",
         | 
| 340 | 
            +
                "\n",
         | 
| 341 | 
            +
                "### Instruction:\n",
         | 
| 342 | 
            +
                "Bạn là một chuyên gia y khoa với kiến thức chuyên sâu về lập luận lâm sàng, chẩn đoán và lập kế hoạch điều trị.\n",
         | 
| 343 | 
            +
                "Vui lòng trả lời câu hỏi y khoa sau đây.\n",
         | 
| 344 | 
            +
                "\n",
         | 
| 345 | 
            +
                "### Question:\n",
         | 
| 346 | 
            +
                "{}\n",
         | 
| 347 | 
            +
                "\n",
         | 
| 348 | 
            +
                "### Response:\n",
         | 
| 349 | 
            +
                "<think>\n",
         | 
| 350 | 
            +
                "{}\n",
         | 
| 351 | 
            +
                "</think>\n",
         | 
| 352 | 
            +
                "{}\"\"\""
         | 
| 353 | 
            +
               ]
         | 
| 354 | 
            +
              },
         | 
| 355 | 
            +
              {
         | 
| 356 | 
            +
               "cell_type": "code",
         | 
| 357 | 
            +
               "execution_count": 16,
         | 
| 358 | 
            +
               "metadata": {
         | 
| 359 | 
            +
                "execution": {
         | 
| 360 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:41.495856Z",
         | 
| 361 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:41.495633Z",
         | 
| 362 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:41.843399Z",
         | 
| 363 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:41.842696Z",
         | 
| 364 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:41.495837Z"
         | 
| 365 | 
            +
                },
         | 
| 366 | 
            +
                "trusted": true
         | 
| 367 | 
            +
               },
         | 
| 368 | 
            +
               "outputs": [],
         | 
| 369 | 
            +
               "source": [
         | 
| 370 | 
            +
                "# EOS_TOKEN = tokenizer.eos_token  # Must add EOS_TOKEN\n",
         | 
| 371 | 
            +
                "\n",
         | 
| 372 | 
            +
                "def formatting_prompts_func(examples):\n",
         | 
| 373 | 
            +
                "    inputs = examples[\"question\"]\n",
         | 
| 374 | 
            +
                "    complex_cots = examples[\"context\"]\n",
         | 
| 375 | 
            +
                "    outputs = examples[\"answer\"]\n",
         | 
| 376 | 
            +
                "    texts = []\n",
         | 
| 377 | 
            +
                "    for question, cot, response in zip(inputs, complex_cots, outputs):\n",
         | 
| 378 | 
            +
                "        # Append the EOS token to the response if it's not already there\n",
         | 
| 379 | 
            +
                "        if not response.endswith(tokenizer.eos_token):\n",
         | 
| 380 | 
            +
                "            response += tokenizer.eos_token\n",
         | 
| 381 | 
            +
                "        text = train_prompt_style.format(question, cot, response)\n",
         | 
| 382 | 
            +
                "        texts.append(text)\n",
         | 
| 383 | 
            +
                "    return {\"text\": texts}"
         | 
| 384 | 
            +
               ]
         | 
| 385 | 
            +
              },
         | 
| 386 | 
            +
              {
         | 
| 387 | 
            +
               "cell_type": "code",
         | 
| 388 | 
            +
               "execution_count": 17,
         | 
| 389 | 
            +
               "metadata": {
         | 
| 390 | 
            +
                "execution": {
         | 
| 391 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:41.845209Z",
         | 
| 392 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:41.844951Z",
         | 
| 393 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:44.981718Z",
         | 
| 394 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:44.980933Z",
         | 
| 395 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:41.845192Z"
         | 
| 396 | 
            +
                },
         | 
| 397 | 
            +
                "trusted": true
         | 
| 398 | 
            +
               },
         | 
| 399 | 
            +
               "outputs": [
         | 
| 400 | 
            +
                {
         | 
| 401 | 
            +
                 "data": {
         | 
| 402 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 403 | 
            +
                   "model_id": "865e7e1848834d178f15ba2f52a50428",
         | 
| 404 | 
            +
                   "version_major": 2,
         | 
| 405 | 
            +
                   "version_minor": 0
         | 
| 406 | 
            +
                  },
         | 
| 407 | 
            +
                  "text/plain": [
         | 
| 408 | 
            +
                   "README.md: 0.00B [00:00, ?B/s]"
         | 
| 409 | 
            +
                  ]
         | 
| 410 | 
            +
                 },
         | 
| 411 | 
            +
                 "metadata": {},
         | 
| 412 | 
            +
                 "output_type": "display_data"
         | 
| 413 | 
            +
                },
         | 
| 414 | 
            +
                {
         | 
| 415 | 
            +
                 "data": {
         | 
| 416 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 417 | 
            +
                   "model_id": "60fbcad80e764d41bb0a1fb5e63a80f7",
         | 
| 418 | 
            +
                   "version_major": 2,
         | 
| 419 | 
            +
                   "version_minor": 0
         | 
| 420 | 
            +
                  },
         | 
| 421 | 
            +
                  "text/plain": [
         | 
| 422 | 
            +
                   "train-00000-of-00001.parquet:   0%|          | 0.00/8.68M [00:00<?, ?B/s]"
         | 
| 423 | 
            +
                  ]
         | 
| 424 | 
            +
                 },
         | 
| 425 | 
            +
                 "metadata": {},
         | 
| 426 | 
            +
                 "output_type": "display_data"
         | 
| 427 | 
            +
                },
         | 
| 428 | 
            +
                {
         | 
| 429 | 
            +
                 "data": {
         | 
| 430 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 431 | 
            +
                   "model_id": "af28edf4b7eb443284613a9d3527b2f3",
         | 
| 432 | 
            +
                   "version_major": 2,
         | 
| 433 | 
            +
                   "version_minor": 0
         | 
| 434 | 
            +
                  },
         | 
| 435 | 
            +
                  "text/plain": [
         | 
| 436 | 
            +
                   "test-00000-of-00001.parquet:   0%|          | 0.00/501k [00:00<?, ?B/s]"
         | 
| 437 | 
            +
                  ]
         | 
| 438 | 
            +
                 },
         | 
| 439 | 
            +
                 "metadata": {},
         | 
| 440 | 
            +
                 "output_type": "display_data"
         | 
| 441 | 
            +
                },
         | 
| 442 | 
            +
                {
         | 
| 443 | 
            +
                 "data": {
         | 
| 444 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 445 | 
            +
                   "model_id": "7f7724a10b7a410693d935a29d256ce4",
         | 
| 446 | 
            +
                   "version_major": 2,
         | 
| 447 | 
            +
                   "version_minor": 0
         | 
| 448 | 
            +
                  },
         | 
| 449 | 
            +
                  "text/plain": [
         | 
| 450 | 
            +
                   "validation-00000-of-00001.parquet:   0%|          | 0.00/478k [00:00<?, ?B/s]"
         | 
| 451 | 
            +
                  ]
         | 
| 452 | 
            +
                 },
         | 
| 453 | 
            +
                 "metadata": {},
         | 
| 454 | 
            +
                 "output_type": "display_data"
         | 
| 455 | 
            +
                },
         | 
| 456 | 
            +
                {
         | 
| 457 | 
            +
                 "data": {
         | 
| 458 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 459 | 
            +
                   "model_id": "cac5cd712e094b1a8fe2a049cd445e5e",
         | 
| 460 | 
            +
                   "version_major": 2,
         | 
| 461 | 
            +
                   "version_minor": 0
         | 
| 462 | 
            +
                  },
         | 
| 463 | 
            +
                  "text/plain": [
         | 
| 464 | 
            +
                   "Generating train split:   0%|          | 0/14121 [00:00<?, ? examples/s]"
         | 
| 465 | 
            +
                  ]
         | 
| 466 | 
            +
                 },
         | 
| 467 | 
            +
                 "metadata": {},
         | 
| 468 | 
            +
                 "output_type": "display_data"
         | 
| 469 | 
            +
                },
         | 
| 470 | 
            +
                {
         | 
| 471 | 
            +
                 "data": {
         | 
| 472 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 473 | 
            +
                   "model_id": "436664aa0ff54a759e760dfe1c8ee612",
         | 
| 474 | 
            +
                   "version_major": 2,
         | 
| 475 | 
            +
                   "version_minor": 0
         | 
| 476 | 
            +
                  },
         | 
| 477 | 
            +
                  "text/plain": [
         | 
| 478 | 
            +
                   "Generating test split:   0%|          | 0/785 [00:00<?, ? examples/s]"
         | 
| 479 | 
            +
                  ]
         | 
| 480 | 
            +
                 },
         | 
| 481 | 
            +
                 "metadata": {},
         | 
| 482 | 
            +
                 "output_type": "display_data"
         | 
| 483 | 
            +
                },
         | 
| 484 | 
            +
                {
         | 
| 485 | 
            +
                 "data": {
         | 
| 486 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 487 | 
            +
                   "model_id": "ac4d330c46344866b9532bcac79ce598",
         | 
| 488 | 
            +
                   "version_major": 2,
         | 
| 489 | 
            +
                   "version_minor": 0
         | 
| 490 | 
            +
                  },
         | 
| 491 | 
            +
                  "text/plain": [
         | 
| 492 | 
            +
                   "Generating validation split:   0%|          | 0/784 [00:00<?, ? examples/s]"
         | 
| 493 | 
            +
                  ]
         | 
| 494 | 
            +
                 },
         | 
| 495 | 
            +
                 "metadata": {},
         | 
| 496 | 
            +
                 "output_type": "display_data"
         | 
| 497 | 
            +
                },
         | 
| 498 | 
            +
                {
         | 
| 499 | 
            +
                 "data": {
         | 
| 500 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 501 | 
            +
                   "model_id": "b4d28d0814344551ad7d357bb8422b8e",
         | 
| 502 | 
            +
                   "version_major": 2,
         | 
| 503 | 
            +
                   "version_minor": 0
         | 
| 504 | 
            +
                  },
         | 
| 505 | 
            +
                  "text/plain": [
         | 
| 506 | 
            +
                   "Map:   0%|          | 0/14121 [00:00<?, ? examples/s]"
         | 
| 507 | 
            +
                  ]
         | 
| 508 | 
            +
                 },
         | 
| 509 | 
            +
                 "metadata": {},
         | 
| 510 | 
            +
                 "output_type": "display_data"
         | 
| 511 | 
            +
                },
         | 
| 512 | 
            +
                {
         | 
| 513 | 
            +
                 "data": {
         | 
| 514 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 515 | 
            +
                   "model_id": "03f6a2990d9b48668f5be2bc4c5cd644",
         | 
| 516 | 
            +
                   "version_major": 2,
         | 
| 517 | 
            +
                   "version_minor": 0
         | 
| 518 | 
            +
                  },
         | 
| 519 | 
            +
                  "text/plain": [
         | 
| 520 | 
            +
                   "Map:   0%|          | 0/785 [00:00<?, ? examples/s]"
         | 
| 521 | 
            +
                  ]
         | 
| 522 | 
            +
                 },
         | 
| 523 | 
            +
                 "metadata": {},
         | 
| 524 | 
            +
                 "output_type": "display_data"
         | 
| 525 | 
            +
                },
         | 
| 526 | 
            +
                {
         | 
| 527 | 
            +
                 "data": {
         | 
| 528 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 529 | 
            +
                   "model_id": "fb98dc647e8f49ea831e5bf7e456ef78",
         | 
| 530 | 
            +
                   "version_major": 2,
         | 
| 531 | 
            +
                   "version_minor": 0
         | 
| 532 | 
            +
                  },
         | 
| 533 | 
            +
                  "text/plain": [
         | 
| 534 | 
            +
                   "Map:   0%|          | 0/784 [00:00<?, ? examples/s]"
         | 
| 535 | 
            +
                  ]
         | 
| 536 | 
            +
                 },
         | 
| 537 | 
            +
                 "metadata": {},
         | 
| 538 | 
            +
                 "output_type": "display_data"
         | 
| 539 | 
            +
                }
         | 
| 540 | 
            +
               ],
         | 
| 541 | 
            +
               "source": [
         | 
| 542 | 
            +
                "from datasets import load_dataset\n",
         | 
| 543 | 
            +
                "\n",
         | 
| 544 | 
            +
                "dataset = load_dataset(\n",
         | 
| 545 | 
            +
                "    \"tmnam20/ViMedAQA\",\n",
         | 
| 546 | 
            +
                "    \"disease\",\n",
         | 
| 547 | 
            +
                "    trust_remote_code=True,\n",
         | 
| 548 | 
            +
                ")\n",
         | 
| 549 | 
            +
                "dataset = dataset.map(\n",
         | 
| 550 | 
            +
                "    formatting_prompts_func,\n",
         | 
| 551 | 
            +
                "    batched=True,\n",
         | 
| 552 | 
            +
                ")\n"
         | 
| 553 | 
            +
               ]
         | 
| 554 | 
            +
              },
         | 
| 555 | 
            +
              {
         | 
| 556 | 
            +
               "cell_type": "code",
         | 
| 557 | 
            +
               "execution_count": 18,
         | 
| 558 | 
            +
               "metadata": {
         | 
| 559 | 
            +
                "execution": {
         | 
| 560 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:44.983057Z",
         | 
| 561 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:44.982673Z",
         | 
| 562 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:45.003356Z",
         | 
| 563 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:45.002622Z",
         | 
| 564 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:44.983029Z"
         | 
| 565 | 
            +
                },
         | 
| 566 | 
            +
                "trusted": true
         | 
| 567 | 
            +
               },
         | 
| 568 | 
            +
               "outputs": [
         | 
| 569 | 
            +
                {
         | 
| 570 | 
            +
                 "name": "stdout",
         | 
| 571 | 
            +
                 "output_type": "stream",
         | 
| 572 | 
            +
                 "text": [
         | 
| 573 | 
            +
                  "Number of training samples: 14121\n",
         | 
| 574 | 
            +
                  "Number of test samples: 1569\n"
         | 
| 575 | 
            +
                 ]
         | 
| 576 | 
            +
                }
         | 
| 577 | 
            +
               ],
         | 
| 578 | 
            +
               "source": [
         | 
| 579 | 
            +
                "# Columns to keep\n",
         | 
| 580 | 
            +
                "columns_to_keep = ['question', 'answer', 'context', 'text']\n",
         | 
| 581 | 
            +
                "\n",
         | 
| 582 | 
            +
                "# Columns to remove (all columns except the ones to keep)\n",
         | 
| 583 | 
            +
                "columns_to_remove = [col for col in dataset['train'].column_names if col not in columns_to_keep]\n",
         | 
| 584 | 
            +
                "\n",
         | 
| 585 | 
            +
                "# Remove unwanted columns from each split\n",
         | 
| 586 | 
            +
                "for split in dataset.keys():\n",
         | 
| 587 | 
            +
                "    dataset[split] = dataset[split].remove_columns(columns_to_remove)\n",
         | 
| 588 | 
            +
                "# Merge training and validation samples\n",
         | 
| 589 | 
            +
                "test_samples = concatenate_datasets([dataset['validation'],\n",
         | 
| 590 | 
            +
                "                                         dataset['test']])\n",
         | 
| 591 | 
            +
                "\n",
         | 
| 592 | 
            +
                "# Shuffle the merged dataset with a fixed random seed\n",
         | 
| 593 | 
            +
                "training_samples = dataset['train']\n",
         | 
| 594 | 
            +
                "test_samples = test_samples.shuffle(seed=42)\n",
         | 
| 595 | 
            +
                "\n",
         | 
| 596 | 
            +
                "print(\"Number of training samples:\", training_samples.num_rows)\n",
         | 
| 597 | 
            +
                "print(\"Number of test samples:\", test_samples.num_rows)"
         | 
| 598 | 
            +
               ]
         | 
| 599 | 
            +
              },
         | 
| 600 | 
            +
              {
         | 
| 601 | 
            +
               "cell_type": "code",
         | 
| 602 | 
            +
               "execution_count": 19,
         | 
| 603 | 
            +
               "metadata": {
         | 
| 604 | 
            +
                "execution": {
         | 
| 605 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:45.004197Z",
         | 
| 606 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:45.004009Z",
         | 
| 607 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:45.129491Z",
         | 
| 608 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:45.128710Z",
         | 
| 609 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:45.004181Z"
         | 
| 610 | 
            +
                },
         | 
| 611 | 
            +
                "trusted": true
         | 
| 612 | 
            +
               },
         | 
| 613 | 
            +
               "outputs": [
         | 
| 614 | 
            +
                {
         | 
| 615 | 
            +
                 "data": {
         | 
| 616 | 
            +
                  "text/plain": [
         | 
| 617 | 
            +
                   "'Bên dưới là một hướng dẫn mô tả nhiệm vụ, đi kèm với thông tin đầu vào để cung cấp thêm ngữ cảnh.\\nHãy viết một phản hồi hoàn thành yêu cầu một cách phù hợp.\\nTrước khi trả lời, hãy suy nghĩ cẩn thận về câu hỏi và tạo ra một chuỗi suy nghĩ từng bước để đảm bảo một phản hồi logic và chính xác.\\n\\n### Instruction:\\nBạn là một chuyên gia y khoa với kiến thức chuyên sâu về lập luận lâm sàng, chẩn đoán và lập kế hoạch điều trị.\\nVui lòng trả lời câu hỏi y khoa sau đây.\\n\\n### Question:\\nKhi soi bằng đèn thường, triệu chứng điển hình của viêm mống mắt là gì?\\n\\n### Response:\\n<think>\\n5.1 Nhìn về mặt đại thể (khi soi bằng đèn thường) - Đồng tử 2 bên không đều nhau (bên bệnh nhỏ hơn) Có thể phát hiện đồng tử co bên bệnh lý co nhiều hơn bên bình thường, điều này có thể do viêm nhiều dẫn đến dính thành sau mống mắt lại với nhau và với thuỷ tinh thể. Thông thường kích thường đồng tử là 2 – 3 mm, đều 2 bên. - Mủ ở đáy mống mắt (mủ tiền phòng) Đây là hiện tượng các tế bào viêm cùng với chất tiết (chứa nhiều fibrin). Mủ nằm ở dưới là do nguyên nhân trọng lực. 5.2 Khám mắt qua đèn khe: (split lamp) Đây là công cụ chuyên dụng của bác sĩ chuyên khoa Mắt. Qua đèn khe, bác sĩ có thể phát hiện: - Các tế bào nằm giữa giác mạc và mống mắt Số lượng tế bào này trên diện tích 1 mm² giúp phân độ nặng của viêm màng bồ đào. - Độ mờ thuỷ dịch Như đã miêu tả, thuỷ dịch là dịch trong suốt nằm giữa giác mạc ở ngoài cùng và mống mắt ở trong. Khi viêm màng bồ đào trước, dịch này sẽ bị vẩn đục. Tương tự như việc đếm số lượng tế bào ở thuỷ dịch, ta cũng phân độ nhờ vào mức độ đục của nó. - Sự lắng đọng keratic Sự lắng đọng keratic tạo thành tam giác Arlt. Tam giác Arlt được tạo thành nhờ các kết tụ tế bào viêm nằm ngay sau giác mạc. Trên đèn khe tam giác này có đỉnh nằm trên, đáy nằm dưới. Tạo hình này do trọng lực gây ra. - Xơ hoá fibrin Do mủ trong thuỷ dịch + viêm màng bồ đào trước diễn ra lâu dài, không điều trị kịp thời. - Ngoài ra còn nhiều dấu hiệu khác trên đèn khe Nốt mống mắt, dính mống mắt – thuỷ tinh thế, thiểu sản mống mắt, tăng sinh mạch máu mống mắt.\\n</think>\\nĐồng tử 2 bên không đều nhau, đồng tử bên bệnh nhỏ hơn bên bình thường.<|eot_id|>'"
         | 
| 618 | 
            +
                  ]
         | 
| 619 | 
            +
                 },
         | 
| 620 | 
            +
                 "execution_count": 19,
         | 
| 621 | 
            +
                 "metadata": {},
         | 
| 622 | 
            +
                 "output_type": "execute_result"
         | 
| 623 | 
            +
                }
         | 
| 624 | 
            +
               ],
         | 
| 625 | 
            +
               "source": [
         | 
| 626 | 
            +
                "training_samples[0]['text']"
         | 
| 627 | 
            +
               ]
         | 
| 628 | 
            +
              },
         | 
| 629 | 
            +
              {
         | 
| 630 | 
            +
               "cell_type": "code",
         | 
| 631 | 
            +
               "execution_count": 20,
         | 
| 632 | 
            +
               "metadata": {
         | 
| 633 | 
            +
                "execution": {
         | 
| 634 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:45.130597Z",
         | 
| 635 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:45.130370Z",
         | 
| 636 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:45.145175Z",
         | 
| 637 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:45.144475Z",
         | 
| 638 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:45.130573Z"
         | 
| 639 | 
            +
                },
         | 
| 640 | 
            +
                "trusted": true
         | 
| 641 | 
            +
               },
         | 
| 642 | 
            +
               "outputs": [],
         | 
| 643 | 
            +
               "source": [
         | 
| 644 | 
            +
                "inference_prompt_style = \"\"\"Bên dưới là một hướng dẫn mô tả một tác vụ, đi kèm với một thông tin đầu vào để cung cấp thêm ngữ cảnh.\n",
         | 
| 645 | 
            +
                "Hãy viết một phản hồi để hoàn thành yêu cầu một cách phù hợp.\n",
         | 
| 646 | 
            +
                "Trước khi trả lời, hãy suy nghĩ cẩn thận về câu hỏi và tạo một chuỗi suy nghĩ từng bước để đảm bảo phản hồi logic và chính xác.\n",
         | 
| 647 | 
            +
                "\n",
         | 
| 648 | 
            +
                "### Instruction:\n",
         | 
| 649 | 
            +
                "Bạn là một chuyên gia y tế có kiến thức chuyên sâu về lập luận lâm sàng, chẩn đoán và lập kế hoạch điều trị.\n",
         | 
| 650 | 
            +
                "Vui lòng trả lời câu hỏi y tế sau đây.\n",
         | 
| 651 | 
            +
                "\n",
         | 
| 652 | 
            +
                "### Question:\n",
         | 
| 653 | 
            +
                "{}\n",
         | 
| 654 | 
            +
                "\n",
         | 
| 655 | 
            +
                "### Response:\n",
         | 
| 656 | 
            +
                "<think>\n",
         | 
| 657 | 
            +
                "\n",
         | 
| 658 | 
            +
                "\"\"\"\n"
         | 
| 659 | 
            +
               ]
         | 
| 660 | 
            +
              },
         | 
| 661 | 
            +
              {
         | 
| 662 | 
            +
               "cell_type": "markdown",
         | 
| 663 | 
            +
               "metadata": {},
         | 
| 664 | 
            +
               "source": [
         | 
| 665 | 
            +
                "# Setup Training Arguments"
         | 
| 666 | 
            +
               ]
         | 
| 667 | 
            +
              },
         | 
| 668 | 
            +
              {
         | 
| 669 | 
            +
               "cell_type": "code",
         | 
| 670 | 
            +
               "execution_count": null,
         | 
| 671 | 
            +
               "metadata": {
         | 
| 672 | 
            +
                "execution": {
         | 
| 673 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:45.146105Z",
         | 
| 674 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:45.145892Z",
         | 
| 675 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:45.195273Z",
         | 
| 676 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:45.194576Z",
         | 
| 677 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:45.146090Z"
         | 
| 678 | 
            +
                },
         | 
| 679 | 
            +
                "trusted": true
         | 
| 680 | 
            +
               },
         | 
| 681 | 
            +
               "outputs": [],
         | 
| 682 | 
            +
               "source": [
         | 
| 683 | 
            +
                "arguments = TrainingArguments(  \n",
         | 
| 684 | 
            +
                "                resume_from_checkpoint=\"./Llama-3.2-3B-Reasoning-Vi-Medical-LoRA\",\n",
         | 
| 685 | 
            +
                "                per_device_train_batch_size=8,  \n",
         | 
| 686 | 
            +
                "                per_device_eval_batch_size=8,  \n",
         | 
| 687 | 
            +
                "                gradient_accumulation_steps=4,  \n",
         | 
| 688 | 
            +
                "                eval_strategy=\"steps\",\n",
         | 
| 689 | 
            +
                "                eval_steps=100,\n",
         | 
| 690 | 
            +
                "                logging_steps=100,\n",
         | 
| 691 | 
            +
                "                save_steps=100,\n",
         | 
| 692 | 
            +
                "                warmup_steps=30,\n",
         | 
| 693 | 
            +
                "                save_total_limit=4,\n",
         | 
| 694 | 
            +
                "                num_train_epochs=12, # 5\n",
         | 
| 695 | 
            +
                "                # max_steps=50,\n",
         | 
| 696 | 
            +
                "                save_strategy=\"steps\",\n",
         | 
| 697 | 
            +
                "                metric_for_best_model=\"eval_loss\",\n",
         | 
| 698 | 
            +
                "                learning_rate=2e-4,  \n",
         | 
| 699 | 
            +
                "                fp16=not is_bfloat16_supported(),  \n",
         | 
| 700 | 
            +
                "                bf16=is_bfloat16_supported(),  \n",
         | 
| 701 | 
            +
                "                optim=\"adamw_8bit\",  \n",
         | 
| 702 | 
            +
                "                weight_decay=0.01,  \n",
         | 
| 703 | 
            +
                "                lr_scheduler_type=\"linear\",  \n",
         | 
| 704 | 
            +
                "                seed=42,  \n",
         | 
| 705 | 
            +
                "                output_dir=\"./Llama-3.2-3B-Reasoning-Vi-Medical-LoRA\",  \n",
         | 
| 706 | 
            +
                "                report_to=\"none\",\n",
         | 
| 707 | 
            +
                "                load_best_model_at_end=True,  # Load weights with lowest val loss\n",
         | 
| 708 | 
            +
                "        \t\tgreater_is_better=False,\n",
         | 
| 709 | 
            +
                "            )"
         | 
| 710 | 
            +
               ]
         | 
| 711 | 
            +
              },
         | 
| 712 | 
            +
              {
         | 
| 713 | 
            +
               "cell_type": "code",
         | 
| 714 | 
            +
               "execution_count": 22,
         | 
| 715 | 
            +
               "metadata": {
         | 
| 716 | 
            +
                "execution": {
         | 
| 717 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:45.196297Z",
         | 
| 718 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:45.196061Z",
         | 
| 719 | 
            +
                 "iopub.status.idle": "2025-07-20T11:14:53.607380Z",
         | 
| 720 | 
            +
                 "shell.execute_reply": "2025-07-20T11:14:53.606842Z",
         | 
| 721 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:45.196276Z"
         | 
| 722 | 
            +
                },
         | 
| 723 | 
            +
                "trusted": true
         | 
| 724 | 
            +
               },
         | 
| 725 | 
            +
               "outputs": [
         | 
| 726 | 
            +
                {
         | 
| 727 | 
            +
                 "data": {
         | 
| 728 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 729 | 
            +
                   "model_id": "bac2552585334433a15caa09108d0f42",
         | 
| 730 | 
            +
                   "version_major": 2,
         | 
| 731 | 
            +
                   "version_minor": 0
         | 
| 732 | 
            +
                  },
         | 
| 733 | 
            +
                  "text/plain": [
         | 
| 734 | 
            +
                   "Unsloth: Tokenizing [\"text\"]:   0%|          | 0/14121 [00:00<?, ? examples/s]"
         | 
| 735 | 
            +
                  ]
         | 
| 736 | 
            +
                 },
         | 
| 737 | 
            +
                 "metadata": {},
         | 
| 738 | 
            +
                 "output_type": "display_data"
         | 
| 739 | 
            +
                },
         | 
| 740 | 
            +
                {
         | 
| 741 | 
            +
                 "data": {
         | 
| 742 | 
            +
                  "application/vnd.jupyter.widget-view+json": {
         | 
| 743 | 
            +
                   "model_id": "09a6f56a689b444583544609ad6f3c6a",
         | 
| 744 | 
            +
                   "version_major": 2,
         | 
| 745 | 
            +
                   "version_minor": 0
         | 
| 746 | 
            +
                  },
         | 
| 747 | 
            +
                  "text/plain": [
         | 
| 748 | 
            +
                   "Unsloth: Tokenizing [\"text\"]:   0%|          | 0/1569 [00:00<?, ? examples/s]"
         | 
| 749 | 
            +
                  ]
         | 
| 750 | 
            +
                 },
         | 
| 751 | 
            +
                 "metadata": {},
         | 
| 752 | 
            +
                 "output_type": "display_data"
         | 
| 753 | 
            +
                }
         | 
| 754 | 
            +
               ],
         | 
| 755 | 
            +
               "source": [
         | 
| 756 | 
            +
                "# Define Trainer\n",
         | 
| 757 | 
            +
                "trainer = SFTTrainer(  \n",
         | 
| 758 | 
            +
                "    model=model,  \n",
         | 
| 759 | 
            +
                "    tokenizer=tokenizer,  \n",
         | 
| 760 | 
            +
                "    train_dataset=training_samples,  \n",
         | 
| 761 | 
            +
                "    eval_dataset=test_samples,  \n",
         | 
| 762 | 
            +
                "    dataset_text_field=\"text\",\n",
         | 
| 763 | 
            +
                "    max_seq_length=2048,  \n",
         | 
| 764 | 
            +
                "    data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer),  \n",
         | 
| 765 | 
            +
                "    dataset_num_proc=2,  \n",
         | 
| 766 | 
            +
                "    packing=False,  # Can make training 5x faster for short sequences.  \n",
         | 
| 767 | 
            +
                "    args=arguments\n",
         | 
| 768 | 
            +
                ")"
         | 
| 769 | 
            +
               ]
         | 
| 770 | 
            +
              },
         | 
| 771 | 
            +
              {
         | 
| 772 | 
            +
               "cell_type": "markdown",
         | 
| 773 | 
            +
               "metadata": {
         | 
| 774 | 
            +
                "execution": {
         | 
| 775 | 
            +
                 "iopub.execute_input": "2025-07-01T13:46:57.349739Z",
         | 
| 776 | 
            +
                 "iopub.status.busy": "2025-07-01T13:46:57.349513Z",
         | 
| 777 | 
            +
                 "iopub.status.idle": "2025-07-01T13:48:14.259335Z",
         | 
| 778 | 
            +
                 "shell.execute_reply": "2025-07-01T13:48:14.258227Z",
         | 
| 779 | 
            +
                 "shell.execute_reply.started": "2025-07-01T13:46:57.349713Z"
         | 
| 780 | 
            +
                }
         | 
| 781 | 
            +
               },
         | 
| 782 | 
            +
               "source": [
         | 
| 783 | 
            +
                "# Start train process"
         | 
| 784 | 
            +
               ]
         | 
| 785 | 
            +
              },
         | 
| 786 | 
            +
              {
         | 
| 787 | 
            +
               "cell_type": "code",
         | 
| 788 | 
            +
               "execution_count": 23,
         | 
| 789 | 
            +
               "metadata": {
         | 
| 790 | 
            +
                "execution": {
         | 
| 791 | 
            +
                 "iopub.execute_input": "2025-07-20T11:14:53.609516Z",
         | 
| 792 | 
            +
                 "iopub.status.busy": "2025-07-20T11:14:53.609307Z",
         | 
| 793 | 
            +
                 "iopub.status.idle": "2025-07-20T17:43:46.140313Z",
         | 
| 794 | 
            +
                 "shell.execute_reply": "2025-07-20T17:43:46.139569Z",
         | 
| 795 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:14:53.609500Z"
         | 
| 796 | 
            +
                },
         | 
| 797 | 
            +
                "trusted": true
         | 
| 798 | 
            +
               },
         | 
| 799 | 
            +
               "outputs": [
         | 
| 800 | 
            +
                {
         | 
| 801 | 
            +
                 "name": "stderr",
         | 
| 802 | 
            +
                 "output_type": "stream",
         | 
| 803 | 
            +
                 "text": [
         | 
| 804 | 
            +
                  "==((====))==  Unsloth - 2x faster free finetuning | Num GPUs used = 1\n",
         | 
| 805 | 
            +
                  "   \\\\   /|    Num examples = 14,121 | Num Epochs = 12 | Total steps = 5,304\n",
         | 
| 806 | 
            +
                  "O^O/ \\_/ \\    Batch size per device = 8 | Gradient accumulation steps = 4\n",
         | 
| 807 | 
            +
                  "\\        /    Data Parallel GPUs = 1 | Total batch size (8 x 4 x 1) = 32\n",
         | 
| 808 | 
            +
                  " \"-____-\"     Trainable parameters = 24,313,856/3,000,000,000 (0.81% trained)\n"
         | 
| 809 | 
            +
                 ]
         | 
| 810 | 
            +
                },
         | 
| 811 | 
            +
                {
         | 
| 812 | 
            +
                 "name": "stdout",
         | 
| 813 | 
            +
                 "output_type": "stream",
         | 
| 814 | 
            +
                 "text": [
         | 
| 815 | 
            +
                  "Unsloth: Will smartly offload gradients to save VRAM!\n"
         | 
| 816 | 
            +
                 ]
         | 
| 817 | 
            +
                },
         | 
| 818 | 
            +
                {
         | 
| 819 | 
            +
                 "data": {
         | 
| 820 | 
            +
                  "text/html": [
         | 
| 821 | 
            +
                   "\n",
         | 
| 822 | 
            +
                   "    <div>\n",
         | 
| 823 | 
            +
                   "      \n",
         | 
| 824 | 
            +
                   "      <progress value='5303' max='5304' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
         | 
| 825 | 
            +
                   "      [5303/5304 6:27:48 < 00:58, 0.02 it/s, Epoch 11/12]\n",
         | 
| 826 | 
            +
                   "    </div>\n",
         | 
| 827 | 
            +
                   "    <table border=\"1\" class=\"dataframe\">\n",
         | 
| 828 | 
            +
                   "  <thead>\n",
         | 
| 829 | 
            +
                   " <tr style=\"text-align: left;\">\n",
         | 
| 830 | 
            +
                   "      <th>Step</th>\n",
         | 
| 831 | 
            +
                   "      <th>Training Loss</th>\n",
         | 
| 832 | 
            +
                   "      <th>Validation Loss</th>\n",
         | 
| 833 | 
            +
                   "    </tr>\n",
         | 
| 834 | 
            +
                   "  </thead>\n",
         | 
| 835 | 
            +
                   "  <tbody>\n",
         | 
| 836 | 
            +
                   "    <tr>\n",
         | 
| 837 | 
            +
                   "      <td>5000</td>\n",
         | 
| 838 | 
            +
                   "      <td>0.209300</td>\n",
         | 
| 839 | 
            +
                   "      <td>0.480698</td>\n",
         | 
| 840 | 
            +
                   "    </tr>\n",
         | 
| 841 | 
            +
                   "    <tr>\n",
         | 
| 842 | 
            +
                   "      <td>5100</td>\n",
         | 
| 843 | 
            +
                   "      <td>0.187000</td>\n",
         | 
| 844 | 
            +
                   "      <td>0.480475</td>\n",
         | 
| 845 | 
            +
                   "    </tr>\n",
         | 
| 846 | 
            +
                   "    <tr>\n",
         | 
| 847 | 
            +
                   "      <td>5200</td>\n",
         | 
| 848 | 
            +
                   "      <td>0.183800</td>\n",
         | 
| 849 | 
            +
                   "      <td>0.477710</td>\n",
         | 
| 850 | 
            +
                   "    </tr>\n",
         | 
| 851 | 
            +
                   "    <tr>\n",
         | 
| 852 | 
            +
                   "      <td>5300</td>\n",
         | 
| 853 | 
            +
                   "      <td>0.162600</td>\n",
         | 
| 854 | 
            +
                   "      <td>0.478503</td>\n",
         | 
| 855 | 
            +
                   "    </tr>\n",
         | 
| 856 | 
            +
                   "  </tbody>\n",
         | 
| 857 | 
            +
                   "</table><p>"
         | 
| 858 | 
            +
                  ],
         | 
| 859 | 
            +
                  "text/plain": [
         | 
| 860 | 
            +
                   "<IPython.core.display.HTML object>"
         | 
| 861 | 
            +
                  ]
         | 
| 862 | 
            +
                 },
         | 
| 863 | 
            +
                 "metadata": {},
         | 
| 864 | 
            +
                 "output_type": "display_data"
         | 
| 865 | 
            +
                },
         | 
| 866 | 
            +
                {
         | 
| 867 | 
            +
                 "name": "stderr",
         | 
| 868 | 
            +
                 "output_type": "stream",
         | 
| 869 | 
            +
                 "text": [
         | 
| 870 | 
            +
                  "Unsloth: Not an error, but LlamaForCausalLM does not accept `num_items_in_batch`.\n",
         | 
| 871 | 
            +
                  "Using gradient accumulation will be very slightly less accurate.\n",
         | 
| 872 | 
            +
                  "Read more on gradient accumulation issues here: https://unsloth.ai/blog/gradient\n"
         | 
| 873 | 
            +
                 ]
         | 
| 874 | 
            +
                },
         | 
| 875 | 
            +
                {
         | 
| 876 | 
            +
                 "data": {
         | 
| 877 | 
            +
                  "text/plain": [
         | 
| 878 | 
            +
                   "TrainOutput(global_step=5303, training_loss=0.014094636538288132, metrics={'train_runtime': 23329.6269, 'train_samples_per_second': 7.263, 'train_steps_per_second': 0.227, 'total_flos': 1.667517122067204e+18, 'train_loss': 0.014094636538288132})"
         | 
| 879 | 
            +
                  ]
         | 
| 880 | 
            +
                 },
         | 
| 881 | 
            +
                 "execution_count": 23,
         | 
| 882 | 
            +
                 "metadata": {},
         | 
| 883 | 
            +
                 "output_type": "execute_result"
         | 
| 884 | 
            +
                }
         | 
| 885 | 
            +
               ],
         | 
| 886 | 
            +
               "source": [
         | 
| 887 | 
            +
                "trainer.train(resume_from_checkpoint=True)"
         | 
| 888 | 
            +
               ]
         | 
| 889 | 
            +
              },
         | 
| 890 | 
            +
              {
         | 
| 891 | 
            +
               "cell_type": "code",
         | 
| 892 | 
            +
               "execution_count": null,
         | 
| 893 | 
            +
               "metadata": {
         | 
| 894 | 
            +
                "execution": {
         | 
| 895 | 
            +
                 "iopub.execute_input": "2025-07-20T17:49:05.368465Z",
         | 
| 896 | 
            +
                 "iopub.status.busy": "2025-07-20T17:49:05.368168Z",
         | 
| 897 | 
            +
                 "iopub.status.idle": "2025-07-20T17:49:06.088573Z",
         | 
| 898 | 
            +
                 "shell.execute_reply": "2025-07-20T17:49:06.087971Z",
         | 
| 899 | 
            +
                 "shell.execute_reply.started": "2025-07-20T17:49:05.368442Z"
         | 
| 900 | 
            +
                },
         | 
| 901 | 
            +
                "trusted": true
         | 
| 902 | 
            +
               },
         | 
| 903 | 
            +
               "outputs": [
         | 
| 904 | 
            +
                {
         | 
| 905 | 
            +
                 "data": {
         | 
| 906 | 
            +
                  "text/plain": [
         | 
| 907 | 
            +
                   "('Llama-3.2-3B-Reasoning-Vi-Medical-LoRA/tokenizer_config.json',\n",
         | 
| 908 | 
            +
                   " 'Llama-3.2-3B-Reasoning-Vi-Medical-LoRA/special_tokens_map.json',\n",
         | 
| 909 | 
            +
                   " 'Llama-3.2-3B-Reasoning-Vi-Medical-LoRA/chat_template.jinja',\n",
         | 
| 910 | 
            +
                   " 'Llama-3.2-3B-Reasoning-Vi-Medical-LoRA/tokenizer.json')"
         | 
| 911 | 
            +
                  ]
         | 
| 912 | 
            +
                 },
         | 
| 913 | 
            +
                 "execution_count": 26,
         | 
| 914 | 
            +
                 "metadata": {},
         | 
| 915 | 
            +
                 "output_type": "execute_result"
         | 
| 916 | 
            +
                }
         | 
| 917 | 
            +
               ],
         | 
| 918 | 
            +
               "source": [
         | 
| 919 | 
            +
                "model.save_pretrained(\"Llama-3.2-3B-Reasoning-Vi-Medical-LoRA\")  # Local saving\n",
         | 
| 920 | 
            +
                "tokenizer.save_pretrained(\"Llama-3.2-3B-Reasoning-Vi-Medical-LoRA\")\n"
         | 
| 921 | 
            +
               ]
         | 
| 922 | 
            +
              },
         | 
| 923 | 
            +
              {
         | 
| 924 | 
            +
               "cell_type": "markdown",
         | 
| 925 | 
            +
               "metadata": {},
         | 
| 926 | 
            +
               "source": [
         | 
| 927 | 
            +
                "# Inference"
         | 
| 928 | 
            +
               ]
         | 
| 929 | 
            +
              },
         | 
| 930 | 
            +
              {
         | 
| 931 | 
            +
               "cell_type": "code",
         | 
| 932 | 
            +
               "execution_count": null,
         | 
| 933 | 
            +
               "metadata": {
         | 
| 934 | 
            +
                "execution": {
         | 
| 935 | 
            +
                 "iopub.status.busy": "2025-07-20T11:13:08.724422Z",
         | 
| 936 | 
            +
                 "iopub.status.idle": "2025-07-20T11:13:08.724698Z",
         | 
| 937 | 
            +
                 "shell.execute_reply": "2025-07-20T11:13:08.724585Z",
         | 
| 938 | 
            +
                 "shell.execute_reply.started": "2025-07-20T11:13:08.724572Z"
         | 
| 939 | 
            +
                },
         | 
| 940 | 
            +
                "trusted": true
         | 
| 941 | 
            +
               },
         | 
| 942 | 
            +
               "outputs": [],
         | 
| 943 | 
            +
               "source": [
         | 
| 944 | 
            +
                "# question = training_samples[10]['question']\n",
         | 
| 945 | 
            +
                "question = \"Tôi bị đau đầu, sốt, đau cứng cổ. Tôi có thể mắc bệnh gì?\"\n",
         | 
| 946 | 
            +
                "inputs = tokenizer(\n",
         | 
| 947 | 
            +
                "    [inference_prompt_style.format(question) + tokenizer.eos_token],\n",
         | 
| 948 | 
            +
                "    return_tensors=\"pt\"\n",
         | 
| 949 | 
            +
                ").to(\"cuda\")\n",
         | 
| 950 | 
            +
                "\n",
         | 
| 951 | 
            +
                "outputs = model.generate(\n",
         | 
| 952 | 
            +
                "    input_ids=inputs.input_ids,\n",
         | 
| 953 | 
            +
                "    attention_mask=inputs.attention_mask,\n",
         | 
| 954 | 
            +
                "    max_new_tokens=2048,\n",
         | 
| 955 | 
            +
                "    eos_token_id=tokenizer.eos_token_id,\n",
         | 
| 956 | 
            +
                "    use_cache=True,\n",
         | 
| 957 | 
            +
                ")\n",
         | 
| 958 | 
            +
                "response = tokenizer.batch_decode(outputs, skip_special_tokens=True)\n",
         | 
| 959 | 
            +
                "print(\"Question: \", question)\n",
         | 
| 960 | 
            +
                "print(response[0].split(\"### Response:\")[1])"
         | 
| 961 | 
            +
               ]
         | 
| 962 | 
            +
              }
         | 
| 963 | 
            +
             ],
         | 
| 964 | 
            +
             "metadata": {
         | 
| 965 | 
            +
              "kaggle": {
         | 
| 966 | 
            +
               "accelerator": "gpu",
         | 
| 967 | 
            +
               "dataSources": [],
         | 
| 968 | 
            +
               "dockerImageVersionId": 31090,
         | 
| 969 | 
            +
               "isGpuEnabled": true,
         | 
| 970 | 
            +
               "isInternetEnabled": true,
         | 
| 971 | 
            +
               "language": "python",
         | 
| 972 | 
            +
               "sourceType": "notebook"
         | 
| 973 | 
            +
              },
         | 
| 974 | 
            +
              "kernelspec": {
         | 
| 975 | 
            +
               "display_name": "Python 3",
         | 
| 976 | 
            +
               "language": "python",
         | 
| 977 | 
            +
               "name": "python3"
         | 
| 978 | 
            +
              },
         | 
| 979 | 
            +
              "language_info": {
         | 
| 980 | 
            +
               "codemirror_mode": {
         | 
| 981 | 
            +
                "name": "ipython",
         | 
| 982 | 
            +
                "version": 3
         | 
| 983 | 
            +
               },
         | 
| 984 | 
            +
               "file_extension": ".py",
         | 
| 985 | 
            +
               "mimetype": "text/x-python",
         | 
| 986 | 
            +
               "name": "python",
         | 
| 987 | 
            +
               "nbconvert_exporter": "python",
         | 
| 988 | 
            +
               "pygments_lexer": "ipython3",
         | 
| 989 | 
            +
               "version": "3.11.13"
         | 
| 990 | 
            +
              }
         | 
| 991 | 
            +
             },
         | 
| 992 | 
            +
             "nbformat": 4,
         | 
| 993 | 
            +
             "nbformat_minor": 4
         | 
| 994 | 
            +
            }
         | 
