Skip to content
TheChatArchiveThe Chat Archive
QwenQwen/Qwen3.6-27B· alibabaHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION OPD losses error for Megatron when i use megatron for compute losses…

  1. 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 using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to 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-$

  2. Qwen Assistant

    I'll start by exploring the repository structure and understanding the issue.

  3. Qwen Assistant

    Let me find the specific file mentioned in the issue.

  4. Qwen Assistant

    The path in the issue description is relative. Let me search for the actual path.

  5. Qwen Assistant

    Found it! Let me open this file.

  6. Qwen Assistant

    The open command doesn't take a command parameter. Let me use the correct syntax.

  7. Qwen 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.

  8. Qwen Assistant

    Let me examine the preprocess_bshd_engine function to understand what input shape it expects:

  9. Qwen Assistant

    Let me look at the preprocess functions:

  10. Qwen Assistant

    Now I can see the issue. Looking at the preprocess_bshd_engine function:

    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_ids to be a nested tensor (it calls input_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_probs has shape [b, length, 64] (3D tensor with topk dimension), but preprocess_bshd_engine expects a 2D tensor or nested tensor [b, length].

    Let me also look at the preprocess_thd_engine function to see how it handles this:

  11. Qwen Assistant

    Let me continue reading the preprocess_thd_engine function:

  12. Qwen 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_engine can handle multi-dimensional inputs (like [b, length, topk]), but preprocess_bshd_engine is hardcoded for 2D tensors.

    Let me look more closely at preprocess_bshd_engine:

  13. Qwen Assistant

    Now I understand the issue. The preprocess_bshd_engine function:

    1. Creates a 2D tensor input_ids_bshd with shape [batch_size, local_max_seqlen] (line 601)
    2. 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_engine handle tensors with additional dimensions (like the topk dimension) similar to how preprocess_thd_engine does it.

    Let me also look at postprocess_bshd_engine to understand the full picture:

  14. Qwen Assistant

    Now let me look at the tests to understand how these functions are supposed to be used:

  15. Qwen Assistant

    Let me look at how the function is called and what input it expects more carefully:

  16. Qwen Assistant

    So the inputs are nested tensors. Let me understand the issue better. Let me create a script to reproduce the error:

  17. Qwen Assistant

    I've confirmed the issue. The problem is that preprocess_bshd_engine creates a 2D tensor but the input can be 3D (with an extra dimension like topk). The fix needs to preserve the extra dimensions.

    Let me look at how preprocess_thd_engine handles this:

  18. Qwen 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 to preprocess_bshd_engine.

    Let me now look at the full preprocess_bshd_engine function and understand all the places that need to be changed:

  19. Qwen Assistant

    Now I understand all the places that need to be fixed. The issue is on lines 600-601 where input_ids_bshd is created with shape [batch_size, local_max_seqlen] but it should preserve any extra dimensions. Let me trace through the changes needed:

    1. Line 600-601: input_ids_bshd should have shape [batch_size, local_max_seqlen, *extra_dims]
    2. Line 612: seq_padded should also preserve extra dimensions
    3. 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:

  20. Qwen Assistant

    Good! Now let me verify my fix by running the reproduction script:

  21. Qwen Assistant
  22. Qwen 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.

  23. Qwen Assistant

    You're right, let me verify the changes I made and run the existing tests to ensure the fix is correct.

  24. Qwen Assistant

    Good, the changes look correct. Now let me check if there are any existing tests for this function and run them.

  25. Qwen 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.

  26. Qwen 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:

    1. Identified the issue in preprocess_bshd_engine in /verl/verl/models/mcore/util.py
    2. Applied a fix to preserve extra dimensions beyond (batch, sequence)
    3. Created a simple test that confirms the fix works

    Now I need to:

    1. Run existing tests to make sure the fix doesn't break anything
    2. 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.

  27. Qwen 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.

  28. Qwen Assistant

    Let me continue by creating a cleaner test script that properly tests my fix without trying to exec the file.

  29. Qwen 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.

  30. Qwen 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.

  31. Qwen Assistant

    Excellent! All existing tests pass. Let me verify my final changes one more time:

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.