nemo_rl.models.automodel.draft.loss#
Module Contents#
Functions#
Per-slot loss weights: eval mask times the positional decay. |
|
Compute exact FP32 probability L1 distances without full-vocab temporaries. |
|
Per-batch numerator/denominator sums for the acceptance diagnostics. |
|
Compute the DSpark block-draft loss (CE + L1 + optional confidence). |
Data#
API#
- nemo_rl.models.automodel.draft.loss._PROBABILITY_CHUNK_TOKENS#
128
- nemo_rl.models.automodel.draft.loss._all_reduce_loss_denominators(
- loss_terms: dict[str, torch.Tensor],
- *,
- process_group: Optional[torch.distributed.ProcessGroup],
- nemo_rl.models.automodel.draft.loss._build_loss_weight_mask(
- *,
- eval_mask: torch.Tensor,
- block_size: int,
- device: torch.device,
- loss_decay_gamma: Optional[float],
- first_supervised_slot: int = 0,
Per-slot loss weights: eval mask times the positional decay.
Decay positions count PROPOSALS, not raw block slots: the k-th supervised slot gets
exp(-k / gamma)so the first proposed token always has weight 1. Under the dflash bonus-anchor layout slot 0 is unsupervised (first_supervised_slot = 1); its clamped weight is irrelevant because the eval mask zeroes it.
- nemo_rl.models.automodel.draft.loss._l1_probability_distance_chunk(
- *,
- draft_logits: torch.Tensor,
- target_logits: torch.Tensor,
- nemo_rl.models.automodel.draft.loss._compute_l1_dist_per_token(
- *,
- draft_logits: torch.Tensor,
- aligned_target_logits: torch.Tensor,
- chunk_size: int = _PROBABILITY_CHUNK_TOKENS,
Compute exact FP32 probability L1 distances without full-vocab temporaries.
- nemo_rl.models.automodel.draft.loss._compute_accept_rate_3d(
- l1_dist_per_token: Optional[torch.Tensor],
- nemo_rl.models.automodel.draft.loss._compute_local_l1_term(
- *,
- l1_dist_per_token: Optional[torch.Tensor],
- loss_weight_mask: torch.Tensor,
- nemo_rl.models.automodel.draft.loss._collect_acceptance_diagnostics(
- *,
- outputs: nemo_rl.models.automodel.draft.common.DSparkForwardOutput,
- accept_rate_3d: Optional[torch.Tensor],
- loss_weight_mask: torch.Tensor,
- has_confidence: bool,
Per-batch numerator/denominator sums for the acceptance diagnostics.
accept_rate_3dis the TV-derived per-token acceptance probability. Every diagnostic is returned as an unreduced(num, den)sum; the recipe sums both across the log window and the data-parallel group and forms the global ratio once (sum(num) / sum(den)), so per-micro-batch token-count imbalance never biases the reported value. Returns zero sums when no teacher signal is available (accept_rate_3d is None).
- nemo_rl.models.automodel.draft.loss._collect_local_terms(
- *,
- outputs: nemo_rl.models.automodel.draft.common.DSparkForwardOutput,
- loss_decay_gamma: Optional[float],
- l1_loss_alpha: float,
- nemo_rl.models.automodel.draft.loss.compute_dspark_loss(
- *,
- outputs: nemo_rl.models.automodel.draft.common.DSparkForwardOutput,
- loss_decay_gamma: Optional[float],
- ce_loss_alpha: float,
- l1_loss_alpha: float,
- confidence_head_alpha: float,
- process_group: Optional[torch.distributed.ProcessGroup] = None,
- return_terms: bool = False,
Compute the DSpark block-draft loss (CE + L1 + optional confidence).
The returned
backward_lossis built fromglobal_denominators(all-reduced acrossprocess_group) so it is correct under FSDP’s gradient averaging. The per-term scalars interms(whenreturn_terms=True) are detached copies of the same global-denominator values that make upbackward_loss– summing them across DP ranks (as the training loop’s metric reduction does) reproduces the backward-scale global mean, unlike a per-rank local-denominator mean, which would not.- Parameters:
outputs – forward-pass outputs from the DSpark draft model.
loss_decay_gamma – exponential per-block-position loss-decay factor, or None to disable decay.
ce_loss_alpha – weight on the cross-entropy term.
l1_loss_alpha – weight on the L1 (confidence-target) term.
confidence_head_alpha – weight on the confidence-head term.
process_group – process group to all-reduce loss denominators over (typically the DP group); None to skip the all-reduce.
return_terms – if True, also return a dict of detached diagnostic terms (global-denominator losses plus unreduced accept-rate/tau sums).
- Returns:
backward_lossalone, or(backward_loss, terms)whenreturn_termsis True.
- nemo_rl.models.automodel.draft.loss.__all__#
[‘compute_dspark_loss’]