An end-to-end GPU-kernel and inference-engine study for
Qwen/Qwen3-0.6B. The repository moves
from individual Triton/CUDA kernels to real prefill and decode integration,
profiling, numerical acceptance gates, and a production-style serving control
plane.
The engineering rule is simple:
Keep a custom kernel only when it passes correctness checks and improves the complete workload. A faster microbenchmark alone is not a model speedup.
No external optimized kernel repository was copied into this project. PyTorch, Triton, Transformers, cuBLAS/CUTLASS dispatch, and PyTorch SDPA/FlashAttention are dependencies and reference implementations.
The repository is now an installable package with a unified model registry and commands for setup, serving, benchmarking, and result validation:
python -m pip install -e ".[cpu,llama]"
kernellab models
kernellab doctor
kernellab download llama-3.2-3b
kernellab serve llama-3.2-3b
kernellab benchmark llama-3.2-3b --engine pytorch --output results/llama-pytorch.json
kernellab validate-result results/llama-pytorch.json
See the architecture, Llama deployment guide, and reproducibility matrix. GPU-dependent claims remain pending until their schema-v2 artifacts exist.
The learning kernels, model integrations, static decode runtime, CUDA Graph experiment, profiling tools, paged-KV allocator, continuous scheduler, sampling, metrics, and OpenAI-compatible API are implemented.
Two outcomes are intentionally kept separate:
The serving control plane now has a correctness-first GPU Qwen executor. It preserves tiled SDPA prefill, writes K/V into fixed physical pages, batches variable-length decode requests, and passed an exact token/logit gate. A faster direct Triton paged-attention candidate remains opt-in because it misses the 99% decode-argmax release threshold.
This repository now hosts multiple optimized model projects under
models/:
| Slot | Model | Optimization focus | Status |
|---|---|---|---|
| 1 | Qwen3-0.6B | Custom Triton/CUDA kernels, static decode, CUDA Graphs, serving control plane | Exact production path; faster experimental paths gated |
| 2 | Llama-3.2-3B | vLLM serving, matched benchmarks, profiling, kernel work | Phase 1 methodology implemented; fresh GPU validation pending |
| 3 | To be selected | Architecture-specific optimization | Planned |
| 4 | To be selected | Architecture-specific optimization | Planned |
The Llama source is pinned at upstream commit 80087ed and included directly,
so a normal clone receives it without submodule commands. See its
provenance record and the
portfolio roadmap.
RTX 3090 24 GiB, PyTorch 2.8.0+cu128, Triton 3.4.0, Transformers 5.16.1, BF16, batch 1, SDPA prefill, greedy one-token decode, context 2,048:
| Metric | PyTorch/dynamic baseline | Best implementation | Change |
|---|---|---|---|
| TTFT p50 | 66.007 ms | 65.332 ms | 1.02% lower |
| TTFT p99* | 70.577 ms | 68.160 ms | 3.43% lower |
| Decode ITL p50 | 22.073 ms | 2.253 ms | 89.79% lower |
| Decode ITL p99* | 22.851 ms | 2.258 ms | 90.12% lower |
| Serial throughput from p50 ITL | 45.30 token/s | 443.90 token/s | 9.80x |
| Decode kernels/token | 693 | 258 | 62.8% fewer |
* p99 is interpolated from five GPU-event repetitions and is preliminary, not
a statistically strong production tail-latency result.
The decode result is the experimental CUDA-Graph path. Its throughput is
1000 / ITL for a serial GPU-only loop and excludes tokenization, networking,
scheduling, EOS handling, and general sampling. It is not a production-server
throughput claim.
The real paged-serving integration was also validated on an RTX 2000 Ada (SM89): 256/256 prefill decisions and 1,024/1,024 decode decisions matched with zero logit difference. The direct paged-attention kernel was 1.74–5.37x faster than gather-plus-SDPA in isolation across contexts 128–4,096, but achieved only 97.65625% decode agreement and is therefore not the production default. See the paged-serving report.
Phase 3/4 was independently checked on an RTX A4000 (SM86). The accepted lossless paged-gather policy retained 100% of 256 prefill and 1,024 decode argmax decisions with zero logit difference, while reducing matched paged decode median latency by 12.84–15.46% across contexts 128–4,096. More aggressive projection fusion did not deliver a consistent 20% without either numerical or long-context regressions, so it remains experimental. See the Ampere validation report.
Phase 5/6 then hardened the production scheduler and portability boundary. Decode-first execution, stable metadata shapes, removal of host synchronizations, and adaptive prefill quanta raised matched concurrent throughput by 13.53% while reducing ITL p50 by 20.85% and ITL p99 by 25.80%. A 64-request real-model run completed without failures or leaked KV blocks at 65.52 generated token/s. See the portable serving report.
| Context | Dynamic ITL | Graph ITL | Dynamic token/s | Graph token/s |
|---|---|---|---|---|
| 128 | 21.906 ms | 2.318 ms | 45.65 | 431.46 |
| 512 | 21.958 ms | 2.321 ms | 45.54 | 430.92 |
| 2,048 | 22.073 ms | 2.253 ms | 45.30 | 443.90 |
| 4,096 | 22.021 ms | 2.599 ms | 45.41 | 384.70 |
See the final GPU report for methodology, Nsight evidence, numerical results, and limitations.
Every chapter under learning/ contains code, tests, benchmarks,
and explanations of mapping, coalescing, precision, reductions, register
pressure, fusion, and trade-offs.
(q_proj, M=2048, K=1024, N=2048) in the accepted whitelist.This is why accepted TTFT improvement is modest: results reflect integrated performance, not a sum of unrelated microbenchmark gains.
The experimental decode pipeline is:
fixed token/position buffers
-> phase-specific projections
-> Q/K RMSNorm + RoPE + direct K/V cache and mask write
-> GQA-aware split-KV online-softmax attention
-> attention residual + post-attention RMSNorm
-> W8A16 dual gate/up GEMV + SwiGLU
-> MLP residual + next-layer RMSNorm across 27 boundaries
-> LM-head GEMV + argmax + token/position update
-> fixed-address CUDA Graph replay
This removed dynamic cache concatenation, old-cache copying, GQA expansion, and many intermediate tensors. Attention residual/RMSNorm is fused per layer; MLP residual/next-layer RMSNorm is fused across 27 layer boundaries. Kernel launches fell from the early 1,624/token baseline to 258/token.
Quantization is not enabled universally. Some A/B paths retain dense weights, so whole-model memory savings are not claimed unless the dense copy is removed.
serving/ provides:
The 5,000-request control-plane stress test recorded 4,864 completions and 136 intentional cancellations, with zero failures, timeouts, rejections, or leaked KV blocks. All 60,000 blocks were reclaimed. Its 197.81 requests/s rate measures the control plane with a fake executor—not GPU model throughput. Full details are in the serving report. Separately, a real four-request GPU smoke matched all 16 greedy Qwen baseline tokens and reclaimed every physical KV block; larger production traffic measurements are still required.
RMS/fusion-only quality reached 98.05% argmax agreement and minimum per-layer
cosine 0.999726. The complete experimental path reached 96.48% agreement, so it
remains opt-in. Nsight Compute counters were blocked by RunPod's
ERR_NVGPUCTRPERM; occupancy, DRAM bandwidth, warp stalls, and counter-derived
rooflines are therefore not invented.
baseline/ reference implementations
learning/ kernels, tests, benchmarks, and teaching notes
models/ self-contained optimized LLM projects and four-model roadmap
profiling/ workloads, ablations, quality gates, and Nsight helpers
serving/ paged KV, scheduler, sampling, metrics, engine, and API
scripts/ RunPod setup, model download, activation, and smoke tests
docs/ roadmap and optimization plans
results/ raw measurements, traces, logs, and acceptance reports
Recommended reading:
git clone https://github.com/yashlabs-trying/gpu-kernel-lab.git
cd gpu-kernel-lab
bash scripts/setup_runpod.sh
source .venv/bin/activate
python scripts/check_environment.py
python scripts/download_model.py
python scripts/smoke_test_model.py
pytest -q
Key profiling commands:
python profiling/model_workload.py --output results/baseline.json
python profiling/rmsnorm_acceptance.py --help
python profiling/static_kv_benchmark.py --context 2048 --hybrid --split-attention
python profiling/decode_graph_check.py
python profiling/serving_stress.py --requests 5000 --output results/stress.json
Use --help and the commands in individual reports for exact options. Keep model
weights and large profiler artifacts on the persistent volume.
Completed for the single-GPU serving core:
Direct paged attention is intentionally deferred because it failed quality, and Phase 7 was excluded from the requested scope. Multi-GPU topology is detected, but distributed tensor-parallel execution is not claimed as implemented.
An end-to-end GPU-kernel and inference-engine study for
Qwen/Qwen3-0.6B. The repository moves
from individual Triton/CUDA kernels to real prefill and decode integration,
profiling, numerical acceptance gates, and a production-style serving control
plane.
The engineering rule is simple:
Keep a custom kernel only when it passes correctness checks and improves the complete workload. A faster microbenchmark alone is not a model speedup.
No external optimized kernel repository was copied into this project. PyTorch, Triton, Transformers, cuBLAS/CUTLASS dispatch, and PyTorch SDPA/FlashAttention are dependencies and reference implementations.
The repository is now an installable package with a unified model registry and commands for setup, serving, benchmarking, and result validation:
python -m pip install -e ".[cpu,llama]"
kernellab models
kernellab doctor
kernellab download llama-3.2-3b
kernellab serve llama-3.2-3b
kernellab benchmark llama-3.2-3b --engine pytorch --output results/llama-pytorch.json
kernellab validate-result results/llama-pytorch.json
See the architecture, Llama deployment guide, and reproducibility matrix. GPU-dependent claims remain pending until their schema-v2 artifacts exist.
The learning kernels, model integrations, static decode runtime, CUDA Graph experiment, profiling tools, paged-KV allocator, continuous scheduler, sampling, metrics, and OpenAI-compatible API are implemented.
Two outcomes are intentionally kept separate:
The serving control plane now has a correctness-first GPU Qwen executor. It preserves tiled SDPA prefill, writes K/V into fixed physical pages, batches variable-length decode requests, and passed an exact token/logit gate. A faster direct Triton paged-attention candidate remains opt-in because it misses the 99% decode-argmax release threshold.
This repository now hosts multiple optimized model projects under
models/:
| Slot | Model | Optimization focus | Status |
|---|---|---|---|
| 1 | Qwen3-0.6B | Custom Triton/CUDA kernels, static decode, CUDA Graphs, serving control plane | Exact production path; faster experimental paths gated |
| 2 | Llama-3.2-3B | vLLM serving, matched benchmarks, profiling, kernel work | Phase 1 methodology implemented; fresh GPU validation pending |
| 3 | To be selected | Architecture-specific optimization | Planned |
| 4 | To be selected | Architecture-specific optimization | Planned |
The Llama source is pinned at upstream commit 80087ed and included directly,
so a normal clone receives it without submodule commands. See its
provenance record and the
portfolio roadmap.
RTX 3090 24 GiB, PyTorch 2.8.0+cu128, Triton 3.4.0, Transformers 5.16.1, BF16, batch 1, SDPA prefill, greedy one-token decode, context 2,048:
| Metric | PyTorch/dynamic baseline | Best implementation | Change |
|---|---|---|---|
| TTFT p50 | 66.007 ms | 65.332 ms | 1.02% lower |
| TTFT p99* | 70.577 ms | 68.160 ms | 3.43% lower |
| Decode ITL p50 | 22.073 ms | 2.253 ms | 89.79% lower |
| Decode ITL p99* | 22.851 ms | 2.258 ms | 90.12% lower |
| Serial throughput from p50 ITL | 45.30 token/s | 443.90 token/s | 9.80x |
| Decode kernels/token | 693 | 258 | 62.8% fewer |
* p99 is interpolated from five GPU-event repetitions and is preliminary, not
a statistically strong production tail-latency result.
The decode result is the experimental CUDA-Graph path. Its throughput is
1000 / ITL for a serial GPU-only loop and excludes tokenization, networking,
scheduling, EOS handling, and general sampling. It is not a production-server
throughput claim.
The real paged-serving integration was also validated on an RTX 2000 Ada (SM89): 256/256 prefill decisions and 1,024/1,024 decode decisions matched with zero logit difference. The direct paged-attention kernel was 1.74–5.37x faster than gather-plus-SDPA in isolation across contexts 128–4,096, but achieved only 97.65625% decode agreement and is therefore not the production default. See the paged-serving report.
Phase 3/4 was independently checked on an RTX A4000 (SM86). The accepted lossless paged-gather policy retained 100% of 256 prefill and 1,024 decode argmax decisions with zero logit difference, while reducing matched paged decode median latency by 12.84–15.46% across contexts 128–4,096. More aggressive projection fusion did not deliver a consistent 20% without either numerical or long-context regressions, so it remains experimental. See the Ampere validation report.
Phase 5/6 then hardened the production scheduler and portability boundary. Decode-first execution, stable metadata shapes, removal of host synchronizations, and adaptive prefill quanta raised matched concurrent throughput by 13.53% while reducing ITL p50 by 20.85% and ITL p99 by 25.80%. A 64-request real-model run completed without failures or leaked KV blocks at 65.52 generated token/s. See the portable serving report.
| Context | Dynamic ITL | Graph ITL | Dynamic token/s | Graph token/s |
|---|---|---|---|---|
| 128 | 21.906 ms | 2.318 ms | 45.65 | 431.46 |
| 512 | 21.958 ms | 2.321 ms | 45.54 | 430.92 |
| 2,048 | 22.073 ms | 2.253 ms | 45.30 | 443.90 |
| 4,096 | 22.021 ms | 2.599 ms | 45.41 | 384.70 |
See the final GPU report for methodology, Nsight evidence, numerical results, and limitations.
Every chapter under learning/ contains code, tests, benchmarks,
and explanations of mapping, coalescing, precision, reductions, register
pressure, fusion, and trade-offs.
(q_proj, M=2048, K=1024, N=2048) in the accepted whitelist.This is why accepted TTFT improvement is modest: results reflect integrated performance, not a sum of unrelated microbenchmark gains.
The experimental decode pipeline is:
fixed token/position buffers
-> phase-specific projections
-> Q/K RMSNorm + RoPE + direct K/V cache and mask write
-> GQA-aware split-KV online-softmax attention
-> attention residual + post-attention RMSNorm
-> W8A16 dual gate/up GEMV + SwiGLU
-> MLP residual + next-layer RMSNorm across 27 boundaries
-> LM-head GEMV + argmax + token/position update
-> fixed-address CUDA Graph replay
This removed dynamic cache concatenation, old-cache copying, GQA expansion, and many intermediate tensors. Attention residual/RMSNorm is fused per layer; MLP residual/next-layer RMSNorm is fused across 27 layer boundaries. Kernel launches fell from the early 1,624/token baseline to 258/token.
Quantization is not enabled universally. Some A/B paths retain dense weights, so whole-model memory savings are not claimed unless the dense copy is removed.
serving/ provides:
The 5,000-request control-plane stress test recorded 4,864 completions and 136 intentional cancellations, with zero failures, timeouts, rejections, or leaked KV blocks. All 60,000 blocks were reclaimed. Its 197.81 requests/s rate measures the control plane with a fake executor—not GPU model throughput. Full details are in the serving report. Separately, a real four-request GPU smoke matched all 16 greedy Qwen baseline tokens and reclaimed every physical KV block; larger production traffic measurements are still required.
RMS/fusion-only quality reached 98.05% argmax agreement and minimum per-layer
cosine 0.999726. The complete experimental path reached 96.48% agreement, so it
remains opt-in. Nsight Compute counters were blocked by RunPod's
ERR_NVGPUCTRPERM; occupancy, DRAM bandwidth, warp stalls, and counter-derived
rooflines are therefore not invented.
baseline/ reference implementations
learning/ kernels, tests, benchmarks, and teaching notes
models/ self-contained optimized LLM projects and four-model roadmap
profiling/ workloads, ablations, quality gates, and Nsight helpers
serving/ paged KV, scheduler, sampling, metrics, engine, and API
scripts/ RunPod setup, model download, activation, and smoke tests
docs/ roadmap and optimization plans
results/ raw measurements, traces, logs, and acceptance reports
Recommended reading:
git clone https://github.com/yashlabs-trying/gpu-kernel-lab.git
cd gpu-kernel-lab
bash scripts/setup_runpod.sh
source .venv/bin/activate
python scripts/check_environment.py
python scripts/download_model.py
python scripts/smoke_test_model.py
pytest -q
Key profiling commands:
python profiling/model_workload.py --output results/baseline.json
python profiling/rmsnorm_acceptance.py --help
python profiling/static_kv_benchmark.py --context 2048 --hybrid --split-attention
python profiling/decode_graph_check.py
python profiling/serving_stress.py --requests 5000 --output results/stress.json
Use --help and the commands in individual reports for exact options. Keep model
weights and large profiler artifacts on the persistent volume.
Completed for the single-GPU serving core:
Direct paged attention is intentionally deferred because it failed quality, and Phase 7 was excluded from the requested scope. Multi-GPU topology is detected, but distributed tensor-parallel execution is not claimed as implemented.