Skip to content

Preserve elementwise mean in logits distillation - #3691

Closed
oj9o wants to merge 2 commits into
NVIDIA:mainfrom
oj9o:distill/preserve-elementwise-kl-mean
Closed

oj9o wants to merge 2 commits into
NVIDIA:mainfrom
oj9o:distill/preserve-elementwise-kl-mean

Conversation

@oj9o

@oj9o oj9o commented Dec 3, 2026 •

Copy link
Copy Markdown

What does this PR do?

Type of change: Bug fix.

The default reduction in LogitsDistillationLoss averages KL divergence over every element. PyTorch warns that its mean reduction will change to batch averaging. For logits shaped [batch, classes], that would multiply the loss and the student gradient by the number of classes.

This change requests the pointwise KL terms and takes their mean explicitly for the default reduction. It preserves the existing training objective and leaves the other reductions on their current paths. Related to #1926, item 2.

Testing

  • Ran the new regression in isolation on CPU with Python 3.14.7 and PyTorch 2.15.0. It failed on the expected warning against the original implementation and passed after the change. It checks the loss and student gradient at temperature 2.
  • Compared loss values and student gradients against the original implementation in 192 CPU cases covering four reductions, four floating-point types, two temperatures, and six input shapes including empty tensors. The results matched.
  • Ruff 0.16.10 lint and format checks and git diff --check passed on the changed files.
  • The full tests/unit/torch/distill/test_distill.py file could not collect locally because OmegaConf was unavailable in the test environment.

Before your PR is ready for review

  • Backward compatible: Yes.
  • Copied third-party code or added dependencies: N/A.
  • Regression test added: Yes.
  • Changelog: N/A. This preserves existing numerical behavior.
  • Claude review: Not run.

Additional information

OpenAI Codex assisted with the investigation, implementation, test design, and local validation.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected the mean-reduction behavior for logits distillation loss. The reported loss now reflects the mean of the elementwise KL-divergence values, while other reduction settings and temperature scaling remain unchanged.
    • Updated gradients for the default reduction to align with the corrected loss calculation.

oj9o added 2 commits December 3, 2026 17:41
Compute the default mean over the pointwise KL terms so its scale stays independent of the planned change to PyTorch’s KL reduction.

Related to NVIDIA#1926, item 2.

Signed-off-by: oj9o <imouza@msn.com>
Exercise the loss at a non-unit temperature and treat the PyTorch KL mean warning as an error in this regression.

Signed-off-by: oj9o <imouza@msn.com>
@oj9o
oj9o requested review from a team as code owners December 3, 2026 13:42
@oj9o
oj9o requested a review from kevalmorabia97 December 3, 2026 13:42
@copy-pr-bot

copy-pr-bot Bot commented Dec 3, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

LogitsDistillationLoss now computes its mean reduction by averaging unreduced KL-divergence values. A regression test checks the resulting loss and student gradient.

Changes: Distillation loss

Layer / File(s) Summary
Mean reduction and regression test
modelopt/torch/distill/losses.py, tests/unit/torch/distill/test_distill.py
The mean reduction averages unreduced KL-divergence values. The test checks the loss and student gradient.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 7dca5

This change keeps the default logits distillation loss as an elementwise mean, so training results stay the same when PyTorch changes how its mean reduction works. No merge-blocking risk was found.

Security architecture risk: Minimal · up to 7dca5

LogitsDistillationLoss keeps its existing elementwise average without relying on PyTorch’s default reduction behavior. The HuggingFace distillation plugin explicitly selects a different reduction and stays on the same path.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Preserve elementwise mean in logits distillation" matches the main change. The raw summary says the default "mean" reduction now computes pointwise KL terms and averages them elementwise, a…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Security Anti-Patterns Passed The PR (NVIDIA/Model-Optimizer #3691, author login oj9o) changes two files: modelopt/torch/distill/losses.py and tests/unit/torch/distill/test_distill.py. I read the full committed diff for the re…
Full details: Title check

Explanation

The title "Preserve elementwise mean in logits distillation" matches the main change. The raw summary says the default "mean" reduction now computes pointwise KL terms and averages them elementwise, and the objectives describe the same fix for PyTorch's changed mean reduction. The title is specific, short, and states the primary change.

Full details: Security Anti-Patterns

Explanation

The PR (NVIDIA/Model-Optimizer #3691, author login oj9o) changes two files: modelopt/torch/distill/losses.py and tests/unit/torch/distill/test_distill.py. I read the full committed diff for the reviewed range. Its added lines contain none of the patterns in the Security anti-patterns check: 1. No torch.load call, and no weights_only=False. 2. No numpy.load, and no allow_pickle. 3. No trust_remote_code. 4. No eval() or exec(). 5. No # nosec comment. 6. No change to pyproject.toml or requirements.txt, so no new pip dependency. The losses.py change only changes the KL-divergence reduction path (reduction="none" followed by an explicit .mean()). The test adds a regression test that uses only torch and mtd.LogitsDistillationLoss. Neither change touches a security-sensitive surface.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4a9180b and 7dca5fe.

Files selected for processing: 2

  • modelopt/torch/distill/losses.py
  • tests/unit/torch/distill/test_distill.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

@AAnoosheh

Copy link
Copy Markdown
Contributor

Hi, I'd rather keep it passed through to Pytorch and use its default behavior rather than trying to preserve an outdated definition. But thank you!

@AAnoosheh AAnoosheh closed this Dec 3, 2026
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