天天看點

針對基于清華大學ChatGLM-6B采用LoRA技術微調開源項目代碼解讀

作者:大狗在海裡

聲明如下:

1.沒有開源就沒有任何行業的進步,那些開源項目的作者值得被每個人尊重

2.本文沒有貶低作者的意思,作為布道者應該盡可能減少學習者的誤區操作成本

在這個“開源為王,資料為王,模型為王”的大時代。持續的學習能力才不會有35歲的危機。

看到一個ChatGLM-6B采用LoRA的開源項目幫你快速在ChatGLM-6B上實作自己私有對話機器人。下面學習解讀下:

1.資料準備階段(資料才是最重要的)

cover_alpaca2jsonl.py

直接将斯坦福大羊駝的資料進行格式轉化換成自己的格式,核心代碼功能:instruction-->Instruction;input-->Input;+Answer:output-->target

def format_example(example: dict) -> dict:

context = f"Instruction: {example['instruction']}\n"

if example.get("input"):

context += f"Input: {example['input']}\n"

context += "Answer: "

target = example["output"]

return {"context": context, "target": target}

斯坦福大羊駝的資料格式樣例:

針對基于清華大學ChatGLM-6B采用LoRA技術微調開源項目代碼解讀

{

"instruction": "Give three tips for staying healthy.",

"input": "",

"output": "1.Eat a balanced diet and make sure to include plenty of fruits and vegetables. \n2. Exercise regularly to keep your body active and strong. \n3. Get enough sleep and maintain a consist

ent sleep schedule."

}

這個cover_alpaca2jsonl.py轉換後的資料格式樣例:請注意:一定要看源代碼!!!!,不知道是作者的疏忽還是其他原因,git代碼生成資料jsonl上是“”“Response:”“”,代碼是“”“Answer: ”“”

針對基于清華大學ChatGLM-6B采用LoRA技術微調開源項目代碼解讀

{

"context":"Instruction: Give three tips for staying healthy.\nAnswer: ",

"target":"1.Eat a balanced diet and make sure to include plenty of fruits and vegetables. \n2. Exercise regularly to keep your body active and strong. \n3. Get enough sleep and maintain a consistent sleep schedule."

}

2.tokenize_dataset_rows.py

#備注文檔寫法錯誤

如果是代碼裡的實作文檔使用應該是這麼寫:

--skip_overlength true/false

針對基于清華大學ChatGLM-6B采用LoRA技術微調開源項目代碼解讀

或者代碼改成這樣才ok

parser.add_argument("--skip_overlength", type=bool,action="store_true", default=False)

針對基于清華大學ChatGLM-6B采用LoRA技術微調開源項目代碼解讀

3.微調finetune.py

請根據實際硬體跟代碼要求注意選擇不同的資料類型,例如fp32,fp16,half,int8等,需要根據實際情況調整

針對基于清華大學ChatGLM-6B采用LoRA技術微調開源項目代碼解讀

假設出現RuntimeError: expected scalar type Half but found Float,直接将--fp16去掉即可

python finetune.py \

--dataset_path data/alpaca \

--lora_rank 8 \

--per_device_train_batch_size 6 \

--gradient_accumulation_steps 1 \

--max_steps 52000 \

--save_steps 1000 \

--save_total_limit 2 \

--learning_rate 1e-4 \

--fp16 \

--remove_unused_columns false \

--logging_steps 50 \

--output_dir output

最後趕緊在單機單卡,一機多卡,多機多卡上訓練自己的大模型吧。

項目git位址:

https://github.com/mymusise/ChatGLM-Tuning.git

繼續閱讀