Repository navigation
tdt-loss: add TDT loss kernel - #882
Conversation
|
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. |
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.
|
Status / next steps
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). |
|
/kernel-bot build-and-stage tdt-loss |
|
Build request processed. Command: Dispatched (1):
Hub uploads: |
|
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.
|
@vasqu I added |
|
This PR changes source code for the following kernel(s): If you have the kernel-bot build permissions, then trigger |
|
/kernel-bot build-and-stage tdt-loss |
|
Build request processed. Command: Dispatched (1):
Hub uploads: |
|
@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! |
| "scattermoe": ["U08Q42K8MGW"], | ||
| "sgl-flash-attn3": [], | ||
| "sonic-moe": ["U08Q42K8MGW", "U054KJK89C2"], | ||
| "tdt-loss": [], |
There was a problem hiding this comment.
Can you add yourself here Steven? Iirc it's the slack id
There was a problem hiding this comment.
Not sure how much sense the readme make iirc only the card will be used to create the readmes
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
/kernel-bot build-and-stage tdt-loss |
|
Build request processed. Command: Dispatched (1):
|
|
Thanks @vasqu! The build failed because |
|
/kernel-bot build-and-stage tdt-loss |
|
Build request processed. Command: Dispatched (1):
Hub uploads: |
|
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 🙏 |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | ||
| from tests.reference import tdt_loss_reference # noqa: E402 |
There was a problem hiding this comment.
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
|
/kernel-bot merge-and-upload tdt-loss |
|
Build request processed. Command: Merge result: Pull Request successfully merged Dispatched (1):
Hub uploads: |
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