model-as-a-kernel

A llama-family language model as one kernel launch, loadable through kernels. The reference baseline is transformers eager, matched op-bitwise where semantics are exactly defined and within transformers' own execution-path spread on whole-model logits.

An eager greedy generation of a small model is roughly 180 kernel launches per token, and at 135M parameters the launches, not the arithmetic, are the wall. This kernel runs the full forward pass, embedding, norms, projections, rope, attention over the KV cache, MLP, LM head, and greedy argmax, inside one persistent phase-interpreter kernel that crosses a software grid barrier between phases instead of returning to the host, and an in-kernel step loop carries the barrier across token boundaries. An entire generation, prompt consumption included, is a single cudaLaunchKernel, and decode runs an order of magnitude past the eager loop.

A typing race: the eager lane crawls with a dense launch comb while the one-launch lane finishes its whole generation

SmolLM2-135M decoding live at true relative speed (clock slowed 6x): the eager loop types at 39 tok/s behind thousands of kernel launches; the persistent kernel types at 805 tok/s behind exactly one, with repeated runs token-identical.

Usage

from kernels import get_kernel

mak = get_kernel("phanerozoic/model-as-a-kernel", version=1, trust_remote_code=True)

mm = mak.MegaModel.from_pretrained("HuggingFaceTB/SmolLM2-135M")

tokens = mm.generate(prompt_ids, max_new=128)     # one kernel launch, total
logits = mm.decode_step(token_id, pos)            # one launch, fp32 [V] logits
logits = mm.prefill(prompt_ids)                   # chunked prompt consumption
streams = mm.generate_batch([ids_a, ids_b], max_new=128)   # batched decode

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark.

API

Symbol Purpose
MegaModel.from_pretrained(model_or_id, device, max_seq, max_gen) pack a checkpoint into a program
decode_step(token, pos, phased=False) one decode step, live fp32 logits [V]
prefill(ids) chunked prompt consumption
generate(prompt_ids, max_new, single_launch=True) greedy generation; one launch total, or one per token
generate_batch(prompts, max_new) / decode_batch(tokens, positions, steps) batched greedy decode, up to batch_max() sequences
decode_step_timed(token, pos) phase-per-launch mode, per-phase ms
ops.mak_* raw program launches

Supported: RMSNorm decoder blocks with rotary attention (GQA, optional Qwen3-style qk RMSNorm), SwiGLU MLP, plain unscaled rope, single device; SmolLM2, TinyLlama, Qwen3 dense, and Llama-class checkpoints without rope scaling. Weights dense bf16 or bitsandbytes nf4.

Method

Model load packs the weights and compiles the architecture into an int64 [n_phases, 16] program naming an op per row with its pointers and sizes. One persistent kernel interprets the program; all resident blocks execute each phase cooperatively and cross a sense-reversing global barrier, with the grid sized from the occupancy API so the barrier cannot deadlock. Arithmetic reproduces transformers eager bf16 semantics op for op, with fixed reduction trees and no floating-point atomics, so outputs are bitwise deterministic. GEMV weights stream through a per-warp cp.async shared-memory ring; attention splits cached positions into 128-token chunks with the softmax combine folded into the O-projection's staging; prompts are consumed in chunks of up to eight tokens, bitwise identical to token-by-token consumption. A phase-per-launch debug mode is bitwise identical to the fused mode.

Measured

Greedy decode, 128 tokens with a 32-token prompt, against transformers eager and a compiled decode loop (torch.compile reduce-overhead, StaticCache, CUDA graphs):

SmolLM2-135M bf16:

GPU this kernel eager compiled static
RTX 6000 Ada (sm89) 902 tok/s 35 n/a (Windows)
H200 (sm90) 715 tok/s 52 586
RTX PRO 6000 (sm120) 726 tok/s 55 721

Llama-3.2-1B bf16:

GPU this kernel eager compiled static
H200 (sm90) 490 tok/s 99 622
RTX PRO 6000 (sm120) 368 tok/s 98 403

Steady-state decode at position 512 on Llama-3.2-1B: 1.85 ms/token on H200 (1.62 TB/s effective weight bandwidth). Prefill: a 2040-token prompt costs 0.73 s against 2.7 s token by token, bitwise-identical logits either way. Batched decode scales Llama-3.2-1B from 256 tok/s at batch 1 to 1673 at batch 16 (6.5x aggregate), with each sequence's logits bitwise identical to decoding alone.

Correctness

Verified against transformers eager through get_kernel on L4, H200, and RTX PRO 6000, and from source on RTX 6000 Ada. Bitwise (torch.equal): the embedding read, fused rmsnorm+GEMV, rope tables and rotated K, attention at S=1, and the layer-0 KV cache along a 48-token sequence against DynamicCache. Whole-model teacher-forced logits sit within transformers' own execution-path spread (mean 0.12-0.15, argmax agreement 93-96 of 96, matching the spread between transformers' own eager and sdpa paths). Fused, phase-per-launch, and repeated runs are mutually bitwise identical, and the single-launch generation emits the same tokens as one launch per token.

Requirements and limits

  • One CUDA device; greedy sampling in-kernel (route the fp32 logits to an external sampler for anything else).
  • Llama-family architectures without rope attention scaling or attention biases; prompts prefill in chunks of at most eight tokens.
  • The program bakes device pointers, so the MegaModel owns its weights and buffers for its lifetime.

References

Persistent-kernel programming and software grid barriers (cooperative groups); the launch-overhead analysis of small-model decode; transformers eager semantics as the reference.

License

Apache-2.0.

Downloads last month
-
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64
Kernel Builder
05f1ed4