Files
qwen-medical/finetune_from_checkpiont.ipynb
ModelHub XC 69649c4719 初始化项目,由ModelHub XC社区提供模型
Model: jaring/qwen-medical
Source: Original Platform
2026-06-23 09:30:12 +08:00

691 lines
45 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "ffc392b5-82ef-4d3e-952e-d6e83317e813",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-12T06:47:40.705036Z",
"iopub.status.busy": "2025-03-12T06:47:40.704701Z",
"iopub.status.idle": "2025-03-12T06:49:19.377267Z",
"shell.execute_reply": "2025-03-12T06:49:19.376711Z",
"shell.execute_reply.started": "2025-03-12T06:47:40.705018Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Looking in indexes: https://mirrors.cloud.aliyuncs.com/pypi/simple\n",
"Collecting unsloth\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/97/85/657d11fef1ef2d6440ead4f0411b9b6658f53a7bf826cac1101bf78adec5/unsloth-2025.3.9-py3-none-any.whl (191 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m191.6/191.6 kB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hCollecting unsloth_zoo>=2025.3.8 (from unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/89/9b/20fbbe089c0e4698ddc3138b6b248ee6b8b93f5fba9b7fbd992cc77779aa/unsloth_zoo-2025.3.8-py3-none-any.whl (112 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m112.5/112.5 kB\u001b[0m \u001b[31m8.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hCollecting torch>=2.4.0 (from unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/37/81/aa9ab58ec10264c1abe62c8b73f5086c3c558885d6beecebf699f0dbeaeb/torch-2.6.0-cp310-cp310-manylinux1_x86_64.whl (766.7 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m766.7/766.7 MB\u001b[0m \u001b[31m4.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting xformers>=0.0.27.post2 (from unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/55/4f/ef63f866ec7d3a23f78629604deaf379cd833fa4fd0cf7e6f8a77906f125/xformers-0.0.29.post3-cp310-cp310-manylinux_2_28_x86_64.whl (43.3 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m43.3/43.3 MB\u001b[0m \u001b[31m60.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: bitsandbytes in /usr/local/lib/python3.10/site-packages (from unsloth) (0.45.2)\n",
"Collecting triton>=3.0.0 (from unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/01/65/3ffa90e158a2c82f0716eee8d26a725d241549b7d7aaf7e4f44ac03ebd89/triton-3.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (253.1 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m253.1/253.1 MB\u001b[0m \u001b[31m14.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: packaging in /usr/local/lib/python3.10/site-packages (from unsloth) (24.2)\n",
"Collecting tyro (from unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/11/4f/5ba9e95cfe4b88ccb439b785356de7073b344abc30bf87ea767fedc0a257/tyro-0.9.16-py3-none-any.whl (117 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m117.2/117.2 kB\u001b[0m \u001b[31m25.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: transformers!=4.47.0,>=4.46.1 in /usr/local/lib/python3.10/site-packages (from unsloth) (4.48.3)\n",
"Requirement already satisfied: datasets>=2.16.0 in /usr/local/lib/python3.10/site-packages (from unsloth) (3.2.0)\n",
"Requirement already satisfied: sentencepiece>=0.2.0 in /usr/local/lib/python3.10/site-packages (from unsloth) (0.2.0)\n",
"Requirement already satisfied: tqdm in /usr/local/lib/python3.10/site-packages (from unsloth) (4.67.0)\n",
"Requirement already satisfied: psutil in /usr/local/lib/python3.10/site-packages (from unsloth) (6.1.0)\n",
"Requirement already satisfied: wheel>=0.42.0 in /usr/local/lib/python3.10/site-packages (from unsloth) (0.44.0)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/site-packages (from unsloth) (1.26.4)\n",
"Requirement already satisfied: accelerate>=0.34.1 in /usr/local/lib/python3.10/site-packages (from unsloth) (1.3.0)\n",
"Collecting trl!=0.15.0,!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,<=0.15.2,>=0.7.9 (from unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/7b/29/25378447c48359843de0e4ce1995d367210601c3b437ddf1c779b6393d74/trl-0.15.2-py3-none-any.whl (318 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m318.9/318.9 kB\u001b[0m \u001b[31m22.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: peft!=0.11.0,>=0.7.1 in /usr/local/lib/python3.10/site-packages (from unsloth) (0.14.0)\n",
"Requirement already satisfied: protobuf<4.0.0 in /usr/local/lib/python3.10/site-packages (from unsloth) (3.20.3)\n",
"Requirement already satisfied: huggingface_hub in /usr/local/lib/python3.10/site-packages (from unsloth) (0.25.2)\n",
"Collecting hf_transfer (from unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/d6/d8/f87ea6f42456254b48915970ed98e993110521e9263472840174d32c880d/hf_transfer-0.1.9-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.6 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.6/3.6 MB\u001b[0m \u001b[31m71.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: diffusers in /usr/local/lib/python3.10/site-packages (from unsloth) (0.32.2)\n",
"Requirement already satisfied: torchvision in /usr/local/lib/python3.10/site-packages (from unsloth) (0.18.1)\n",
"Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/site-packages (from accelerate>=0.34.1->unsloth) (6.0.2)\n",
"Requirement already satisfied: safetensors>=0.4.3 in /usr/local/lib/python3.10/site-packages (from accelerate>=0.34.1->unsloth) (0.5.2)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/site-packages (from datasets>=2.16.0->unsloth) (3.16.1)\n",
"Requirement already satisfied: pyarrow>=15.0.0 in /usr/local/lib/python3.10/site-packages (from datasets>=2.16.0->unsloth) (19.0.0)\n",
"Requirement already satisfied: dill<0.3.9,>=0.3.0 in /usr/local/lib/python3.10/site-packages (from datasets>=2.16.0->unsloth) (0.3.8)\n",
"Requirement already satisfied: pandas in /usr/local/lib/python3.10/site-packages (from datasets>=2.16.0->unsloth) (2.2.3)\n",
"Requirement already satisfied: requests>=2.32.2 in /usr/local/lib/python3.10/site-packages (from datasets>=2.16.0->unsloth) (2.32.3)\n",
"Requirement already satisfied: xxhash in /usr/local/lib/python3.10/site-packages (from datasets>=2.16.0->unsloth) (3.5.0)\n",
"Requirement already satisfied: multiprocess<0.70.17 in /usr/local/lib/python3.10/site-packages (from datasets>=2.16.0->unsloth) (0.70.16)\n",
"Requirement already satisfied: fsspec<=2024.9.0,>=2023.1.0 in /usr/local/lib/python3.10/site-packages (from fsspec[http]<=2024.9.0,>=2023.1.0->datasets>=2.16.0->unsloth) (2024.9.0)\n",
"Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/site-packages (from datasets>=2.16.0->unsloth) (3.11.12)\n",
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/site-packages (from huggingface_hub->unsloth) (4.12.2)\n",
"Requirement already satisfied: networkx in /usr/local/lib/python3.10/site-packages (from torch>=2.4.0->unsloth) (3.4.2)\n",
"Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/site-packages (from torch>=2.4.0->unsloth) (3.1.4)\n",
"Collecting nvidia-cuda-nvrtc-cu12==12.4.127 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/2c/14/91ae57cd4db3f9ef7aa99f4019cfa8d54cb4caa7e00975df6467e9725a9f/nvidia_cuda_nvrtc_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (24.6 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m24.6/24.6 MB\u001b[0m \u001b[31m81.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting nvidia-cuda-runtime-cu12==12.4.127 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/ea/27/1795d86fe88ef397885f2e580ac37628ed058a92ed2c39dc8eac3adf0619/nvidia_cuda_runtime_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (883 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m883.7/883.7 kB\u001b[0m \u001b[31m71.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hCollecting nvidia-cuda-cupti-cu12==12.4.127 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/67/42/f4f60238e8194a3106d06a058d494b18e006c10bb2b915655bd9f6ea4cb1/nvidia_cuda_cupti_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (13.8 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.8/13.8 MB\u001b[0m \u001b[31m102.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting nvidia-cudnn-cu12==9.1.0.70 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/9f/fd/713452cd72343f682b1c7b9321e23829f00b842ceaedcda96e742ea0b0b3/nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl (664.8 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m664.8/664.8 MB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting nvidia-cublas-cu12==12.4.5.8 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/ae/71/1c91302526c45ab494c23f61c7a84aa568b8c1f9d196efa5993957faf906/nvidia_cublas_cu12-12.4.5.8-py3-none-manylinux2014_x86_64.whl (363.4 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m363.4/363.4 MB\u001b[0m \u001b[31m9.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting nvidia-cufft-cu12==11.2.1.3 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/27/94/3266821f65b92b3138631e9c8e7fe1fb513804ac934485a8d05776e1dd43/nvidia_cufft_cu12-11.2.1.3-py3-none-manylinux2014_x86_64.whl (211.5 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m211.5/211.5 MB\u001b[0m \u001b[31m11.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting nvidia-curand-cu12==10.3.5.147 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/8a/6d/44ad094874c6f1b9c654f8ed939590bdc408349f137f9b98a3a23ccec411/nvidia_curand_cu12-10.3.5.147-py3-none-manylinux2014_x86_64.whl (56.3 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.3/56.3 MB\u001b[0m \u001b[31m28.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting nvidia-cusolver-cu12==11.6.1.9 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/3a/e1/5b9089a4b2a4790dfdea8b3a006052cfecff58139d5a4e34cb1a51df8d6f/nvidia_cusolver_cu12-11.6.1.9-py3-none-manylinux2014_x86_64.whl (127.9 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m127.9/127.9 MB\u001b[0m \u001b[31m11.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting nvidia-cusparse-cu12==12.3.1.170 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/db/f7/97a9ea26ed4bbbfc2d470994b8b4f338ef663be97b8f677519ac195e113d/nvidia_cusparse_cu12-12.3.1.170-py3-none-manylinux2014_x86_64.whl (207.5 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m207.5/207.5 MB\u001b[0m \u001b[31m16.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting nvidia-cusparselt-cu12==0.6.2 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/78/a8/bcbb63b53a4b1234feeafb65544ee55495e1bb37ec31b999b963cbccfd1d/nvidia_cusparselt_cu12-0.6.2-py3-none-manylinux2014_x86_64.whl (150.1 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m150.1/150.1 MB\u001b[0m \u001b[31m22.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting nvidia-nccl-cu12==2.21.5 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/df/99/12cd266d6233f47d00daf3a72739872bdc10267d0383508b0b9c84a18bb6/nvidia_nccl_cu12-2.21.5-py3-none-manylinux2014_x86_64.whl (188.7 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m188.7/188.7 MB\u001b[0m \u001b[31m15.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting nvidia-nvtx-cu12==12.4.127 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/87/20/199b8713428322a2f22b722c62b8cc278cc53dffa9705d744484b5035ee9/nvidia_nvtx_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (99 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m99.1/99.1 kB\u001b[0m \u001b[31m22.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hCollecting nvidia-nvjitlink-cu12==12.4.127 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/ff/ff/847841bacfbefc97a00036e0fce5a0f086b640756dc38caea5e1bb002655/nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (21.1 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m21.1/21.1 MB\u001b[0m \u001b[31m113.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hCollecting sympy==1.13.1 (from torch>=2.4.0->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/b2/fe/81695a1aa331a842b582453b605175f419fe8540355886031328089d840a/sympy-1.13.1-py3-none-any.whl (6.2 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.2/6.2 MB\u001b[0m \u001b[31m116.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/site-packages (from sympy==1.13.1->torch>=2.4.0->unsloth) (1.3.0)\n",
"Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/site-packages (from transformers!=4.47.0,>=4.46.1->unsloth) (2024.11.6)\n",
"Requirement already satisfied: tokenizers<0.22,>=0.21 in /usr/local/lib/python3.10/site-packages (from transformers!=4.47.0,>=4.46.1->unsloth) (0.21.0)\n",
"Requirement already satisfied: rich in /usr/local/lib/python3.10/site-packages (from trl!=0.15.0,!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,<=0.15.2,>=0.7.9->unsloth) (13.9.4)\n",
"Collecting cut_cross_entropy (from unsloth_zoo>=2025.3.8->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/df/5f/62fdb048f84d19e2123b6bbd722fe09c8c79b4964c50094d1e979db808e2/cut_cross_entropy-25.1.1-py3-none-any.whl (22 kB)\n",
"Requirement already satisfied: pillow in /usr/local/lib/python3.10/site-packages (from unsloth_zoo>=2025.3.8->unsloth) (11.0.0)\n",
"Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.10/site-packages (from diffusers->unsloth) (8.5.0)\n",
"INFO: pip is looking at multiple versions of torchvision to determine which version is compatible with other requirements. This could take a while.\n",
"Collecting torchvision (from unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/5e/44/32e2d2d174391374d5ff3c4691b802e8efda9ae27ab9062eca2255b006af/torchvision-0.21.0-cp310-cp310-manylinux1_x86_64.whl (7.2 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.2/7.2 MB\u001b[0m \u001b[31m118.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hCollecting docstring-parser>=0.15 (from tyro->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/d5/7c/e9fcff7623954d86bdc17782036cbf715ecab1bec4847c008557affe1ca8/docstring_parser-0.16-py3-none-any.whl (36 kB)\n",
"Collecting shtab>=1.5.6 (from tyro->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/e2/d1/a1d3189e7873408b9dc396aef0d7926c198b0df2aa3ddb5b539d3e89a70f/shtab-1.7.1-py3-none-any.whl (14 kB)\n",
"Collecting typeguard>=4.0.0 (from tyro->unsloth)\n",
" Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/cf/4b/9a77dc721aa0b7f74440a42e4ef6f9a4fae7324e17f64f88b96f4c25cc05/typeguard-4.4.2-py3-none-any.whl (35 kB)\n",
"Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.10/site-packages (from aiohttp->datasets>=2.16.0->unsloth) (2.4.6)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/site-packages (from aiohttp->datasets>=2.16.0->unsloth) (1.3.2)\n",
"Requirement already satisfied: async-timeout<6.0,>=4.0 in /usr/local/lib/python3.10/site-packages (from aiohttp->datasets>=2.16.0->unsloth) (5.0.1)\n",
"Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/site-packages (from aiohttp->datasets>=2.16.0->unsloth) (24.2.0)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/site-packages (from aiohttp->datasets>=2.16.0->unsloth) (1.5.0)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/site-packages (from aiohttp->datasets>=2.16.0->unsloth) (6.1.0)\n",
"Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.10/site-packages (from aiohttp->datasets>=2.16.0->unsloth) (0.2.1)\n",
"Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.10/site-packages (from aiohttp->datasets>=2.16.0->unsloth) (1.18.3)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/site-packages (from requests>=2.32.2->datasets>=2.16.0->unsloth) (3.4.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/site-packages (from requests>=2.32.2->datasets>=2.16.0->unsloth) (3.10)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/site-packages (from requests>=2.32.2->datasets>=2.16.0->unsloth) (2.2.3)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/site-packages (from requests>=2.32.2->datasets>=2.16.0->unsloth) (2024.8.30)\n",
"Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/site-packages (from rich->trl!=0.15.0,!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,<=0.15.2,>=0.7.9->unsloth) (3.0.0)\n",
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/site-packages (from rich->trl!=0.15.0,!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,<=0.15.2,>=0.7.9->unsloth) (2.18.0)\n",
"Requirement already satisfied: zipp>=3.20 in /usr/local/lib/python3.10/site-packages (from importlib-metadata->diffusers->unsloth) (3.20.2)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/site-packages (from jinja2->torch>=2.4.0->unsloth) (2.1.5)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/site-packages (from pandas->datasets>=2.16.0->unsloth) (2.9.0.post0)\n",
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/site-packages (from pandas->datasets>=2.16.0->unsloth) (2025.1)\n",
"Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/site-packages (from pandas->datasets>=2.16.0->unsloth) (2025.1)\n",
"Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/site-packages (from markdown-it-py>=2.2.0->rich->trl!=0.15.0,!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,<=0.15.2,>=0.7.9->unsloth) (0.1.2)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/site-packages (from python-dateutil>=2.8.2->pandas->datasets>=2.16.0->unsloth) (1.16.0)\n",
"\u001b[33mDEPRECATION: omegaconf 2.0.6 has a non-standard dependency specifier PyYAML>=5.1.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of omegaconf or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mDEPRECATION: pytorch-lightning 1.7.7 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mInstalling collected packages: triton, nvidia-cusparselt-cu12, typeguard, sympy, shtab, nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, hf_transfer, docstring-parser, nvidia-cusparse-cu12, nvidia-cudnn-cu12, tyro, nvidia-cusolver-cu12, torch, xformers, torchvision, cut_cross_entropy, trl, unsloth_zoo, unsloth\n",
" Attempting uninstall: triton\n",
" Found existing installation: triton 2.3.1\n",
" Uninstalling triton-2.3.1:\n",
" Successfully uninstalled triton-2.3.1\n",
" Attempting uninstall: typeguard\n",
" Found existing installation: typeguard 2.13.3\n",
" Uninstalling typeguard-2.13.3:\n",
" Successfully uninstalled typeguard-2.13.3\n",
" Attempting uninstall: sympy\n",
" Found existing installation: sympy 1.13.3\n",
" Uninstalling sympy-1.13.3:\n",
" Successfully uninstalled sympy-1.13.3\n",
" Attempting uninstall: nvidia-nvtx-cu12\n",
" Found existing installation: nvidia-nvtx-cu12 12.1.105\n",
" Uninstalling nvidia-nvtx-cu12-12.1.105:\n",
" Successfully uninstalled nvidia-nvtx-cu12-12.1.105\n",
" Attempting uninstall: nvidia-nvjitlink-cu12\n",
" Found existing installation: nvidia-nvjitlink-cu12 12.6.77\n",
" Uninstalling nvidia-nvjitlink-cu12-12.6.77:\n",
" Successfully uninstalled nvidia-nvjitlink-cu12-12.6.77\n",
" Attempting uninstall: nvidia-nccl-cu12\n",
" Found existing installation: nvidia-nccl-cu12 2.20.5\n",
" Uninstalling nvidia-nccl-cu12-2.20.5:\n",
" Successfully uninstalled nvidia-nccl-cu12-2.20.5\n",
" Attempting uninstall: nvidia-curand-cu12\n",
" Found existing installation: nvidia-curand-cu12 10.3.2.106\n",
" Uninstalling nvidia-curand-cu12-10.3.2.106:\n",
" Successfully uninstalled nvidia-curand-cu12-10.3.2.106\n",
" Attempting uninstall: nvidia-cufft-cu12\n",
" Found existing installation: nvidia-cufft-cu12 11.0.2.54\n",
" Uninstalling nvidia-cufft-cu12-11.0.2.54:\n",
" Successfully uninstalled nvidia-cufft-cu12-11.0.2.54\n",
" Attempting uninstall: nvidia-cuda-runtime-cu12\n",
" Found existing installation: nvidia-cuda-runtime-cu12 12.1.105\n",
" Uninstalling nvidia-cuda-runtime-cu12-12.1.105:\n",
" Successfully uninstalled nvidia-cuda-runtime-cu12-12.1.105\n",
" Attempting uninstall: nvidia-cuda-nvrtc-cu12\n",
" Found existing installation: nvidia-cuda-nvrtc-cu12 12.1.105\n",
" Uninstalling nvidia-cuda-nvrtc-cu12-12.1.105:\n",
" Successfully uninstalled nvidia-cuda-nvrtc-cu12-12.1.105\n",
" Attempting uninstall: nvidia-cuda-cupti-cu12\n",
" Found existing installation: nvidia-cuda-cupti-cu12 12.1.105\n",
" Uninstalling nvidia-cuda-cupti-cu12-12.1.105:\n",
" Successfully uninstalled nvidia-cuda-cupti-cu12-12.1.105\n",
" Attempting uninstall: nvidia-cublas-cu12\n",
" Found existing installation: nvidia-cublas-cu12 12.1.3.1\n",
" Uninstalling nvidia-cublas-cu12-12.1.3.1:\n",
" Successfully uninstalled nvidia-cublas-cu12-12.1.3.1\n",
" Attempting uninstall: nvidia-cusparse-cu12\n",
" Found existing installation: nvidia-cusparse-cu12 12.1.0.106\n",
" Uninstalling nvidia-cusparse-cu12-12.1.0.106:\n",
" Successfully uninstalled nvidia-cusparse-cu12-12.1.0.106\n",
" Attempting uninstall: nvidia-cudnn-cu12\n",
" Found existing installation: nvidia-cudnn-cu12 8.9.2.26\n",
" Uninstalling nvidia-cudnn-cu12-8.9.2.26:\n",
" Successfully uninstalled nvidia-cudnn-cu12-8.9.2.26\n",
" Attempting uninstall: nvidia-cusolver-cu12\n",
" Found existing installation: nvidia-cusolver-cu12 11.4.5.107\n",
" Uninstalling nvidia-cusolver-cu12-11.4.5.107:\n",
" Successfully uninstalled nvidia-cusolver-cu12-11.4.5.107\n",
" Attempting uninstall: torch\n",
" Found existing installation: torch 2.3.1\n",
" Uninstalling torch-2.3.1:\n",
" Successfully uninstalled torch-2.3.1\n",
" Attempting uninstall: xformers\n",
" Found existing installation: xformers 0.0.27\n",
" Uninstalling xformers-0.0.27:\n",
" Successfully uninstalled xformers-0.0.27\n",
" Attempting uninstall: torchvision\n",
" Found existing installation: torchvision 0.18.1\n",
" Uninstalling torchvision-0.18.1:\n",
" Successfully uninstalled torchvision-0.18.1\n",
" Attempting uninstall: trl\n",
" Found existing installation: trl 0.15.0\n",
" Uninstalling trl-0.15.0:\n",
" Successfully uninstalled trl-0.15.0\n",
"\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",
"xtuner 0.1.23 requires lagent>=0.1.2, which is not installed.\n",
"xtuner 0.1.23 requires mmengine>=0.10.3, which is not installed.\n",
"autoawq 0.2.8 requires huggingface-hub>=0.26.5, but you have huggingface-hub 0.25.2 which is incompatible.\n",
"autoawq 0.2.8 requires transformers<=4.47.1,>=4.45.0, but you have transformers 4.48.3 which is incompatible.\n",
"fairseq 0.12.2 requires hydra-core<1.1,>=1.0.7, but you have hydra-core 1.3.2 which is incompatible.\n",
"fastai 2.7.18 requires torch<2.6,>=1.10, but you have torch 2.6.0 which is incompatible.\n",
"funcodec 0.2.0 requires typeguard==2.13.3, but you have typeguard 4.4.2 which is incompatible.\n",
"lmdeploy 0.6.2 requires peft<=0.11.1, but you have peft 0.14.0 which is incompatible.\n",
"lmdeploy 0.6.2 requires torch<=2.4.0,>=2.0.0, but you have torch 2.6.0 which is incompatible.\n",
"lmdeploy 0.6.2 requires torchvision<=0.19.0,>=0.15.0, but you have torchvision 0.21.0 which is incompatible.\n",
"lmdeploy 0.6.2 requires triton<=3.0.0,>=2.2.0; sys_platform == \"linux\", but you have triton 3.2.0 which is incompatible.\n",
"pai-easycv 0.11.6 requires timm==0.5.4, but you have timm 1.0.14 which is incompatible.\n",
"torchaudio 2.3.1 requires torch==2.3.1, but you have torch 2.6.0 which is incompatible.\n",
"vllm 0.5.3 requires torch==2.3.1, but you have torch 2.6.0 which is incompatible.\n",
"vllm 0.5.3 requires torchvision==0.18.1, but you have torchvision 0.21.0 which is incompatible.\n",
"vllm 0.5.3 requires xformers==0.0.27, but you have xformers 0.0.29.post3 which is incompatible.\n",
"vllm-flash-attn 2.5.9.post1 requires torch==2.3.1, but you have torch 2.6.0 which is incompatible.\u001b[0m\u001b[31m\n",
"\u001b[0mSuccessfully installed cut_cross_entropy-25.1.1 docstring-parser-0.16 hf_transfer-0.1.9 nvidia-cublas-cu12-12.4.5.8 nvidia-cuda-cupti-cu12-12.4.127 nvidia-cuda-nvrtc-cu12-12.4.127 nvidia-cuda-runtime-cu12-12.4.127 nvidia-cudnn-cu12-9.1.0.70 nvidia-cufft-cu12-11.2.1.3 nvidia-curand-cu12-10.3.5.147 nvidia-cusolver-cu12-11.6.1.9 nvidia-cusparse-cu12-12.3.1.170 nvidia-cusparselt-cu12-0.6.2 nvidia-nccl-cu12-2.21.5 nvidia-nvjitlink-cu12-12.4.127 nvidia-nvtx-cu12-12.4.127 shtab-1.7.1 sympy-1.13.1 torch-2.6.0 torchvision-0.21.0 triton-3.2.0 trl-0.15.2 typeguard-4.4.2 tyro-0.9.16 unsloth-2025.3.9 unsloth_zoo-2025.3.8 xformers-0.0.29.post3\n",
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
"\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.0.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
]
}
],
"source": [
"!pip install unsloth"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c5475de-22a3-43ef-a15a-62b26d3e69a8",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!python -c \"from modelscope.hub.snapshot_download import snapshot_download; snapshot_download('Qwen/Qwen2.5-7B', cache_dir='/mnt/workspace/models')\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a69c658c-6d31-4536-aa1f-7fbd118a983f",
"metadata": {
"ExecutionIndicator": {
"show": true
},
"execution": {
"iopub.execute_input": "2025-03-12T06:49:19.378570Z",
"iopub.status.busy": "2025-03-12T06:49:19.378272Z",
"iopub.status.idle": "2025-03-12T06:51:15.648856Z",
"shell.execute_reply": "2025-03-12T06:51:15.648229Z",
"shell.execute_reply.started": "2025-03-12T06:49:19.378550Z"
},
"tags": []
},
"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-03-12 14:49:24.753713: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n",
"2025-03-12 14:49:24.793058: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
"To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"2025-03-12 14:49:25.791081: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"🦥 Unsloth Zoo will now patch everything to make training faster!\n",
"[2025-03-12 14:49:31,238] [INFO] [real_accelerator.py:222:get_accelerator] Setting ds_accelerator to cuda (auto detect)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"df: /root/.triton/autotune: 没有那个文件或目录\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING 03-12 14:49:32 _custom_ops.py:14] Failed to import from vllm._C with ImportError('/usr/local/lib/python3.10/site-packages/vllm/_C.abi3.so: undefined symbol: _ZN5torch3jit11parseSchemaERKSs')\n",
"==((====))== Unsloth 2025.3.9: Fast Qwen2 patching. Transformers: 4.48.3. vLLM: 0.5.3.\n",
" \\\\ /| NVIDIA A10. Num GPUs = 1. Max memory: 23.69 GB. Platform: Linux.\n",
"O^O/ \\_/ \\ Torch: 2.6.0+cu124. CUDA: 8.6. CUDA Toolkit: 12.4. Triton: 3.2.0\n",
"\\ / Bfloat16 = TRUE. FA [Xformers = 0.0.29.post3. FA2 = True]\n",
" \"-____-\" Free license: http://github.com/unslothai/unsloth\n",
"Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "abb304d817e14275824b51922d51af3b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/4 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"/mnt/workspace/models/Qwen/Qwen2.5-7B does not have a padding token! Will use pad_token = <|vision_pad|>.\n",
"成功从检查点加载模型\n"
]
}
],
"source": [
"from unsloth import FastLanguageModel\n",
"import torch\n",
"import os\n",
"\n",
"max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!\n",
"dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+\n",
"load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.\n",
"\n",
"# 基础模型路径\n",
"base_model_path = \"/mnt/workspace/models/Qwen/Qwen2.5-7B\"\n",
"# 检查点路径 - 这是之前训练保存的检查点\n",
"\n",
"# 从基础模型和检查点加载模型\n",
"\n",
"\n",
"model, tokenizer = FastLanguageModel.from_pretrained(\n",
" model_name = base_model_path,\n",
" max_seq_length = max_seq_length,\n",
" dtype = dtype,\n",
" load_in_4bit = load_in_4bit,\n",
")\n",
"\n",
"print(\"成功从检查点加载模型\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "075045ac-b3f9-410c-8def-ee00d009e2a3",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-12T06:51:15.649791Z",
"iopub.status.busy": "2025-03-12T06:51:15.649601Z",
"iopub.status.idle": "2025-03-12T06:51:15.654695Z",
"shell.execute_reply": "2025-03-12T06:51:15.654141Z",
"shell.execute_reply.started": "2025-03-12T06:51:15.649774Z"
},
"tags": []
},
"outputs": [],
"source": [
"# -*- coding: utf-8 -*-\n",
"import json\n",
"from datasets import Dataset\n",
"\n",
"\n",
"custom_prompt = \"\"\"下面列出了一个问题. 请写出问题的答案.\n",
"### 问题:\n",
"{}\n",
"### 答案:\n",
"{}\"\"\"\n",
"\n",
"\n",
"class LocalJsonDataset:\n",
" def __init__(self, json_file, tokenizer, max_seq_length=2048):\n",
" self.json_file = json_file\n",
" self.tokenizer = tokenizer\n",
" self.max_seq_length = max_seq_length\n",
" self.dataset = self.load_dataset()\n",
"\n",
" def load_dataset(self):\n",
" with open(self.json_file, 'r', encoding='utf-8') as f:\n",
" data = json.load(f)\n",
"\n",
" texts = []\n",
" for item in data:\n",
" text = custom_prompt.format(item['Question'], item['Response']) + self.tokenizer.eos_token\n",
" texts.append(text)\n",
"\n",
" dataset_dict = {\n",
" 'text': texts # 添加'text'字段以适配SFTTrainer\n",
" }\n",
"\n",
" dataset = Dataset.from_dict(dataset_dict)\n",
" return dataset\n",
"\n",
" def get_dataset(self):\n",
" return self.dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2bfabb01-41e5-45ab-b2bc-aec847e0d8a5",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-12T06:51:15.656107Z",
"iopub.status.busy": "2025-03-12T06:51:15.655827Z",
"iopub.status.idle": "2025-03-12T06:51:16.374665Z",
"shell.execute_reply": "2025-03-12T06:51:16.374194Z",
"shell.execute_reply.started": "2025-03-12T06:51:15.656092Z"
},
"tags": []
},
"outputs": [],
"source": [
"# -*- coding: utf-8 -*-\n",
"# 加载和预处理数据集\n",
"custom_dataset = LocalJsonDataset(json_file='/mnt/workspace/medical_o1_sft_Chinese.json', tokenizer=tokenizer, max_seq_length=max_seq_length)\n",
"dataset = custom_dataset.get_dataset()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad5db9ae-664a-401c-bd0a-cf00e22112a0",
"metadata": {
"ExecutionIndicator": {
"show": true
},
"execution": {
"iopub.execute_input": "2025-03-12T06:51:16.375417Z",
"iopub.status.busy": "2025-03-12T06:51:16.375256Z",
"iopub.status.idle": "2025-03-12T06:51:21.278991Z",
"shell.execute_reply": "2025-03-12T06:51:21.278537Z",
"shell.execute_reply.started": "2025-03-12T06:51:16.375401Z"
},
"tags": []
},
"outputs": [],
"source": [
"model = FastLanguageModel.get_peft_model(\n",
" model,\n",
" r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128\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, # Supports any, but = 0 is optimized\n",
" bias = \"none\", # Supports any, but = \"none\" is optimized\n",
" # [NEW] \"unsloth\" uses 30% less VRAM, fits 2x larger batch sizes!\n",
" use_gradient_checkpointing = \"unsloth\", # True or \"unsloth\" for very long context\n",
" random_state = 3407,\n",
" use_rslora = False, # We support rank stabilized LoRA\n",
" loftq_config = None, # And LoftQ\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c3dd4cf-28ba-46ff-a4cb-1f239f4d9920",
"metadata": {
"ExecutionIndicator": {
"show": true
},
"execution": {
"iopub.execute_input": "2025-03-12T06:51:21.279806Z",
"iopub.status.busy": "2025-03-12T06:51:21.279624Z"
},
"tags": []
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "686840cdebdf44159b4cf2fa47c0f0ff",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Tokenizing to [\"text\"] (num_proc=2): 0%| | 0/24772 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from trl import SFTTrainer\n",
"from transformers import TrainingArguments\n",
"from unsloth import is_bfloat16_supported\n",
"\n",
"trainer = SFTTrainer(\n",
" model = model,\n",
" tokenizer = tokenizer,\n",
" train_dataset = dataset,\n",
" dataset_text_field = \"text\",\n",
" max_seq_length = max_seq_length,\n",
" dataset_num_proc = 2,\n",
" packing = False, # Can make training 5x faster for short sequences.\n",
" args = TrainingArguments(\n",
" per_device_train_batch_size = 2,\n",
" gradient_accumulation_steps = 4,\n",
" warmup_steps = 5,\n",
" num_train_epochs = 3, # Set this for 1 full training run.\n",
" # max_steps = 128,\n",
" learning_rate = 5e-5,\n",
" fp16 = not is_bfloat16_supported(),\n",
" bf16 = is_bfloat16_supported(),\n",
" logging_steps = 1,\n",
" optim = \"adamw_8bit\",\n",
" weight_decay = 0.01,\n",
" lr_scheduler_type = \"linear\",\n",
" seed = 3407,\n",
" output_dir = \"outputs\",\n",
" report_to = \"none\", # Use this for WandB etc\n",
" ),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "698c1056-aa04-42c0-9691-f01c08e4f536",
"metadata": {
"ExecutionIndicator": {
"show": true
},
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"# 开始训练\n",
"trainer.train()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90e8dca0",
"metadata": {},
"outputs": [],
"source": [
"# 或者选择从checkpoint继续训练\n",
"trainer.train(resume_from_checkpoint = True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24af8b57-2e0f-485e-8712-18787fd8da14",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e878fa3-03c9-47f9-8d46-a20702980851",
"metadata": {
"ExecutionIndicator": {
"show": true
},
"tags": []
},
"outputs": [],
"source": [
"FastLanguageModel.for_inference(model) # Enable native 2x faster inference\n",
"inputs = tokenizer(\n",
"[\n",
" custom_prompt.format(\n",
" \"经常性反酸打嗝体重下降腹部CT完全正常、肝肾功能正常癌胚抗原指标正常这种情况下最可能的诊断是什么\", # input\n",
" \"\", # output - leave this blank for generation!\n",
" )\n",
"], return_tensors = \"pt\").to(\"cuda\")\n",
"\n",
"from transformers import TextStreamer\n",
"text_streamer = TextStreamer(tokenizer)\n",
"_ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 2048)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3a6b4dd-6922-45f5-9548-f5ca41f0e735",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"new_model_local = \"qwen-medical-7b\"\n",
"model.save_pretrained_merged(new_model_local, tokenizer, save_method = \"merged_16bit\")"
]
},
{
"cell_type": "markdown",
"id": "90de7813",
"metadata": {},
"source": [
"保存至modelscope"
]
},
{
"cell_type": "markdown",
"id": "307724e8",
"metadata": {},
"source": [
"modelscope login --token *******\n",
"\n",
"modelscope upload username/qwen-medical /mnt/workspace/qwen-medical-7b"
]
},
{
"cell_type": "markdown",
"id": "1ffde40c",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}