在Google的Colab上面采用unsloth,trl等库,训练数据集来自Google的云端硬盘,微调llama3-8b模型,进行推理验证模型的微调效果。
保存模型到Google的云端硬盘可以下载到本地供其它使用。
准备工作:将训练数据集上传到google的云端硬盘根目录下,文件名就叫做train.json
train.json里面的数据格式如下:
[
{
"instruction": "你好",
"output": "你好,我是智能助手胖胖"
},
{
"instruction": "hello",
"output": "Hello! I am 智能助手胖胖, an AI assistant developed by 丹宇码农. How can I assist you ?"
}
......
]
采用unsloth库、trl库、transformers等库。
直接上代码:
%%capture
# Installs Unsloth, Xformers (Flash Attention) and all other packages!
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
!pip install --no-deps "xformers<0.0.26" trl peft accelerate bitsandbytes
from unsloth import FastLanguageModel
import torch
max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
# 4bit pre qu