Repository navigation
Cross entropy: 16384 / 8 warp chunked forward on T4 and A100 - #14498
Merged
Merged
Conversation
The chunked forward (vocab > 65536) launched 65536-wide blocks with 32 warps on every NVIDIA arch. On sm_75 and sm_80 16384-wide blocks with 8 warps are faster for Llama-3, Qwen3 and Gemma vocabs. Route (BLOCK_SIZE, num_warps) by compute capability; every other arch keeps the previous config.
…vocab At Gemma's 262144 vocab 16384 / 8 is 1 to 2.4% slower than 65536 / 32 on A100 with torch 2.10 and 2 to 3.5% faster with torch 2.14, so only Llama-3 / Qwen3 sized vocabs route there. T4 keeps 16384 / 8 at every vocab.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
pushed a commit
to shimmyshimmer/unsloth-staging-4
that referenced
this pull request
Oct 25, 2026
… 65536 (unslothai#14712) ## What `Fast_CrossEntropyLoss.forward` finished the chunked path (vocab > 65536) with two in-place ops on the tensor it returns: `losses += logsumexp` and `losses.masked_fill_(...)`. Under `torch.compile` (default or `fullgraph=True`) the traced backward then received a zero gradient for that output, so every logit gradient came back as exactly zero while the loss value stayed correct. This PR computes the result out of place: `losses = (losses + logsumexp).masked_fill(labels == -100, 0)`. ## Evidence (B200, torch 2.11, fp16 and bf16, fp64 reference) | vocab | eager | compiled | fullgraph | dynamic=True | |---|---|---|---|---| | 32768 (single block) | ok | ok | ok | ok | | 65537, 151936, 262144 before | ok | grad rel err 1.0 (all zero) | grad rel err 1.0 | ok (graph breaks) | | 65537, 151936, 262144 after | ok | 2e-4 (fp16) / 2e-3 (bf16) | same | ok | The same zero gradients show up at the parent of unslothai#14498, so this predates it. Graph break counts are unchanged (0 static, 8 under `dynamic=True`). The compiled `LOSS_MAPPING` loss Unsloth installs uses `dynamic=True`, which graph-breaks around the function and was already correct; the broken case is a user or Trainer `torch.compile` over a forward that computes the loss (default `dynamic=None` compiles statically first). ## Tests `tests/test_cross_entropy_compiled_grads.py`: compiled and fullgraph gradients vs an fp64 reference for vocab 32768, 65537 and 151936. The chunked cases fail on main and pass here. Existing cross entropy tests pass. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
danielhanchen
added a commit
to danielhanchen/unsloth-staging-2
that referenced
this pull request
Oct 25, 2026
…NaN (unslothai#14713) ## What The chunked forward computes a per-chunk logsumexp as `c + log(sum(exp(x - c)))` with `c` the chunk max. When a whole chunk is `-inf` (a masked or banned token range), `c` is `-inf` and `x - c` is `-inf - -inf = NaN`, so the final logsumexp, the loss and every gradient in the row become NaN. This PR shifts by 0 when the chunk max is `-inf`, which makes that chunk's partial logsumexp `-inf` and drops it out of the final reduction, the same way PyTorch's logsumexp handles infinite maxima. Finite chunks are unchanged bit for bit. ## Why now unslothai#14498 moved T4 and A100 to 16384-wide chunks, so a masked range of 16384 aligned columns now fills a whole chunk where it used to share a 65536 chunk with finite logits. A fully masked 65536 range already hit this before unslothai#14498. ## Evidence (B200, real kernels, fp64 reference, 131073 vocab, target 0) | masked columns | 65536 / 32 (before) | 16384 / 8 (T4, A100 now) | after this PR | |---|---|---|---| | 16384..32768 | 11.6499796 ok | NaN, non-finite grads | matches reference | | 65536..131072 | NaN | NaN | matches reference | ## Tests `test_an_all_masked_chunk_matches_the_fp64_reference` in `tests/test_cross_entropy_chunk_config.py` for both chunk widths and both masked ranges: fails on main, passes here. The rest of the cross entropy tests pass.
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.
For vocabs above 65536 (Llama-3, Qwen3, Gemma),
Fast_CrossEntropyLossruns the chunked forward kernel with 65536-wide chunks and 32 warps on every GPU. On T4 and A100 a 16384-wide chunk with 8 warps is faster.Change
_CHUNKED_FORWARD_CONFIGSinunsloth/kernels/cross_entropy_loss.py: (BLOCK_SIZE, num_warps, max vocab) per NVIDIA capability, cached per device and vocab like the existing eviction-hint lookup.Validation
Kaggle T4, Colab A100 and Colab L4, torch 2.10 and 2.14. Each arm ran in a fresh process, three repeats with the order rotated, plus an identical copy of the old config as an A/A arm. Old time / new time, so above 1 is faster:
Accuracy, on T4 fp16, A100 bf16 and L4 bf16, both torch versions:
New
tests/test_cross_entropy_chunk_config.py: the routing per capability and vocab, ROCm / CDNA, and accuracy on the real device. Locally on B200: main 44 failed / 10 passed (the routing tests), this PR 54 passed. On T4, A100 and L4 with both torch versions, the first revision: main 23 failed / 10 passed, this PR 33 passed.End to end, Llama-3.2-1B-Instruct SFT, 20 steps, batch 4, sequence up to 2048. These runs set
UNSLOTH_RETURN_LOGITS=1, since by default Llama computes the loss throughunsloth_fused_ce_lossand does not reach this kernel. A counter confirmed 20 calls per run, with the expected config on each side. Runs went main, PR, PR, main:ForCausalLMLoss) and large-vocab models with long sequences.Not covered: