自学内容网 自学内容网

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

下载好东西:
在这里插入图片描述

启动容器环境:

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

安装微调的环境:

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的格式:

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的标记维度(不要更改)。
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

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
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

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


原文地址:https://blog.csdn.net/x1131230123/article/details/140611195

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!