Skip to content

feat(tokamax): add narrow tokamax 0.0.17 TPU kernel - #1307

Open
tengomucho wants to merge 4 commits into
huggingface:mainfrom
tengomucho:feat/tokamax-kernel
Open

tengomucho wants to merge 4 commits into
huggingface:mainfrom
tengomucho:feat/tokamax-kernel

Conversation

@tengomucho

Copy link
Copy Markdown
Member

Summary

Adds kernels-community/tokamax: the Pallas Mosaic TPU kernel bodies of
Tokamax 0.0.17 as a noarch TPU kernel,
so that TPU kernels can use them through kernel-depends. For instance,
ragged-dot-tpu (#1208) currently needs an undeclared tokamax install.

This follows the review of huggingface/kernels#972: no new Python
dependencies. Tokamax's Op, autotuning and benchmarking layers need
pydantic, jaxtyping, absl and more at import time, so they are not ported.
The kernel needs nothing beyond JAX at runtime.

  • Generated by a kernel-port recipe (tokamax/port/port.kdl), like
    einops. tokamax/src is not edited by hand.
  • Exposed ops, each as its kernel module (e.g. tokamax.ragged_dot.gmm,
    tokamax.gmm_v2.gmm_v2):
    • ragged dot, including qwix-quantized operands, and gmm_v2/tgmm_v2;
    • splash and ring attention;
    • the kernels that were already JAX-only: topk, router_topk, ragged_gather,
      ragged_gather_reduce, deepseek_rope, csa_gather, sparse_mla,
      masked_dense_mla, proj_and_save_state, grouped_lowrank_proj,
      dsv4_compressor, mla, mla_swa, fused_fp8_matmul, fused_moe_decode, mhc.
  • Vendored:
    • qwix's QArray, quantize and dequantize (3 files of qwix 0.1.8).
      flax.struct is replaced with dataclasses plus
      jax.tree_util.register_dataclass, and the flax nnx registration is
      dropped.
    • immutabledict (1 file), which keeps AsQArray's pytree metadata hashable.
  • Patches to upstream code, all in the recipe and pinned by count:
    • The ragged dot and splash attention kernels no longer import their op's
      base module (the Op layer). They only used a type alias, and names
      re-exported from reference.
    • GMM v2 replaces its one absl flag (disable_multi_core_mode) with the
      default, False.
    • Splash attention imports jax.extend explicitly. Upstream only gets it
      through modules that are not ported.
    • expect guards fail the port if an upstream bump brings back pydantic,
      jaxtyping, absl, flax or a bare qwix import.
  • scripts/check_ports.py (separate commit) now passes each vendor
    op's pinned checkout to kernel-port, so the Check Ports workflow can
    reproduce recipes that vendor a second repository.
  • AGENTS.md gets a tokamax sync section.

Testing

On a TPU v6e VM:

  • scripts/check_ports.py tokamax einops: both regenerate byte for byte.
  • nix build .#bundle (torch-tpu variant): passes, including the
    get_kernel import check.
  • pytest -m kernels_ci tokamax/src/tests with LOCAL_KERNELS, in a venv
    without tokamax, qwix, flax, pydantic or jaxtyping: 7 passed in 21s.
    • One test loads the kernel in a subprocess with those packages blocked.
    • The others compare ragged dot (bf16, and int8 rhs as QArray and
      AsQArray), gmm_v2 and causal splash attention with references.
  • The vendored qwix is bit-identical to PyPI qwix 0.1.8 (int8 and fp8,
    several tilings, eager and jit).
  • A throwaway consumer kernel with
    kernel-depends = [{ repo-id = "kernels-community/tokamax", version = 1 }]
    got the kernel through get_kernel_dep. It ran gmm_v2 on Torch TPU
    tensors through jax_op and matched a Torch reference.

Not tested:

  • A consumer's kernels.lock and Nix build: lock-kernel-deps resolves
    dependencies on the Hub, so this needs tokamax to be published first.
  • Numerically, only ragged dot, gmm_v2 and splash attention are checked.
    The other modules are covered by the import and public API tests.
  • There is no TPU CI runner, so the TPU tests were run by hand.

🤖 Generated with Claude Code

Recipes can vendor a second pinned repository with the `vendor` op, which
kernel-port only runs when given `--vendor <name>=<checkout>`. Parse the
vendor ops like the source op, clone each pin, and pass it on, so such
ports can be checked in CI too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the feature New functionality / capability label Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

This PR changes source code for the following kernel(s): tokamax.

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

Port the Pallas Mosaic TPU kernel bodies of Tokamax 0.0.17 with a
kernel-port recipe, as a noarch TPU kernel that other TPU kernels can use
through kernel-depends. Tokamax's Op, autotuning and benchmarking layers
are left out, so the kernel needs nothing beyond JAX at runtime and adds
no Python dependencies.

Each op is exposed as its kernel module: ragged dot (including qwix
quantized operands), gmm_v2/tgmm_v2, splash and ring attention, and the
kernels that were already JAX-only (topk, router_topk, ragged_gather,
ragged_gather_reduce, deepseek_rope, csa_gather, sparse_mla,
masked_dense_mla, proj_and_save_state, grouped_lowrank_proj,
dsv4_compressor, mla, mla_swa, fused_fp8_matmul, fused_moe_decode, mhc).

The few parts of qwix (QArray, quantize, dequantize) and immutabledict
that the kernels use are vendored; qwix's flax pytree registration is
replaced with jax.tree_util.register_dataclass.

The kernels_ci tests check that the kernel loads without the dropped
dependencies and compare ragged dot, gmm_v2 and splash attention with
references on TPU.

The kernel is also added to the maintainers registry and to the freshness
check's source mapping.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tengomucho

Copy link
Copy Markdown
Member Author

/kernel-bot build tokamax

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot build tokamax
Mode: build only
Target branch: pr-1307
PR head SHA: fab846135ccb11d6d6d2f7bdb67a6306e16f1dc5
Workflows: build.yaml, build-mac.yaml, build-windows.yaml

Dispatched (1):

@tengomucho
tengomucho marked this pull request as ready for review November 10, 2026 14:45
Comment on lines +17 to +34
LOAD_WITHOUT_DROPPED = """
import sys


class Block:
def find_spec(self, name, path=None, target=None):
if name.split(".")[0] in {dropped!r}:
raise ModuleNotFoundError(f"{{name}} is blocked", name=name)
return None


sys.meta_path.insert(0, Block())

import kernels

tokamax = kernels.get_kernel("kernels-community/tokamax", version=1)
print(tokamax.__version__)
"""

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.

this string and executable is a bit strange - can we handle in a different way?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, agreed and removed. A dropped dependency can only come back with an upstream sync, so the check now lives in the port recipe: port.kdl fails the port if any of them is imported (I added einshape and xprof to the existing checks).

Comment thread .github/kernel-maintainers.json Resolved

@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.

looks good just left some tiny nits and will approve once resolved.

also I'm looking forward to revisiting this port file soon after the kernel-port tool is updated since I think we can make it a bit more concise (but that will be in a follow up pr)

tengomucho and others added 2 commits November 13, 2026 16:12
The dropped-dependency test ran a code string in a fresh interpreter.
The port recipe already rejects pydantic, jaxtyping, absl, flax and qwix
imports; add einshape and xprof so it covers every dropped dependency,
and drop the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tengomucho
tengomucho requested a review from drbh November 13, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New functionality / capability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants