【深度学习】大模型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

相关推荐
智能汽车人3 分钟前
行业分析---领跑汽车2025第二季度财报
人工智能·microsoft
先做个垃圾出来………13 分钟前
迁移学习(Transfer Learning)
人工智能·机器学习·迁移学习
许泽宇的技术分享15 分钟前
ReAct Agent:让AI像人类一样思考与行动的革命性框架
人工智能·agent·react
eBest数字化转型方案1 小时前
2025年快消品行业渠道数字化营销系统全景透视与选型策略
人工智能
kkcodeer1 小时前
大模型Prompt原理、编写原则与技巧以及衡量方法
人工智能·prompt·ai大模型
DevSecOps选型指南2 小时前
SBOM风险预警 | NPM前端框架 javaxscript 遭受投毒窃取浏览器cookie
前端·人工智能·前端框架·npm·软件供应链安全厂商·软件供应链安全工具
rocksun2 小时前
MCP利用流式HTTP实现实时AI工具交互
人工智能·mcp
xiaok2 小时前
docker network create langbot-network这条命令在dify输入还是在langbot中输入
人工智能
It_张2 小时前
LLM(大语言模型)的工作原理 图文讲解
人工智能·语言模型·自然语言处理