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
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 find the specific file mentioned in the issue.
Assistant
The path in the issue description is relative. Let me search for the actual path.
Assistant
Found it! Let me open this file.
Assistant
The open command doesn't take a command parameter. Let me use the correct syntax.
Assistant
The user seems to have encountered an error with the tool call format. Let me use the correct format for the open command - it should just be the file path without any arguments key.
Assistant
Let me examine the
preprocess_bshd_enginefunction to understand what input shape it expects:Assistant
Let me look at the preprocess functions:
Assistant
Now I can see the issue. Looking at the
preprocess_bshd_enginefunction:def preprocess_bshd_engine( input_ids: torch.Tensor, pre_process: bool = True, need_roll: bool = False, use_fp8_padding: bool = False ):The function expects
input_idsto be a nested tensor (it callsinput_ids.offsets().diff()on line 430), and it expects it to have shape[batch, length](2D), not[batch, length, topk](3D).Looking at line 248 in losses.py:
teacher_topk_log_probs_cp_split, *_ = preprocess_bshd_engine(teacher_topk_log_probs, pre_process=True)The issue is that
teacher_topk_log_probshas shape[b, length, 64](3D tensor with topk dimension), butpreprocess_bshd_engineexpects a 2D tensor or nested tensor[b, length].Let me also look at the
preprocess_thd_enginefunction to see how it handles this:Assistant
Let me continue reading the
preprocess_thd_enginefunction:Assistant
I see! The key line is 378:
shape = list(input_ids.shape[1:])This preserves the shape beyond the batch dimension. So
preprocess_thd_enginecan handle multi-dimensional inputs (like[b, length, topk]), butpreprocess_bshd_engineis hardcoded for 2D tensors.Let me look more closely at
preprocess_bshd_engine:Assistant
Now I understand the issue. The
preprocess_bshd_enginefunction:- Creates a 2D tensor
input_ids_bshdwith shape[batch_size, local_max_seqlen](line 601) - Assigns
input_ids[i]which would be[seqlen, topk]if the input is 3D
But it tries to assign a
[seqlen, topk]tensor into a[local_max_seqlen]slot, which would fail.The fix should make
preprocess_bshd_enginehandle tensors with additional dimensions (like the topk dimension) similar to howpreprocess_thd_enginedoes it.Let me also look at
postprocess_bshd_engineto understand the full picture:- Creates a 2D tensor
Assistant
Now let me look at the tests to understand how these functions are supposed to be used:
Assistant
Let me look at how the function is called and what input it expects more carefully:
Assistant
So the inputs are nested tensors. Let me understand the issue better. Let me create a script to reproduce the error:
Assistant
I've confirmed the issue. The problem is that
preprocess_bshd_enginecreates a 2D tensor but the input can be 3D (with an extra dimension liketopk). The fix needs to preserve the extra dimensions.Let me look at how
preprocess_thd_enginehandles this:Assistant
Perfect! The key line is 378:
shape = list(input_ids.shape[1:]). This preserves all dimensions after the batch dimension. Now I need to apply the same fix topreprocess_bshd_engine.Let me now look at the full
preprocess_bshd_enginefunction and understand all the places that need to be changed:Assistant
Now I understand all the places that need to be fixed. The issue is on lines 600-601 where
input_ids_bshdis created with shape[batch_size, local_max_seqlen]but it should preserve any extra dimensions. Let me trace through the changes needed:- Line 600-601:
input_ids_bshdshould have shape[batch_size, local_max_seqlen, *extra_dims] - Line 612:
seq_paddedshould also preserve extra dimensions - Line 642 and 655:
position_ids.expand_as(input_ids_bshd)- this should still work because it will expand to match the shape
Let me create the fix:
- Line 600-601:
Assistant
Good! Now let me verify my fix by running the reproduction script:
Assistant
Assistant
The previous test script failed because of import issues with megatron. Let me create a simpler test that directly tests the logic without importing the full module.
Assistant
You're right, let me verify the changes I made and run the existing tests to ensure the fix is correct.
Assistant
Good, the changes look correct. Now let me check if there are any existing tests for this function and run them.
Assistant
Let me continue by running the existing tests to make sure my fix doesn't break anything. I found earlier that there are tests related to distillation that might be affected.
Assistant
The user is asking me to continue. Let me check what I've done so far and continue from where I left off.
I've:
- Identified the issue in
preprocess_bshd_enginein/verl/verl/models/mcore/util.py - Applied a fix to preserve extra dimensions beyond (batch, sequence)
- Created a simple test that confirms the fix works
Now I need to:
- Run existing tests to make sure the fix doesn't break anything
- Verify the fix works in the context it was originally failing
Let me check the test file I found earlier that tests the special megatron KL loss.
- Identified the issue in
Assistant
You're right, I apologize for the malformed function call. Let me continue properly by verifying my fix works correctly.
Looking at the test file, I notice it only tests the
data_format="thd"path, not the"bshd"path. Let me create a script that reproduces the original issue and confirms my fix works.Assistant
Let me continue by creating a cleaner test script that properly tests my fix without trying to exec the file.
Assistant
I've successfully verified that the fix works. Now let me check if there are any existing tests I should run to ensure I haven't broken anything.
Assistant
Let me verify my changes one more time and also check if there are any other tests in the workers directory that might test distillation functionality.
Assistant
Excellent! All existing tests pass. Let me verify my final changes one more time:
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
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →