Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,105 @@
|
|
1 |
---
|
2 |
license: unknown
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: unknown
|
3 |
---
|
4 |
+
### Motivation
|
5 |
+
项目的主要动机由于百川baichuan -7B是一个pretrain的大模型,尽管它在一些无监督的评估数据集上效果很好,但是并不能开箱即用,因为它没有 supervised finetune 这一步,没有和人类意图进行对齐。
|
6 |
+
随采用belle 0.5M 指令微调数据,采用qlora的量化微调的方式对百川大模型进行人类意图对齐训练。
|
7 |
+
|
8 |
+
### 大模型
|
9 |
+
百川7B https://huggingface.co/baichuan-inc/baichuan-7B
|
10 |
+
|
11 |
+
### sft 数据集
|
12 |
+
采用的是belle 0.5M https://huggingface.co/datasets/BelleGroup/train_0.5M_CN
|
13 |
+
|
14 |
+
训练方法和过程可视化
|
15 |
+
+ 先将百川LLM 采用qlora的 nf4 和双重量化方式进行量化
|
16 |
+
+ 在采用lora进行指令微调
|
17 |
+
+ 训练过程采用tensorborad 可视化,执行下方代码即可在localhost:6006去监控你的训练和测试loss
|
18 |
+
tensorboard --logdir ./runs/ --bind_all
|
19 |
+
|
20 |
+
|
21 |
+
### 资源消耗
|
22 |
+
由于采用了int4量化和lora等技术 整个资源消耗只需要12G左右的显存
|
23 |
+
|
24 |
+
### 预测代码
|
25 |
+
```
|
26 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
27 |
+
from transformers import BitsAndBytesConfig
|
28 |
+
from peft import (
|
29 |
+
LoraConfig,
|
30 |
+
PeftModel,
|
31 |
+
get_peft_model,
|
32 |
+
prepare_model_for_kbit_training,
|
33 |
+
set_peft_model_state_dict,
|
34 |
+
)
|
35 |
+
import torch
|
36 |
+
|
37 |
+
###加载量化模型
|
38 |
+
device_map = {"": 0}
|
39 |
+
tokenizer = AutoTokenizer.from_pretrained("./baichuan-7B",trust_remote_code=True)
|
40 |
+
model = AutoModelForCausalLM.from_pretrained("./baichuan-7B",
|
41 |
+
trust_remote_code=True,
|
42 |
+
quantization_config=BitsAndBytesConfig(
|
43 |
+
load_in_4bit=True,
|
44 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
45 |
+
bnb_4bit_use_double_quant=True,
|
46 |
+
bnb_4bit_quant_type='nf4'
|
47 |
+
),
|
48 |
+
device_map=device_map)
|
49 |
+
|
50 |
+
### 组装lora
|
51 |
+
LORA_WEIGHTS = "./baichuansft/"
|
52 |
+
device = "cuda:0"
|
53 |
+
model_lora = PeftModel.from_pretrained(
|
54 |
+
model,
|
55 |
+
LORA_WEIGHTS
|
56 |
+
).to(device)
|
57 |
+
|
58 |
+
|
59 |
+
### 进行预测
|
60 |
+
device = "cuda:0"
|
61 |
+
from transformers import GenerationConfig
|
62 |
+
generation_config = GenerationConfig(
|
63 |
+
temperature=0.2,
|
64 |
+
top_p = 0.85,
|
65 |
+
do_sample = True,
|
66 |
+
repetition_penalty=2.0,
|
67 |
+
max_new_tokens=1024, # max_length=max_new_tokens+input_sequence
|
68 |
+
|
69 |
+
)
|
70 |
+
|
71 |
+
prompt = """
|
72 |
+
北京有啥好玩的地方
|
73 |
+
"""
|
74 |
+
inputttext ="""###Human:\n{}###Assistant:\n:
|
75 |
+
""".format(prompt)
|
76 |
+
inputs = tokenizer(prompt,return_tensors="pt").to(device)
|
77 |
+
generate_ids = model_lora.generate(**inputs, generation_config=generation_config)
|
78 |
+
output = tokenizer.decode(generate_ids[0])
|
79 |
+
print(output)
|
80 |
+
```
|
81 |
+
|
82 |
+
### sft后的效果
|
83 |
+
微调前:
|
84 |
+
|
85 |
+
human : 台湾有什么好吃的
|
86 |
+
|
87 |
+
bot: 台北有哪些值得一去的景点?
|
88 |
+
|
89 |
+
human :北京有啥好玩的地方
|
90 |
+
|
91 |
+
bot: < href="http://www.bj-tourisme" target=_blank>北京旅游网<BR
|
92 |
+
|
93 |
+
微调后:
|
94 |
+
|
95 |
+
human : 台湾有什么好吃的
|
96 |
+
|
97 |
+
bot: 1.蚵仔煎(Oyster Pancake):用鸡蛋和面粉混合后摊成薄饼,再加入葱花、猪肉末等佐料。最后将生蚝肉放在上面即可食用;20世纪初由闽南移民传入台北市的传统小吃之一 ;3大肠包饭 (Da Dang Bao): 大米蒸熟后再加酱油调味并配以猪肚或鸡肝做成的馅儿,然后把这些食材包裹在糯香的大白菜叶子中一起煮制而成的一种美食 ,是当地人最爱的早餐食品!
|
98 |
+
|
99 |
+
human : 北京有啥好玩的地方
|
100 |
+
|
101 |
+
bot: 1.故宫博物院:中国古代皇家建筑群,是明清两代的皇帝居住和办公场所。门票价格为60元/人次(淡季)、85 元 / 人 (旺季节假日期间);开放时间: 周二至周日9点-下午4点半;周一闭馆维护检修不对外售票,请提前购置参观券或预约入场方式进园游览观光拍照留念等活动需在指定区域内进行不得进入其他禁区及文物保护单位范围内游玩以免破坏古迹造成损失影响安全问题后果自负!2北京天坛公园位于北京市中心东城区永定门外大街南侧占地面积约73万平方米是中国现存最大的祭祀性园林之一也是世界上保存最完整的天文台遗址!免费景点无需购买任何费用但需要排队等待安保人员检查后方可通行建议游客最好早到避免长时间拥堵交通高峰期前往景区观赏美景感受历史文化魅力~
|
102 |
+
|
103 |
+
### Reference
|
104 |
+
https://github.com/artidoro/qlora
|
105 |
+
https://github.com/LianjiaTech/BELLE
|