Skip to content

Add new top1 loss functions - #868

Open
klei22 wants to merge 2 commits into
ReaLLMASIC:masterfrom
klei22:add_new_top1_loss_functions
Open

Add new top1 loss functions#868
klei22 wants to merge 2 commits into
ReaLLMASIC:masterfrom
klei22:add_new_top1_loss_functions

Conversation

@klei22

@klei22 klei22 commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

This pull request introduces new loss functions inspired by the avg_top1_correct metric to improve top-1 prediction accuracy during training. It adds two new loss variants—top1_corrective_ce and top1_confidence_gap—along with their configuration options, documentation, and sweep YAML for experimentation. The changes also refactor some existing loss code for clarity and reusability.

New loss functions and configuration:

  • Added the top1_corrective_ce_loss function, which up-weights cross-entropy loss for currently top-1-incorrect tokens based on the batch's top-1 error rate, and the top1_confidence_gap_loss, which adds a differentiable penalty for the target logit's gap to the strongest competitor. Both are registered in the loss function registry and can be selected via command-line arguments. [1] [2] [3]
  • Introduced --top1_corrective_boost and --top1_confidence_gap_beta CLI arguments in train_args.py to control the strength of the new losses.

Documentation and experiment configuration:

  • Added documentation/top1_correct_losses.md to explain the motivation, implementation, and usage of the new loss variants, with practical sweep suggestions.
  • Created explorations/top1_correctness_loss_sweep.yaml to define a sweep comparing the new losses against cross-entropy, including hyperparameter ranges and warm-start schedules.

Refactoring and utility improvements:

  • Added utility functions _flatten_logits_targets and top1_correct_mask to standardize handling of logits, targets, and masks for loss computation and correctness checking.
  • Refactored top1_focus_loss to use the new top1_correct_mask utility and clarified the batch penalty computation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds two new top-1-accuracy-oriented loss variants to the training loss registry (top1_corrective_ce and top1_confidence_gap), along with CLI knobs, documentation, and a sweep YAML to experiment with their hyperparameters.

Changes:

  • Added utilities to standardize logits/targets flattening and top-1 correctness masking, and refactored top1_focus_loss to use them.
  • Implemented and registered two new loss variants (top1_corrective_ce, top1_confidence_gap) plus their CLI arguments.
  • Added documentation and an exploration sweep YAML for comparing these losses against a cross-entropy baseline and warm-start schedules.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
train_variations/loss_variants.py Adds new top-1 loss variants, utilities (_flatten_logits_targets, top1_correct_mask), and wires them into the loss registry/arg dispatch.
train_args.py Introduces CLI hyperparameters for the new loss variants.
explorations/top1_correctness_loss_sweep.yaml Adds a sweep spec to compare new losses and hyperparameter ranges, including warm-start schedules.
documentation/top1_correct_losses.md Documents motivation, implementation, usage, and sweep suggestions for top-1-oriented loss variants.
Comments suppressed due to low confidence (1)

train_variations/loss_variants.py:425

  • top1_corrective_ce_loss and top1_confidence_gap_loss are each defined twice in this module (first at lines ~339/368, then again starting at ~421/450). The later redefinitions silently override the earlier ones, which is confusing and makes future edits error-prone. Remove the duplicate second block and keep a single canonical definition for each loss.
def top1_corrective_ce_loss(
    logits: torch.Tensor,
    targets: torch.Tensor,
    *,
    iter_num: int | None = None,

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +339 to +365
def top1_corrective_ce_loss(
logits: torch.Tensor,
targets: torch.Tensor,
*,
iter_num: int | None = None,
boost: float = 1.0,
) -> torch.Tensor:
"""Cross entropy that up-weights currently top-1-incorrect tokens.

This directly mirrors ``avg_top1_correct`` at batch scale: the loss estimates
the current batch's top-1 error rate and uses it to increase emphasis on
tokens whose argmax prediction is wrong.
"""

logits_flat, targets_flat, mask = _flatten_logits_targets(logits, targets)
losses = F.cross_entropy(logits_flat, targets_flat, reduction="none", ignore_index=-1)
if not mask.any():
return losses.new_full((), 0.0)

with torch.no_grad():
predictions = torch.argmax(logits_flat, dim=-1)
incorrect = (predictions != targets_flat) & mask
batch_error_rate = incorrect.float().sum() / mask.float().sum().clamp_min(1.0)
weights = torch.ones_like(losses)
weights[incorrect] = 1.0 + boost * batch_error_rate

return (losses[mask] * weights[mask]).mean()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants