Skip to content
TheChatArchiveThe Chat Archive
fable-5· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

OPD losses error for Megatron when i use megatron for compute losses the code position…

  1. User

    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.

  2. Assistant

    Let me look at how the function is used and check for related tests.

  3. Assistant

    Now let me verify megatron is available so I can test:

  4. Assistant

    Megatron isn't installed here, so I'll test with a stubbed megatron.core module:

  5. Assistant

    The package __init__ pulls heavier megatron deps. I'll load util.py directly:

  6. Assistant

    CI CPU tests use *_on_cpu.py pattern. I'll add a regression test that skips when megatron isn't importable:

  7. Assistant

    All 3 pass with real megatron-core. Now run the existing distillation CPU test and lint checks:

  8. Assistant

    The exact reported failure path now works end-to-end, including backward. Let me clean up temp files and show the final diff:

  9. Assistant

    Fixed. The root cause: preprocess_bshd_engine hardcoded a 2-D (batch, seqlen) output buffer, while the Megatron OPD loss (verl/trainer/distillation/megatron/losses.py:291) feeds it teacher top-k nested tensors shaped (b, seqlen, topk). The thd path already handled this (it builds shape = list(input_ids.shape[1:]) at util.py:378), so I brought the bshd path to parity.

    Changes

    verl/models/mcore/util.py (preprocess_bshd_engine) — no interface changes:

    1. Allocate the output buffer with trailing dims preserved: (batch, local_max_seqlen, *input_ids.shape[2:])
    2. Pad per-sequence buffers in the CP branch with trailing dims
    3. Replace the two position_ids.expand_as(input_ids_bshd) calls with explicit expand(batch_size, local_max_seqlen) — expand_as would otherwise break (or produce a wrong 3-D position_ids) on 3-D inputs

    tests/models/test_mcore_bshd_engine_trailing_dims_on_cpu.py (new) — regression tests covering 2-D backward-compat, (b, seqlen, topk) with TP alignment padding, and the zigzag CP split with trailing dims (verifies rank r holds chunks r and 2·cp−1−r, positions included). Skips when megatron-core isn't installed.

    Verification

    • Installed megatron-core — all 3 new tests pass
    • End-to-end repro of the reported failure: compute_forward_kl_topk(..., data_format="bshd") with teacher tensors [b, len, 64] on a 1-proc gloo group — forward + backward succeed, outputs shaped (b, seqlen) with finite grads
    • Existing tests/workers/test_distillation_topk_symmetry_on_cpu.py — 4 passed
    • pre-commit run on both files — all hooks pass (ruff, mypy, license, etc.)

    One heads-up per the repo's contribution policy: before opening a PR, run the duplicate-work checks (gh pr list --repo verl-project/verl --state open --search "bshd distillation" etc.), and note the PR description must state AI assistance was used along with the test commands above.

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