# ChatGLM-FineTune(P-Tuning v2)
此微調方式基於P-Tuning v2 微調所需的vram大約7.7G,受限於硬體所以我們只使用這個方法
# 數據集格式
官方例子(https://github.com/THUDM/ChatGLM-6B/blob/main/ptuning/README.md)
示例1
Input: 类型#上衣*材质#牛仔布*颜色#白色*风格#简约*图案#刺绣*衣样式#外套*衣款式#破洞
Label: 简约而不简单的牛仔外套,白色的衣身十分百搭。衣身多处有做旧破洞设计,打破单调乏味,增加一丝造型看点。衣身后背处有趣味刺绣装饰,丰富层次感,彰显别样时尚。
Output[微调前]: 这件上衣的材质是牛仔布,颜色是白色,风格是简约,图案是刺绣,衣样式是外套,衣款式是破洞。
Output[微调后]: 这是一款简约的牛仔外套,破洞设计,将牛仔布破洞,带来一种随意与个性。破洞的牛仔外套,展现出时尚气息,带来一种休闲感。同时,刺绣图案,让整件外套更加立体。
示例2
我們的excel數據集

excel文件轉為json形式


訓練文件--train_file

驗證文件--validation_file

# Ptuning訓練參數
--train_file ..\answers.json
--validation_file ..\dev.json
--prompt_column prompt
--response_column response
--overwrite_cache
--model_name_or_path ..\model
--output_dir ..\output
--overwrite_output_dir
以上為路徑設定
---
--max_source_length 256(原128改為256)
--max_target_length 256(原128改為256)
--per_device_train_batch_size 1(原為6改為1)
--per_device_eval_batch_size 1 (原為6改為1)
--gradient_accumulation_steps 16
--predict_with_generate
--max_steps 500
--logging_steps 10
--save_steps 50
--learning_rate 2e-2(原為2e-4)
--pre_seq_len 128 (這裡設定多少下面的也要一樣)
--quantization_bit 4(int4量化)

# 訓練完成
只需把訓練好的pytorch_model.bin抓回chatglm

pytorch_model.bin放回chatglm目錄下

如果需要加载的是新 Checkpoint(只包含 PrefixEncoder 参数)
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True)
config = AutoConfig.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True, pre_seq_len=128)
model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4", config=config, trust_remote_code=True).half().cuda()
prefix_state_dict = torch.load("D:/4a9g0046/ChatGLM-6B-main/pytorch_model.bin")
new_prefix_state_dict = {}
for k, v in prefix_state_dict.items():
if k.startswith("transformer.prefix_encoder."):
new_prefix_state_dict[k[len("transformer.prefix_encoder."):]] = v
model.transformer.prefix_encoder.load_state_dict(new_prefix_state_dict)

注意你可能需要將 pre_seq_len 改成你訓練時的實際值
# 訓練畫面

# 實際回答畫面
問題如下:
1.德國的首都
2.天氣如何
3.你的名字
4.美國首都

這是一張文本轉音檔的圖片,不使用對話紀錄是因為每回答一個問題就會看不到上一個。
<style>
.blue {
color: red;
}
</style>
{%hackmd sMV2zv-CTsuIqnpb0hZLmA %}