Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
64218fb
Initial commit, missing .cu file.
ebezzam May 19, 2026
0d6fe8d
tdt-loss: add CUDA kernels
Deep-unlearning Sep 28, 2026
0bee960
tdt-loss: add benchmark
Deep-unlearning Sep 28, 2026
3447945
tdt-loss: fix test references for unreachable nodes and long targets
Deep-unlearning Sep 28, 2026
8e5cb43
tdt-loss: run the lattice recursions in float64
Deep-unlearning Sep 28, 2026
3301b5d
tdt-loss: keep float32 transcendentals in the lattice recursions
Deep-unlearning Sep 28, 2026
da05018
tdt-loss: update README
Deep-unlearning Sep 28, 2026
a76e9fa
tdt-loss: load the kernel with get_kernel in tests, add kernels_ci ma…
Deep-unlearning Sep 28, 2026
850114b
tdt-loss: validate inputs, add launch bounds, share the test reference
Deep-unlearning Sep 28, 2026
f711276
tdt-loss: add A100/H200 benchmarks and fine-tuning results to the README
Deep-unlearning Sep 28, 2026
cfeecb2
tdt-loss: register in the maintainer registry and freshness source ma…
Deep-unlearning Sep 28, 2026
3b9907c
tdt-loss: add a TDTLoss layer for the kernels exchange pattern
Deep-unlearning Sep 28, 2026
02647af
tdt-loss: support torch.compile
Deep-unlearning Oct 2, 2026
ab47fc9
tdt-loss: add maintainer, drop README (the card is used for the Hub)
Deep-unlearning Oct 5, 2026
febfe21
tdt-loss: move to the Torch stable ABI
Deep-unlearning Oct 5, 2026
474a55f
tdt-loss: stable ABI fixes for Torch 2.10
Deep-unlearning Oct 5, 2026
18a1056
Merge main
Deep-unlearning Oct 6, 2026
1eefa6f
tdt-loss: use build.toml edition 5, bump kernel-builder
Deep-unlearning Oct 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/kernel-maintainers.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"scattermoe": ["U08Q42K8MGW"],
"sgl-flash-attn3": [],
"sonic-moe": ["U08Q42K8MGW", "U054KJK89C2"],
"tdt-loss": ["U07U53UJECT"],
"tinygrad-rms": [],
"topk": ["U0586KAH2E9"],
"trimul-gpumode": [],
Expand Down
1 change: 1 addition & 0 deletions scripts/check_kernel_freshness.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"scattermoe": "https://github.com/shawntan/scattermoe",
"sgl-flash-attn3": "https://github.com/sgl-project/sgl-flash-attn",
"sonic-moe": "https://github.com/Dao-AILab/sonic-moe",
"tdt-loss": "",
"tinygrad-rms": "https://github.com/tinygrad/tinygrad",
"trimul-gpumode": "https://github.com/davidberard98/gpumode-trimul",
"triton-kernels": "https://github.com/triton-lang/triton.git",
Expand Down
42 changes: 42 additions & 0 deletions tdt-loss/CARD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
library_name: kernels
{% if license %}license: {{ license }}
{% endif %}---

This is the repository card of {{ repo_id }} that has been pushed on the Hub. It was built to be used with the [`kernels` library](https://github.com/huggingface/kernels). This card was automatically generated.

## How to use
{% if functions %}

```python
# make sure `kernels` is installed: `pip install -U kernels`
from kernels import get_kernel

kernel_module = get_kernel("{{ repo_id }}")
{{ functions[0] }} = kernel_module.{{ functions[0] }}

{{ functions[0] }}(...)
```
{% else %}

Usage example not available.
{% endif %}

## Available functions
{% if functions %}
{% for func in functions %}
- `{{ func }}`
{% endfor %}
{% else %}

Function list not available.
{% endif %}

## Benchmarks
{% if has_benchmark %}

Benchmarking script is available for this kernel. Run `kernels benchmark {{ repo_id }}`.
{% else %}

Benchmarking script is not available.
{% endif %}
81 changes: 81 additions & 0 deletions tdt-loss/benchmarks/benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import sys
from pathlib import Path

import torch

from kernels.benchmark import Benchmark

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

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


DURATIONS = [0, 1, 2, 3, 4]


class TDTLossBenchmark(Benchmark):
seed: int = 42

def _make_inputs(self, B, T, U, V, dtype):
D = len(DURATIONS)
# Slices of a joint output, as produced by ParakeetForTDT.
self.logits = torch.randn(B, T, U + 1, V + D, device=self.device, dtype=dtype, requires_grad=True)
self.targets = torch.randint(1, V, (B, U), device=self.device, dtype=torch.int32)
self.logit_lengths = torch.full((B,), T, device=self.device, dtype=torch.int32)
self.target_lengths = torch.full((B,), U, device=self.device, dtype=torch.int32)
self.V = V

def _run(self):
self.logits.grad = None
losses = self.kernel.tdt_loss(
self.logits[..., : self.V],
self.logits[..., self.V :],
self.targets,
self.logit_lengths,
self.target_lengths,
0,
DURATIONS,
reduction="none",
)
losses.sum().backward()
self.out = losses.detach()

def _reference(self):
with torch.no_grad():
return tdt_loss_reference(
self.logits[..., : self.V],
self.logits[..., self.V :],
self.targets,
self.logit_lengths,
self.target_lengths,
0,
DURATIONS,
)

# Small vocabulary, short utterances.
def setup(self):
self._make_inputs(B=8, T=100, U=20, V=1025, dtype=torch.float32)

def benchmark_base(self):
self._run()

def verify_base(self) -> torch.Tensor:
return self._reference()

# Parakeet TDT v3 sizes (8192 tokens + blank), float32.
def setup_large(self):
self._make_inputs(B=4, T=200, U=40, V=8193, dtype=torch.float32)

def benchmark_large(self):
self._run()

def verify_large(self) -> torch.Tensor:
return self._reference()

# Same sizes with bfloat16 logits.
def setup_large_bf16(self):
self._make_inputs(B=4, T=200, U=40, V=8193, dtype=torch.bfloat16)

def benchmark_large_bf16(self):
self._run()

def verify_large_bf16(self) -> torch.Tensor:
return self._reference()
28 changes: 28 additions & 0 deletions 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?

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[general]
name = "tdt-loss"
license = "Apache-2.0"
version = 1
edition = 5
backends = ["cuda"]

[general.hub]
repo-id = "kernels-community/tdt-loss"

[torch]
src = [
"torch-ext/torch_binding.cpp",
"torch-ext/torch_binding.h",
]

[torch.stable-abi]
cuda = "2.10"

[kernel.tdt_loss]
backend = "cuda"
depends = ["torch"]
src = [
"tdt_loss/common.cuh",
"tdt_loss/lattice.cu",
"tdt_loss/logprobs.cu",
"tdt_loss/stable.h",
]
117 changes: 117 additions & 0 deletions tdt-loss/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tdt-loss/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
description = "Flake for tdt-loss kernel";

inputs = {
kernel-builder.url = "github:huggingface/kernels";
};

outputs =
{
self,
kernel-builder,
}:
kernel-builder.lib.genKernelFlakeOutputs {
inherit self;
path = ./.;
};
}
Loading
Loading