Skip to content

Add fused TP1 cross entropy with deferred backward - #550

Merged
wenxie-amd merged 6 commits into
mainfrom
suachong/turbo-ce-main-20261010
Oct 11, 2026
Merged

wenxie-amd merged 6 commits into
mainfrom
suachong/turbo-ce-main-20261010

Conversation

@suachong

@suachong suachong commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Add a TransformerEngine-derived Triton TP=1 vocabulary cross-entropy operator with one loss/statistics forward kernel and one gradient-reconstruction backward kernel. The BF16/FP32 API supports per-token loss, ignored labels, label smoothing and supported strided inputs.

Logits are preserved by default. Optional overwrite_input=True reuses exclusively owned contiguous logits storage during backward. Both modes support one backward per forward; higher-order gradients are unsupported. NVIDIA Apache attribution is retained, and the operator has no runtime TransformerEngine dependency or native build changes.

Validation:

  • 87 GPU correctness tests and four production-shape numerical checks passed on MI355X using the same operator source before rebasing onto main. Coverage includes storage ownership and large vocabulary-stride addressing.
  • Isolated BF16 CE forward + backward at [8192, 4, 128256]: installed TE 9.702 ms, Turbo preserve 6.655 ms, Turbo overwrite 5.295 ms (median, 30 repetitions after five warmups). These are operator-only measurements. The included benchmark supports reproduction against the installed TE version.
  • Local pre-commit checks pass; ci:gpu requests GPU CI on this branch.

Upstream source: TransformerEngine cross entropy. Companion integration: Primus #1245.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Destructive overwrite mode needs a safe torch.compile boundary to prevent invalid alias tracing.

1 open finding
What changed in this PR

Adds a fused TP=1 Triton cross-entropy operator with deferred gradient reconstruction.

Changes:

  • Adds BF16/FP32 kernels and PyTorch API.
  • Adds correctness tests, documentation, licensing, and benchmarks.
  • Exports the operator publicly.
File Description
primus_turbo/​triton/​cross_entropy.py Implements forward/backward kernels.
primus_turbo/​pytorch/​ops/​cross_entropy.py Adds validation and autograd integration.
primus_turbo/​pytorch/​ops/​__init__.py Exports the operator.
tests/​pytorch/​ops/​test_cross_entropy.py Covers correctness, layouts, and ownership.
benchmark/​ops/​training/​bench_cross_entropy.py Benchmarks Turbo and TE variants.
docs/​cross_entropy.md Documents usage and constraints.
LICENSE Records NVIDIA-derived source licensing.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

raise ValueError("label_smoothing must be between zero and one")
if overwrite_input and not logits.is_contiguous():
raise ValueError("overwrite_input=True requires contiguous logits")
return _CrossEntropy.apply(logits, target, ignore_index, label_smoothing, overwrite_input)
@wenxie-amd
wenxie-amd merged commit 4e6bd90 into main Oct 11, 2026
7 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants