ISSUE DESCRIPTION OPD losses error for Megatron when i use megatron for compute losses…
- User
ISSUE DESCRIPTION
OPD losses error for Megatron when i use megatron for compute losses the code position is: /verl/trainer/distillation/megatron/losses.py Line 248 teacher_topk_log_probs_cp_split, *_ = preprocess_bshd_engine(teacher_topk_log_probs, pre_process=True)
the teacher_topk_log_probs is [b, length, 64] but preprocess_bshd_engine this function need [n, length] dim, so error happens
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /verl, current file: none) bash-$
Assistant
I'll start by exploring the relevant file and understanding the issue.
Assistant
Let me look at the
preprocess_bshd_enginefunction andpreprocess_thd_engineto understand what shapes they expect.Assistant
Now I understand the issue. Let me look more closely at
preprocess_bshd_engineandpreprocess_thd_engine.The
preprocess_bshd_enginefunction expectsinput_idsto be a nested tensor (it callsinput_ids.offsets()andinput_ids[i]returns a 1D tensor of length n). But incompute_forward_kl_topk,teacher_topk_log_probshas shape(bsz, seqlen, topk)- it's a 3D nested tensor where each element is[seqlen, topk].Let me look at
preprocess_thd_engineas well to understand the difference.Assistant
Let me understand the nested tensor shape behavior. Let me search for how these functions are used and what shapes the nested tensors have.
Assistant
Let me look at
model_forward.pyaround line 386 to see howpreprocess_bshd_engineis used with 1D input_ids, and around 404 where it's used with potentially multi-dim args.Assistant
Let me check the nested tensor shape behavior with a quick test.
Assistant
Now I can see the issue clearly. The
preprocess_bshd_enginefunction assumes 1D inner tensors, butteacher_topk_log_probshas 2D inner tensors[seqlen, topk]. Let me write a reproduction script with mocked megatron.Assistant
Now let me see the full
preprocess_bshd_enginefunction again:Assistant
Let me verify the shape behavior with a 1D nested tensor:
Assistant
Now I have a clear understanding. Let me write a reproduction script with mocked megatron's mpu:
Assistant
I need to mock more of the megatron imports. Let me check if megatron is installed.
Assistant
Megatron is not installed. Let me create a more thorough mock to load just the util module:
Assistant
The error confirms the bug. The
preprocess_bshd_enginefunction doesn't handle the trailingtopkdimension. Let me fix the function to handle arbitrary trailing dimensions:Assistant
Now let me apply the fix. I need to handle the trailing dimensions:
Assistant
Now let me fix the
seq_paddedfor the CP > 1 case and theposition_idsexpand_asissue:Assistant
Now let me fix the
position_idsexpand_asissue, which fails wheninput_ids_bshdhas trailing dims:Assistant
Now let me run the reproduction script to verify the cp_size=1 case works:
Assistant
The cp_size=1 case works now. Let me write a comprehensive test to verify correctness (values match input), the cp_size > 1 case, and the original 1D case:
Assistant
All cp_size=1 tests pass. Now let me test the cp_size > 1 case (the zigzag-CP split logic) for 2D tensors. I'll simulate different ranks:
Assistant
All CP tests pass, including short-sequence padding. Let me also test the
need_rollpath with 2D tensors and find existing tests:Assistant
Let me test the
need_rollpath with 2D tensors (for completeness, even though losses use default):Assistant
All tests pass. Let me review the full diff:
Assistant
The diff looks clean and is fully backward compatible. Let me clean up the temp files:
Assistant
The fix is complete. Let me do a final review of the complete updated function:
Assistant
The fix is complete and verified. Let me provide a summary of my work.
Summary
Issue
When using Megatron for distillation losses, calling
preprocess_bshd_engineonteacher_topk_log_probs(a nested tensor with shape(bsz, seqlen, topk)) atverl/trainer/distillation/megatron/losses.pyline 248 caused an error. The functionpreprocess_bshd_engineinverl/models/mcore/util.pywas written assuming each nested element is 1-D (input_idsof shape[seqlen]), so it:- Created
input_ids_bshdas 2-D[batch, local_max_seqlen], which couldn't hold the trailingtopkdimension → assignment/broadcast error. - Used
position_ids.unsqueeze(0).expand_as(input_ids_bshd), which fails wheninput_ids_bshdis 3-D (can't expand[1, S]to[B, S, topk]). - In the CP>1 padding path, created
seq_paddedas 1-D, which couldn't hold thetopkdimension.
Fix (
verl/models/mcore/util.py)- Compute
trailing_shape = list(input_ids.shape[2:])to capture any dimensions after the ragged sequence dim. - Allocate
input_ids_bshdand the CP-paddingseq_paddedwith*trailing_shape, so they're sized[B, S, topk](or[B, S]for plain 1-Dinput_ids). - Replaced
position_ids.unsqueeze(0).expand_as(input_ids_bshd)with explicit.expand(batch_size, local_max_seqlen)/.expand(batch_size, -1), keepingposition_idscorrectly 2-D[B, S]regardless of trailing dims.
Checklist
- Reproduced the error: Yes — wrote a reproduction with mocked Megatron
mpuand confirmed theexpand/broadcastRuntimeErrorbefore the fix. - Confirmed fix resolves the error: Yes — the same reproduction now succeeds and produces
[2, 5, 64]results. - Edge cases: Tested cp_size=1, cp_size=2 (zigzag-CP split with correctness assertions), short-sequence padding path (seqlen < max_seqlen),
need_rollpath, 1-D backward compatibility (themodel_forward.pyinput_ids/labels/loss_mask usage), and 2-D(bsz, seqlen, topk)tensors. The fix is fully backward compatible — for 1-D nested tensorstrailing_shapeis empty and all tensor shapes/position_ids behavior is identical to before. - Existing tests: The tests touching this code (
tests/special_distributed/test_mcore_config_converter.py) require Megatron, which isn't installed in this environment. I verified the module's syntax is valid and confirmed backward compatibility by reproducing themodel_forward.pyusage patterns (1-D nested tensors). The fix does not change any behavior for the existing 1-D call sites.
- Created
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →