Skip to content

tdt-loss: add TDT loss kernel - #882

Merged
github-actions[bot] merged 18 commits into
huggingface:mainfrom
ebezzam:tdt_loss
Oct 9, 2026
Merged

github-actions[bot] merged 18 commits into
huggingface:mainfrom
ebezzam:tdt_loss

Conversation

@ebezzam

@ebezzam ebezzam commented May 19, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Goal of this PR is to add a kernel that has been drafted by @eustlb for the recently merged Parakeet TDT.

It's a very rough draft with a lot of Claude help as it's my first time working with kernels 😅

TODO

Perhaps we can see how other kernels used in Transformers were integrated. As the kernel is for a function, maybe rotary_pos_emb is a good example? Here are the files in this repo.

Here is a draft PR for adding/using this kernel in Transformers: huggingface/transformers#46048

cc @vasqu

@ebezzam
ebezzam requested review from danieldk and drbh as code owners May 19, 2026 07:51
@github-actions

Copy link
Copy Markdown

Hi @ebezzam, thanks for your interest in contributing!

This project requires that pull request authors are vouched, and you are not in the list of vouched users.

This PR will be closed automatically. See https://github.com/huggingface/kernels-community/blob/main/CONTRIBUTING.md for more details.

@github-actions github-actions Bot closed this May 19, 2026
@ebezzam ebezzam changed the title Initial commit, missing .cu file. Add TDT loss kernel May 19, 2026
@danieldk danieldk reopened this May 19, 2026
@danieldk danieldk changed the title Add TDT loss kernel tdt-loss: add TDT loss kernel May 19, 2026
@drbh drbh added area: build-system build.toml, Nix flakes, packaging, and kernel-builder integration area: docs README, CARD.md, guides, and repository documentation area: tests Tests, validation code, and benchmark harnesses new-kernel A brand-new kernel package is added size: L Diff <= 1000 lines stale No update in 30 days type: feature New functionality / capability feature New functionality / capability cuda NVIDIA CUDA kernels and removed stale No update in 30 days area: build-system build.toml, Nix flakes, packaging, and kernel-builder integration area: docs README, CARD.md, guides, and repository documentation area: tests Tests, validation code, and benchmark harnesses size: L Diff <= 1000 lines type: feature New functionality / capability labels Jun 30, 2026
ebezzam and others added 7 commits September 28, 2026 14:26
Implement the TDT loss kernels from scratch:
- logprobs.cu: log-softmax gather over the vocabulary (online softmax, one
  block per lattice node) and a fused gradient kernel w.r.t. the logits.
- lattice.cu: alpha/beta recursions over anti-diagonals, one block per sample
  with threads striding over u (no U <= 1024 limit).

Logits are read in float32/float16/bfloat16 with arbitrary strides on the
(batch, T, U) dimensions, so slices of the joint output need no copy.
Supports all NeMo reductions (mean_volume, mean_batch, mean, sum, none).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
alpha + beta - log_likelihood cancels in float32 when the log-likelihood is
large (long targets). The recursions are ~2% of the runtime, so accumulate
them in float64; log-probs and logits stay in float32 or lower.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only the log-values are float64; exp/log act on differences in float32, and
each node uses a two-pass logsumexp instead of chained log_add calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move the implementation from __init__.py to loss.py, add layers.TDTLoss (a
stateless layer with the same signature as transformers' tdt_loss) and use
the transformers argument order in the functional API.
@Deep-unlearning

Copy link
Copy Markdown
Contributor

Status / next steps

  • CUDA kernels, tests (43 passing on L4/A100/H200), benchmarks, README
  • layers.TDTLoss for the kernels exchange pattern (thanks @vasqu)
  • Registered in kernel-maintainers.json and check_kernel_freshness.py
  • Build: could someone with write access run /kernel-bot build tdt-loss? This is the first kernel-builder/nix build of this kernel (I could only test with the PyTorch JIT builder).
  • Review and merge, then publish kernels-community/tdt-loss v1
  • Then merge Add TDT loss kernel transformers#46048 (it needs the published kernel for use_kernels=True)

The "Comment with build instructions" check fails with a 403 from the bot's token. It does the same on other fork PRs (e.g. #1182).

@vasqu

vasqu commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

/kernel-bot build-and-stage tdt-loss

@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot build-and-stage tdt-loss
Mode: build and stage
Target branch: pr-882
PR head SHA: 3b9907c58a98e3874b16b454a72fe8fb2a8b67c7
Workflows: build.yaml, build-mac.yaml, build-windows.yaml

Dispatched (1):

Hub uploads:

@vasqu

vasqu commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Using the staged build so you can use it via hf hub as well

Register fake implementations for the ops (they only write to preallocated
outputs), build the durations tensor without the eager cache while compiling,
and mark the TDTLoss layer as compile-friendly. Test with
torch.compile(fullgraph=True) and torch.library.opcheck.
@Deep-unlearning

Copy link
Copy Markdown
Contributor

@vasqu I added torch.compile support (02647af, following your comment on huggingface/transformers#46048): fake implementations for the ops, can_torch_compile = True on the TDTLoss layer, and torch.compile(fullgraph=True) + torch.library.opcheck tests (46 tests pass on an L4). Could you re-run /kernel-bot build-and-stage tdt-loss? The staged build is from before this change.

@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown

This PR changes source code for the following kernel(s): tdt-loss.

If you have the kernel-bot build permissions, then trigger /kernel-bot build tdt-loss yourself. Otherwise, a maintainer will review the PR and will take an appropriate action. Thanks for your patience.

@vasqu

vasqu commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

/kernel-bot build-and-stage tdt-loss

@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot build-and-stage tdt-loss
Mode: build and stage
Target branch: pr-882
PR head SHA: 02647aff4a7c7a6640823f9b298465844c86bb73
Workflows: build.yaml, build-mac.yaml, build-windows.yaml

Dispatched (1):

Hub uploads:

@Deep-unlearning

Copy link
Copy Markdown
Contributor

@vasqu friendly ping: all comments are addressed here and on huggingface/transformers#46048 (torch.compile support + the test split), and the new build passed. Could you approve/merge when you get a chance, so #46048 can follow? Thanks!

Comment thread .github/kernel-maintainers.json Outdated
"scattermoe": ["U08Q42K8MGW"],
"sgl-flash-attn3": [],
"sonic-moe": ["U08Q42K8MGW", "U054KJK89C2"],
"tdt-loss": [],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you add yourself here Steven? Iirc it's the slack id

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.

Done in ab47fc9 (U07U53UJECT).

Comment thread tdt-loss/README.md Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not sure how much sense the readme make iirc only the card will be used to create the readmes

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.

Makes sense, removed in ab47fc9.

Comment thread tdt-loss/build.toml

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would it be possible to port to torch ABI stable, e.g. see #1180

With this we can support more torch versions. Otherwise we will be stuck with the latest 2 which is not good and can lead to discrepancies

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.

Done: ported to the stable ABI in febfe21 + 474a55f, following #1180 ([torch.stable-abi] cuda = "2.10", torch::stable::Tensor, STD_TORCH_CHECK, THO dispatch, stable DeviceGuard, AOTI stream, STABLE_TORCH_LIBRARY). The CUDA kernels are unchanged.

Tested on an L4 with -DTORCH_STABLE_ONLY -DTORCH_TARGET_VERSION=0x020a000000000000: all 46 tests (including torch.compile and opcheck) pass on both Torch 2.10 and 2.11, same speed. Could you re-run /kernel-bot build-and-stage tdt-loss?

Use torch::stable::Tensor, STD_TORCH_CHECK, THO dispatch, the stable
DeviceGuard and the AOTI shim for the current stream, and register the ops
with STABLE_TORCH_LIBRARY. The CUDA kernels are unchanged.
@vasqu

vasqu commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

/kernel-bot build-and-stage tdt-loss

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot build-and-stage tdt-loss
Mode: build and stage
Target branch: pr-882
PR head SHA: 474a55f7115008cac1ef6cd8e5ba3c8fd2197310
Workflows: build.yaml, build-mac.yaml, build-windows.yaml

Dispatched (1):

@Deep-unlearning

Copy link
Copy Markdown
Contributor

Thanks @vasqu! The build failed because build.toml was missing edition = 5. Fixed in 1eefa6f, and I merged main so the checks are green again. Could you re-run /kernel-bot build-and-stage tdt-loss? Once it's merged, a release would let huggingface/transformers#46048 follow 🙏

@vasqu

vasqu commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

/kernel-bot build-and-stage tdt-loss

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot build-and-stage tdt-loss
Mode: build and stage
Target branch: pr-882
PR head SHA: 1eefa6f976267c980fb91022fd524a1d9ee6c924
Workflows: build.yaml, build-mac.yaml, build-windows.yaml

Dispatched (1):

Hub uploads:

@Deep-unlearning

Copy link
Copy Markdown
Contributor

Hi @vasqu @danieldk @drbh! The staged build on 1eefa6f passes (build + GPU tests), and all review comments are addressed. Would you be able to approve and merge when you have a moment, and trigger the release? That would unblock huggingface/transformers#46048. Thanks a lot 🙏

Comment on lines +8 to +9
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from tests.reference import tdt_loss_reference # noqa: E402

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we should update this to use the kernels library like the test, not blocking since its only for benching, but should probably be followed up in the next pr that touches this kernel

@drbh drbh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm! nice addition!

@drbh

drbh commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

/kernel-bot merge-and-upload tdt-loss

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot merge-and-upload tdt-loss
Mode: merge, build and upload
Target branch: ``
PR head SHA: 1eefa6f976267c980fb91022fd524a1d9ee6c924
Workflows: `build.yaml, build-mac.yaml, build-windows.yaml`

Merge result: Pull Request successfully merged

Dispatched (1):

Hub uploads:

@github-actions
github-actions Bot merged commit 8bdd234 into huggingface:main Oct 9, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Version bumps, releases, misc maintenance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants