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 repository structure and understanding the issue.

  3. GLM Assistant

    Let me find the file.

  4. GLM Assistant

    Now let me look at the preprocess_bshd_engine and preprocess_thd_engine functions.

  5. GLM Assistant

    Let me look at the full util.py to understand preprocess_bshd_engine and preprocess_thd_engine better. Let me also look at how compute_forward_kl_topk is called.

  6. GLM Assistant

    Let me look at the test file to understand the expected input shape.

  7. GLM Assistant

    Let me look at preprocess_thd_engine.

  8. GLM Assistant

    Let me look at the distillation losses.py to understand the flow and how data is prepared.

  9. GLM Assistant

    Let me look at how teacher_logprobs/teacher_ids are prepared and where the logits processor calls compute_topk_loss.

  10. GLM Assistant

    Let me look at the full preprocess_bshd_engine function once more, carefully.

  11. GLM Assistant

    Let me find all usages of preprocess_bshd_engine to understand the impact of changes.

  12. GLM Assistant

    Let me look at how preprocess_bshd_engine is used in model_forward.py.

  13. GLM Assistant

    Let me write a quick test script to understand the shape behavior of jagged nested tensors with trailing dims.

  14. GLM Assistant

    Let me look at the megatron transformer_impl.py to understand the bshd data flow for distillation.

  15. GLM Assistant

    Let me check whether megatron is available and run the existing test setup to understand the environment.

  16. GLM Assistant

    Megatron isn't installed. Let me write a reproduction script that mocks the mpu functions to test preprocess_bshd_engine directly with a 2D-element nested tensor (simulating the bshd distillation path).

  17. GLM Assistant

    Let me make a more complete mock of megatron to bypass the import chain.

  18. GLM Assistant

    I've reproduced the error. The root cause is that preprocess_bshd_engine doesn't preserve the trailing dense dimension (e.g., topk) of the jagged nested tensor, while preprocess_thd_engine does. Let me examine the exact lines to fix.

  19. GLM Assistant

    Now I understand the issue completely. Let me implement the fix in preprocess_bshd_engine to preserve the trailing dense dimension (like preprocess_thd_engine does). First, let me add the trailing_shape computation and update the input_ids_bshd creation.

  20. GLM Assistant

    Let me verify the changes look correct.

  21. GLM Assistant

    Now let me run the reproduction script again to verify the fix works for the 2D case, and also add a check for the 1D case to ensure I didn't break it.

  22. GLM Assistant

    The fix works for both 1D and 2D cases. Now let me test the CP (context parallel) branch with cp_size=2 to ensure it handles 2D elements correctly too.

  23. GLM Assistant

    The CP branch works correctly too. Let me also test the need_roll=True path with 2D elements to ensure the model forward path (labels) isn't broken.

  24. GLM Assistant

    Let me re-examine the compute_forward_kl_topk in losses.py to confirm the data flow and assertion ordering.

  25. GLM Assistant

    The data flow is confirmed. Now let me try to run the full compute_forward_kl_topk end-to-end with a single process to validate the bshd path. First, let me check if I can install megatron-core.

  26. GLM Assistant

    Megatron-core is installed. Let me verify the import works and check the existing test can at least be collected.

  27. GLM Assistant

    Megatron imports work. Now let me write a single-process end-to-end test that runs compute_forward_kl_topk with data_format="bshd" and compares against the FSDP reference implementation.

  28. GLM Assistant

    The assertion fails because my test passes student logits in the wrong shape for bshd. For bshd, the model produces logits as (B, S, V) (not packed (1, B*S, V) like thd). Let me fix the test to use the correct shapes per format.

  29. GLM Assistant

    The VP (megatron) bshd path now passes its assertion. The error moved to the FSDP reference. Let me look at the FSDP compute_forward_kl_topk.

  30. GLM Assistant

    The FSDP reference always packs into (1, total_nnz, ...). Let me rewrite the test to flatten both outputs for comparison since the shapes differ between bshd (VP keeps (B,S)) and the packed FSDP reference.

  31. GLM Assistant

    Both bshd and thd paths work end-to-end and match the FSDP reference. Let me run the existing relevant tests to make sure nothing is broken. Let me find them.

  32. GLM Assistant

    The existing test passes. Let me check if there are GPUs available to run the multi-process TP test.

  33. GLM Assistant

    No GPUs. The multi-process TP test requires CUDA. My single-process gloo test already validates the megatron KL loss path. Let me search for any other tests touching preprocess_bshd_engine or the model_forward bshd path.

  34. GLM Assistant

    No tests directly test preprocess_bshd_engine. Let me verify that input_ids.shape[2:] returns concrete ints (not symbolic) that can be safely passed to torch.zeros.

  35. GLM Assistant

    trailing_shape returns concrete ints. Now let me check if postprocess_bshd_engine already supports 3D (to confirm consistency), and run any related mcore model tests.

  36. GLM Assistant

    Confirmed — postprocess_bshd_engine (line 720) and the thd variants already support trailing dims via *output.shape[2:]. My fix makes preprocess_bshd_engine consistent. Let me run a broader set of related tests.

  37. GLM Assistant

    Let me check the tests/utils/megatron directory and the distillation tests directory.

  38. GLM Assistant

    Let me verify the full module imports correctly with the installed megatron, and run a final comprehensive validation.

  39. GLM Assistant

    Let me run all my validation scripts and the existing test one final time to confirm everything works.

  40. GLM Assistant

    All tests pass. Let me clean up the temporary test files and review the final diff.

  41. GLM Assistant

    The diff is clean and minimal. Let me do a final check of git status and confirm only the intended file is changed.

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