InternLM / xtuner

An efficient, flexible and full-featured toolkit for fine-tuning LLM (InternLM2, Llama3, Phi3, Qwen, Mistral, ...)
https://xtuner.readthedocs.io/zh-cn/latest/
Apache License 2.0
3.64k stars 297 forks source link

复现官方教程出现 grad_norm:nan #507

Open kaisersama112 opened 5 months ago

kaisersama112 commented 5 months ago

根据官方教程进行复现 进行微调时出现grad_norm:nan 参数配置如下:

Model

pretrained_model_name_or_path = 'internlm/internlm2-chat-7b' use_varlen_attn = False

Data

data_path = 'data' prompt_template = PROMPT_TEMPLATE.internlm2_chat max_length = 2048 pack_to_max_length = True

Scheduler & Optimizer

batch_size = 1 # per_device accumulative_counts = 16 dataloader_num_workers = 0 max_epochs = 3 optim_type = AdamW lr = 2e-4 betas = (0.9, 0.999) weight_decay = 0 max_norm = 1 # grad clip warmup_ratio = 0.03 出现错误:

image

随后我对batchsize及其lr进行调整:

Model

pretrained_model_name_or_path = 'internlm/internlm2-chat-7b' use_varlen_attn = False

Data

data_path = 'data' prompt_template = PROMPT_TEMPLATE.internlm2_chat max_length = 2048 pack_to_max_length = True

Scheduler & Optimizer

batch_size = 2 # per_device accumulative_counts = 16 dataloader_num_workers = 0 max_epochs = 3 optim_type = AdamW lr = 2e-7 betas = (0.9, 0.999) weight_decay = 0 max_norm = 1 # grad clip warmup_ratio = 0.03

错误依旧存在: image

LZHgrla commented 5 months ago

尝试多训几百个iter? 在某些模型、数据上确实会出现一段时间的grad_norm NaN 的情况,但一般在几百个iter后会正常。

kaisersama112 commented 5 months ago

目前我将模型转移到服务器进行微调 

  1. 本地微调模型为internlm2-chat-7b 我通过更改学习率为0.1 经过接近20个iter 还是会出现相同情况  
  2. 同时目前服务器采用的与本地相同的数据集 模型改为internlm-chat-7b 目前并没有出现梯度爆炸问题,
  3. 后续我会在服务器也对internlm2-chat-7b进行微调复现 看下问题是否还存在

尝试多训几百个iter? 在某些模型、数据上确实会出现一段时间的grad_norm NaN 的情况,但一般在几百个iter后会正常。

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

kaisersama112 commented 5 months ago

对了 尝试多训几百个iter 我觉得并无多大意义了 本地loss 已经开始趋近于无限大

LZHgrla commented 5 months ago

@kaisersama112 可以训练一下开源数据集,比如alpaca ,查看是否会出现类似的情况。如果开源数据集是正常的,可能是构造的数据集有一些问题。

https://github.com/InternLM/xtuner/blob/main/xtuner/configs/internlm/internlm2_chat_7b/internlm2_chat_7b_qlora_alpaca_e3.py

xmy0916 commented 4 months ago

请教下max_norm = 1 # grad clip这个不会生效吗? @LZHgrla 这个梯度裁剪不会把大于1的梯度控制在1内么

202030481266 commented 3 months ago

一开始确实会有nan,但是后面会有的。 image