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

相关推荐
上海锝秉工控4 分钟前
「光域」系列激光测距传感器:以光为尺,重构空间认知边界
人工智能·重构
Tech Synapse7 分钟前
Unity ML-Agents实战指南:构建多技能游戏AI训练系统
人工智能·游戏·unity
神码小Z23 分钟前
Midjourney-V7:支持参考图片头像或背景生成新保真图
人工智能·ai绘画
Francek Chen1 小时前
【现代深度学习技术】注意力机制05:多头注意力
人工智能·pytorch·深度学习·神经网络·注意力机制
犬余1 小时前
模型上下文协议(MCP):AI的“万能插座”
人工智能·mcp
芯盾时代2 小时前
数据出境的安全合规思考
大数据·人工智能·安全·网络安全·信息与通信
Sylvan Ding2 小时前
PyTorch Lightning实战 - 训练 MNIST 数据集
人工智能·pytorch·python·lightning
大白技术控2 小时前
浙江大学 deepseek 公开课 第三季 第3期 - 陈喜群 教授 (附PPT下载) by 突破信息差
人工智能·互联网·deepseek·deepseek公开课·浙大deepseek公开课课件·deepseek公开课ppt·人工智能大模型
Silence4Allen2 小时前
大模型微调指南之 LLaMA-Factory 篇:一键启动LLaMA系列模型高效微调
人工智能·大模型·微调·llama-factory