chatglm3-6b微调实战

chatglm3-6b-sft

chatglm3-6b, chatglm-3b微调/LORA/推理/单机多卡/deepspeed/支持多轮对话

源码:https://github.com/yongzhuo/ChatGLM3-SFT

踩坑(截至20231031)

0.官方prompt, 支持chatml(system), system可以改,但是要注意<|user|>最好放第一位, 如果换成<|assistant|>直接预测、微调会变得很差

<|system|>
You are ChatGLM3, a large language model trained by Zhipu.AI. Follow the user's instructions carefully. Respond using markdown.
<|user|>
Hello
<|assistant|>
Hello, I'm ChatGLM3. What can I assist you today?
  1. torch>=2.0, 否则微调会报很多错误(单纯推理可以用低版本);

  2. tokenizer.encode输出为 [gMASK, sop, 真实文本token]

     "[MASK]": 64789,
     "[gMASK]": 64790,
     "[sMASK]": 64791,
     "sop": 64792,
     "eop": 64793,
     "<|system|>": 64794,
     "<|user|>": 64795,
     "<|assistant|>": 64796,
     "<|observation|>": 64797
    
  3. modeling_chatglm.py自带get_masks()的代码full_attention_mask -= padding_mask.unsqueeze(-1) - 1改为
    full_attention_mask = full_attention_mask.long() - padding_mask.unsqueeze(-1).long() - 1

  4. 不支持gradient_checkpointing, 修复的话需要modeling_chatglm.py新增get_input_embeddings, set_input_embeddings;

  5. 官方prompt, chatglm3支持工具调用

    <|system|>
    Answer the following questions as best as you can. You have access to the following tools:
    [
    {
    "name": "get_current_weather",
    "description": "Get the current weather in a given location",
    "parameters": {
    "type": "object",
    "properties": {
    "location": {
    "type": "string",
    "description": "The city and state, e.g. San Francisco, CA",
    },
    "unit": {"type": "string"},
    },
    "required": ["location"],
    },
    }
    ]
    <|user|>
    今天北京的天气怎么样?
    <|assistant|>
    好的,让我们来查看今天的天气
    <|assistant|>get_current_weather
    '''python
    tool_call(location="beijing", unit="celsius")
    '''
    <|observation|>
    {"temperature": 22}
    <|assistant|>
    根据查询结果,今天北京的气温为 22 摄氏度。

  6. 官方prompt构建chat实现代码:

python 复制代码
def build_chat_input(self, query, history=None, role="user"):
    if history is None:
        history = []
    input_ids = []
    for item in history:
        content = item["content"]
        if item["role"] == "system" and "tools" in item:
            content = content + "\n" + json.dumps(item["tools"], indent=4, ensure_ascii=False)
        input_ids.extend(self.build_single_message(item["role"], item.get("metadata", ""), content))
    input_ids.extend(self.build_single_message(role, "", query))
    input_ids.extend([self.get_command("<|assistant|>")])
    return self.batch_encode_plus([input_ids], return_tensors="pt", is_split_into_words=True)

环境配置

shell 复制代码
transformers==4.27.1
torch>=2.0
sentencepiece
cpm_kernels
mdtex2html
accelerate
protobuf
gradio

微调样例

shell 复制代码
地址: chatglm3_6b/ft_chatglm3

配置: chatglm3_6b/ft_chatglm3/config.py
训练: python train.py
接口: python post_api.py

实验日志

微调日志(metalwoz数据集, 英文主题多轮对话)

推理样例(LoRA, R=8)

回答得比较短了, 也确实遵循了主题

参考/感谢

免责申明

本项目相关资源仅供学术研究之用,使用涉及第三方代码的部分时,请严格遵循相应的开源协议。模型生成的内容受模型计算、随机性和量化精度损失等因素影响,本项目不对其准确性作出保证。对于模型输出的任何内容,本项目不承担任何法律责任,亦不对因使用相关资源和输出结果而可能产生的任何损失承担责任。

型输出的任何内容,本项目不承担任何法律责任,亦不对因使用相关资源和输出结果而可能产生的任何损失承担责任。

相关推荐
m0_6090004241 分钟前
向日葵好用吗?4款稳定的远程控制软件推荐。
运维·服务器·网络·人工智能·远程工作
开MINI的工科男2 小时前
深蓝学院-- 量产自动驾驶中的规划控制算法 小鹏
人工智能·机器学习·自动驾驶
AI大模型知识分享3 小时前
Prompt最佳实践|如何用参考文本让ChatGPT答案更精准?
人工智能·深度学习·机器学习·chatgpt·prompt·gpt-3
张人玉5 小时前
人工智能——猴子摘香蕉问题
人工智能
草莓屁屁我不吃5 小时前
Siri因ChatGPT-4o升级:我们的个人信息还安全吗?
人工智能·安全·chatgpt·chatgpt-4o
小言从不摸鱼5 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
AI科研视界5 小时前
ChatGPT+2:修订初始AI安全性和超级智能假设
人工智能·chatgpt
霍格沃兹测试开发学社测试人社区5 小时前
人工智能 | 基于ChatGPT开发人工智能服务平台
软件测试·人工智能·测试开发·chatgpt
小R资源6 小时前
3款免费的GPT类工具
人工智能·gpt·chatgpt·ai作画·ai模型·国内免费
artificiali8 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python