Skip to content

Keep fused-loss engagement counters out of traced code - #15575

Merged
danielhanchen merged 10 commits into
mainfrom
fused-route-counters-compile
Nov 3, 2026
Merged

danielhanchen merged 10 commits into
mainfrom
fused-route-counters-compile

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #14185. Fused-loss engagement counters were updated inside traced scoring helpers, so repeated calls guarded on an ever-changing Python integer and recompiled until fullgraph=True reached the limit.

Count engagement only outside tracing. Eager calls still update the counters; compiled calls retain the same loss and gradient calculations. This covers the preference, DPO, KTO, online DPO, GKD, PPO and RLOO scoring helpers.

The branch is synced with main. The original TRL contract-whitelist half has been removed because #16220 already fixes its reported compatibility crash. The fused-head adapter and installer are identical to main, including TRL 1.17 score-ID support.

Validation completed so far:

  • Against current main, all three compilation modes reproduce the counter failure before the fix and pass afterward. The ten existing numerical and gradient cases pass on both revisions, with no skips in this differential.
  • AST comparison confirms the nine changed scoring functions differ from main only by engagement-counter tracing guards.
  • On torch 2.15.0, Transformers 5.22.1, TRL 1.17.1 and PEFT 0.20.0, the compiled-loss and score-ID suites report 21 passed with no skips.
  • The final focused suite reports 35 passed with no skips. The full current-base differential reports four FIXED and 31 UNCHANGED checks, no head failures and no skipped cases.
  • Final-source SFT, DPO and GPT-OSS GRPO on Kaggle T4, plus ordinary GRPO and inference on A100, report NO_REGRESSION. DPO losses and gradient norms match exactly; peak memory is equal.
  • Remote regression smoke reports SAME: 66 passed, 13 XPASS and 24 skipped on each side, with no changed failures, metric moves or missing cases.
  • The main switchboard has no functional regression. Two crawler visual differences did not reproduce on confirmation.
  • Full-size vision GRPO crashes before step one on both base and head with the same pre-existing checkpoint metadata mismatch on torch 2.13 and 2.14.1. The upstream FIFO workaround does not resolve it. The supported tiny-Gemma4 comparison passes on the exact current head: three steps on both arms, all image forwards engaged, identical final loss, zero graph breaks and equal compiled-graph counts. The default full-size checkpoint failure remains a pre-existing limitation.
  • Final-source scoped staging passed all 35 tests on Linux, Windows and Apple Silicon macOS.

The tests use small synthetic tensors and random model initialization. No model checkpoint weights are required for the reproductions.

The trainer counters (_unsloth_*_chunked_calls / *_fused_calls) were read
and written inside the chunked and fused loss helpers. Under torch.compile
Dynamo guards on the counter value, so every step recompiled; with
fullgraph=True ORPO / CPO hit the recompile limit after 8 steps. Count only
when not compiling; eager runs still report engagement.

The ORPO decline test also assumed importing unsloth had rebound TRL's
ORPOTrainer to the patched class. Where that build fails the stock class has
no rewritten method, so the override check has nothing to compare against and
the test failed. Build the patched class in the test instead.
The installer wrapped any _ChunkedLogProbFunction whose forward took a bias.
TRL main now passes support_ids and score_ids on every call and returns six
outputs, so the wrapped apply() raised TypeError before any fallback could
run. Compare the forward signature with the contracts the adapter reproduces
(TRL 1.14, 1.15 - 1.16, and the support_ids slot) and leave any other kernel,
or one whose signature cannot be read, untouched.

Tests: a TRL-main-shaped kernel stays TRL's and serves its 11-argument call;
the installed TRL kernel (1.14, 1.15, 1.16) is still wrapped and the fused
path serves it, matching the float64 reference in value and gradient.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T05:02:34.446999Z 43d0363 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: c63094ba54

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 7f96c39c96

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 6cfbd82e34

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@shimmyshimmer shimmyshimmer changed the title Fused-loss routes: counters out of traced code, wrap only known TRL head contracts Keep fused-loss counters out of tracing and gate adapter contracts Nov 3, 2026
@shimmyshimmer shimmyshimmer changed the title Keep fused-loss counters out of tracing and gate adapter contracts Keep fused-loss engagement counters out of traced code Nov 3, 2026
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 3, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 43d0363b99

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from danielhanchen Nov 3, 2026
@unslothai unslothai deleted a comment from danielhanchen Nov 3, 2026
@danielhanchen
danielhanchen merged commit 54657ed into main Nov 3, 2026
@danielhanchen
danielhanchen deleted the fused-route-counters-compile branch November 3, 2026 05:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant