nemo_rl.algorithms.loss.utils#
Module Contents#
Functions#
Change global loss denominators while preserving raw counts and extrema. |
|
Restrict full-vocab teacher logits to the draft vocabulary via |
|
Left-shift a packed tensor by one along |
|
Build the packed draft-loss mask |
|
Project each sample’s hidden states through its own teacher’s LM head. |
|
Turn the transported teacher payload into this rank’s teacher logit shard. |
|
Pack input_ids from [B, S] to [1, T_packed // CP] using sequence boundaries. |
API#
- nemo_rl.algorithms.loss.utils.rescale_loss_metrics(
- metrics: dict[str, Any],
- normalizers: dict[str, nemo_rl.algorithms.loss.interfaces.MetricNormalizer],
- *,
- token_factor: float,
- sequence_factor: float,
Change global loss denominators while preserving raw counts and extrema.
Metrics absent from
normalizersare returned unchanged. Unlike split-API normalization of raw sums, this only adjusts denominators explicitly advertised by the loss.
- nemo_rl.algorithms.loss.utils.map_teacher_logits_to_draft_vocab(
- teacher_logits: torch.Tensor,
- d2t: Optional[torch.Tensor],
- vocab_parallel_rank: Optional[int] = None,
- vocab_parallel_group: Optional[torch.distributed.ProcessGroup] = None,
Restrict full-vocab teacher logits to the draft vocabulary via
d2t.d2tmaps draft-vocab indexito target-vocab indexi + d2t[i]. Under tensor parallelism the teacher logits arrive vocab-sharded, so they are gathered to the full vocab and re-sliced to this rank’s shard of the draft vocabulary (the draft output layer is sharded the same way). No-op whend2tis None (full-vocab drafts).
- nemo_rl.algorithms.loss.utils.roll_packed_seq_dim(
- tensor: torch.Tensor,
- cu_seqlens_padded: torch.Tensor,
- seq_dim: int,
Left-shift a packed tensor by one along
seq_dimwithin each segment.Equivalent to a per-sequence
torch.roll(shifts=-1)over the packed layout: one global roll followed by zeroing each segment’s final slot (the only positions where the global roll would leak the next segment’s first row). Segment boundaries come fromcu_seqlens_padded, the physical offsets of the packed layout.
- nemo_rl.algorithms.loss.utils.pack_rolled_draft_token_mask(
- token_mask: torch.Tensor,
- sample_mask: torch.Tensor,
- cu_seqlens: torch.Tensor,
- cu_seqlens_padded: torch.Tensor,
Build the packed draft-loss mask
[1, T_packed]from unpacked masks.Mirrors the non-packed DRAFT prepare (
token_maskleft-shifted by one, scaled bysample_mask), laid out at each sequence’s padded offset. Each sequence’s last real slot (whose shifted target would cross the boundary) and all padding slots stay zero.Reuses
_pack_input_idsfor the padded-offset layout (including its clamp for bin-alignment padding absorbed into the last sequence’s effective length) androll_packed_seq_dimfor the boundary-safe per-segment left shift.
- payload: torch.Tensor,
- weight_by_index: dict[int, torch.Tensor],
- teacher_index: torch.Tensor,
Project each sample’s hidden states through its own teacher’s LM head.
payloadis[B, S, H];teacher_indexis[B]int, naming which loaded teacher LM head produced each row (seeOPD_FULL_TEACHER_INDEX_FIELD). Grouped by teacher so this costs one matmul per distinct teacher present in the microbatch, not a per-sample loop – when sequence packing is on, each call already hasB == 1(one sequence at a time), so it takes the single-teacher path and never materializes the scatter buffer.- Raises:
ValueError – If
teacher_indexdoes not carry one entry per payload row, if a row’s teacher_index has no loaded LM head, or if an LM head disagrees with the payload or with the other teachers’.
- nemo_rl.algorithms.loss.utils.reconstruct_opd_full_teacher_logits(
- payload: torch.Tensor,
- *,
- teacher_payload: str,
- student_logits: torch.Tensor,
- vocab_parallel_rank: Optional[int],
- context_parallel_group: Optional[torch.distributed.ProcessGroup],
- teacher_output_layer_weight_by_index: Optional[dict[int, torch.Tensor]] = None,
- teacher_index: Optional[torch.Tensor] = None,
Turn the transported teacher payload into this rank’s teacher logit shard.
The payload is canonical
[B, S, D](already TP/CP-gathered on the teacher side), so it is re-sharded onto the student’s own CP window here, exactly asfrom_parallel_logits_to_logprobsdoes for the student logits.- Parameters:
payload – Teacher payload
[B, S, D]– hidden states or full logits.teacher_payload –
"hidden_states"or"logits".student_logits – Student logits
[B, S_local, V_local], used for the target device, dtype-independent shapes, and CP geometry.vocab_parallel_rank – This rank’s vocabulary-parallel rank.
context_parallel_group – Context-parallel process group, if any.
teacher_output_layer_weight_by_index –
[V_local, H_teacher]teacher LM-head shards for the hidden-state path, keyed by the stable index rows are tagged with. Routed byteacher_indexwhenever that column is present.teacher_index –
[B]int, this microbatch’s per-row teacher index (seeOPD_FULL_TEACHER_INDEX_FIELD). Required onceteacher_output_layer_weight_by_indexholds more than one entry, and validated against the loaded heads whenever present.
- Returns:
Teacher logits
[B, S_local, V_local]aligned withstudent_logits.- Raises:
ValueError – If the payload and student shard cannot be aligned, if the hidden-state path is used without a teacher LM-head shard, or if several shards are loaded without a
teacher_indexto route by.
- nemo_rl.algorithms.loss.utils._pack_input_ids(
- input_ids: torch.Tensor,
- cu_seqlens_q: nemo_rl.utils.sequence_lengths.CpuIntTuple,
- cu_seqlens_q_padded: nemo_rl.utils.sequence_lengths.CpuIntTuple,
- cp_rank: int = 0,
- cp_size: int = 1,
- roll_shift: int = 0,
Pack input_ids from [B, S] to [1, T_packed // CP] using sequence boundaries.
Each sequence is individually padded to its padded length (from cu_seqlens_q_padded), optionally rolled, and CP-sharded at that padded length before being placed into the packed output. This matches how Megatron packs and CP-shards sequences in _pack_sequences_for_megatron.
- Parameters:
input_ids – Unpacked input IDs [B, S].
cu_seqlens_q – Unpadded cumulative sequence lengths [B+1].
cu_seqlens_q_padded – Padded cumulative sequence lengths [B+1].
cp_rank – Context parallelism rank.
cp_size – Context parallelism size.
roll_shift – If non-zero, roll each padded sequence by this amount before CP-sharding. Use -1 to build shifted targets for next-token prediction.