Repository navigation
Fix silently wrong torch.compile results in the Triton loss kernels on torch < 2.11 - #7688
Merged
Merged
Conversation
…le works before torch 2.11
3 tasks done
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
qgallouedec
reviewed
Oct 13, 2026
| # Before torch 2.15, torch.compile passes Python float arguments as fp64; keep the math in fp32 | ||
| beta, s_scale, s_softcap = beta.to(tl.float32), s_scale.to(tl.float32), s_softcap.to(tl.float32) | ||
| t_scale, t_softcap, inv_t = t_scale.to(tl.float32), t_softcap.to(tl.float32), inv_t.to(tl.float32) | ||
| # tl.cast, not .to: before torch 2.11, Inductor's mutation analysis passes them as constexpr/float |
Member
There was a problem hiding this comment.
Suggested change
| # tl.cast, not .to: before torch 2.11, Inductor's mutation analysis passes them as constexpr/float |
let's avoid documenting what we don't do, unless there is strong reason to do so
qgallouedec
approved these changes
Oct 13, 2026
qgallouedec
left a comment
Member
There was a problem hiding this comment.
Thanks! Matches what I saw on #7642: the compile tests gave NaN from 2.6 to 2.9.1 and passed on 2.14.1. Whichever of the two merges second drops the xfail.
| # Before torch 2.15, torch.compile passes Python float arguments as fp64; keep the math in fp32 | ||
| beta, s_scale, s_softcap = beta.to(tl.float32), s_scale.to(tl.float32), s_softcap.to(tl.float32) | ||
| t_scale, t_softcap, inv_t = t_scale.to(tl.float32), t_softcap.to(tl.float32), inv_t.to(tl.float32) | ||
| # tl.cast, not .to: before torch 2.11, Inductor's mutation analysis passes them as constexpr/float |
Member
There was a problem hiding this comment.
Suggested change
| # tl.cast, not .to: before torch 2.11, Inductor's mutation analysis passes them as constexpr/float |
| def _transform(z, logit_scale, softcap, inv_t, HAS_SOFTCAP: tl.constexpr): | ||
| # Before torch 2.15, torch.compile passes Python float arguments as fp64; keep the math in fp32 | ||
| logit_scale, softcap, inv_t = logit_scale.to(tl.float32), softcap.to(tl.float32), inv_t.to(tl.float32) | ||
| # tl.cast, not .to: before torch 2.11, Inductor's mutation analysis passes them as constexpr/float |
Member
There was a problem hiding this comment.
Suggested change
| # tl.cast, not .to: before torch 2.11, Inductor's mutation analysis passes them as constexpr/float |
| # Overwrite one block of the logits tile with the gradient of the loss with respect to the projection output | ||
| # Before torch 2.15, torch.compile passes Python float arguments as fp64; keep the math in fp32 | ||
| logit_scale, softcap, inv_t = logit_scale.to(tl.float32), softcap.to(tl.float32), inv_t.to(tl.float32) | ||
| # tl.cast, not .to: before torch 2.11, Inductor's mutation analysis passes them as constexpr/float |
Member
There was a problem hiding this comment.
Suggested change
| # tl.cast, not .to: before torch 2.11, Inductor's mutation analysis passes them as constexpr/float |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR makes the Triton loss kernels compile correctly on torch < 2.11, where
torch_compile=Truecurrently trains with a zero loss and NaN gradients.Fix #7687.
Motivation
Before torch 2.11, Inductor's analysis of which tensors a Triton kernel mutates passes the kernels' scalar arguments as
constexpror Pythonfloat. The.to(tl.float32)casts added in #7600 raise on those, so Inductor assumes every input is mutated and miscompiles the kernels. On torch 2.10.0, SFT withtorch_compile=Truelogsloss=0.0andgrad_norm=nanat every step; with this PR it matches eager.Changes
tl.cast(..., tl.float32)instead of.to(tl.float32)inchunked_logprobandchunked_divergenceThe kernel compile tests pass on torch 2.7.0, 2.10.0 and 2.14.1, and on 2.6.0 apart from the fullgraph test, which is a separate torch 2.6 limitation. #7642 marks these tests xfail below torch 2.11; whichever of the two PRs merges second removes that xfail.
Note
Medium Risk
Touches core Triton training loss/divergence math under
torch.compile, but the change is a narrow casting fix with tests across multiple torch versions; incorrect compile behavior was the main risk before this fix.Overview
Fixes broken
torch.compiletraining on PyTorch before 2.11 for SFT and other paths that use the chunked Triton loss kernels.Scalar kernel args (
beta,logit_scale,softcap,inv_t, etc.) are now promoted to fp32 withtl.cast(..., tl.float32)instead of.to(tl.float32)in_transform, JSD/backward paths inchunked_divergence.py, and the logprob backward path inchunked_logprob.py. On older Inductor, compile-time scalars/constexprs cannot use.to, which led to incorrect mutation analysis, miscompiled kernels, zero loss, and NaN gradients; eager behavior is unchanged and compile matches eager again.Reviewed by Cursor Bugbot for commit fad9a21. Bugbot is set up for automated code reviews on this repo. Configure here.