Repository navigation
perf(sft): reduce custom and chunked cross-entropy work - #10353
Merged
hjh0119 merged 1 commit intoOct 16, 2026
Merged
Conversation
Gather supervised rows before the FP32 cast and CE while keeping the per-token loss shape, detached DFT weighting, and dense fast path. Use the analytical chunked CE gradient with a separate output buffer and preserve the forward compute dtype under autocast. Co-authored-by: OpenAI Codex
hjh0119
approved these changes
Oct 16, 2026
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.
PR type
PR information
Related issue: #10129. This preserves the separate-gradient-buffer fix from #10130; it does not reopen that correctness issue.
Custom SFT loss currently casts every retained logit row to FP32 and computes CE before ignoring unsupervised tokens. Multi-sample
logits_to_keepsuffix cropping can leave unsupervised holes. Separately, sequence-parallel chunked CE rebuilds a CE graph and callsautograd.gradfor each backward chunk.This change:
g * softmax(logits) - g * one_hot(labels), with zero upstream weights for ignored labels. It retains a separate gradient buffer and respects the forward CE compute dtype under autocastDFT probabilities remain detached. External token weights, normalization, and channel-loss logging keep their existing semantics. The backbone context and output-head projection are unchanged. This does not add fused-linear-CE integration; chunked CE remains specific to
CELOSS_PARALLEL_SIZE > 0.Only the two production files are included; validation scripts are kept outside the repository.
AI assistance: OpenAI Codex was used for implementation, code review, local validation, and PR preparation.
Experiment results
NVIDIA RTX A1000 Laptop GPU, PyTorch 2.12.1+cu130. Timings cover loss forward and weighted backward on preallocated logits, with four warmups and the median of five groups of 15 steps, synchronized on CUDA.
The sparse case uses two length-1024 samples with four assistant spans each. Existing suffix cropping retains 1922 rows, of which 256 are supervised, with vocabulary size 4096. CE-saved vocabulary-sized FP32 storage, deduplicated by underlying storage, decreases from 30.03125 MiB to 4 MiB. For ordinary CE, loss and output-head parameter gradients match exactly in this example; the DFT control also matches exactly.
For chunk size 64, CE and log-softmax calls each decrease from 32 to 16; NLL/log-softmax backward calls decrease from 16 to zero, replaced by 16 softmax calls.
The dense control (1920 supervised rows out of 1922) is approximately 3.1% slower, from 1.400 to 1.443 ms, due to valid-index detection, which includes CUDA synchronization. These are loss-phase measurements, not end-to-end training speed or memory claims. Analytical backward may round differently from log-softmax backward in FP16/BF16; AMP comparisons also used a same-logit FP64 gradient reference. No NPU/MPS/XPU validation was performed.
Validation commands (using the checkout):
python -m pytest tests/train/test_chunked_cross_entropy.py tests/train/test_cross_entropy_loss.py -q --tb=short CELOSS_PARALLEL_SIZE=3 python -m pytest tests/sequence_parallel/test_custom_cross_entropy.py -k '1-2-1' -q --tb=short uvx --offline pre-commit run --all-files git diff --checkExisting trainer tests: 14 passed, including 8 subtests. Two-process CPU Gloo sequence-parallel test: 1 passed, 3 deselected. All pre-commit hooks and the whitespace check passed.
Additional local validation, excluded from the diff:
Local checks passed in two batches: 102 passed, 12 skipped (inapplicable AMP/FP32-FP64 combinations). Together with the existing tests: 117 passed. Coverage includes CPU/CUDA, FP32/FP64/FP16/BF16, AMP and GradScaler optimizer steps, CE/DFT, token weighting, channel logging, non-contiguous logits, all-ignored/dense masks, actual suffix cropping, incomplete chunks, repeated backward, auxiliary-loss sharing, and saved-logit immutability.
Checklist