Triton vs CUDA: the DSA FP8 top-K indexer on B200

The same kernel, written once in hand-tuned CUDA with inline PTX and once in Triton, benchmarked side by side on the 128 contest workloads. TileLang and ThunderKittens ports of the same plan are compared in four ways to write this kernel.

November 19, 2026 · NVIDIA B200 on Modal · definition dsa_topk_indexer_fp8_h64_d128_topk2048_ps64 · repository

CUDA, mean over 128 workloads
8.00 µs
13 files, 1,246 lines of code
Triton 3.7.0 (contest image)
8.18 µs
2% slower than CUDA · 1 file, 190 lines of code
Triton 3.9.0, same source
6.78 µs
16% faster than CUDA (8.06 µs in that run)

What was compared

The kernel is the indexer of DeepSeek sparse attention. For every row b of a batch and every token t of its paged context it computes

logit[t] = scale[page, slot] * sum_h ReLU(Q[b, h] . K[page, slot]) * w[b, h]

with 64 heads of dimension 128 in FP8 (E4M3), and returns the physical cache slots of the 2,048 largest logits. Both implementations use the same plan:

Sources: solution/python (CUDA, the contest submission) and solution/triton/solution.py.

Measurement

Results

Mean latency by dispatch plan
CUDATriton 3.7.0Triton 3.9.0
Microseconds, lower is better. The figure under each group is Triton latency divided by CUDA latency in the same run.
Every two-stage workload
CUDA (run A)Triton 3.7.0 (run A)Triton 3.9.0 (run B)
Latency in µs for the 59 workloads that run both stages, ordered by context size and then batch size. The vertical axis starts at 9 µs. Hover a marker for the exact value.
All 128 workloads

Where the time goes

GPU time per kernel
CUDATriton 3.7.0Triton 3.9.0 solid: Stage 1 (scoring)light: Stage 2 (top-K selection)
Mean of per-workload medians, in µs. CUDA and Triton 3.7.0 come from the run A trace, Triton 3.9.0 from the run B trace; CUDA measured within 0.3 µs of these values in run B. The gap between the two kernels is 0.2 µs in every case and is left out.

Same techniques, two spellings

TechniqueCUDATriton
Fast path topk_fast_path_kernel: 128 threads, block table staged in shared memory, vectorised int4 stores. _fast_path: one broadcast [32, 64] expression and a single tl.store.
FP8 tensor-core scoring Hand-written PTX wrappers for tcgen05.mma (kind::f8f6f4), instruction and shared-memory descriptors, 128-byte swizzled operand layout. 275 lines in tcgen05_ptx.h and umma_desc.h. tl.dot(k, qt, out_dtype=tl.float32) on float8_e4m3fn tensors. The compiler emits the same tcgen05.mma with a TMEM accumulator.
TMEM accumulator Explicit allocation, mbarrier wait on the MMA commit, tcgen05.ld of 64 columns into registers, deallocation. Implicit in tl.dot and the ReLU/weight reduction that follows.
K pipeline Persistent kernel: producer warps stream K into a triple-buffered cp.async ring, handed to the MMA warp through mbarriers. tl.range(first, last, num_stages=3). The compiler emits cp.async.cg prefetch for the block-table-indirect K loads.
Dispatch by context size Short kernel: one page per MMA, one CTA per page. Persistent kernel: two pages per MMA, each CTA loops over tiles. One kernel, same thresholds. Two pages per MMA everywhere; a program scores one tile below 64 pages and a loop of tiles above.
Skipping work Rows that fit in top-K are skipped in Stage 1. Padding past seq_len is written as FP16 −inf. Same row skip. Tiles past seq_len are skipped and nothing is written for padding, because Stage 2 masks by seq_len.
FP16 logits Yes.Yes. The evaluator's 1e-2 tolerance covers the rounding.
Radix select Two rounds of 8 bits. One atomicAdd into a shared-memory histogram per key, 1,024 threads, ordered keys cached in shared memory. Three rounds of 5, 5 and 6 bits through tl.histogram, 512 threads for rows up to 4,096 tokens and 1,024 above.
Emitting the result Three passes over the row with a shared atomic counter (keys above the pivot, ties, then the block-table rewrite). One tl.cumsum ranks both groups; one masked scatter store writes physical slots directly. The output order is deterministic.
Two launches as one CUDA graph cached per stream and shape; cudaGraphExecUpdate retargets the pointers on each call. torch.cuda.CUDAGraph cached per tensor addresses and shape, since torch cannot retarget a captured graph.

Scoring one tile

CUDA, stage1_short.cuh (abridged)

const auto instr = dsa_umma::make_instr_desc_f8f6f4(
    dsa_umma::F8F6F4Format::E4M3,
    dsa_umma::F8F6F4Format::E4M3,
    dsa_umma::CFormat::F32,
    dsa_umma::Major::K, dsa_umma::Major::K,
    kUMMA_M, kUMMA_N);
issue_umma_k_commit<kKIters>(
    tmem_addr,
    make_kmajor_desc(smem_k, kSboBytes),
    make_kmajor_desc(smem_q, kSboBytes),
    instr,
    dsa_ptx::smem_ptr_to_uint(&smem_mbar),
    warp_id, lane);

dsa_ptx::mbarrier_wait_parity(
    dsa_ptx::smem_ptr_to_uint(&smem_mbar), 0u);
dsa_ptx::tcgen05_fence_after_thread_sync();

uint32_t regs[kUMMA_N];
dsa_ptx::tcgen05_ld_32x32b_x64_b32(tmem_addr, regs);
dsa_ptx::tcgen05_wait_ld();
// ...
for (int h = 0; h < kNumHeads; ++h) {
    const float r = acc[h] > 0.0f ? acc[h] : 0.0f;
    acc_relu = fmaf(r, smem_w[h], acc_relu);
}
const float scaled = acc_relu * smem_kscale[tid];
logits_b[kv_abs] = (kv_abs < seq_len)
    ? __float2half_rn(scaled) : kHalfNegInf;

Triton, solution.py (reflowed)

for t in tl.range(first, last, num_stages=STAGES):
    tok = t * TILE + m
    slot = tl.minimum(tok // PAGE, last_page)
    page = tl.load(bt_ptr + b * max_num_pages + slot)
    page = page.to(tl.int64)
    row = tok % PAGE
    # k: [TILE, D] fp8
    k = tl.load(k_ptr
                + (page * PAGE_BYTES + row * D)[:, None]
                + d[None, :])
    scale = tl.load(kscale_ptr + page * (PAGE_BYTES // 4)
                    + SCALE_OFFSET // 4 + row)

    # acc: [TILE, H] f32, one FP8 MMA
    acc = tl.dot(k, qt, out_dtype=tl.float32)
    logit = tl.sum(tl.maximum(acc, 0.0) * w[None, :],
                   axis=1) * scale
    tl.store(logits_ptr + b * row_stride + tok,
             logit.to(tl.float16), mask=tok < seq_len)

One radix-select round

CUDA, stage2_topk.cuh (abridged)

for (int i = tid; i < cached_len; i += blockDim.x) {
    const uint16_t ordered = smem_ordered[i];
    if ((uint16_t)(ordered & prefix_mask) == prefix)
        atomicAdd(
            &smem_hist[(ordered >> shift) & 0xFFu], 1u);
}
__syncthreads();

if (tid < kRadix) smem_suffix[tid] = smem_hist[tid];
__syncthreads();
for (int stride = 1; stride < kRadix; stride <<= 1) {
    uint32_t v = 0u;
    if (tid < kRadix) {
        v = smem_suffix[tid];
        if (tid + stride < kRadix)
            v += smem_suffix[tid + stride];
    }
    __syncthreads();
    if (tid < kRadix) smem_suffix[tid] = v;
    __syncthreads();
}
// ...
if (tid < kRadix) {
    const uint32_t cge = smem_suffix[tid];
    const uint32_t cgt =
        (tid + 1 < kRadix) ? smem_suffix[tid + 1] : 0u;
    if (cge >= smem_remaining && cgt < smem_remaining) {
        smem_found_bucket    = (uint32_t)tid;
        smem_found_remaining = smem_remaining - cgt;
    }
}

Triton, solution.py (reflowed)

@triton.jit
def _select_digit(digit, mask, need,
                  BINS: tl.constexpr):
    hist = tl.histogram(digit, BINS, mask=mask)
    at_or_above = tl.cumsum(hist, 0, reverse=True)
    # Bins [0, d] are exactly the ones that still
    # have `need` keys at or above them. One sum
    # gives both answers: how many such bins (high
    # bits) and how many keys sit in the bins above
    # d (low bits).
    packed = tl.sum(
        tl.where(at_or_above >= need, 1 << 20, hist),
        axis=0)
    return (packed >> 20) - 1, packed & 0xFFFFF

Stage 2 and the Triton version

Top-K selection needs a histogram of key digits, and the usual way to build one on a GPU is one shared-memory atomic add per key. Triton has no shared-memory atomics in the language. The closest construct is tl.histogram, and the two releases compile it differently.

Several selection designs were measured in isolation before choosing one. The table gives the time of the whole Stage 2 kernel in µs on synthetic inputs at batch 16, for the best of 8, 16 and 32 warps.

Selection designTriton 3.7.0Triton 3.9.0
4,096 tokens8,192 tokens4,096 tokens8,192 tokens
Histogram, 8 + 8 bits (the CUDA design)9.715.75.68.1
Histogram, 5 + 5 + 6 bits (shipped)8.012.95.98.5
Bisection, 16 counting passes8.511.27.010.5
64-bin histogram, then 10 bisection passes8.512.46.710.0
4-way search, 8 passes8.912.28.011.9

Of the roughly 8 µs at 4,096 tokens on Triton 3.7.0, about 1.9 µs is the cost of launching a kernel at all, 0.7 µs loads the row and builds the keys, 1.7 µs ranks and writes the result, and the rest is selection.

One design is shipped for both compiler versions. Two measured gains are therefore left unused: on 3.7.0, bisection is about 1.7 µs faster for 8,192-token rows, and on 3.9.0 the 8 + 8 split is about 0.3 µs faster.

What the port says about the CUDA version

The Triton version was written to answer one question, and it ended up measuring the CUDA version from the outside. Three differences stand out. None has been applied to the CUDA code yet.

  1. Fast path: 0.46 µs. Triton's kernel is faster on all 69 fast-path workloads, 54% of the set. Matching it would lower the overall mean by about 0.25 µs.
  2. Short-plan scoring at large batch: 2.8 µs. At batch 29 to 31 the CUDA Stage 1 takes 8.15 µs against 5.33 µs for Triton. The CUDA short kernel launches one CTA per page, 1,320 CTAs at batch 30 with 44 pages; Triton scores two pages per MMA and launches 660 programs. Whether the CTA count is the cause has not been tested. Ten workloads are affected.
  3. Top-K selection: about 2 µs. With the 3.9.0 histogram, Triton's Stage 2 takes 5.3 and 8.0 µs against 7.7 and 9.9 µs for the CUDA kernel in the same trace, although both now count keys with per-key shared-memory atomics. The designs differ in three ways: three narrow rounds over 32 and 64 bins instead of two rounds over 256, one prefix sum and one scatter instead of three passes over a shared atomic counter, and half the threads for short rows. Which of these carries the gain has not been tested. All 59 two-stage workloads are affected.

Taking the better of the two implementations for each kernel in the run B trace gives a mean of 6.7 µs against 8.3 µs for CUDA in that trace. That is an upper estimate of what the CUDA version could gain, and it carries the trace caveat below.

Limits and caveats

Reproducing

# Benchmark both solutions on Modal B200 (about 20 minutes)
modal run scripts/run_modal_triton_vs_cuda.py

# Same with Triton 3.9.0
DSA_TRITON_VERSION=3.9.0 modal run scripts/run_modal_triton_vs_cuda.py

# Per-kernel GPU time
modal run scripts/run_modal_triton_vs_cuda.py --kernels --iterations 50

# Short checks
modal run scripts/run_modal_triton_vs_cuda.py --smoke
modal run scripts/run_modal_triton_vs_cuda.py --quick

The numbers behind every chart and table on this page are in data.js.