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
Good, confirms the nested tensor jagged layout behavior with trailing dims. Now let's write the fix for
preprocess_bshd_engineto support extra trailing dims.Assistant
Now let's edit the
preprocess_bshd_enginefunction to properly support tensors with extra trailing dims (e.g., top-k dimension).Assistant
Now let's replace lines 599-662 with a version supporting extra trailing dims beyond the ragged sequence dimension.
Assistant
Good, this looks correct. Now let's check
postprocess_bshd_enginetoo, to see if it will be affected (used for teacher_topk output? probably not, since only preprocessing is used on teacher inputs). Let's check where preprocess_bshd_engine is invoked, and confirm shape assertion in losses.py works now.Assistant
Good, all these are 2D. Now let's write a reproduction script to validate the fix works with 3D nested tensors (with topk dim), including cp_size>1 simulated logic (though actual distributed testing is hard). Let's at least test cp_size<=1 case and check shapes, plus mock cp_size>1 by monkeypatching mpu functions.
Assistant
All three tests pass, confirming the fix works for both the original 2D case (no regression) and the new 3D case with an extra top-k dimension, for both
cp_size=1andcp_size>1.Now let's check for any existing tests related to this function in the repo.
Assistant
This is unrelated (FSDP path). Let's run this test to be safe, plus check that megatron isn't required.
Assistant
Good, megatron isn't actually needed there (that test only imports fsdp path). Now let's double check
git difffor the change.Assistant
Duplicate lines were introduced (419-425). Let's remove the duplicate.
Assistant
Good, that's fixed now. Let's verify with a python compile check and also re-run our reproduction test plus a quick test on thd engine for correctness.
Assistant
Now let's run more comprehensive tests, including thd engine with cp_size>1 and extra dims (edge case with short sequences to trigger padding).
Assistant
Both thd and bshd engines now correctly handle extra trailing dims (e.g., top-k). Let's also run the existing repo test suite for anything referencing
util.pyin mcore, and any megatron-related tests that can run without actual megatron installed (likely skipped).Assistant
Let's view the full diff once more to double check correctness and cleanliness.
Assistant
Good, blank line restored. Now let's re-verify final diff and re-run compile + tests.
Assistant
Clean diff now. Let's rerun our comprehensive test scripts once more to confirm everything works after the final edits, and run existing repo test suite broadly (quick sanity, not full since huge).
Assistant
All comprehensive tests pass. Now let's run the broader existing test suite related to distillation and mcore to ensure nothing broke.
Assistant
Confirmed pre-existing failures unrelated to my change (missing FlashAttention2 package, RANK env var). These failures exist before my modifications too. My change doesn't introduce any new failures.
Assistant
Let's do a final review of the complete diff and check for any stray temp scripts to clean up.
Assistant
No temp scripts were left on disk (they were run inline via heredoc, not saved as files). Let's confirm.
Assistant
Only ignored cache directories, no stray temp files. The change is clean and minimal, confined to
verl/models/mcore/util.py.Now let's go through the checklist explicitly:
Assistant
The fix is complete and verified. Let me do a final sanity check of the diff before submission.
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 →
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 →