【深度学习】大模型GLM-4-9B Chat ,微调与部署

下载好东西:

启动容器环境:

bash 复制代码
docker run -it --gpus all --net host  --shm-size=8g -v /ssd/xiedong/glm-4-9b-xd:/ssd/xiedong/glm-4-9b-xd  kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-yolov8train  bash

pip install typer tiktoken numpy==1.25 -i https://pypi.tuna.tsinghua.edu.cn/simple

安装微调的环境:

bash 复制代码
cd /ssd/xiedong/glm-4-9b-xd/GLM-4/finetune_demo/

pip install -r requirements.txt   -i https://pypi.tuna.tsinghua.edu.cn/simple

下载数据集ccfbdci.jsonl到同级目录下。
https://huggingface.co/datasets/qgyd2021/chinese_ner_sft/tree/main/data

将数据集处理为glm4的格式:

bash 复制代码
import json
import random

def convert_jsonl(input_file, train_output_file, test_output_file, split_ratio=0.8):
    system_message = {"role": "system", "content": "你是一个命名实体提取的专家。"}
    all_data = []

    with open(input_file, 'r', encoding='utf-8') as infile:
        for line in infile:
            data = json.loads(line)
            user_content = data["text"]
            entities = data["entities"]

            if entities:
                entity_texts = [entity["entity_text"] for entity in entities]
                assistant_content = ", ".join(entity_texts)
            else:
                assistant_content = "无"

            conversation = {
                "messages": [
                    system_message,
                    {"role": "user", "content": user_content},
                    {"role": "assistant", "content": assistant_content}
                ]
            }

            all_data.append(conversation)

    # Shuffle the data for random splitting
    random.shuffle(all_data)

    # Calculate split index
    split_index = int(len(all_data) * split_ratio)

    # Split the data into training and testing sets
    train_data = all_data[:split_index]
    test_data = all_data[split_index:]

    # Write training data to file
    with open(train_output_file, 'w', encoding='utf-8') as train_outfile:
        for item in train_data:
            json.dump(item, train_outfile, ensure_ascii=False)
            train_outfile.write('\n')

    # Write testing data to file
    with open(test_output_file, 'w', encoding='utf-8') as test_outfile:
        for item in test_data:
            json.dump(item, test_outfile, ensure_ascii=False)
            test_outfile.write('\n')

input_file = 'ccfbdci.jsonl'
train_output_file = 'ccfbdci_train.jsonl'
test_output_file = 'ccfbdci_test.jsonl'
convert_jsonl(input_file, train_output_file, test_output_file)

配置文件

微调的配置文件位于config目录中,包括以下文件:

  • ds_zero_2.json / ds_zero_3.json:DeepSpeed配置文件。
  • lora.yaml / ptuning_v2.yaml / sft.yaml:不同模式模型的配置文件,包括模型参数、优化器参数、训练参数等。

一些重要参数解释如下:

data_config部分

  • train_file:训练数据集的文件路径。
  • val_file:验证数据集的文件路径。
  • test_file:测试数据集的文件路径。
  • num_proc:加载数据时使用的进程数量。
  • max_input_length:输入序列的最大长度。
  • max_output_length:输出序列的最大长度。

training_args部分

  • output_dir:保存模型和其他输出的目录。
  • max_steps:最大训练步数。
  • per_device_train_batch_size:每个设备(如GPU)的训练批次大小。
  • dataloader_num_workers:加载数据时使用的工作线程数量。
  • remove_unused_columns:是否移除数据中未使用的列。
  • save_strategy:模型保存策略(例如,每多少步保存一次)。
  • save_steps:每多少步保存一次模型。
  • log_level:日志级别(例如,info)。
  • logging_strategy:日志记录策略。
  • logging_steps:每多少步记录一次日志。
  • per_device_eval_batch_size:每个设备的评估批次大小。
  • evaluation_strategy:评估策略(例如,每多少步进行一次评估)。
  • eval_steps:每多少步评估一次。
  • predict_with_generate:是否使用生成模式进行预测。

generation_config部分

  • max_new_tokens:生成的新标记的最大数量。

peft_config部分

  • peft_type:使用的参数微调类型(支持LORA和PREFIX_TUNING)。
  • task_type:任务类型,这里是因果语言模型(不要更改)。
LoRA参数
  • r:LoRA的秩。
  • lora_alpha:LoRA的缩放因子。
  • lora_dropout:LoRA层中使用的dropout概率。
P-TuningV2参数
  • num_virtual_tokens:虚拟标记的数量。
  • num_attention_heads:P-TuningV2的注意力头数量(不要更改)。
  • token_dim:P-TuningV2的标记维度(不要更改)。
bash 复制代码
CUDA_VISIBLE_DEVICES=2,3 OMP_NUM_THREADS=1 torchrun --standalone --nnodes=1 --nproc_per_node=2  finetune.py  /ssd/xiedong/glm-4-9b-xd/GLM-4/finetune_demo/ /ssd/xiedong/glm-4-9b-xd/glm-4-9b-chat configs/ptuning_v2.yaml # For Chat Fine-tune

可以训练,但是多张卡保存模型报错了,重启一个镜像试试。

docker commit b512e777882f kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-glm4train

bash 复制代码
docker run -it --gpus all --net host  --shm-size=8g -v /ssd/xiedong/glm-4-9b-xd:/ssd/xiedong/glm-4-9b-xd  kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-glm4train  bash
bash 复制代码
cd /ssd/xiedong/glm-4-9b-xd/GLM-4/finetune_demo/

CUDA_VISIBLE_DEVICES=2,3 OMP_NUM_THREADS=1 torchrun --standalone --nnodes=1 --nproc_per_node=2  finetune.py  /ssd/xiedong/glm-4-9b-xd/GLM-4/finetune_demo/ /ssd/xiedong/glm-4-9b-xd/glm-4-9b-chat configs/ptuning_v2.yaml # For Chat Fine-tune
bash 复制代码
CUDA_VISIBLE_DEVICES=2 python finetune.py  /ssd/xiedong/glm-4-9b-xd/GLM-4/finetune_demo/ /ssd/xiedong/glm-4-9b-xd/glm-4-9b-chat configs/ptuning_v2.yaml # For Chat Fine-tune

6,还是报错,换个项目的训练方法:

https://github.com/hiyouga/LLaMA-Factory/blob/main/README_zh.md

相关推荐
美狐美颜sdk3 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
DeepSeek-大模型系统教程3 小时前
推荐 7 个本周 yyds 的 GitHub 项目。
人工智能·ai·语言模型·大模型·github·ai大模型·大模型学习
有Li3 小时前
通过具有一致性嵌入的大语言模型实现端到端乳腺癌放射治疗计划制定|文献速递-最新论文分享
论文阅读·深度学习·分类·医学生
郭庆汝3 小时前
pytorch、torchvision与python版本对应关系
人工智能·pytorch·python
小雷FansUnion5 小时前
深入理解MCP架构:智能服务编排、上下文管理与动态路由实战
人工智能·架构·大模型·mcp
资讯分享周5 小时前
扣子空间PPT生产力升级:AI智能生成与多模态创作新时代
人工智能·powerpoint
叶子爱分享6 小时前
计算机视觉与图像处理的关系
图像处理·人工智能·计算机视觉
鱼摆摆拜拜6 小时前
第 3 章:神经网络如何学习
人工智能·神经网络·学习
一只鹿鹿鹿6 小时前
信息化项目验收,软件工程评审和检查表单
大数据·人工智能·后端·智慧城市·软件工程
张较瘦_7 小时前
[论文阅读] 人工智能 | 深度学习系统崩溃恢复新方案:DaiFu框架的原位修复技术
论文阅读·人工智能·深度学习