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 repository structure and understanding the issue.
Assistant
Let me look at the
preprocess_bshd_enginefunction to understand what it expects.Assistant
Let me look at how
compute_forward_kl_topkis called and the related context.Assistant
Let me look at the
preprocess_thd_enginefunction in detail.Assistant
Let me look at the test files to understand the expected input shapes.
Assistant
Let me look at the full
preprocess_bshd_enginefunction and its callers in model_forward.py.Assistant
Let me look at how the loss functions are called and the distillation losses file.
Assistant
Let me look at how
preprocess_bshd_engineis used inmodel_forward.py.Assistant
Let me reproduce the issue first. Let me check the environment and write a reproduction script.
Assistant
Megatron is not installed. Let me check if I can install it, or mock it. Let me look at the test files to understand how to set up the environment.
Let me check if there's a CPU-compatible way to test this. Let me look at how the test imports and what's needed.
Actually, let me look at what's installed and whether I can install megatron.
Assistant
Let me try to install Megatron-Core to enable testing.
Assistant
Megatron-Core installed. Now let me try to write a reproduction script.
Assistant
Let me look at how the test sets up the parallel state, and check the distributed init utilities.
Assistant
Let me write a reproduction script that mocks the megatron parallel state for CPU testing.
Assistant
I've reproduced the error. Let me get the full traceback to understand exactly where it fails.
Assistant
I've confirmed the exact failure. Now let me verify how nested tensor shapes report trailing dims with varying sequence lengths.
Assistant
I've confirmed the failure point. Now let me understand the broader flow - how
data_format="bshd"is used for distillation and how student_logits is shaped. Let me search for where the loss is invoked.Assistant
Let me look at the megatron transformer_impl.py to understand the data flow for bshd.
Assistant
Let me look at the exact lines in
preprocess_bshd_enginethat need modification.Assistant
Now I understand the fix needed. The
preprocess_bshd_enginefunction is hardcoded for 1D sequences (token ids). I need to make it handle trailing dimensions (like thetopkdim inteacher_topk_log_probs). Let me view the exact region and make the edits.Assistant
Now let me make the edits. First, add the
trailing_shapecomputation and updateinput_ids_bshdcreation:Assistant
Now update the
seq_paddedcreation:Assistant
Now update the
position_idsexpand_ascalls to not depend on the trailing dims:Assistant
Let me verify the changes:
Assistant
Now let me run the reproduction script again to verify the fix:
Assistant
The basic fix works. Now let me test edge cases: 1D input (existing token-id usage should not break), variable seqlens, and the need_roll path.
Assistant
I need to fix the test script - can't create a ragged 2D tensor directly.
Assistant
All edge cases pass. Now let me test the full end-to-end loss function with
data_format="bshd"on CPU. I'll set up a single-process gloo group for the TP all-reduce.Assistant
The BSHD end-to-end works and the loss matches the FSDP reference (diff 2.4e-7). The grad comparison failed only because I didn't call backward on the reference. Let me write a more thorough test with variable sequence lengths (to exercise padding) and compare valid positions against the packed thd reference.
Assistant
Excellent! The fix is correct - with variable sequence lengths, the valid positions match the packed thd reference exactly (loss diff 1e-7, grad diff 5e-8). Now let me run the existing tests that might be affected. Let me check what tests exist.
Assistant
Let me search for any tests that exercise the bshd path or the
preprocess_bshdfunctions directly.Assistant
Let me run the existing CPU test to ensure nothing is broken.
Assistant
The existing CPU test passes. Let me also verify the full flow including
postprocess_bshd_engine(which converts the dense 2D loss outputs back to nested) works end-to-end, since that's part of the bshd distillation pipeline.Assistant
I need to also mock
get_context_parallel_groupfor cp_size=1 (postprocess only uses it in the cp_size>1 branch, but calls it unconditionally). Let me add the mock.Assistant
The full flow works end-to-end: preprocess handles the topk dim, loss computes correctly, and postprocess recovers the original per-sequence lengths. Let me review my final changes with git diff.
Assistant
Let me verify the edge case where
trailing_shapeis an emptytorch.Size(1D input) unpacks correctly intorch.zeros.Assistant
All unpacking cases work correctly. Let me run the original reproduction one final time to confirm the fix, and also re-run the existing CPU test.
Assistant
Now let me clean up the temporary test files.
Assistant
Let me do a final check to ensure no stray files were left in the repo and review the final diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.