LLaMA-Factory是一个相当优秀的微调工具。这里提供一个dockerfile和一个train脚本,用于多卡微调,供大家参考。
Dockerfile
FROM nvidia/cuda:12.1.0-cudnn8-devel-ubuntu22.04# python3RUN apt-get update && apt-get install -y python3.10 python3-pip# torchCOPY torch-2.2.0+cu121-cp310-cp310-linux_x86_64.whl torch-2.2.0+cu121-cp310-cp310-linux_x86_64.whlRUN pip3 install torch-2.2.0+cu121-cp310-cp310-linux_x86_64.whl# llama factory requirementsRUN pip3 install transformers==4.37.2 datasets==2.16.1 accelerate==0.25.0 peft==0.7.1 trl==0.7.10 gradio==3.50.2 \ deepspeed modelscope ipython scipy einops sentencepiece protobuf jieba rouge-chinese nltk sse-starlette matplotlib \ --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple# unslothRUN apt-get install -y gitRUN pip install --upgrade pipRUN pip install triton --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simpleRUN pip install "unsloth[cu121_ampere_torch220] @ git+https://github.com/unslothai/unsloth.git"
train.sh
docker run \ -it \ --rm \ --name llm \ --network=host \ --shm-size 32G \ --gpus all \ -v /home/[user_name]/.cache/modelscope/hub/:/root/.cache/modelscope/hub/ \ -v /home/[user_name]/LLaMA-Factory/:/LLaMA-Factory/ \ -v /home/[user_name]/.cache/huggingface/accelerate/default_config.yaml:/root/.cache/huggingface/accelerate/default_config.yaml \ -w /LLaMA-Factory \ -e USE_MODELSCOPE_HUB=1 \ llm:v1.1 \ accelerate launch src/train_bash.py \ --stage sft \ --do_train True \ --model_name_or_path ZhipuAI/chatglm3-6b \ --finetuning_type lora \ --use_unsloth True \ --template chatglm3 \ --dataset_dir data \ --dataset alpaca_gpt4_zh \ --cutoff_len 512 \ --learning_rate 5e-05 \ --num_train_epochs 2.0 \ --max_samples 8000 \ --per_device_train_batch_size 1 \ --gradient_accumulation_steps 2 \ --lr_scheduler_type cosine \ --max_grad_norm 1.0 \ --logging_steps 5 \ --save_steps 1000 \ --warmup_steps 0 \ --lora_rank 8 \ --lora_dropout 0.1 \ --lora_target query_key_value \ --output_dir saves/ChatGLM3-6B-Chat/lora/train_20240212 \ --fp16 True \ --plot_loss True
注意事项:
–shm-size 32G --gpus all 这两个参数是必要的–use_unsloth True 可以调用unsloth实现加速需要保证–gradient_accumulation_steps 2在deepspeed配置中的一致性default_config.yaml
compute_environment: LOCAL_MACHINEdebug: false# distributed_type: MULTI_GPUdeepspeed_config: deepspeed_multinode_launcher: standard gradient_accumulation_steps: 2 offload_optimizer_device: none offload_param_device: none zero3_init_flag: false zero3_save_16bit_model: false zero_stage: 2distributed_type: DEEPSPEED downcast_bf16: 'no'gpu_ids: allmachine_rank: 0main_training_function: mainmixed_precision: bf16num_machines: 1num_processes: 2rdzv_backend: staticsame_network: truetpu_env: []tpu_use_cluster: falsetpu_use_sudo: falseuse_cpu: false
感谢以下两篇博客:
单卡 3 小时训练专属大模型 Agent:基于 LLaMA Factory 实战
Accelerate 0.24.0文档 二:DeepSpeed集成