verl icon indicating copy to clipboard operation
verl copied to clipboard

[mbridge error] RuntimeError: shape mismatch in Qwen3.5-A17B + mbridge + VERL Training

Open xuhang-2 opened this issue 8 months ago • 1 comments

Describe the bug

训练:VERL + DAPO + Qwen3.5 A17B 环境:Megatron-LM and mbridge backend

环境

=== Python ===
Python 3.12.13

=== Pip Packages ===
Name: megatron-core
Version: 0.16.0
Name: mbridge
Version: 0.15.1
Name: vllm
Version: 0.17.0
Name: transformers
Version: 5.2.0

=== PyTorch + CUDA ===
PyTorch: 2.10.0+cu129
CUDA Available: True
CUDA Version: 12.9

报错

Traceback (most recent call last):
  File "/workspace/verl/recipe/dapo/main_dapo.py", line 112, in main
    run_ppo(config, task_runner_class=ray.remote(num_cpus=1)(DAPOTaskRunner))
  File "/workspace/verl/verl/trainer/main_ppo.py", line 99, in run_ppo
    ray.get(runner.run.remote(config))
  # ... [ray internal stack trace omitted] ...
ray.exceptions.RayTaskError(RuntimeError): ray::DAPOTaskRunner.run() (pid=234873, ip=xx.xx.xx.xx, actor_id=xxx)
  File "/workspace/verl/recipe/dapo/main_dapo.py", line 105, in run
    trainer.fit()
  File "/workspace/verl/recipe/dapo/dapo_ray_trainer.py", line 326, in fit
    batch = self.compute_kl_related_metrics(batch, metrics, timing_raw)
  File "/workspace/verl/recipe/dapo/dapo_ray_trainer.py", line 87, in compute_kl_related_metrics
    ref_log_prob = self._compute_ref_log_prob(batch)
  File "/workspace/verl/verl/trainer/ppo/ray_trainer.py", line 1128, in _compute_ref_log_prob
    ref_log_prob = self.ref_policy_wg.compute_ref_log_prob(batch)
  File "/workspace/verl/verl/single_controller/ray/base.py", line 55, in __call__
    output = ray.get(output)
ray.exceptions.RayTaskError(RuntimeError): ray::WorkerDict.ref_compute_ref_log_prob() (pid=144386, ip=xx.xx.xx.xx, actor_id=xxx)
  # ...[ray internal stack trace omitted] ...
  File "/workspace/verl/verl/workers/megatron_workers.py", line 858, in compute_ref_log_prob
    output, _, _ = self.ref_policy.compute_log_prob(data=data, calculate_entropy=False)
  File "/workspace/verl/verl/workers/actor/megatron_actor.py", line 259, in compute_log_prob
    output = self.forward_backward_batch(
  File "/workspace/verl/verl/workers/actor/megatron_actor.py", line 723, in forward_backward_batch
    losses_reduced = forward_backward_func(
  File "/usr/local/lib/python3.12/dist-packages/megatron/core/pipeline_parallel/schedules.py", line 2202, in forward_backward_pipelining_without_interleaving
    output_tensor, num_tokens = forward_step(
  File "/usr/local/lib/python3.12/dist-packages/megatron/core/pipeline_parallel/schedules.py", line 423, in forward_step
    output_tensor, loss_func = forward_step_func(data_iterator, model)
  File "/workspace/verl/verl/workers/actor/megatron_actor.py", line 683, in forward_step
    output = forward_fn(
  File "/workspace/verl/verl/models/mcore/model_forward.py", line 147, in model_forward
    output_orig = model(
  File "/usr/local/lib/python3.12/dist-packages/megatron/core/transformer/module.py", line 489, in forward
    outputs = self.module(*inputs, **kwargs)
  File "/usr/local/lib/python3.12/dist-packages/mbridge/models/qwen3_5/model.py", line 301, in forward
    combined_embeddings[vision_mask] = vision_embeds
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
RuntimeError: shape mismatch: value tensor of shape [66, 3072] cannot be broadcast to indexing result of shape[67, 3072]

训练脚本

export CUDA_DEVICE_MAX_CONNECTIONS=1
export VLLM_USE_V1=1
export VLLM_ALLREDUCE_USE_SYMM_MEM=0

# System Limits setup
ulimit -n 1048576

########################### Quick Config ###########################

TP=${TP:-4}
PP=${PP:-2}
CP=${CP:-1}
EP=${EP:-8}
ETP=${ETP:-1}
GEN_TP=${GEN_TP:-16}
ALL_OFFLOAD=${ALL_OFFLOAD:-True}

rollout_name="vllm"
OUTPUT_DIR="/path/to/output"
CKPTS_DIR=${OUTPUT_DIR}/"${project_name}"/"${exp_name}/ckpts"
LOG_DIR="${OUTPUT_DIR}/${project_name}/${exp_name}/logs/$(date +%Y%m%d_%H%M%S)"

adv_estimator=grpo
NNODES=16
max_prompt_length=1024
max_response_length=10240

train_prompt_bsz=64
over_sampling_ratio=1 
gen_prompt_bsz=$((train_prompt_bsz * over_sampling_ratio))
train_prompt_mini_bsz=${train_prompt_bsz}

# Prepare model and data
HF_MODEL_PATH="/path/to/vlm/hf/model"
train_path="/path/to/data/train.parquet"
test_path="/path/to/data/test.parquet"

########################### Parameter Arrays ###########################

DATA=(
    data.train_files=${train_path}
    data.val_files=${test_path}
    data.shuffle=True
    data.train_batch_size=${train_prompt_bsz}
    data.gen_batch_size=${gen_prompt_bsz}
    data.max_prompt_length=${max_prompt_length}
    data.max_response_length=${max_response_length}
    data.truncation='error'
    data.filter_overlong_prompts=True
    data.filter_overlong_prompts_workers=8
)

MODEL=(
    actor_rollout_ref.model.path=${HF_MODEL_PATH}
    actor_rollout_ref.model.trust_remote_code=True
    actor_rollout_ref.model.use_remove_padding=False
)

ACTOR=(
    actor_rollout_ref.actor.ppo_mini_batch_size=${train_prompt_mini_bsz}
    actor_rollout_ref.actor.megatron.use_mbridge=True
    actor_rollout_ref.actor.megatron.vanilla_mbridge=True
    actor_rollout_ref.actor.megatron.tensor_model_parallel_size=${TP}
    actor_rollout_ref.actor.megatron.pipeline_model_parallel_size=${PP}
    # ... [megatron configs omitted for brevity] ...
)

ROLLOUT=(
    actor_rollout_ref.rollout.name=${rollout_name}
    actor_rollout_ref.rollout.tensor_model_parallel_size=${GEN_TP}
    actor_rollout_ref.rollout.gpu_memory_utilization=0.4
    actor_rollout_ref.rollout.n=8
    # ... [rollout configs omitted for brevity] ...
)

REF=(
    actor_rollout_ref.ref.megatron.tensor_model_parallel_size=${TP}
    actor_rollout_ref.ref.megatron.pipeline_model_parallel_size=${PP}
    actor_rollout_ref.ref.megatron.context_parallel_size=${CP}
    actor_rollout_ref.ref.megatron.expert_model_parallel_size=${EP}
)

TRAINER=(
    trainer.project_name=${project_name}
    trainer.experiment_name=${exp_name}
    trainer.n_gpus_per_node=8
    trainer.nnodes=${NNODES}
)

########################### Launch ###########################

python3 -m recipe.dapo.main_dapo \
    --config-name='dapo_megatron_trainer' \
    "${DATA[@]}" \
    "${MODEL[@]}" \
    "${ROLLOUT[@]}" \
    "${ACTOR[@]}" \
    "${REF[@]}" \
    "${TRAINER[@]}" \
    "$@" 2>&1 | tee "$LOG_DIR/train.log"

xuhang-2 avatar Mar 18 '26 10:03 xuhang-2

Thank you for the detailed bug report. The error indicates a shape mismatch in the vision embedding indexing:

RuntimeError: shape mismatch: value tensor of shape [66, 3072] cannot be broadcast to indexing result of shape [67, 3072]

This occurs in mbridge/models/qwen3_5/model.py at line 301, where vision_embeds (shape [66, 3072]) is being assigned to combined_embeddings[vision_mask] which expects shape [67, 3072].

Potential causes:

  1. Vision token count mismatch: The vision_mask indicates 67 vision tokens, but the processed vision embeddings only have 66 tokens. This could be due to:

    • Image preprocessing/resizing differences between rollout (vLLM) and training (Megatron)
    • Different vision encoder configurations
  2. Padding/alignment issues: The vision tokens may need additional padding to match the expected shape.

Debugging suggestions:

  1. Check if your vision data preprocessing is consistent between rollout and training.
  2. Verify the image_resolution and patch_size settings match between vLLM and Megatron configurations.
  3. Try running with a smaller batch size (e.g., train_batch_size=16) to isolate if this is batch-dependent.

Related: This might be similar to issues with hybrid attention models requiring specific sp_size settings (see #5552).

Could you share:

  • Your image/video resolution and preprocessing settings
  • Whether this error occurs consistently or intermittently

This will help identify if the issue is in the mbridge Qwen3.5 vision integration or in the data pipeline.

Kirrito-k423 avatar Mar 18 '26 16:03 Kirrito-k423