Skip to content

Best Practices for Inference and Fine-Tuning with MiniCPM-V 2.6 #1613

Closed

Description

@Jintao-Huang

模型:https://modelscope.cn/models/OpenBMB/MiniCPM-V-2_6

通常,多模态大模型微调会使用自定义数据集进行微调。在这里,我们将展示可直接运行的demo。

在开始微调之前,请确保您的环境已准备妥当。

git clone https://github.com/modelscope/swift.git
cd swift
pip install -e .[llm]

模型推理

CUDA_VISIBLE_DEVICES=0 swift infer \
  --model_type minicpm-v-v2_6-chat \
  --model_id_or_path OpenBMB/MiniCPM-V-2_6
<<< 你好
你好!今天我能为您提供什么帮助?
--------------------------------------------------
<<< clear
<<< <image>描述这张图片
Input an image path or URL <<< http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png
这张图片展示了一只小猫的特写,它有着引人注目的外貌。小猫有着大大的、圆圆的、蓝色的眼睛,看起来充满了好奇和天真。它的毛色主要是白色,带有灰色和黑色的条纹,特别是在脸部和耳朵周围,这些地方的条纹更加明显。小猫的耳朵竖立着,尖尖的,内侧是粉红色的。它的胡须又长又白,从脸颊上伸出来。小猫的鼻子是粉红色的,嘴巴微微张开,露出一点粉红色的舌头。背景模糊,将焦点集中在小猫身上,暗示着一个室内环境,柔和的光线照亮了小猫的毛发。
--------------------------------------------------
<<< clear
<<< <video>描述这段视频
Input a video path or URL <<< https://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/baby.mp4
这段视频展示了一个年幼的孩子坐在床上,专心阅读一本书。孩子戴着深色眼镜,穿着浅蓝色无袖上衣和粉色裤子。床上铺着白色床单,孩子旁边放着一件白色衣物。背景中有一个木制婴儿床,暗示着一个家庭环境。房间光线柔和,氛围平静。视频中没有明显的动作或活动,孩子似乎完全沉浸在阅读中。

图片微调

我们使用 coco-en-mini 数据集进行微调,该数据集的任务是对图片内容进行描述。您可以在 modelscope 上找到该数据集:https://modelscope.cn/datasets/modelscope/coco_2014_caption/summary

# 默认会将lora_target_modules设置为llm和resampler所有的linear
CUDA_VISIBLE_DEVICES=0,1,2,3 NPROC_PER_NODE=4 swift sft \
  --model_type minicpm-v-v2_6-chat \
  --model_id_or_path OpenBMB/MiniCPM-V-2_6 \
  --sft_type lora \
  --dataset coco-en-mini#20000 \
  --deepspeed default-zero2

如果要使用自定义数据集,只需按以下方式进行指定:

  --dataset train.jsonl \
  --val_dataset val.jsonl \

自定义数据集支持json和jsonl样式,以下是自定义数据集的样例:

{"query": "<image>55555", "response": "66666", "images": ["image_path"]}
{"query": "eeeee<image>eeeee<image>eeeee", "response": "fffff", "history": [], "images": ["image_path1", "image_path2"]}
{"query": "EEEEE", "response": "FFFFF", "history": [["query1", "response2"], ["query2", "response2"]], "images": []}

显存占用:
image

微调后推理脚本如下:

# 如果要全量测试请设置: `--show_dataset_sample -1`
CUDA_VISIBLE_DEVICES=0 swift infer \
    --ckpt_dir output/minicpm-v-v2_6-chat/vx-xxx/checkpoint-xxx \
    --load_dataset_config true --merge_lora true

微调后模型对验证集进行推理的示例(时间原因,只训练了300个step):

image

视频微调

我们使用 video-chatgpt 数据集进行微调,该数据集的任务是对视频内容进行描述。您可以在 modelscope 上找到该数据集:https://modelscope.cn/datasets/swift/VideoChatGPT

CUDA_VISIBLE_DEVICES=0,1,2,3 NPROC_PER_NODE=4 swift sft \
  --model_type minicpm-v-v2_6-chat \
  --model_id_or_path OpenBMB/MiniCPM-V-2_6 \
  --sft_type lora \
  --dataset video-chatgpt \
  --deepspeed default-zero2

自定义数据集支持json和jsonl样式,以下是自定义数据集的样例:

{"query": "<video>55555", "response": "66666", "videos": ["video_path"]}
{"query": "eeeee<video>eeeee<video>eeeee", "response": "fffff", "history": [], "videos": ["video_path1", "video_path2"]}
{"query": "EEEEE", "response": "FFFFF", "history": [["query1", "response2"], ["query2", "response2"]], "videos": []}

显存占用:
image

微调后推理脚本如下:

CUDA_VISIBLE_DEVICES=0 swift infer \
    --ckpt_dir output/minicpm-v-v2_6-chat/vx-xxx/checkpoint-xxx \
    --load_dataset_config true --merge_lora true

微调后模型对验证集进行推理的示例(时间原因,只训练了50个step):
image

Activity

  1. changed the title [-]MiniCPM-V 2.6的图片和视频微调[/-] [+]MiniCPM-V 2.6的图片和视频微调 最佳实践[/+] on Aug 6, 2024
  2. changed the title [-]MiniCPM-V 2.6的图片和视频微调 最佳实践[/-] [+]MiniCPM-V 2.6的图片和视频微调的最佳实践[/+] on Aug 6, 2024
  3. pinned this issue on Aug 6, 2024
  4. unpinned this issue on Aug 6, 2024
  5. changed the title [-]MiniCPM-V 2.6的图片和视频微调的最佳实践[/-] [+]MiniCPM-V 2.6的推理和微调最佳实践[/+] on Aug 6, 2024
  6. demoninpiano commented on Aug 7, 2024

    @demoninpiano

    官方文档的多图理解和in-context有在swift api里支持吗?

  7. Jintao-Huang commented on Aug 7, 2024

    @Jintao-Huang
    CollaboratorAuthor

    支持多图和多轮的

    多图需要使用多个标签即可. 可以查看上面的自定义数据集的格式

  8. guihonghao commented on Aug 8, 2024

    @guihonghao
    Contributor

    需要升级swift到什么版本啊?

  9. Jintao-Huang commented on Aug 8, 2024

    @Jintao-Huang
    CollaboratorAuthor

    还在main分支

  10. compleXuan commented on Aug 11, 2024

    @compleXuan

    单样本视频推理的代码可以提供吗

  11. Jintao-Huang commented on Aug 12, 2024

    @Jintao-Huang
    CollaboratorAuthor
    import os
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'
    
    from swift.llm import (
        get_model_tokenizer, get_template, inference, ModelType,
        get_default_template_type, inference_stream
    )
    from swift.utils import seed_everything
    import torch
    
    model_type = ModelType.minicpm_v_v2_6_chat
    model_id_or_path = None
    template_type = get_default_template_type(model_type)
    print(f'template_type: {template_type}')
    
    model, tokenizer = get_model_tokenizer(model_type, torch.bfloat16, model_id_or_path=model_id_or_path,
                                           model_kwargs={'device_map': 'auto'})
    model.generation_config.max_new_tokens = 256
    template = get_template(template_type, tokenizer)
    seed_everything(42)
    
    query = '<video>描述这段视频'
    videos = ['https://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/baby.mp4']
    response, history = inference(model, template, query, videos=videos)
    print(f'query: {query}')
    print(f'response: {response}')
    
    # 流式(streaming)
    query = '<image>描述这张图片'
    images = ['http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png']
    gen = inference_stream(model, template, query, images=images)
    print_idx = 0
    print(f'query: {query}\nresponse: ', end='')
    for response, history in gen:
        delta = response[print_idx:]
        print(delta, end='', flush=True)
        print_idx = len(response)
    print()
    """
    query: <video>描述这段视频
    response: 这段视频展示了一个年幼的孩子,可能是一个蹒跚学步的幼儿,坐在床上专心阅读一本书。孩子戴着深色眼镜,穿着浅绿色无袖上衣和粉色裤子。床上铺着白色床单,背景中有一个木制婴儿床,暗示着一个家庭环境。房间光线充足,氛围温馨舒适。孩子专注的表情和姿势表明他们对书本内容很投入。
    query: <image>描述这张图片
    response: 这张图片展示了一只小猫的特写,它有着引人注目的面部特征。小猫的毛色主要是白色,带有灰色和黑色的条纹,特别是在眼睛周围和耳朵上。它的眼睛又大又圆,有着蓝色的虹膜,看起来非常好奇或专注。小猫的耳朵竖立着,内耳是粉红色的,与毛色形成对比。小猫的鼻子是粉红色的,有着小小的黑色鼻子,嘴巴微微张开,露出一点粉红色的舌头。小猫的胡须又长又白,从脸颊上伸出来。背景模糊,将焦点集中在小猫身上,暗示着一个室内环境,有自然光线,可能来自窗户。
    """
  12. okideal commented on Aug 12, 2024

    @okideal

    请问官方的few-shot推理方式 swift有支持么?

  13. yingdachen commented on Aug 12, 2024

    @yingdachen
    Member
    import os
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'
    
    from swift.llm import (
        get_model_tokenizer, get_template, inference, ModelType,
        get_default_template_type, inference_stream
    )
    from swift.utils import seed_everything
    import torch
    
    model_type = ModelType.minicpm_v_v2_6_chat
    model_id_or_path = None
    template_type = get_default_template_type(model_type)
    print(f'template_type: {template_type}')
    
    model, tokenizer = get_model_tokenizer(model_type, torch.bfloat16, model_id_or_path=model_id_or_path,
                                           model_kwargs={'device_map': 'auto'})
    model.generation_config.max_new_tokens = 256
    template = get_template(template_type, tokenizer)
    seed_everything(42)
    
    query = '<video>描述这段视频'
    videos = ['https://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/baby.mp4']
    response, history = inference(model, template, query, videos=videos)
    print(f'query: {query}')
    print(f'response: {response}')
    
    # 流式(streaming)
    query = '<image>描述这张图片'
    images = ['http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png']
    gen = inference_stream(model, template, query, images=images)
    print_idx = 0
    print(f'query: {query}\nresponse: ', end='')
    for response, history in gen:
        delta = response[print_idx:]
        print(delta, end='', flush=True)
        print_idx = len(response)
    print()
    """
    query: <video>描述这段视频
    response: 这段视频展示了一个年幼的孩子,可能是一个蹒跚学步的幼儿,坐在床上专心阅读一本书。孩子戴着深色眼镜,穿着浅绿色无袖上衣和粉色裤子。床上铺着白色床单,背景中有一个木制婴儿床,暗示着一个家庭环境。房间光线充足,氛围温馨舒适。孩子专注的表情和姿势表明他们对书本内容很投入。
    query: <image>描述这张图片
    response: 这张图片展示了一只小猫的特写,它有着引人注目的面部特征。小猫的毛色主要是白色,带有灰色和黑色的条纹,特别是在眼睛周围和耳朵上。它的眼睛又大又圆,有着蓝色的虹膜,看起来非常好奇或专注。小猫的耳朵竖立着,内耳是粉红色的,与毛色形成对比。小猫的鼻子是粉红色的,有着小小的黑色鼻子,嘴巴微微张开,露出一点粉红色的舌头。小猫的胡须又长又白,从脸颊上伸出来。背景模糊,将焦点集中在小猫身上,暗示着一个室内环境,有自然光线,可能来自窗户。
    """

    is this included in documentation somewhere...

  14. Jintao-Huang commented on Aug 12, 2024

    @Jintao-Huang
    CollaboratorAuthor

    is this included in documentation somewhere...

    Thank you for the excellent suggestions. We will update the document within this week.

  15. 77 remaining items

  16. NirHeaven commented on Oct 22, 2024

    @NirHeaven

    A800上使用
    CUDA_VISIBLE_DEVICES=0,1,2,3 NPROC_PER_NODE=4 swift sft
    --model_type minicpm-v-v2_6-chat
    --model_id_or_path $MODEL
    --sft_type lora
    --dataset coco-en-mini#20000
    --deepspeed default-zero2

    会直接卡死
    
    image
  17. 2013358072 commented on Oct 28, 2024

    @2013358072

    eval 数据集的格式是什么。

    如何验证 eval 数据集以及 result dataset 中 label key 的含义是什么。

    eval 数据中的响应键应该为空吗?

    the same question

  18. chen10089 commented on Nov 11, 2024

    @chen10089

    如何使用sft后的模型进行推理?
    CUDA_VISIBLE_DEVICES=1 swift infer
    --model_type minicpm-v-v2_6-chat
    --model_id_or_path OpenBMB/MiniCPM-V-2_6
    --ckpt_dir output/minicpm-v-v2_6-chat/v1-20241111-150624/checkpoint-10
    是这样设置吗

  19. chen10089 commented on Nov 11, 2024

    @chen10089

    如何使用sft后的模型进行推理? CUDA_VISIBLE_DEVICES=1 swift infer --model_type minicpm-v-v2_6-chat --model_id_or_path OpenBMB/MiniCPM-V-2_6 --ckpt_dir output/minicpm-v-v2_6-chat/v1-20241111-150624/checkpoint-10 是这样设置吗

    参考了https://swift.readthedocs.io/en/latest/Multi-Modal/minicpm-v-best-practice.html,原来是这样设置:
    CUDA_VISIBLE_DEVICES=1 swift infer
    --ckpt_dir output/minicpm-v-v2_6-chat/v1-20241111-150624/checkpoint-10-merged

  20. zhaoyangwei123 commented on Nov 13, 2024

    @zhaoyangwei123

    @Jintao-Huang
    利用8卡4090微调minicpm-v-v2_6-chat-int4模型报CUDA out of memory,int4模型要比正常模型要小很多呀,但是lora的时候显存持续上涨一直到超过24G,这是为什么呢?
    命令行:
    CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 NPROC_PER_NODE=8 swift sft --model_type minicpm-v-v2_6-chat-int4 --model_id_or_path OpenBMB/MiniCPM-V-2_6-int4 --sft_type lora --dataset /home/wzy/disk1/MiniCPM-V/finetune/relation_datasets/standard_train_no_history.jsonl --deepspeed zero2-offload --eval_steps 1000 --lora_dtype fp16

    {'loss': 1.6643486, 'acc': 0.54588017, 'grad_norm': 4.12383413, 'learning_rate': 9.978e-05, 'memory(GiB)': 18.58, 'train_speed(iter/s)': 0.49618, 'epoch': 0.08, 'global_step/max_steps': '865/11043', 'percentage': '7.83%', 'elapsed_time': '28m 56s', 'remaining_time': '5h 40m 31s'}
    {'loss': 1.60014324, 'acc': 0.59105425, 'grad_norm': 4.04182339, 'learning_rate': 9.977e-05, 'memory(GiB)': 18.58, 'train_speed(iter/s)': 0.49637, 'epoch': 0.08, 'global_step/max_steps': '870/11043', 'percentage': '7.88%', 'elapsed_time': '29m 5s', 'remaining_time': '5h 40m 14s'}
    Train: 8%|████████▌ | 874/11043 [29:14<6:30:05, 2.30s/it]Traceback (most recent call last):
    File "/mnt/sdb/MiniCPM-V/ms-swift/swift/cli/sft.py", line 5, in
    sft_main()
    File "/mnt/sdb/MiniCPM-V/ms-swift/swift/utils/run_utils.py", line 32, in x_main
    result = llm_x(args, **kwargs)
    File "/mnt/sdb/MiniCPM-V/ms-swift/swift/llm/sft.py", line 546, in llm_sft
    return trainer_train(args, model, template, train_dataset, val_dataset, callbacks=callbacks, msg=msg)
    File "/mnt/sdb/MiniCPM-V/ms-swift/swift/llm/sft.py", line 496, in trainer_train
    trainer.train(training_args.resume_from_checkpoint)
    File "/mnt/sdb/MiniCPM-V/ms-swift/swift/trainers/mixin.py", line 493, in train
    res = super().train(resume_from_checkpoint, *args, **kwargs)
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/transformers/trainer.py", line 1859, in train
    return inner_training_loop(
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/transformers/trainer.py", line 2203, in _inner_training_loop
    tr_loss_step = self.training_step(model, inputs)
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/transformers/trainer.py", line 3147, in training_step
    self.accelerator.backward(loss)
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/accelerate/accelerator.py", line 2117, in backward
    self.deepspeed_engine_wrapped.backward(loss, **kwargs)
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/accelerate/utils/deepspeed.py", line 166, in backward
    self.engine.backward(loss, **kwargs)
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/deepspeed/utils/nvtx.py", line 15, in wrapped_fn
    ret_val = func(*args, **kwargs)
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/deepspeed/runtime/engine.py", line 1976, in backward
    self.optimizer.backward(loss, retain_graph=retain_graph)
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/deepspeed/runtime/zero/stage_1_and_2.py", line 2051, in backward
    self.loss_scaler.backward(loss.float(), retain_graph=retain_graph)
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/deepspeed/runtime/fp16/loss_scaler.py", line 63, in backward
    scaled_loss.backward(retain_graph=retain_graph)
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/torch/_tensor.py", line 492, in backward
    torch.autograd.backward(
    File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/torch/autograd/init.py", line 251, in backward
    Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
    torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 7.96 GiB. GPU 0 has a total capacty of 23.65 GiB of which 7.77 GiB is free. Including non-PyTorch memory, this process has 15.87 GiB memory in use. Of the allocated memory 13.98 GiB is allocated by PyTorch, and 1.29 GiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

  21. compleXuan commented on Dec 31, 2024

    @compleXuan

    您好,我目前需要进行多图推理,是否方便提供多图的单样本推理的代码。同时我需要进行大量的调用,每次调用最多20张图片,请问这会产生什么问题吗?感谢您的回答

  22. primarybaby commented on Jan 2, 2025

    @primarybaby

    (MiniCPM-V) aipc@bogon swift % CUDA_VISIBLE_DEVICES=0 swift infer \

    --model_type minicpm-v-v2_6-chat
    --model_id_or_path OpenBMB/MiniCPM-V-2_6
    error: unable to invoke subcommand: swift-infer (No such file or directory)您好,这个问题怎么解决?

  23. Jintao-Huang commented on Feb 5, 2025

    @Jintao-Huang
    CollaboratorAuthor
  24. zhuqh19 commented on Apr 11, 2025

    @zhuqh19

    @Jintao-Huang 利用8卡4090微调minicpm-v-v2_6-chat-int4模型报CUDA out of memory,int4模型要比正常模型要小很多呀,但是lora的时候显存持续上涨一直到超过24G,这是为什么呢? 命令行: CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 NPROC_PER_NODE=8 swift sft --model_type minicpm-v-v2_6-chat-int4 --model_id_or_path OpenBMB/MiniCPM-V-2_6-int4 --sft_type lora --dataset /home/wzy/disk1/MiniCPM-V/finetune/relation_datasets/standard_train_no_history.jsonl --deepspeed zero2-offload --eval_steps 1000 --lora_dtype fp16

    {'loss': 1.6643486, 'acc': 0.54588017, 'grad_norm': 4.12383413, 'learning_rate': 9.978e-05, 'memory(GiB)': 18.58, 'train_speed(iter/s)': 0.49618, 'epoch': 0.08, 'global_step/max_steps': '865/11043', 'percentage': '7.83%', 'elapsed_time': '28m 56s', 'remaining_time': '5h 40m 31s'} {'loss': 1.60014324, 'acc': 0.59105425, 'grad_norm': 4.04182339, 'learning_rate': 9.977e-05, 'memory(GiB)': 18.58, 'train_speed(iter/s)': 0.49637, 'epoch': 0.08, 'global_step/max_steps': '870/11043', 'percentage': '7.88%', 'elapsed_time': '29m 5s', 'remaining_time': '5h 40m 14s'} Train: 8%|████████▌ | 874/11043 [29:14<6:30:05, 2.30s/it]Traceback (most recent call last): File "/mnt/sdb/MiniCPM-V/ms-swift/swift/cli/sft.py", line 5, in sft_main() File "/mnt/sdb/MiniCPM-V/ms-swift/swift/utils/run_utils.py", line 32, in x_main result = llm_x(args, **kwargs) File "/mnt/sdb/MiniCPM-V/ms-swift/swift/llm/sft.py", line 546, in llm_sft return trainer_train(args, model, template, train_dataset, val_dataset, callbacks=callbacks, msg=msg) File "/mnt/sdb/MiniCPM-V/ms-swift/swift/llm/sft.py", line 496, in trainer_train trainer.train(training_args.resume_from_checkpoint) File "/mnt/sdb/MiniCPM-V/ms-swift/swift/trainers/mixin.py", line 493, in train res = super().train(resume_from_checkpoint, *args, **kwargs) File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/transformers/trainer.py", line 1859, in train return inner_training_loop( File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/transformers/trainer.py", line 2203, in _inner_training_loop tr_loss_step = self.training_step(model, inputs) File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/transformers/trainer.py", line 3147, in training_step self.accelerator.backward(loss) File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/accelerate/accelerator.py", line 2117, in backward self.deepspeed_engine_wrapped.backward(loss, **kwargs) File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/accelerate/utils/deepspeed.py", line 166, in backward self.engine.backward(loss, **kwargs) File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/deepspeed/utils/nvtx.py", line 15, in wrapped_fn ret_val = func(*args, **kwargs) File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/deepspeed/runtime/engine.py", line 1976, in backward self.optimizer.backward(loss, retain_graph=retain_graph) File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/deepspeed/runtime/zero/stage_1_and_2.py", line 2051, in backward self.loss_scaler.backward(loss.float(), retain_graph=retain_graph) File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/deepspeed/runtime/fp16/loss_scaler.py", line 63, in backward scaled_loss.backward(retain_graph=retain_graph) File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/torch/_tensor.py", line 492, in backward torch.autograd.backward( File "/home/wzy/anaconda3/envs/MiniCPM-V/lib/python3.10/site-packages/torch/autograd/init.py", line 251, in backward Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 7.96 GiB. GPU 0 has a total capacty of 23.65 GiB of which 7.77 GiB is free. Including non-PyTorch memory, this process has 15.87 GiB memory in use. Of the allocated memory 13.98 GiB is allocated by PyTorch, and 1.29 GiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

    请问这个问题解决了吗

  25. dpramanik2289 commented on Jul 3, 2025

    @dpramanik2289

    @Jintao-Huang I need this model for video captioning. I am trying to train minicpm-v-v2_6 model but getting error -
    CUDA_VISIBLE_DEVICES=0,1,2,3 NPROC_PER_NODE=4 swift sft
    --model_type minicpm-v-v2_6-chat
    --model_id_or_path OpenBMB/MiniCPM-V-2_6
    --sft_type lora
    --dataset /home/pramand/Download/train2.json
    --deepspeed default-zero2
    why I am not able to train the model raise ValueError(f"model_type: '{model_type}' not in {list(MODEL_MAPPING.keys())}")
    ValueError: model_type: 'minicpm-v-v2_6-chat' not in

    why this - --model_type minicpm-v-v2_6-chat not available ? and --model_id_or_path OpenBMB/MiniCPM-V-2_6 not available its saying? any changes happened ??

  26. dpramanik2289 commented on Jul 3, 2025

    @dpramanik2289

    @Jintao-Huang @yingdachen @qxzheng

    please help me why getting error -
    I need this model for video captioning. I am trying to train minicpm-v-v2_6 model but getting error -
    CUDA_VISIBLE_DEVICES=0,1,2,3 NPROC_PER_NODE=4 swift sft
    --model_type minicpm-v-v2_6-chat
    --model_id_or_path OpenBMB/MiniCPM-V-2_6
    --sft_type lora
    --dataset /home/pramand/Download/train2.json
    --deepspeed default-zero2
    why I am not able to train the model raise ValueError(f"model_type: '{model_type}' not in {list(MODEL_MAPPING.keys())}")
    ValueError: model_type: 'minicpm-v-v2_6-chat' not in

    why this - --model_type minicpm-v-v2_6-chat not available ? and --model_id_or_path OpenBMB/MiniCPM-V-2_6 not available its saying? any changes happened ??

  27. Jintao-Huang commented on Jul 3, 2025

    @Jintao-Huang
    CollaboratorAuthor

    @Jintao-Huang 利用8卡4090微调minicpm-v-v2_6-chat-int4模型报CUDA out of memory,int4模型要比正常模型要小很多呀,但是lora的时候显存持续上涨一直到超过24G,这是为什么呢?

    请问这个问题解决了吗

    长文本 主要的显存占用 不在模型。建议降低上下文长度,即降低图片分辨率

  28. Jintao-Huang commented on Jul 3, 2025

    @Jintao-Huang
    CollaboratorAuthor

    @Jintao-Huang @yingdachen @qxzheng

    please help me why getting error - I need this model for video captioning. I am trying to train minicpm-v-v2_6 model but getting error - CUDA_VISIBLE_DEVICES=0,1,2,3 NPROC_PER_NODE=4 swift sft --model_type minicpm-v-v2_6-chat --model_id_or_path OpenBMB/MiniCPM-V-2_6 --sft_type lora --dataset /home/pramand/Download/train2.json --deepspeed default-zero2 why I am not able to train the model raise ValueError(f"model_type: '{model_type}' not in {list(MODEL_MAPPING.keys())}") ValueError: model_type: 'minicpm-v-v2_6-chat' not in

    why this - --model_type minicpm-v-v2_6-chat not available ? and --model_id_or_path OpenBMB/MiniCPM-V-2_6 not available its saying? any changes happened ??

    Here we are introducing the shell commands for swift2. For swift3, please refer to https://github.com/modelscope/ms-swift/tree/main/examples/train/multimodal.

    You only need to modify the --model parameter.

    For more information, please refer to the documentation.

  29. dpramanik2289 commented on Jul 11, 2025

    @dpramanik2289

    @Jintao-Huang @yingdachen @qxzheng

    I have 8 GPU machine trying to do full fine tune of MiniCPM-V-2_6 But getting lots of error how to fix those error any help ??
    run_command -
    CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
    torchrun --nproc_per_node=8 swift/cli/pt.py
    --model_type minicpmv2_6
    --model OpenBMB/MiniCPM-V-2_6
    --dataset /Download/train2.json
    --streaming true
    --train_type full
    --output_dir output
    --max_steps 5
    --num_train_epochs 1
    --eval_steps 2
    --save_steps 5
    --logging_steps 1
    --gradient_checkpointing True
    --per_device_train_batch_size 4
    --gradient_accumulation_steps 1
    --deepspeed /Download/swift/deepspeed_config.json
    --learning_rate 1e-5
    --weight_decay 0.1
    --adam_beta2 0.95

    deepspeed_config.json:
    {
    "train_batch_size": 64,
    "train_micro_batch_size_per_gpu": 4,
    "gradient_accumulation_steps": 1,

    "zero_optimization": {
    "stage": 2,
    "offload_optimizer": {
    "device": "cpu",
    "pin_memory": true
    },
    "offload_param": {
    "device": "cpu",
    "pin_memory": true
    },
    "overlap_comm": false,
    "contiguous_gradients": false
    },

    "bf16": {
    "enabled": true
    },

    "gradient_clipping": 1.0,
    "wall_clock_breakdown": false,
    "steps_per_print": 100
    }

    INFO:swift] [LABELS] [-100 * 67]<TSA_SPEED_LIMIT_CHANGE_5916644.mp4> what is happening in the scenarioSpeed limit updated correct<|im_end|>
    [INFO:swift] The TrainArguments will be saved in: /Download/swift/output/v36-20250711-195416/args.json
    [INFO:swift] model: MiniCPMV(
      (llm): Qwen2ForCausalLM(
        (model): Qwen2Model(
          (embed_tokens): Embedding(151666, 3584)
          (layers): ModuleList(
            (0-27): 28 x Qwen2DecoderLayer(
              (self_attn): Qwen2SdpaAttention(
                (q_proj): Linear(in_features=3584, out_features=3584, bias=True)
                (k_proj): Linear(in_features=3584, out_features=512, bias=True)
                (v_proj): Linear(in_features=3584, out_features=512, bias=True)
                (o_proj): Linear(in_features=3584, out_features=3584, bias=False)
                (rotary_emb): Qwen2RotaryEmbedding()
              )
              (mlp): Qwen2MLP(
                (gate_proj): Linear(in_features=3584, out_features=18944, bias=False)
                (up_proj): Linear(in_features=3584, out_features=18944, bias=False)
                (down_proj): Linear(in_features=18944, out_features=3584, bias=False)
                (act_fn): SiLU()
              )
              (input_layernorm): Qwen2RMSNorm((3584,), eps=1e-06)
              (post_attention_layernorm): Qwen2RMSNorm((3584,), eps=1e-06)
            )
          )
          (norm): Qwen2RMSNorm((3584,), eps=1e-06)
        )
        (lm_head): Linear(in_features=3584, out_features=151666, bias=False)
      )
      (vpm): SiglipVisionTransformer(
        (embeddings): SiglipVisionEmbeddings(
          (patch_embedding): Conv2d(3, 1152, kernel_size=(14, 14), stride=(14, 14), padding=valid)
          (position_embedding): Embedding(4900, 1152)
        )
        (encoder): SiglipEncoder(
          (layers): ModuleList(
            (0-26): 27 x SiglipEncoderLayer(
              (self_attn): SiglipAttention(
                (k_proj): Linear(in_features=1152, out_features=1152, bias=True)
                (v_proj): Linear(in_features=1152, out_features=1152, bias=True)
                (q_proj): Linear(in_features=1152, out_features=1152, bias=True)
                (out_proj): Linear(in_features=1152, out_features=1152, bias=True)
              )
              (layer_norm1): LayerNorm((1152,), eps=1e-06, elementwise_affine=True)
              (mlp): SiglipMLP(
                (activation_fn): PytorchGELUTanh()
                (fc1): Linear(in_features=1152, out_features=4304, bias=True)
                (fc2): Linear(in_features=4304, out_features=1152, bias=True)
              )
              (layer_norm2): LayerNorm((1152,), eps=1e-06, elementwise_affine=True)
            )
          )
        )
        (post_layernorm): LayerNorm((1152,), eps=1e-06, elementwise_affine=True)
      )
      (resampler): Resampler(
        (kv_proj): Linear(in_features=1152, out_features=3584, bias=False)
        (attn): MultiheadAttention(
          (out_proj): Linear(in_features=3584, out_features=3584, bias=True)
        )
        (ln_q): LayerNorm((3584,), eps=1e-06, elementwise_affine=True)
        (ln_kv): LayerNorm((3584,), eps=1e-06, elementwise_affine=True)
        (ln_post): LayerNorm((3584,), eps=1e-06, elementwise_affine=True)
      )
    )
    [INFO:swift] model_parameter_info: MiniCPMV: 8099.1752M Params (7612.7636M Trainable [93.9943%]), 252.4444M Buffers.
    W0711 19:55:11.244000 453032 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 453106 closing signal SIGTERM
    W0711 19:55:11.246000 453032 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 453107 closing signal SIGTERM
    W0711 19:55:11.249000 453032 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 453109 closing signal SIGTERM
    W0711 19:55:11.253000 453032 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 453110 closing signal SIGTERM
    W0711 19:55:11.256000 453032 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 453111 closing signal SIGTERM
    W0711 19:55:11.262000 453032 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 453112 closing signal SIGTERM
    W0711 19:55:11.264000 453032 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 453113 closing signal SIGTERM
    E0711 19:55:12.796000 453032 site-packages/torch/distributed/elastic/multiprocessing/api.py:874] failed (exitcode: 1) local_rank: 2 (pid: 453108) of binary: /home/pramand/miniconda3/envs/dcg_llm/bin/python3.10
    Traceback (most recent call last):
      File "/home/pramand/miniconda3/envs/dcg_llm/bin/torchrun", line 8, in <module>
        sys.exit(main())
      File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/torch/distributed/elastic/multiprocessing/errors/__init__.py", line 355, in wrapper
        return f(*args, **kwargs)
      File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/torch/distributed/run.py", line 892, in main
        run(args)
      File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/torch/distributed/run.py", line 883, in run
        elastic_launch(
      File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/torch/distributed/launcher/api.py", line 139, in __call__
        return launch_agent(self._config, self._entrypoint, list(args))
      File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/torch/distributed/launcher/api.py", line 270, in launch_agent
        raise ChildFailedError(
    torch.distributed.elastic.multiprocessing.errors.ChildFailedError: 
    ============================================================
    swift/cli/pt.py FAILED
    ------------------------------------------------------------
    
    
    ank0]:     return next(self.gen)
    [rank0]:   File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/deepspeed/runtime/engine.py", line 2243, in no_sync
    [rank0]:     assert not self.zero_optimization_partition_gradients(), \
    [rank0]: AssertionError: no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 2
    Train:   0%|                                                                            | 0/5 [02:58<?, ?it/s]
    [rank0]:[W711 19:48:55.401901399 ProcessGroupNCCL.cpp:1479] Warning: WARNING: destroy_process_group() was not called before program exit, which can leak resources. For more info, please see https://pytorch.org/docs/stable/distributed.html#shutdown (function operator())
    W0711 19:49:00.996000 354475 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 354549 closing signal SIGTERM
    W0711 19:49:00.996000 354475 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 354550 closing signal SIGTERM
    W0711 19:49:00.997000 354475 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 354551 closing signal SIGTERM
    W0711 19:49:00.997000 354475 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 354552 closing signal SIGTERM
    W0711 19:49:00.997000 354475 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 354553 closing signal SIGTERM
    W0711 19:49:00.997000 354475 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 354555 closing signal SIGTERM
    W0711 19:49:00.997000 354475 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 354556 closing signal SIGTERM
    E0711 19:49:20.958000 354475 site-packages/torch/distributed/elastic/multiprocessing/api.py:874] failed (exitcode: 1) local_rank: 5 (pid: 354554) of binary: /miniconda3/envs/dcg_llm/bin/python3.10
    Traceback (most recent call last):
      File "/home/pramand/miniconda3/envs/dcg_llm/bin/torchrun", line 8, in <module>
        sys.exit(main())
      File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/torch/distributed/elastic/multiprocessing/errors/__init__.py", line 355, in wrapper
        return f(*args, **kwargs)
      File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/torch/distributed/run.py", line 892, in main
        run(args)
      File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/torch/distributed/run.py", line 883, in run
        elastic_launch(
      File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/torch/distributed/launcher/api.py", line 139, in __call__
        return launch_agent(self._config, self._entrypoint, list(args))
      File "/home/pramand/miniconda3/envs/dcg_llm/lib/python3.10/site-packages/torch/distributed/launcher/api.py", line 270, in launch_agent
        raise ChildFailedError(
    torch.distributed.elastic.multiprocessing.errors.ChildFailedError: 
    ============================================================
    swift/cli/pt.py FAILED
    ------------------------------------------------------------
    Failures:
      <NO_OTHER_FAILURES>
    ------------------------------------------------------------
    Root Cause (first observed failure):
    [0]:
      time      : 2025-07-11_19:49:00
      host      : ip-172-12-6-129.eu-west-1.compute.internal
      rank      : 5 (local_rank: 5)
      exitcode  : 1 (pid: 354554)
      error_file: <N/A>
      traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html 
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions