ms-swift icon indicating copy to clipboard operation
ms-swift copied to clipboard

MiniCPM-V4微调时出现loss为0

Open dong-168 opened this issue 1 year ago • 20 comments

问题:

{'loss': 0.0, 'token_acc': 0.0, 'grad_norm': nan, 'learning_rate': 0.0, 'memory(GiB)': 9.22, 'train_speed(iter/s)': 0.056143, 'epoch': 0.01, 'global_step/max_steps': '1/312', 'percentage': '0.32%', 'elapsed_time': '17s', 'remaining_time': '1h 30m 3s'} {'loss': 0.0, 'token_acc': 0.0, 'grad_norm': nan, 'learning_rate': 0.0, 'memory(GiB)': 9.22, 'train_speed(iter/s)': 0.074897, 'epoch': 0.05, 'global_step/max_steps': '5/312', 'percentage': '1.60%', 'elapsed_time': '1m 6s', 'remaining_time': '1h 7m 52s'}

解决:请问这个怎么解决呢

dong-168 avatar Sep 04 '25 03:09 dong-168

兄弟,我在复现这个模型的时候,遇到了RuntimeError: shape '[256, 8192, 128]' is invalid for input of size 1048576 请问你遇到了吗?

File "/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py", line 6298, in multi_head_attention_forward [rank8]: k = k.view(k.shape[0], bsz * num_heads, head_dim).transpose(0, 1) rank8]: RuntimeError: shape '[256, 8192, 128]' is invalid for input of size 1048576

ZHUXUHAN avatar Sep 04 '25 05:09 ZHUXUHAN

训练脚本发一下

Jintao-Huang avatar Sep 04 '25 05:09 Jintao-Huang

swift sft
--model_type minicpmv4_5
--model /mnt/pre_trained_models/MiniCPM-V-4_5/
--dataset "/mnt/data"
--train_type full
--weight_decay 0.01
--torch_dtype bfloat16
--attn_impl flash_attn
--use_liger_kernel true
--num_train_epochs 1
--per_device_train_batch_size 1
--per_device_eval_batch_size 1
--learning_rate 1e-6
--gradient_accumulation_steps 1
--eval_steps 500
--save_steps 200
--save_total_limit 2
--logging_steps 5
--max_length 2048
--truncation_strategy right
--output_dir ${DIR}
--warmup_ratio 0.05
--dataloader_num_workers 0
--deepspeed zero2
--freeze_vit true
${RESUME_ARG}
--split_dataset_ratio 0 2>&1 | tee ${LOG_FILE}
@Jintao-Huang

ZHUXUHAN avatar Sep 04 '25 05:09 ZHUXUHAN

兄弟,我在复现这个模型的时候,遇到了RuntimeError: shape '[256, 8192, 128]' is invalid for input of size 1048576 请问你遇到了吗?

File "/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py", line 6298, in multi_head_attention_forward [rank8]: k = k.view(k.shape[0], bsz * num_heads, head_dim).transpose(0, 1) rank8]: RuntimeError: shape '[256, 8192, 128]' is invalid for input of size 1048576


我遇到过类似的问题,我当时是采用MiniCPM-V-4_5模型,而model_type='minicpmv4',就会出现你那种情况,设置类型与模型不匹配。model_type=minicpmv4_5与MiniCPM-V-4_5模型我没试过

dong-168 avatar Sep 04 '25 06:09 dong-168

兄弟,我在复现这个模型的时候,遇到了RuntimeError: shape '[256, 8192, 128]' is invalid for input of size 1048576 请问你遇到了吗? File "/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py", line 6298, in multi_head_attention_forward [rank8]: k = k.view(k.shape[0], bsz * num_heads, head_dim).transpose(0, 1) rank8]: RuntimeError: shape '[256, 8192, 128]' is invalid for input of size 1048576

我遇到过类似的问题,我当时是采用MiniCPM-V-4_5模型,而model_type='minicpmv4',就会出现你那种情况,设置类型与模型不匹配。model_type=minicpmv4_5与MiniCPM-V-4_5模型我没试过


我这里设置model_type='minicpmv4_5',会报错,没有这个枚举类型

dong-168 avatar Sep 04 '25 06:09 dong-168

兄弟,我在复现这个模型的时候,遇到了RuntimeError: shape '[256, 8192, 128]' is invalid for input of size 1048576 请问你遇到了吗? File "/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py", line 6298, in multi_head_attention_forward [rank8]: k = k.view(k.shape[0], bsz * num_heads, head_dim).transpose(0, 1) rank8]: RuntimeError: shape '[256, 8192, 128]' is invalid for input of size 1048576

我遇到过类似的问题,我当时是采用MiniCPM-V-4_5模型,而model_type='minicpmv4',就会出现你那种情况,设置类型与模型不匹配。model_type=minicpmv4_5与MiniCPM-V-4_5模型我没试过

我这里设置model_type='minicpmv4_5',会报错,没有这个枚举类型

emm,那你是直接训吗?可以把训练脚本发一下,我这边是都没出losss就报错了。

ZHUXUHAN avatar Sep 04 '25 06:09 ZHUXUHAN

训练脚本发一下


python代码 | 我找不到minicpm-v4的模型结构定义文件(即包含Loss计算、logits和labels实现的Python代码)

MODEL_TYPE = "minicpmv4"
MODEL_PATH="model/OpenBMB/MiniCPM-V-4"
LOCAL_REPO_PATH = None
VAL_DATASET = None
TRAIN_TYPE = "lora"
lora_rank=8
lora_alpha=32
EPOCH=3
LR=5e-4
grad_step=8

def main():
    OUTPUT_DIR = get_FileName()
    torch_dtype = get_torch_dtype()
    target_modules = get_target_modules()
    args = TrainArguments(
        model_type=MODEL_TYPE,
        model=MODEL_PATH,
        local_repo_path=LOCAL_REPO_PATH,
        train_type=TRAIN_TYPE,
        dataset=DATASET_PATH,
        # val_dataset=VAL_DATASET,
        split_dataset_ratio=0,      # 划分数据集 默认0.01 开启不验证
        torch_dtype=torch_dtype,
        num_train_epochs=EPOCH,
        per_device_train_batch_size=1,
        per_device_eval_batch_size=1,
        learning_rate=LR,
        lora_rank=lora_rank,
        lora_alpha=lora_alpha,
        target_modules=target_modules,
        gradient_accumulation_steps=grad_step,
        eval_steps=20,
        save_steps=1000,
        save_total_limit=5,
        logging_steps=5,
        max_length=4096,    # 2048  4096
        output_dir=OUTPUT_DIR,
        system="You are a helpful assistant.",
        warmup_ratio=0.1,
        dataloader_num_workers=4,
        adam_beta2=0.95,
        max_grad_norm=1.0,
    )
    
    sft_main(args)

if __name__ == '__main__':
    
    main()
    print("model path   : ", MODEL_PATH)
    print("train dataset: ", DATASET_PATH)

dong-168 avatar Sep 04 '25 07:09 dong-168

感觉4和4.5都有问题,艾特大佬帮忙看看吧@ Jintao-Huang

ZHUXUHAN avatar Sep 04 '25 07:09 ZHUXUHAN

MiniCPM-V-4_5 is supported in swift main branch, please install from source and retry

hjh0119 avatar Sep 04 '25 07:09 hjh0119

Thank you for your answer, I just do as you say.

ZHUXUHAN avatar Sep 04 '25 10:09 ZHUXUHAN

MiniCPM-V-4_5 is supported in swift main branch, please install from source and retry


Thank you very much. I have completed the fine-tuning of V4.5 as you suggested. However, the issue of the loss value being zero during V4 fine-tuning still persists and cannot be resolved.

  • MiniCPM-V-4_5 ✅
  • MiniCPM-V-4 ❌ (loss=0)

dong-168 avatar Sep 05 '25 02:09 dong-168

https://github.com/modelscope/ms-swift/blob/main/examples/models/minicpmv/train.sh

Image

can this script run

Jintao-Huang avatar Sep 05 '25 03:09 Jintao-Huang

https://github.com/modelscope/ms-swift/blob/main/examples/models/minicpmv/train.sh

Image can this script run

I tried the code you provided, but the loss is still zero.

Image

dong-168 avatar Sep 05 '25 03:09 dong-168

It's also fine to tell me where to find the Python code for the V4 model that includes the implementation of Loss calculation, logits, and labels.

dong-168 avatar Sep 05 '25 03:09 dong-168

MiniCPM-V-4_5 is supported in swift main branch, please install from source and retry

Thank you very much. I have completed the fine-tuning of V4.5 as you suggested. However, the issue of the loss value being zero during V4 fine-tuning still persists and cannot be resolved.

  • MiniCPM-V-4_5 ✅
  • MiniCPM-V-4 ❌ (loss=0)

hi, can you provide your script? i still meet the issue in https://github.com/modelscope/ms-swift/issues/5658#issuecomment-3251969382

ZHUXUHAN avatar Sep 05 '25 05:09 ZHUXUHAN

MiniCPM-V-4_5 is supported in swift main branch, please install from source and retry

It still raises the runtimeError: shape '[256, 8192, 128]' is invalid for input of size 1048576

cxmscb avatar Sep 05 '25 06:09 cxmscb

--truncation_strategy right

Try removing this line

Jintao-Huang avatar Sep 05 '25 06:09 Jintao-Huang

train.sh 文件(MiniCPM-V-4_5 ✅)

CUDA_VISIBLE_DEVICES=0,1,2,3 \
swift sft \
    --model OpenBMB/MiniCPM-V-4_5 \
    --dataset "train.json" \
    --split_dataset_ratio 0 \
    --train_type lora \
    --torch_dtype float16 \
    --num_train_epochs 1 \
    --per_device_train_batch_size 1 \
    --per_device_eval_batch_size 1 \
    --learning_rate 1e-4 \
    --lora_rank 8 \
    --lora_alpha 32 \
    --target_modules all-linear \
    --freeze_vit true \
    --gradient_accumulation_steps 16 \
    --eval_steps 50 \
    --save_steps 50 \
    --save_total_limit 2 \
    --logging_steps 5 \
    --max_length 2048 \
    --output_dir output \
    --warmup_ratio 0.05 \
    --dataset_num_proc 4 \
    --dataloader_num_workers 4

dong-168 avatar Sep 05 '25 06:09 dong-168

This issue has been inactive for over 3 months and will be automatically closed in 7 days. If this issue is still relevant, please reply to this message.

github-actions[bot] avatar Dec 05 '25 00:12 github-actions[bot]

This issue has been automatically closed due to inactivity. If needed, it can be reopened.

github-actions[bot] avatar Jan 02 '26 00:01 github-actions[bot]