LLM微调实践

LLM微调实践

西溪暮雪 Lv1

安装相关库

1
2
3
4
5
6
7
8
pip install transformers
pip install accelerate
pip install huggingface_hub
pip install ipywidgets
pip install datasets
pip install trl
pip install peft
pip install swanlab

下载模型与数据集

1
2
3
import os
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
from huggingface_hub import snapshot_download
  • 注意HF_ENDPOINT环境变量需在huggingface相关库之前,否则不生效

然后下载模型与数据集

1
2
3
4
5
6
snapshot_download(repo_id="b-mc2/sql-create-context",
repo_type="dataset",
local_dir="data",
max_workers=8,
resume_download=True
)
1
2
3
4
5
6
snapshot_download(
repo_id="codellama/CodeLlama-7b-hf",
local_dir="CodeLlama-7b-hf",
resume_download=True,
max_workers=8
)

以下是训练代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from datasets import load_dataset
import swanlab
from swanlab.integration.huggingface import SwanLabCallback
import torch
from trl import SFTTrainer
from peft import LoraConfig
from transformers import TrainingArguments
from transformers import AutoTokenizer, AutoModelForCausalLM
from trl import setup_chat_format

import swanlab

swanlab.login(api_key='')

model_id = "internlm2_5-1_8b-chat" # or `mistralai/Mistral-7B-v0.1`
dataset = load_dataset("json",data_files="train_dataset.json",split="train",)

model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
# attn_implementation="flash_attention_2",
torch_dtype=torch.bfloat16,
trust_remote_code=True
)

tokenizer = AutoTokenizer.from_pretrained(model_id,trust_remote_code=True)
tokenizer.padding_side = 'right' # 以防止警告

model, tokenizer = setup_chat_format(model, tokenizer)

# 基于QLoRA论文和Sebastian Raschka实验的LoRA配置
peft_config = LoraConfig(
lora_alpha=128,
lora_dropout=0.05,
r=256,
bias="none",
target_modules="all-linear",
task_type="CAUSAL_LM",
)

args = TrainingArguments(
output_dir="code-llama-7b-text-to-sql", # 要保存的目录和存储库ID
num_train_epochs=3, # 训练周期数
per_device_train_batch_size=3, # 训练期间每个设备的批量大小
gradient_accumulation_steps=2, # 反向/更新前的步骤数
gradient_checkpointing=True, # 使用渐变检查点来节省内存
optim="adamw_torch", # 使用融合的adamw优化器
logging_steps=10, # 每10步记录一次
save_strategy="epoch", # 每个epoch保存检查点
learning_rate=2e-4, # 学习率,基于QLoRA论文
# bf16=True, # 使用bfloat16精度
max_grad_norm=0.3, # 基于QLoRA论文的最大梯度范数
warmup_ratio=0.03, # 根据QLoRA论文的预热比例
lr_scheduler_type="constant", # 使用恒定学习率调度器
report_to="tensorboard", # 将指标报告到Tensorboard
)

swanlab_callback = SwanLabCallback(experiment_name="TransformersTest", cloud=False)

max_seq_length = 3072 # 数据集模型和打包的最大序列长度
trainer = SFTTrainer(
model=model,
args=args,
train_dataset=dataset,
peft_config=peft_config,
max_seq_length=max_seq_length,
tokenizer=tokenizer,
callbacks=[swanlab_callback],
packing=True,
dataset_kwargs={
"add_special_tokens": False, # 我们使用特殊 tokens
"append_concat_token": False, # 不需要添加额外的分隔符 token
})
trainer.train()
trainer.save_model()
  • 标题: LLM微调实践
  • 作者: 西溪暮雪
  • 创建于 : 2024-08-28 19:07:05
  • 更新于 : 2024-08-29 21:32:09
  • 链接: https://hexo.skaura.fun/2024/08/28/LLM微调/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论