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

相关推荐
gorgor在码农几秒前
卷积神经网络CNN
人工智能·神经网络·cnn
城电科技1 小时前
城电科技 | 探索光伏景观廊道:适用于零碳园区/公园/景区/校园/乡村长廊建设
大数据·人工智能·科技
liuyunshengsir1 小时前
LangChain使用大语言模型构建强大的应用程序
人工智能·语言模型·langchain
点我头像干啥1 小时前
第1节:计算机视觉发展简史
人工智能·深度学习·神经网络·计算机视觉
深度学习lover1 小时前
<数据集>苹果识别数据集<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果识别
EasyGBS1 小时前
国标GB28181协议EasyCVR视频融合平台:5G时代远程监控赋能通信基站安全管理
大数据·网络·人工智能·安全·音视频
新加坡内哥谈技术2 小时前
微软庆祝它成立整整50周年
人工智能
_一条咸鱼_2 小时前
深入剖析 AI 大模型的反向传播原理
人工智能·深度学习·机器学习
(initial)2 小时前
超越简单检索:探索知识图谱与大型语言模型的协同进化之路
人工智能·语言模型·知识图谱