# TokenSpeed-kernel TokenSpeed-kernel aims to provide a collection of the best portable and performant kernels for multi-silicon AI inference. It features: - A clean layered API for maximal structured flexibility - Kernel registration and selection logic to decouple complexity and increase reuse - A plugin mechanism for multi-silicon extensibility - A minimal list of curated dependencies for fast iteration TokenSpeed-kernel is pip-installable on its own. Others can use it directly. The vendored CUDA routing, sampling, and softmax kernels share reduction operators selected by the CCCL header version. CCCL releases before 2.8 use CUB operators; newer releases use CUDA operators. This supports building the same reductions with CUDA 12.8 without changing their algorithms. ## Public API ```python from tokenspeed_kernel.ops.gemm import mm from tokenspeed_kernel.ops.layernorm import grouped_gemma_rmsnorm from tokenspeed_kernel.ops.moe import moe_apply, moe_plan, moe_process_weights, moe_topk from tokenspeed_kernel.ops.residual import gated_residual_combine, gated_residual_mix from tokenspeed_kernel.ops.attention.gdn import gdn_chunk_prefill from tokenspeed_kernel.ops.attention.mha import ( mha_decode_with_kvcache, mha_prefill, ) from tokenspeed_kernel.ops.attention.msa import ( msa_decode_with_kvcache, msa_extend_with_kvcache, ) ``` The preceding platform- and solution-agnostic public APIs provide the most value from TokenSpeed-kernel. You can also call directly into a specific solution under `ops//`, or run `select_kernel` manually with targeted filters. For targeted selection: ```python from tokenspeed_kernel.selection import select_kernel, kernel_override ``` For platform checks: ```python from tokenspeed_kernel.platform import current_platform ``` Ascend platform detection enables timing by default for `torch.npu.Event()`. Explicit `enable_timing` arguments and the other event options retain their original behavior. ## Design goals TokenSpeed-kernel is designed with these functionality goals in mind: - Support various kernels in AI models (such as attention and MoE) - Support multiple silicon vendors and generations - Marry default portability and performance solutions Additional goals target a better devflow for fast iteration: - Provide unified infra to verify and debug kernel numerics standalone - Provide unified infra to run and benchmark kernels standalone - Support tracing shapes and profiling workloads at runtime - Stay forward-looking, with guardrails for agentic devflow ## Overall design TokenSpeed-kernel makes these opinionated design choices toward the preceding goals (still evolving; subject to change): ### Layered system ``` public API (attention.mha.mha_prefill, mm, ...) │ ┌───────────┴───────────┐ │ select_kernel │ (family, mode, format_signature, traits, ...) └───────────┬───────────┘ │ queries ┌──────────┴──────────┐ │ KernelRegistry │ ← @register_kernel(...) populates this └──────────┬──────────┘ │ ┌──────────────┬────────────────┼────────────────┬───────────────┐ attention gemm moe norm ... (op family) │ │ │ │ ┌────┼────┐ ┌────┼────┐ ┌────┼────┐ ┌────┼────┐ triton triton triton triton ← in-tree portable JIT gluon (...) cute_dsl (...) ← in-tree perf JIT flash_mla flashinfer (...) (...) ← vendor library wrappers ... │ │ │ │ └──────────────┴────────────────┴────────────────┴── reference (PyTorch ground truth) ``` - **Registration** — backends register with `@register_kernel(family, mode, ...)`, declaring supported `format_signatures`, arch capability requirements, non-format traits (head dim, GQA factor, ...), and a priority band. - **Auto-selection** — `select_kernel` filters by capability and traits, ranks the survivors with an optional per-family `SelectionOracle` and priority, and returns a callable. Selection supports per-call `solution=` and `override=` plus config-file overrides for development. For call sites requiring batch invariance, `KernelPlan.select()` selects from declared capabilities at initialization using static geometry. Execution reuses that plan and validates its input contract without re-ranking by batch size. This is opt-in; ordinary auto-selection keeps its existing policy. A planned leaf that can be recorded into a CUDA graph also declares the `cuda_graph` feature; callers that plan their leaves capture only through leaves declaring it. ### Directory structure ``` tokenspeed_kernel/ __init__.py # Public API re-exports platform.py # PlatformInfo, capability detection signature.py # TensorFormat, ScaleFormat, FormatSignature weights.py # Weak storage-layout tracking and tensor replacement registry.py # KernelRegistry, register_kernel, Priority bands selection.py # select_kernel, oracles, overrides profiling.py # ShapeCapture, kernel_scope, Proton bootstrap _triton.py # Single import point for the vendored Triton fork ops/ attention/ { mha/, mla/, dsa/, ... } gemm/ { triton.py, trtllm.py, ... } moe/ { triton/, flashinfer/, marlin/, ... } ... numerics/ # Reference impls + tolerance + comparison + CLI torch/ # PyTorch ground truth, laid out like ops/ benchmark/ # Unified runner, throughput model, report, CLI plugins/ # Out-of-tree backend discovery thirdparty/ # Vendored / wrapped third-party kernel sources ``` Each `ops//` directory groups implementations by operator variant and then solution. For example, attention uses `attention//.py` such as `attention/mha/triton.py`. A solution is either an in-tree JIT kernel (Triton/Gluon/CuteDSL) or a thin wrapper around an external library. All of them register through the same decorator, and the same selection logic scores them, so adding a backend is one new file in the right family folder. ### Solution choices - **Triton** — in-tree; default portable JIT path for various kernels, including precomputed-routing MoE with unquantized or MXFP4 expert weights - **Gluon / CuteDSL** — in-tree; performant JIT path for key kernels - **gfx950 block-FP8 MoE** — direct compact-weight Gluon warp GEMVs for decode-shaped batches and tuned BF16 Gluon kernels for prefill. BF16 expert copies are created at load time while compact FP8 experts remain available for decode - **Vendor libraries** — wrapped (such as FlashAttention and TRT-LLM); no in-tree C++ build - **PyTorch reference** — under `numerics/torch/`, one module per `ops/` family (`attention/.py` for attention), registered with `solution="torch"`; never auto-selects over a real backend but always available as ground truth. Kernel tests compare against these references instead of writing their own. TokenSpeed-kernel carefully curates external dependencies and actively re-evaluates their inclusion to maintain minimal dependencies and enable faster iteration. CUDA provider discovery tolerates missing optional native packages, including FlashMLA and DeepGEMM. Providers expose native implementations when their corresponding packages are available. Direct calls to unavailable native entry points raise an error. ### Numerics, benchmarking, profiling - `python -m tokenspeed_kernel.numerics` — dtype-aware tolerances, standard input generators, and a comparison/bisect flow that pits any registered kernel against the reference implementation. - `python -m tokenspeed_kernel.benchmark` — unified timing, throughput (FLOPs / bytes) per op family, tabular reports, and Proton integration. - `KernelBenchmarkHarness` — registration-level device timing through warmed graph replay, with raw samples, resolved registration metadata, and explicit failure outcomes. - Runtime shape capture feeds replay and tuning workflows. `kernel_scope` scopes are visible in Proton/Chrome traces. The joint BF16 `mm` fast path records the same shape metadata and scopes as registry-selected kernels. - End-to-end serving: start a profile with the `PROTON` activity (the engine's `ProfileReq` control operation, which the frontend driving the engine sends), run the workload, then stop it. Each scheduler process — the process where kernels actually launch — runs its own Proton session and finalizes it when the profile stops, writing `/[-DP]-TP.proton.` per rank. `PROTON` composes only with host-side activities (`CPU`, `MEM`, `VIZTRACER`). To see Python activity and Proton's kernel lanes on one Perfetto timeline, profile with `VIZTRACER` + `PROTON` (`TOKENSPEED_KERNEL_PROFILE_DATA=trace`, `TOKENSPEED_KERNEL_PROFILE_OUTPUT_FORMAT=chrome_trace`), then merge the traces with `tokenspeed merge-traces`. Registration-level benchmarks combine operation-owned input and correctness logic with graph-replay device timing. Each operation family and mode contributes one benchmark generator. Suites reference them by family, mode, and parameters. Pull request CI compares compatible cases between the merge base and candidate revision. See the [benchmark documentation](benchmarks/README.md) for the harness and suite contract, and the [CI documentation](../test/ci/README.md#registration-level-kernel-benchmarks) for workflow behavior and runner requirements. ### Plugins `python -m tokenspeed_kernel.plugins list` lists discovered out-of-tree backends. Plugins register through the same `@register_kernel` decorator from their own package, set their own priority, and participate in selection like in-tree backends. See `tokenspeed_kernel/plugins/README.md`. ### Weight preprocessing Some kernels need loaded weights converted into a representation suited to their computation. During model initialization, the layer chooses a compatible preprocessor. A shared weight broker installs the transformed weights and tracks their storage layout, making that representation visible to kernel selection. At execution, the registry matches the prepared weights to kernels that support their layout alongside the operation's other requirements. This lets preparation and execution remain separate while different kernels can share prepared weights. ### Kernel state and graph capture Some kernels keep device state that must start at zero and that every launch restores to zero, such as split-K arrival counters. Such kernels take the state as an argument and report how much they need; the registered op gets it from `tokenspeed_kernel.workspace.zeroed_state`. Eager launches share one buffer per stream. A graph captured inside `capture_session` gets buffers of its own, zeroed once after capture outside the graph and kept alive as long as the graph, so replays carry no zeroing node. The runtime opens a session around every graph it captures. Prepared plans can use `retain_for_capture` in that session to keep their allocations alive until the graph owner is released. Operators can prepare compilation variants during ordinary execution with `ops.tuning.kernel_warmup(enabled=True)` and `is_warming_up()`. FlashInfer profiling uses a separate `autotune` context. Triton operators can register finite launch variants with `warmup.register_triton_warmup` for scalar alignment classes and bounded tile choices that vary with request shapes. `warmup.kernel_launcher` prepares these variants during ordinary startup execution, then launches the original call. Fixed-geometry cache management operations retain lazy compilation on first use; they need no synthetic startup writes, transfers, or waits. Per-request sizes must still be runtime arguments, even when their variants are warmed. ### JIT compilation while serving Compile-time kernel parameters (`tl.constexpr`, `gl.constexpr`) key the Triton compile cache. A per-batch value passed as one compiles a new binary on the forward thread for every new batch shape (100 ms to seconds each). `tokenspeed_kernel.compile_monitor` hooks Triton's JIT (Gluon shares it) and CuTe DSL's, and records every compilation. The runtime installs it in each scheduler process and marks the end of startup. After that, the monitor logs each compilation with its duration, what changed in the compile key, and the launching call site. When a compile-time parameter keeps taking new values from one call site, the monitor names it (`TOKENSPEED_JIT_COMPILE_CHECK=warn`, the default) or raises (`error`, which CI serving jobs use). Kernel tests guard batch-varying launches with `assert_no_triton_compile` and `assert_no_cute_compile` in `test/utils.py`. The monitor does not observe other JITs, such as DeepGEMM's per-shape kernels; they need the same discipline at their call sites. CuTe DSL leaves the compile cache to the caller: `cute.compile` traces and compiles on every call, and the caller keeps the result under a key of its choosing. That key must hold only what the kernel specializes on: its `cutlass.Constexpr` arguments, the dtypes and static layouts of its tensors, and the kernel object's attributes. A per-batch size belongs in a dynamic dimension (`mark_layout_dynamic`, `cute.sym_int`) or an `Int32` argument, and then out of the key. A key that holds a value the kernel takes at runtime compiles the same kernel again for every new value; the monitor reports a call site that keeps doing so. Calling a `@cute.jit` function directly, instead of the compiled function, traces it in Python on every call even when CuTe DSL has cached its binary, so the monitor reports such a call while serving. Across processes, `tokenspeed_kernel.cute_compile_cache` keeps `cute.compile` binaries in CuTe DSL's on-disk cache (`CUTE_DSL_CACHE_DIR`), which cutlass-dsl itself uses only for direct `@cute.jit` calls. It wraps `BaseDSL.compile_and_cache`, so tracing still runs and a hit skips lowering and ptxas (about half of the startup CuTe compile time). The key adds the source of the kernel's defining module, the cutlass-dsl version, the target and device architecture, the compile options and, with `--enable-tvm-ffi`, the TVM-FFI argument spec (attached after compiling, so an explicit stream and the environment stream need separate entries) to cutlass-dsl's own module hash, so an edit to a kernel file never loads a stale binary. The runtime installs it with the monitor; `CUTE_DSL_DISABLE_FILE_CACHING=1` turns it off. An unset `CUTE_DSL_CACHE_DIR` becomes `$XDG_CACHE_HOME/tokenspeed/cute-dsl` (beside the FlashInfer autotune cache), because cutlass-dsl's default under `$TMPDIR` starts empty in each new container; point it at persistent storage in deployments. Like Triton's cache it is never pruned, and each kernel-file edit adds entries for that file's kernels, so clear it from time to time while developing. cutlass-dsl's own helpers are not cached: their traces carry process addresses. The wrapped function is private to cutlass-dsl, so the cache installs only on the cutlass-dsl releases it was verified against, and only when the parameters it reads are still there; otherwise it stays off with one warning. Verify a new release with `test/nvidia/test_cute_compile_cache.py` before widening that range. A runtime argument still keys the cache: Triton specializes an integer on whether it is 1 or divisible by 16, and a pointer on 16-byte alignment. Startup warms only the classes graph capture happens to see. On the serving path, a per-batch count (tokens, rows, requests) belongs in `do_not_specialize`, and a pointer into a buffer sliced at a per-batch offset belongs in `do_not_specialize_on_alignment`. `do_not_specialize` already drops the alignment key too, so its arguments need no second listing in `do_not_specialize_on_alignment`. A stride that changes between call sites but stays a multiple of 16, such as a projection's row width, stays a plain runtime argument: one class covers it, and the hint keeps row loads vectorized. Use a fixed block with a loop for a batch-derived block size rather than a power-of-two bucket; the bucket form still compiles once per new bucket while serving. The end-of-startup mark is also the package's compile switch, set whether or not the monitor is installed. A kernel whose library compiles once per batch shape and cannot bucket it, such as FlashInfer's joint BF16 GEMM, checks `compile_monitor.is_serving()` where it is dispatched: startup tuning and graph capture use it, and eager calls while serving take a GEMM that never compiles. ## Nightly installation CUDA 13 nightly wheels are published daily from `main` for Linux x86_64 and ARM64, with Python 3.11–3.13. Versions append the UTC build date to the base version, for example `0.1.3.post20260929`. ```bash pip install --upgrade tokenspeed-kernel \ --extra-index-url https://lightseek.org/whl/nightly ``` PyPI supplies dependencies that are absent from the nightly index. To select a specific nightly, use `tokenspeed-kernel==0.1.3.post20260929`. Post releases sort above the corresponding base release and do not require `--pre`. The `Build and Release tokenspeed-kernel` workflow also supports manual nightly builds from pull request branches. To publish manually, run it from `main` with both `nightly` and `publish_github` enabled. Same-day reruns preserve already published wheels. Historical nightlies are retained. Automatic cleanup is deferred. ### Ascend source installation Select the NPU dependency set explicitly when installing from source: ```bash TOKENSPEED_KERNEL_BACKEND=npu pip install --no-build-isolation ./python ``` This installs the Python registry and Ascend Triton provider and skips CUDA compilation. Install a compatible Torch/TorchNPU stack first. The `tokenspeed-kernel-npu` package and its native operator dependencies are separate installations. Shared modules may define GPU kernels during import. The Ascend Triton adapter accepts Gluon JIT and constexpr helper decorators without loading the GPU provider; calling these decorated functions still raises an explicit error. Capability selection must exclude them on Ascend.