tpu-megakernels README

README проекта с github.com/Inferact/tpu-megakernels. Одним файлом markdown: README.md.

TPU megakernels for Kimi K3, Qwen3.8-27B, and Muse-Spark-Open

TPU megakernel: 709 tokens per second on Kimi K3, 16x TPU v7 vs 452 on 16x GB200

Companion code for the blog post 700 TPS on Kimi K3: A Case for TPU Megakernels.

This repository contains fused decode implementations for three models:

The demos support terminal interaction and an OpenAI-compatible HTTP server.

Results

With speculative decoding at an acceptance length of 6, the megakernels reach 709 accepted tokens per second on Kimi K3 (16× TPU v7 vs 16× GB200, 1.57×) and 1,515 on Qwen3.8-27B (4× TPU v7 vs 4× GB200, 2.18×):

Speculative decode throughput at acceptance length 6: Kimi K3 709 vs 452 tokens/s; Qwen3.8-27B 1,515 vs 695 tokens/s

DSpark keeps a large lead at shorter acceptance lengths as well:

Accepted tokens per second with DSpark speculative decoding at acceptance lengths 3 and 6

Without speculative decoding, the megakernels deliver roughly 1.4–2× the decode throughput of the GB200 baseline at batch sizes 1 through 8:

Aggregate decode throughput at batch sizes 1, 2, 4 and 8, TPU megakernel vs GB200 baseline

See the blog post for the full analysis and measurement setup.

Setup

Create the environment from the repository root:

uv sync

The launchers run Hugging Face in offline mode, so the model snapshots must already be present in the cache selected by HF_HOME, or supplied as explicit local paths. All scripts automatically load an optional, untracked .env file from the repository root. A typical configuration is:

HF_HOME=/path/to/huggingface
KIMI_PRESHARDED_WEIGHTS=/path/to/kimi-k3-tp32
KIMI_DRAFT_PRESHARDED_WEIGHTS=/path/to/kimi-k3-tp32/dspark
MPK_PACKED_WEIGHTS_TP8=/path/to/muse-spark-tp8-pack
MPK_PACKED_WEIGHTS_TP16=/path/to/muse-spark-tp16-pack
MPK_PACKED_WEIGHTS_TP32=/path/to/muse-spark-tp32-pack

The Kimi pre-sharded variables are optional. Without them, the demo prepares its kernel layouts from moonshotai/Kimi-K3 and RedHatAI/Kimi-K3-speculator.dspark. The Qwen demo loads Qwen/Qwen3.8-27B and z-lab/Qwen3.8-27B-DFlash2; packed weight containers under checkpoints/qwen38-tp8 (or $QWEN_PACKED_WEIGHTS when set) are used instead when present. The optional KIMI_CHECKPOINT_REVISION, KIMI_DRAFT_REVISION, QWEN_CHECKPOINT_REVISION, and QWEN_DRAFT_REVISION variables pin the commits resolved from the HF_HOME cache for the default repositories. Explicit --checkpoint, --draft, --weights, and --draft-weights arguments override these defaults.

Muse-Spark-Open loads meta-models/Muse-Spark-Open-816B-A42B-NVFP4 and Inferact/Muse-Spark-Open-DSpark. Set the matching MPK_PACKED_WEIGHTS_TP8, MPK_PACKED_WEIGHTS_TP16, or MPK_PACKED_WEIGHTS_TP32 to an existing target pack, or pass --weights. To create a pack from an existing checkpoint on a high-memory compute node:

JOBID=<slurm-job> bash scripts/pack_mpk.sh \
  --checkpoint /path/to/Muse-Spark-Open-816B-A42B-NVFP4 \
  --tp 16 --output /path/to/model-storage/muse-spark-tp16-pack

Use --tp 8, 16, or 32 and a separate output directory on model-weight storage for each. The DSpark draft loads directly from its checkpoint. MPK_CHECKPOINT_REVISION and MPK_DRAFT_REVISION optionally pin cached revisions.

For parallel GCS packing, install uv sync --extra gcs, set GCS_BUCKET and GCS_MOUNT_ROOT in .env, and run .venv/bin/python gcs_parallel_pack.py pack_mpk.py with the same packing arguments on a compute node. Output must be under the bucket's root mount; authentication uses Application Default Credentials.

Kimi requires a four-node Slurm allocation with eight TPU devices per host. Qwen uses one host with eight TPU devices and can run on the first node of any allocation containing at least one host.

CPU tests

On an allocated compute node, run the reference and CPU-interpreter tests with:

JAX_PLATFORMS=cpu uv run pytest -m "not cpu32"

Run the complete suite, including 32-device integration tests, with 32 virtual CPU devices:

JAX_PLATFORMS=cpu \
XLA_FLAGS=--xla_force_host_platform_device_count=32 \
uv run pytest

These tests validate numerical behavior, not TPU-specific Mosaic lowering, layouts, VMEM capacity, DMA scheduling, collectives, or performance.

OpenAI-compatible servers

Start the Kimi K3 + DSpark server on a four-node allocation:

JOBID=<four-node-job> bash scripts/demo_kimi_dspark.sh \
  --serve 0.0.0.0:8000 --context 4096 --max-tokens 512

Start the Qwen3.8-27B + DFlash2 server on the first host of an allocation:

JOBID=<slurm-job> bash scripts/demo_qwen_dflash.sh \
  --serve 0.0.0.0:8000 --context 2048 --max-tokens 512

Start Muse-Spark-Open + DSpark on a two-host allocation:

JOBID=<two-host-job> bash scripts/demo_mpk_dspark.sh --tp 16 \
  --serve 0.0.0.0:8000 --context 4096 --max-tokens 512

For TP8, use --tp 8 and optionally HOST=<allocated-host>. For TP32, use scripts/demo_mpk_tp32.sh on four hosts and add --draft to enable DSpark. All Muse-Spark-Open paths use steps_per_call=4; DSpark supports one sequence at a time.

For non-speculative TP8/TP16 serving, add --no-spec; --batch-size 1, 2, 4, or 8 sets the maximum batch. TP32 defaults to non-speculative batch size one; set MPK_TP32_EXPERT_PARALLEL=1 to select TP8×EP4 and enable larger batches. Batched serving waits up to 10 ms to collect requests; --batch-wait-ms overrides this.

scripts/eval_mpk_gsm8k.sh and scripts/benchmark_mpk.sh support all three layouts through --tp 8, 16, or 32. Both default to non-speculative decoding; --spec enables DSpark. The evaluator supports --reuse-server. Run each script with --help for options; both run clients on compute nodes and stop only servers they launch.

The servers expose GET /health, GET /v1/models, POST /tokenize, POST /detokenize, POST /v1/completions, and POST /v1/chat/completions. Kimi uses DSpark by default and supports greedy or temperature/top-p generation; --target-only serves the target megakernel without speculation. Qwen uses DFlash2 by default with greedy generation; --no-spec selects its plain single-token decoder. Omit --serve to use the launchers interactively.

Source layout

Generated weights, checkpoints, compilation caches, evaluation output, benchmark output, and Slurm logs are excluded from version control.

Citation

If you use this codebase or build on our results, please cite the companion blog post:

@misc{novack2026tpumegakernels,
  author       = {Novack, George and Liu, Xuting and Ma, Jeff and Kwon, Woosuk},
  title        = {700 TPS on Kimi K3: A Case for TPU Megakernels},
  howpublished = {\url{https://inferact.ai/blog/tpu-megakernels}},
  year         = {2026},
  month        = sep,
  note         = {Inferact blog post, September 23, 2026},
}

tpu-megakernels (Inferact/tpu-megakernels)

Ищем в базе…

Что это?Сопроводительный исследовательский репозиторий со слитными megakernel-реализациями декодирования для двух моделей на Google TPU: Kimi K3 со спекулятивным декодированием DSpark и Qwen3.8-27B со спекулятивным декодированием DFlash2; написан в дополнение к блогпосту '700 TPS on Kimi K3: A Case for TPU Megakernels'.
Это универсальная библиотека инференса или демо-бенчмарк для этих двух моделей?Это демо-код, построенный специально вокруг двух модельных пайплайнов, а не универсальная библиотека для сервинга: у каждой модели своё слитое TPU-ядро плюс интерактивный режим в терминале и HTTP-сервер, совместимый с OpenAI API.
Как это запустить?Разверните окружение Python/JAX из корня репозитория, убедитесь, что снапшоты моделей Hugging Face закэшированы (лаунчеры запускают HF в офлайн-режиме), затем запустите demokimidspark.py или demoqwendflash.py интерактивно либо с --serve, чтобы поднять HTTP API.
Нужно ли настоящее TPU-железо (и кластер Slurm), чтобы это запустить?Да — Kimi K3 требует выделения Slurm из четырёх узлов с восемью TPU-устройствами на хост (всего 32), а Qwen3.8-27B — одного хоста с восемью TPU-устройствами; тесты корректности на CPU есть, но реальное исполнение на TPU они не проверяют.
Доказывают ли CPU-тесты, что TPU-ядра реально корректно работают на железе?Нет — набор тестов на CPU/интерпретаторе проверяет только численную корректность; он не проверяет специфичные для TPU понижение (lowering) в Mosaic, раскладки памяти, ёмкость VMEM, планирование DMA, коллективные операции и реальную производительность.
Это работает на GPU или только на Google TPU?Работает только на устройствах Google TPU; GPU GB200 фигурируют лишь как базовая линия производительности, с которой сравниваются мегаядра, а не как поддерживаемая цель исполнения.
Насколько эти мегаядра быстрее GPU?Со спекулятивным декодированием Kimi K3 выдаёт 709 принятых токенов/с на 16x TPU v7 против базовой линии из 16x GB200 (ускорение 1.57x), а Qwen3.8-27B — 1 515 токенов/с на 4x TPU v7 против 4x GB200 (2.18x). Без спекуляции TPU-ядра всё равно дают примерно в 1.4–2 раза большую пропускную способность декодирования, чем GB200, при размерах батча 1–8.
Что такое DSpark и DFlash2?Это слитые схемы спекулятивного декодирования, идущие в комплекте с каждой моделью: DSpark сочетает Kimi K3 с одной якорной и семью черновыми строками, а DFlash2 добавляет плотному BF16-декодеру Qwen3.8-27B опциональный слитый цикл черновика и спекуляции.
Что предоставляет сервер, совместимый с OpenAI?Каждый сервер предоставляет GET /health, GET /v1/models, POST /tokenize, POST /detokenize, POST /v1/completions и POST /v1/chat/completions, то есть к нему можно обращаться как к стандартному эндпоинту OpenAI API.
Зачем писать собственные TPU-мегаядра вместо стандартного фреймворка инференса?Слияние всего шага декодирования — внимания (attention), MoE, нормализации, остаточных связей и коллективных операций — в единое TPU-ядро снижает накладные расходы на диспетчеризацию каждой операции, позволяя чипам TPU v7 догонять или обгонять GPU GB200 по пропускной способности декодирования; подробности — в сопроводительном блогпосте.
Какие есть похожие проекты?vllm, deltafin, resin, vllm-torchtpu, jeeves, kaggle-tpu-lab, v100-skinny, sglang
Автор
Inferact
Размер
1.4 MB
Звёзды
189
Обновлён
КоммитOct 15
Возраст
2 мес
Коммиты · 90 дней