verl icon indicating copy to clipboard operation
verl copied to clipboard

OPD losses error for Megatron

Open zhaozhaoooo opened this issue 6 months ago • 2 comments

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

zhaozhaoooo avatar May 27 '26 04:05 zhaozhaoooo

I think your shape diagnosis is right.

In the OPD top-k distillation path, teacher_topk_log_probs / teacher_topk_ids are not plain sequence tensors. Each token carries topk values, so their effective shape is:

[batch, seq_len, topk]

For example, with topk=64, the trailing dimension is the 64 teacher top-k logprobs / ids.

The Megatron BSHD path currently sends these tensors through preprocess_bshd_engine, but that helper appears to assume inputs are sequence-shaped:

[batch, seq_len]

So the error is likely not in the KL loss formula itself, but in the BSHD preprocessing step before the loss: it pads/splits the sequence dimension without preserving the extra top-k dimension.

A reasonable fix should be to make preprocess_bshd_engine handle inputs shaped like:

[batch, seq_len, ...]

That way it still pads/splits only along seq_len, while preserving trailing dense dimensions such as topk=64. The attention mask and position ids should remain [batch, seq_len].

I am testing this direction with regression coverage for teacher top-k tensors in the Megatron BSHD distillation path.

anzhsoft avatar May 27 '26 11:05 anzhsoft

I think the issue is that the Megatron BSHD preprocessing path currently assumes sequence-shaped inputs like [batch, seq_len], while OPD top-k distillation passes teacher tensors shaped like [batch, seq_len, topk].

My fix direction is to update preprocess_bshd_engine so it treats inputs as [batch, seq_len, ...]: it should only pad/split along the sequence dimension and preserve any trailing dense dimensions, such as topk=64. The attention mask and position ids should still stay sequence-shaped.

I’ll also add regression coverage for the teacher top-k tensors so the BSHD path is checked against this case.

anzhsoft avatar May 27 '26 12:05 anzhsoft