diff --git "a/notebooks/qwen-3-0-6b-reasoning-vi-medical-lora.ipynb" "b/notebooks/qwen-3-0-6b-reasoning-vi-medical-lora.ipynb" new file mode 100644--- /dev/null +++ "b/notebooks/qwen-3-0-6b-reasoning-vi-medical-lora.ipynb" @@ -0,0 +1,3513 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", + "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", + "execution": { + "iopub.execute_input": "2025-07-06T11:09:36.629664Z", + "iopub.status.busy": "2025-07-06T11:09:36.629419Z", + "iopub.status.idle": "2025-07-06T11:12:42.232385Z", + "shell.execute_reply": "2025-07-06T11:12:42.231459Z", + "shell.execute_reply.started": "2025-07-06T11:09:36.629644Z" + }, + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.9/46.9 kB\u001b[0m \u001b[31m1.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m294.5/294.5 kB\u001b[0m \u001b[31m7.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0mta \u001b[36m0:00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m375.8/375.8 kB\u001b[0m \u001b[31m19.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m154.2/154.2 kB\u001b[0m \u001b[31m10.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m31.5/31.5 MB\u001b[0m \u001b[31m60.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m865.2/865.2 MB\u001b[0m \u001b[31m2.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m156.5/156.5 MB\u001b[0m \u001b[31m7.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m393.1/393.1 MB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m8.9/8.9 MB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m23.7/23.7 MB\u001b[0m \u001b[31m6.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m897.7/897.7 kB\u001b[0m \u001b[31m43.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m571.0/571.0 MB\u001b[0m \u001b[31m3.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m200.2/200.2 MB\u001b[0m \u001b[31m8.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m45.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.3/56.3 MB\u001b[0m \u001b[31m31.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m158.2/158.2 MB\u001b[0m \u001b[31m7.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m216.6/216.6 MB\u001b[0m \u001b[31m7.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m156.8/156.8 MB\u001b[0m \u001b[31m10.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m201.3/201.3 MB\u001b[0m \u001b[31m1.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m19.7/19.7 MB\u001b[0m \u001b[31m12.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m89.3/89.3 kB\u001b[0m \u001b[31m6.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m72.9/72.9 MB\u001b[0m \u001b[31m24.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.4/7.4 MB\u001b[0m \u001b[31m100.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m129.0/129.0 kB\u001b[0m \u001b[31m9.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m193.6/193.6 kB\u001b[0m \u001b[31m13.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.3/6.3 MB\u001b[0m \u001b[31m101.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m210.7/210.7 kB\u001b[0m \u001b[31m15.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", + "cesium 0.12.4 requires numpy<3.0,>=2.0, but you have numpy 1.26.4 which is incompatible.\n", + "torchaudio 2.6.0+cu124 requires torch==2.6.0, but you have torch 2.7.0 which is incompatible.\n", + "bigframes 1.42.0 requires rich<14,>=12.4.4, but you have rich 14.0.0 which is incompatible.\n", + "gcsfs 2025.3.2 requires fsspec==2025.3.2, but you have fsspec 2025.3.0 which is incompatible.\n", + "fastai 2.7.19 requires torch<2.7,>=1.10, but you have torch 2.7.0 which is incompatible.\u001b[0m\u001b[31m\n", + "\u001b[0m" + ] + } + ], + "source": [ + "!pip install -q unsloth" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-05T08:11:04.100619Z", + "iopub.status.busy": "2025-07-05T08:11:04.100237Z", + "iopub.status.idle": "2025-07-05T08:11:34.999544Z", + "shell.execute_reply": "2025-07-05T08:11:34.998784Z", + "shell.execute_reply.started": "2025-07-05T08:11:04.100592Z" + }, + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2025-07-05 08:11:15.174279: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", + "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n", + "E0000 00:00:1751703075.342853 35 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", + "E0000 00:00:1751703075.461285 35 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🦥 Unsloth Zoo will now patch everything to make training faster!\n", + "datasets.__version__ 3.6.0\n", + "numpy.__version__ 1.26.4\n", + "unsloth.__version__ 2025.6.12\n", + "torch.__version__ 2.7.0+cu126\n", + "transformers.__version__ 4.51.3\n", + "trl.__version__ 0.19.0\n" + ] + } + ], + "source": [ + "import datasets\n", + "import numpy as np\n", + "import unsloth\n", + "import torch\n", + "import trl \n", + "import transformers\n", + "import unsloth\n", + "\n", + "print(\"datasets.__version__\", datasets.__version__)\n", + "print(\"numpy.__version__\", np.__version__)\n", + "print(\"unsloth.__version__\", unsloth.__version__)\n", + "print(\"torch.__version__\", torch.__version__)\n", + "print(\"transformers.__version__\", transformers.__version__)\n", + "print(\"trl.__version__\", trl.__version__)\n", + "\n", + "# datasets.__version__ 3.6.0\n", + "# numpy.__version__ 1.26.4\n", + "# unsloth.__version__ 2025.6.3\n", + "# torch.__version__ 2.7.0+cu126\n", + "# transformers.__version__ 4.51.3\n", + "# trl.__version__ 0.19.0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-06T11:12:42.234163Z", + "iopub.status.busy": "2025-07-06T11:12:42.233893Z", + "iopub.status.idle": "2025-07-06T11:12:58.668872Z", + "shell.execute_reply": "2025-07-06T11:12:58.668079Z", + "shell.execute_reply.started": "2025-07-06T11:12:42.234140Z" + }, + "trusted": true + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "b3b5ba312a6b4a3d81250785bf465432", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Fetching 182 files: 0%| | 0/182 [00:00\")\n", + "api = HfApi()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-06T11:12:58.670012Z", + "iopub.status.busy": "2025-07-06T11:12:58.669729Z", + "iopub.status.idle": "2025-07-06T11:13:30.782470Z", + "shell.execute_reply": "2025-07-06T11:13:30.781929Z", + "shell.execute_reply.started": "2025-07-06T11:12:58.669988Z" + }, + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2025-07-06 11:13:10.169046: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", + "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n", + "E0000 00:00:1751800390.435353 35 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", + "E0000 00:00:1751800390.493002 35 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🦥 Unsloth Zoo will now patch everything to make training faster!\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "from datasets import concatenate_datasets, load_dataset\n", + "\n", + "from unsloth import FastLanguageModel\n", + "from unsloth import is_bfloat16_supported\n", + "from unsloth.chat_templates import train_on_responses_only \n", + "import torch\n", + "\n", + "from trl import SFTTrainer\n", + "from transformers import TrainingArguments, DataCollatorForSeq2Seq" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-06T11:17:13.817972Z", + "iopub.status.busy": "2025-07-06T11:17:13.817246Z", + "iopub.status.idle": "2025-07-06T11:17:35.429882Z", + "shell.execute_reply": "2025-07-06T11:17:35.429104Z", + "shell.execute_reply.started": "2025-07-06T11:17:13.817938Z" + }, + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "==((====))== Unsloth 2025.6.12: Fast Qwen3 patching. Transformers: 4.51.3.\n", + " \\\\ /| Tesla P100-PCIE-16GB. Num GPUs = 1. Max memory: 15.888 GB. Platform: Linux.\n", + "O^O/ \\_/ \\ Torch: 2.7.0+cu126. CUDA: 6.0. CUDA Toolkit: 12.6. Triton: 3.3.0\n", + "\\ / Bfloat16 = FALSE. FA [Xformers = 0.0.30. FA2 = False]\n", + " \"-____-\" Free license: http://github.com/unslothai/unsloth\n", + "Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!\n" + ] + } + ], + "source": [ + "model, tokenizer = FastLanguageModel.from_pretrained(\n", + " model_name = \"Qwen/Qwen3-0.6B\",\n", + " max_seq_length = 2048, # Context length - can be longer, but uses more memory\n", + " load_in_4bit = True, # 4bit uses much less memory\n", + " load_in_8bit = False, # A bit more accurate, uses 2x memory\n", + " full_finetuning = False, # We have full finetuning now!\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-06T11:17:44.514487Z", + "iopub.status.busy": "2025-07-06T11:17:44.513913Z", + "iopub.status.idle": "2025-07-06T11:17:51.099758Z", + "shell.execute_reply": "2025-07-06T11:17:51.098951Z", + "shell.execute_reply.started": "2025-07-06T11:17:44.514462Z" + }, + "trusted": true + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Unsloth 2025.6.12 patched 28 layers with 28 QKV layers, 28 O layers and 28 MLP layers.\n" + ] + } + ], + "source": [ + "model = FastLanguageModel.get_peft_model( \n", + " model, \n", + " r=16, \n", + " target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\", \n", + " \"gate_proj\", \"up_proj\", \"down_proj\"], \n", + " lora_alpha=16, \n", + " lora_dropout=0, \n", + " bias=\"none\", \n", + " use_gradient_checkpointing=\"unsloth\", \n", + " random_state=42, \n", + " use_rslora=False, \n", + " loftq_config=None\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Data Preparetation" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-06T11:17:51.101392Z", + "iopub.status.busy": "2025-07-06T11:17:51.101134Z", + "iopub.status.idle": "2025-07-06T11:17:51.105420Z", + "shell.execute_reply": "2025-07-06T11:17:51.104831Z", + "shell.execute_reply.started": "2025-07-06T11:17:51.101373Z" + }, + "trusted": true + }, + "outputs": [], + "source": [ + "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", + "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", + "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", + "\n", + "### Instruction:\n", + "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", + "Vui lòng trả lời câu hỏi y khoa sau đây.\n", + "\n", + "### Question:\n", + "{}\n", + "\n", + "### Response:\n", + "\n", + "{}\n", + "\n", + "{}\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-06T11:17:51.106342Z", + "iopub.status.busy": "2025-07-06T11:17:51.106128Z", + "iopub.status.idle": "2025-07-06T11:17:51.121509Z", + "shell.execute_reply": "2025-07-06T11:17:51.120908Z", + "shell.execute_reply.started": "2025-07-06T11:17:51.106327Z" + }, + "trusted": true + }, + "outputs": [], + "source": [ + "# EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN\n", + "\n", + "def formatting_prompts_func(examples):\n", + " inputs = examples[\"question\"]\n", + " complex_cots = examples[\"context\"]\n", + " outputs = examples[\"answer\"]\n", + " texts = []\n", + " for question, cot, response in zip(inputs, complex_cots, outputs):\n", + " # Append the EOS token to the response if it's not already there\n", + " if not response.endswith(tokenizer.eos_token):\n", + " response += tokenizer.eos_token\n", + " text = train_prompt_style.format(question, cot, response)\n", + " texts.append(text)\n", + " return {\"text\": texts}" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-06T11:17:51.122972Z", + "iopub.status.busy": "2025-07-06T11:17:51.122759Z", + "iopub.status.idle": "2025-07-06T11:17:58.327195Z", + "shell.execute_reply": "2025-07-06T11:17:58.326516Z", + "shell.execute_reply.started": "2025-07-06T11:17:51.122951Z" + }, + "trusted": true + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "7a411b6f59824d8eb23e58c0fad8f6fa", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Generating train split: 0%| | 0/14121 [00:00\n", + "\n", + "\"\"\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-06T11:17:58.645858Z", + "iopub.status.busy": "2025-07-06T11:17:58.645605Z", + "iopub.status.idle": "2025-07-06T11:18:18.621520Z", + "shell.execute_reply": "2025-07-06T11:18:18.620888Z", + "shell.execute_reply.started": "2025-07-06T11:17:58.645841Z" + }, + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "Suy nghĩ từng bước:\n", + "\n", + "1. **Xác định nguyên nhân**: Gout là một bệnh lý về đường dỷ hóa, liên quan đến quá trình tạo thành đường. Nguyên nhân chính là sự thay đổi trong lượng đường trong cơ thể, đặc biệt là sau khi tiêu hóa đường. Điều này dẫn đến các phản ứng tăng đường huyết, có thể gây ra các vấn đề như gout.\n", + "\n", + "2. **Cấu trúc cơ bản**: Gout xảy ra khi đường huyết tăng cao, dẫn đến sự phân chia của đường trong cơ thể. Điều này thường xảy ra do đường bị tiêu hóa, có thể do thiếu vitamin B12, vitamin B1, hoặc do đường huyết cao.\n", + "\n", + "3. **Sự khác biệt về tỷ lệ**: Nam giới có nguy cơ cao hơn phụ nữ trong việc kiểm soát và điều trị bệnh gout, đặc biệt là sau khi tiêu hóa đường. Điều này có thể do nam giới thường xuyên thay đổi đường trong cơ thể, dẫn đến tình trạng tăng đường huyết.\n", + "\n", + "4. **Bệnh lý y tế**: Gout là một bệnh lý đơn giản, có thể được điều trị bằng cách kiểm soát đường huyết, bổ sung vitamin B12, và điều chỉnh chế độ ăn. Do đó, việc lâm sàng thường xuyên kiểm tra và điều trị phù hợp là cần thiết.\n", + "\n", + "Vậy phản hồi: Nam giới có nguy cơ mắc bệnh gout cao hơn phụ nữ vì cơ thể thường xuyên thay đổi lượng đường, dẫn đến tình trạng tăng đường huyết. Điều này có thể ảnh hưởng đến khả năng kiểm soát bệnh và cần được điều trị kịp thời.\n" + ] + } + ], + "source": [ + "question = training_samples[10]['question']\n", + "inputs = tokenizer(\n", + " [inference_prompt_style.format(question) + tokenizer.eos_token],\n", + " return_tensors=\"pt\"\n", + ").to(\"cuda\")\n", + "\n", + "outputs = model.generate(\n", + " input_ids=inputs.input_ids,\n", + " attention_mask=inputs.attention_mask,\n", + " max_new_tokens=1200,\n", + " eos_token_id=tokenizer.eos_token_id,\n", + " use_cache=True,\n", + ")\n", + "response = tokenizer.batch_decode(outputs, skip_special_tokens=True)\n", + "print(response[0].split(\"### Response:\")[1])" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-06T11:20:30.194246Z", + "iopub.status.busy": "2025-07-06T11:20:30.193969Z", + "iopub.status.idle": "2025-07-06T11:20:30.221692Z", + "shell.execute_reply": "2025-07-06T11:20:30.221135Z", + "shell.execute_reply.started": "2025-07-06T11:20:30.194225Z" + }, + "trusted": true + }, + "outputs": [], + "source": [ + "arguments = TrainingArguments( \n", + " resume_from_checkpoint=\"./Qwen-3-0.6B-Reasoning-Vi-Medical-LoRA\",\n", + " per_device_train_batch_size=8, \n", + " per_device_eval_batch_size=8, \n", + " gradient_accumulation_steps=4, \n", + " eval_strategy=\"steps\",\n", + " eval_steps=100,\n", + " logging_steps=100,\n", + " save_steps=100,\n", + " warmup_steps=30,\n", + " save_total_limit=4,\n", + " num_train_epochs=17, # 5\n", + " # max_steps=50,\n", + " save_strategy=\"steps\",\n", + " metric_for_best_model=\"eval_loss\",\n", + " learning_rate=2e-4, \n", + " fp16=not is_bfloat16_supported(), \n", + " bf16=is_bfloat16_supported(), \n", + " optim=\"adamw_8bit\", \n", + " weight_decay=0.01, \n", + " lr_scheduler_type=\"linear\", \n", + " seed=42, \n", + " output_dir=\"./Qwen-3-0.6B-Reasoning-Vi-Medical-LoRA\", \n", + " report_to=\"none\",\n", + " load_best_model_at_end=True, # Load weights with lowest val loss\n", + " \t\tgreater_is_better=False,\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-06T11:20:32.569180Z", + "iopub.status.busy": "2025-07-06T11:20:32.568905Z", + "iopub.status.idle": "2025-07-06T11:20:41.709986Z", + "shell.execute_reply": "2025-07-06T11:20:41.709391Z", + "shell.execute_reply.started": "2025-07-06T11:20:32.569161Z" + }, + "trusted": true + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "aeda8041da054066b08610037e1bbb13", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Unsloth: Tokenizing [\"text\"]: 0%| | 0/14121 [00:00\n", + " \n", + " \n", + " [6796/7497 2:14:37 < 2:38:53, 0.07 it/s, Epoch 15.41/17]\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
StepTraining LossValidation Loss
63000.4171000.762564
64000.4262000.757956
65000.4230000.749194
66000.4219000.744881
67000.4077000.747354

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Unsloth: Not an error, but Qwen3ForCausalLM does not accept `num_items_in_batch`.\n", + "Using gradient accumulation will be very slightly less accurate.\n", + "Read more on gradient accumulation issues here: https://unsloth.ai/blog/gradient\n" + ] + } + ], + "source": [ + "trainer.train(resume_from_checkpoint=True)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Inference" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-05T08:03:45.539957Z", + "iopub.status.busy": "2025-07-05T08:03:45.539471Z", + "iopub.status.idle": "2025-07-05T08:03:50.954695Z", + "shell.execute_reply": "2025-07-05T08:03:50.953913Z", + "shell.execute_reply.started": "2025-07-05T08:03:45.539931Z" + }, + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Question: Tại sao nam giới có nguy cơ mắc bệnh gout cao hơn phụ nữ?\n", + "\n", + "\n", + "\n", + "\n", + "Nam giới có nguy cơ mắc bệnh gout cao hơn phụ nữ. Nguyên nhân là do nam giới có hệ vi khuẩn Pseudomonas trong cơ thể cao hơn so với nữ giới. Vi khuẩn này gây ra các sản phẩm tích tụ trong các sụn khớp, từ đó gây ra bệnh gout.\n", + "\n", + "Nam giới có nguy cơ mắc bệnh gout cao hơn phụ nữ vì có hệ vi khuẩn Pseudomonas trong cơ thể cao hơn so với nữ giới.\n" + ] + } + ], + "source": [ + "question = training_samples[10]['question']\n", + "inputs = tokenizer(\n", + " [inference_prompt_style.format(question) + tokenizer.eos_token],\n", + " return_tensors=\"pt\"\n", + ").to(\"cuda\")\n", + "\n", + "outputs = model.generate(\n", + " input_ids=inputs.input_ids,\n", + " attention_mask=inputs.attention_mask,\n", + " max_new_tokens=1200,\n", + " eos_token_id=tokenizer.eos_token_id,\n", + " use_cache=True,\n", + ")\n", + "response = tokenizer.batch_decode(outputs, skip_special_tokens=True)\n", + "print(\"Question: \", question)\n", + "print(response[0].split(\"### Response:\")[1])" + ] + } + ], + "metadata": { + "kaggle": { + "accelerator": "gpu", + "dataSources": [], + "dockerImageVersionId": 31041, + "isGpuEnabled": true, + "isInternetEnabled": true, + "language": "python", + "sourceType": "notebook" + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.11" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}