qwen3vl megatron-bridge 和swift sft精度差异过大
Checklist / 检查清单
- [x] I have searched existing issues, and this is a new question or discussion topic. / 我已经搜索过现有的 issues,确认这是一个新的问题与讨论。
Question Description / 问题描述
使用官方镜像3.10.3,qwenvl-4b 5w数据, 单机8卡,分别用megatron sft (并行和无并行) 和swift sft 保持参数一致微调,测试均使用vllm,调用使用openai方式,测试mmbench,megatron训练精度在66左右,swift在76左右,差异10个点。
megatron 训练脚本:
PYTORCH_CUDA_ALLOC_CONF='expandable_segments:True'
NPROC_PER_NODE=8
MAX_PIXELS=1638400
MIN_PIXELS=784
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
megatron sft
--model ./Qwen3-VL-4B-Instruct/
--load_safetensors true
--save_safetensors true
--dataset './5w.json'
--load_from_cache_file false
--pipeline_model_parallel_size 1
--tensor_model_parallel_size 1
--sequence_parallel false
--context_parallel_size 1
--packing false
--bf16 True
--freeze_vit true
--freeze_llm false
--freeze_aligner false
--micro_batch_size 1
--global_batch_size 8
--finetune false
--cross_entropy_loss_fusion false
--lr 1e-5
--lr_decay_style "cosine"
--lr_warmup_fraction 0.05
--weight_decay 0
--max_epochs 1
--save megatron_output/Qwen3-VL-4B-finetune_align
--save_interval 1000
--vit_gradient_checkpointing false
--max_length 8192
--num_workers 4
--dataset_num_proc 8
swift sft 训练脚本:
PYTORCH_CUDA_ALLOC_CONF='expandable_segments:True'
NPROC_PER_NODE=8
IMAGE_MAX_TOKEN_NUM=1600
IMAGE_MIN_TOKEN_NUM=4
MAX_PIXELS=1638400
MIN_PIXELS=784
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
swift sft
--model ./Qwen3-VL-4B-Instruct/
--dataset '5w.json'
--load_from_cache_file false
--train_type full
--torch_dtype bfloat16
--num_train_epochs 1
--per_device_train_batch_size 1
--per_device_eval_batch_size 1
--attn_impl flash_attn
--freeze_vit true
--freeze_llm false
--freeze_aligner false
--packing false
--gradient_checkpointing true
--vit_gradient_checkpointing false
--gradient_accumulation_steps 1
--eval_strategy "no"
--save_steps 1000
--save_total_limit 5
--logging_steps 1
--max_length 8192
--max_grad_norm 1
--output_dir swift_output/swift_train_4b
--learning_rate 1e-5
--weight_decay 0
--warmup_ratio 0.05
--lr_scheduler_type "cosine"
--deepspeed zero2
--dataset_num_proc 4
--dataloader_num_workers 4
部署脚本:
CUDA_VISIBLE_DEVICES=0,1,2,3
MAX_PIXELS=1638400
MIN_PIXELS=784
VIDEO_MAX_PIXELS=1638400
FPS_MAX_FRAMES=32
TENSOR_PARALLEL_SIZE=4
swift deploy
--model "./checkpoint-6250"
--infer_backend vllm
--host 0.0.0.0
--port 9000
--vllm_gpu_memory_utilization 0.7
--vllm_max_model_len 50000
--vllm_tensor_parallel_size 4
--max_new_tokens 4096
--vllm_limit_mm_per_prompt '{"image": 5, "video": 2}'
--served_model_name "Qwen/Qwen3-VL-4B-Instruct"
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.
建议先在训练前跑一次转换精度检查,确认 HF 和 MCore 加载同一份权重时已经对齐,再排训练参数:
CUDA_VISIBLE_DEVICES=0 swift export \
--model ./Qwen3-VL-4B-Instruct \
--to_mcore true \
--torch_dtype bfloat16 \
--output_dir ./qwen3vl-convert-check \
--test_convert_precision true
这个开关会对比转换前后的模型输出,当前实现也会为多模态模型构造图像样例,见检查函数。转换会同时占用 HF / MCore 模型的资源,单卡空间不够时按导出说明使用多卡。
训练侧再拿同一条带图样本,比较两边实际的 image_grid_thw、编码长度、有效 label 数和首步 loss。你两份命令里 IMAGE_MIN/MAX_TOKEN_NUM 的设置并不完全相同,先看处理后的网格,能避免只按环境变量的数值判断是否一致。若训练前就有精度差,先查转换/部署;若训练前一致,再查实际参与训练的样本、监督 token 和更新步数。