当前位置:AIGC资讯 > AIGC > 正文

LLaMA-Factory 8卡4090 deepspeed zero3 微调Qwen14B-chat

环境安装

推荐使用docker,Ubuntu20.04
https://www.modelscope.cn/docs/%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85

docker pull registry.cn-beijing.aliyuncs.com/modelscope-repo/modelscope:ubuntu22.04-cuda12.1.0-py310-torch2.1.2-tf2.14.0-1.12.0

下载模型

在modelscope主页,找到模型
https://modelscope.cn/models/qwen/Qwen-14B-Chat/summary

可以使用如下脚本

import os
from modelscope import snapshot_download

# cache_dir 指定你的保存模型的路径
model_dir = snapshot_download('qwen/Qwen-14B-Chat',cache_dir="/workspace/models/AI-ModelScope")

微调

使用LLaMA-Factory,
下载下面仓库的代码,
https://github.com/hiyouga/LLaMA-Factory

在代码目录,新建一个脚本 run_train_bash.sh

CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 accelerate launch  src/train_bash.py \
    --deepspeed ds_config_zero3.json \
    --stage sft \
    --do_train True \
    --model_name_or_path /workspace/models/AI-ModelScope/qwen/Qwen-14B-Chat/ \
    --finetuning_type lora \
    --template qwen \
    --dataset_dir data \
    --dataset lhs_merged_data \
    --cutoff_len 1024 \
    --learning_rate 5e-04 \
    --num_train_epochs 3 \
    --max_samples 100000 \
    --per_device_train_batch_size 4 \
    --gradient_accumulation_steps 4 \
    --lr_scheduler_type cosine \
    --max_grad_norm 1.0 \
    --logging_steps 5 \
    --save_steps 100 \
    --warmup_steps 0 \
    --neftune_noise_alpha 0 \
    --lora_rank 8 \
    --lora_dropout 0.1 \
    --lora_target  c_attn \
    --output_dir output/qwen_14b_ds/train_2024_02_27 \
    --bf16 True \
    --plot_loss True

另外,还需要新建一个deepspeed的配置文件,这里使用qwen官方给的例子,新建一个ds_config_zero3.json
https://github.com/QwenLM/Qwen/blob/main/finetune/ds_config_zero3.json

{
    "fp16": {
        "enabled": "auto",
        "loss_scale": 0,
        "loss_scale_window": 1000,
        "initial_scale_power": 16,
        "hysteresis": 2,
        "min_loss_scale": 1
    },
    "bf16": {
        "enabled": "auto"
    },
    "optimizer": {
        "type": "AdamW",
        "params": {
            "lr": "auto",
            "betas": "auto",
            "eps": "auto",
            "weight_decay": "auto"
        }
    },

    "scheduler": {
        "type": "WarmupLR",
        "params": {
            "warmup_min_lr": "auto",
            "warmup_max_lr": "auto",
            "warmup_num_steps": "auto"
        }
    },

    "zero_optimization": {
        "stage": 3,
        "offload_optimizer": {
            "device": "none",
            "pin_memory": true
        },
        "offload_param": {
            "device": "none",
            "pin_memory": true
        },
        "overlap_comm": true,
        "contiguous_gradients": true,
        "sub_group_size": 1e9,
        "reduce_bucket_size": "auto",
        "stage3_prefetch_bucket_size": "auto",
        "stage3_param_persistence_threshold": "auto",
        "stage3_max_live_parameters": 1e9,
        "stage3_max_reuse_distance": 1e9,
        "stage3_gather_16bit_weights_on_model_save": true
    },

    "gradient_accumulation_steps": "auto",
    "gradient_clipping": "auto",
    "steps_per_print": 100,
    "train_batch_size": "auto",
    "train_micro_batch_size_per_gpu": "auto",
    "wall_clock_breakdown": false
}

具体的代码路径如下:

ds_config_zero3.json

运行

直接在终端输入即可,缺什么包,再安装对应的包就可以了。

bash run_train_bash.sh

合并权重

训练完成后,按照LLaMA-Factory readme中提到的合并权重命令进行合并即可,自行修改其中的路径参数
https://github.com/hiyouga/LLaMA-Factory?tab=readme-ov-file#merge-lora-weights-and-export-model

python src/export_model.py \
    --model_name_or_path path_to_llama_model \
    --adapter_name_or_path path_to_checkpoint \
    --template default \
    --finetuning_type lora \
    --export_dir path_to_export \
    --export_size 2 \
    --export_legacy_format False

最后,可以使用加载模型测试微调结果。

显存占用:

更新时间 2024-03-17