Skip to content
TheChatArchiveThe Chat Archive
GLMz-ai/glm-5.2· unknownHugging 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

    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 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. GLM Assistant

    I'll start by exploring the relevant file and understanding the issue.

  3. GLM Assistant

    Let me look at the preprocess_bshd_engine function and preprocess_thd_engine to understand what shapes they expect.

  4. GLM Assistant

    Now I understand the issue. Let me look more closely at preprocess_bshd_engine and preprocess_thd_engine.

    The preprocess_bshd_engine function expects input_ids to be a nested tensor (it calls input_ids.offsets() and input_ids[i] returns a 1D tensor of length n). But in compute_forward_kl_topk, teacher_topk_log_probs has shape (bsz, seqlen, topk) - it's a 3D nested tensor where each element is [seqlen, topk].

    Let me look at preprocess_thd_engine as well to understand the difference.

  5. GLM 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.

  6. GLM Assistant

    Let me look at model_forward.py around line 386 to see how preprocess_bshd_engine is used with 1D input_ids, and around 404 where it's used with potentially multi-dim args.

  7. GLM Assistant

    Let me check the nested tensor shape behavior with a quick test.

  8. GLM Assistant

    Now I can see the issue clearly. The preprocess_bshd_engine function assumes 1D inner tensors, but teacher_topk_log_probs has 2D inner tensors [seqlen, topk]. Let me write a reproduction script with mocked megatron.

  9. GLM Assistant

    Now let me see the full preprocess_bshd_engine function again:

  10. GLM Assistant

    Let me verify the shape behavior with a 1D nested tensor:

  11. GLM Assistant

    Now I have a clear understanding. Let me write a reproduction script with mocked megatron's mpu:

  12. GLM Assistant

    I need to mock more of the megatron imports. Let me check if megatron is installed.

  13. GLM Assistant

    Megatron is not installed. Let me create a more thorough mock to load just the util module:

  14. GLM Assistant

    The error confirms the bug. The preprocess_bshd_engine function doesn't handle the trailing topk dimension. Let me fix the function to handle arbitrary trailing dimensions:

  15. GLM Assistant

    Now let me apply the fix. I need to handle the trailing dimensions:

  16. GLM Assistant

    Now let me fix the seq_padded for the CP > 1 case and the position_ids expand_as issue:

  17. GLM Assistant

    Now let me fix the position_ids expand_as issue, which fails when input_ids_bshd has trailing dims:

  18. GLM Assistant

    Now let me run the reproduction script to verify the cp_size=1 case works:

  19. GLM 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:

  20. GLM 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:

  21. GLM Assistant

    All CP tests pass, including short-sequence padding. Let me also test the need_roll path with 2D tensors and find existing tests:

  22. GLM Assistant

    Let me test the need_roll path with 2D tensors (for completeness, even though losses use default):

  23. GLM Assistant

    All tests pass. Let me review the full diff:

  24. GLM Assistant

    The diff looks clean and is fully backward compatible. Let me clean up the temp files:

  25. GLM Assistant

    The fix is complete. Let me do a final review of the complete updated function:

  26. GLM 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_engine on teacher_topk_log_probs (a nested tensor with shape (bsz, seqlen, topk)) at verl/trainer/distillation/megatron/losses.py line 248 caused an error. The function preprocess_bshd_engine in verl/models/mcore/util.py was written assuming each nested element is 1-D (input_ids of shape [seqlen]), so it:

    1. Created input_ids_bshd as 2-D [batch, local_max_seqlen], which couldn't hold the trailing topk dimension → assignment/broadcast error.
    2. Used position_ids.unsqueeze(0).expand_as(input_ids_bshd), which fails when input_ids_bshd is 3-D (can't expand [1, S] to [B, S, topk]).
    3. In the CP>1 padding path, created seq_padded as 1-D, which couldn't hold the topk dimension.

    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_bshd and the CP-padding seq_padded with *trailing_shape, so they're sized [B, S, topk] (or [B, S] for plain 1-D input_ids).
    • Replaced position_ids.unsqueeze(0).expand_as(input_ids_bshd) with explicit .expand(batch_size, local_max_seqlen) / .expand(batch_size, -1), keeping position_ids correctly 2-D [B, S] regardless of trailing dims.

    Checklist

    • Reproduced the error: Yes — wrote a reproduction with mocked Megatron mpu and confirmed the expand/broadcast RuntimeError before 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_roll path, 1-D backward compatibility (the model_forward.py input_ids/labels/loss_mask usage), and 2-D (bsz, seqlen, topk) tensors. The fix is fully backward compatible — for 1-D nested tensors trailing_shape is 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 the model_forward.py usage patterns (1-D nested tensors). The fix does not change any behavior for the existing 1-D call sites.

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