OPD losses error for Megatron when i use megatron for compute losses the code position…
- 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.
- Assistant
Investigating the Megatron OPD loss path and how
preprocess_bshd_engineexpects tensors shaped. - Assistant
Verifying nested tensor shapes and implementing the fix.
- Assistant
Updating
preprocess_bshd_engineto handle nested tensors with trailing dimensions (e.g. top-k), matchingpreprocess_thd_engine. - Assistant
Keeping
position_ids2D when inputs have trailing dimensions. - Assistant
The failure came from a shape mismatch in the BSHD preprocessing path for OPD top-k tensors.
Root cause: With Megatron and
data_format="bshd"(use_remove_padding=False),compute_forward_kl_topkcallspreprocess_bshd_engineonteacher_topk_log_probs/teacher_topk_ids. Those are nested tensors shaped[batch, jagged, topk](each sequence is[length, 64]).preprocess_bshd_engineonly handled 1D sequences and built a(batch, local_max_seqlen)buffer, so assigning[length, 64]into[length]failed.The THD path already worked because
preprocess_thd_enginekeeps trailing dimensions viashape = list(input_ids.shape[1:]).Fix in
verl/models/mcore/util.py:- Detect trailing dims from
input_ids.shape[2:](e.g.topk=64) - Allocate BSHD output as
(batch, local_max_seqlen, *trailing_dims) - Pad multi-dimensional sequences correctly in the CP path
- Keep
position_ids2D(batch, seq)even when inputs have a top-k dimension
- Detect trailing dims from
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.