安装LLaMA-Factory微调chatglm3,修改自我认知

本文详细介绍了如何通过Git克隆LLaMA-Factory项目,使用Conda环境管理Python3.10版本,设置CUDA和命令行参数进行单卡训练。涉及了模型训练、模型合并和使用DeepSpeed进行分布式训练,以及可能出现的问题及解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装git clone https://github.com/hiyouga/LLaMA-Factory.git
conda create -n llama_factory python=3.10
conda activate llama_factory
cd LLaMA-Factory
pip install -r requirements.txt

之后运行

单卡训练,

CUDA_VISIBLE_DEVICES=0 python src/train_web.py,按如下配置

demo_tran.sh

CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \
    --stage sft \
    --model_name_or_path /data/models/llm/chatglm3-lora/ \
    --do_train \
    --overwrite_output_dir \
    --dataset self_cognition \
    --template chatglm3 \
    --finetuning_type lora \
    --lora_target query_key_value \
    --output_dir export_chatglm3 \
    --overwrite_cache \
    --per_device_train_batch_size 4 \
    --gradient_accumulation_steps 4 \
    --lr_scheduler_type cosine \
    --logging_steps 10 \
    --save_steps 1000 \
    --learning_rate 1e-3 \
    --num_train_epochs 10.0 \
    --plot_loss \
    --fp16

 export_model.sh

python src/export_model.py \
    --model_name_or_path /data/models/llm/chatglm3-lora/ \
    --template chatglm3 \
    --finetuning_type lora \
    --checkpoint_dir /data/projects/LLaMA-Factory/export_chatglm3 \
    --export_dir lora_merge_chatglm3

 cli_demo.sh

python src/cli_demo.py \
    --model_name_or_path /data/models/llm/chatglm3-lora/ \
    --template default \
    --finetuning_type lora 

注意合并模型的时候,最后复制chatglm3的tokenizer.model和tokenizer_config.json到合并后模型覆盖之后,要修改

 不覆盖会有这个错误,

 Use DeepSpeed方法

deepspeed --num_gpus 3 --master_port=9901 src/train_bash.py \
    --deepspeed ds_config.json \
    --stage sft \
    --model_name_or_path /media/cys/65F33762C14D581B/chatglm2-6b \
    --do_train True \
    --finetuning_type lora \
    --template chatglm2 \
    --flash_attn False \
    --shift_attn False \
    --dataset_dir data \
    --dataset self_cognition,sharegpt_zh \
    --cutoff_len 1024 \
    --learning_rate 0.001 \
    --num_train_epochs 10.0 \
    --max_samples 1000 \
    --per_device_train_batch_size 4 \
    --gradient_accumulation_steps 4 \
    --lr_scheduler_type cosine \
    --max_grad_norm 1.0 \
    --logging_steps 10 \
    --save_steps 1000 \
    --warmup_steps 0 \
    --neft_alpha 0 \
    --train_on_prompt False \
    --upcast_layernorm False \
    --lora_rank 8 \
    --lora_dropout 0.1 \
    --lora_target query_key_value \
    --resume_lora_training True \
    --output_dir saves/ChatGLM2-6B-Chat/lora/train_2023-12-12-23-26-49 \
    --fp16 True \
    --plot_loss True

 ds_config.json的格式下面的:

{
  "train_batch_size": "auto",
  "train_micro_batch_size_per_gpu": "auto",
  "gradient_accumulation_steps": "auto",
  "gradient_clipping": "auto",
  "zero_allow_untested_optimizer": true,
  "fp16": {
    "enabled": "auto",
    "loss_scale": 0,
    "initial_scale_power": 16,
    "loss_scale_window": 1000,
    "hysteresis": 2,
    "min_loss_scale": 1
  },  
  "zero_optimization": {
    "stage": 2,
    "allgather_partitions": true,
    "allgather_bucket_size": 5e8,
    "reduce_scatter": true,
    "reduce_bucket_size": 5e8,
    "overlap_comm": false,
    "contiguous_gradients": true
  }
}

 跑成功的效果图:

如果出现下面 这个问题,

[E ProcessGroupNCCL.cpp:916] [Rank 3] NCCL watchdog thread terminated with exception: CUDA error: the launch timed out and was terminated CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1. Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.

可能原因是显卡坏了或者显卡不是同一个型号!

### 解决 LLaMA-Factory 微调 ChatGLM3 模型时的训练错误 #### 错误描述 在使用 LLaMA-FactoryChatGLM3 进行微调的过程中遇到了 `Segmentation fault (core dumped)` 的错误信息[^3]。 #### 可能原因分析 `Segmentation fault (core dumped)` 是一种常见的运行时错误,通常表示程序尝试访问未分配给它的内存区域。对于深度学习模型来说,这可能是由于以下几个原因之一: - **资源不足**:特别是 GPU 或 CPU 内存不足以支持当前任务的需求。 - **依赖库冲突**:某些特定版本之间的不兼容可能导致崩溃。 - **数据集处理不当**:如果数据预处理过程中存在异常情况也可能引发此问题。 - **代码逻辑缺陷**:可能存在潜在 bug 导致非法操作发生。 #### 解决策略 ##### 调整资源配置 确保有足够的计算资源来支撑模型训练是非常重要的一步。可以考虑增加可用 RAM 和/或显卡 VRAM 容量;如果是分布式设置,则需合理规划节点间通信开销并适当减少批大小(batch size),以降低单次迭代所需占用的空间总量。 ```bash # 修改 Dockerfile 中关于 CUDA_VISIBLE_DEVICES 设置 ENV CUDA_VISIBLE_DEVICES=0,1 # 假设有两块 GPU 卡可选 ``` ##### 更新依赖项至稳定版 有时旧版本软件包之间会存在兼容性问题,因此建议升级所有必要的 Python 库到最新稳定发行版,并保持 PyTorch 版本与所使用的硬件驱动相匹配。 ```bash pip install --upgrade torch torchvision torchaudio transformers datasets accelerate optimum ``` ##### 数据集验证与清理 仔细检查输入的数据文件是否存在损坏或者格式不符的情况,必要时重新生成干净副本用于后续实验。另外还可以简化初始测试阶段采用的小规模样本来排除外部干扰因素的影响。 ##### 优化代码结构 针对具体业务场景定制化改造源码片段,比如调整超参数配置、引入断点续训机制等措施有助于提高整体稳定性。同时注意捕获可能出现的各种异常状况以便及时响应处理。 ```python try: trainer.train() except RuntimeError as e: print(f"Runtime error occurred: {e}") finally: cleanup_resources() # 清理释放不再需要的对象实例 ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MC数据局

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值