MiniCPM-V4微调时出现loss为0
问题:
{'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'}
解决:请问这个怎么解决呢
兄弟,我在复现这个模型的时候,遇到了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
训练脚本发一下
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
兄弟,我在复现这个模型的时候,遇到了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模型我没试过
兄弟,我在复现这个模型的时候,遇到了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',会报错,没有这个枚举类型
兄弟,我在复现这个模型的时候,遇到了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就报错了。
训练脚本发一下
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)
感觉4和4.5都有问题,艾特大佬帮忙看看吧@ Jintao-Huang
MiniCPM-V-4_5 is supported in swift main branch, please install from source and retry
Thank you for your answer, I just do as you say.
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)
https://github.com/modelscope/ms-swift/blob/main/examples/models/minicpmv/train.sh
can this script run
https://github.com/modelscope/ms-swift/blob/main/examples/models/minicpmv/train.sh
can this script run
I tried the code you provided, but the loss is still zero.
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.
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
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
--truncation_strategy right
Try removing this line
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
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.
This issue has been automatically closed due to inactivity. If needed, it can be reopened.
can this script run