The rapid proliferation of Large Language Models (LLMs) has sparked a parallel revolution in the infrastructure required to run them. While the majority of the industry relies on established, high-level frameworks such as llama.cpp or vLLM, a growing contingent of systems engineers is advocating for a return to "bare-metal" development. This movement seeks to move beyond the "black box" nature of production runtimes to gain granular control over every barrier, kernel launch, and memory transaction. A prominent example of this technical pursuit is the recently released "annotated-llm-runtime," a streamlined C++/CUDA project designed specifically for the NVIDIA H100 (Hopper) architecture, targeting the Qwen2.5-Coder-7B model.
Building a custom inference engine is no longer merely an academic exercise; it is a strategic necessity for organizations looking to implement novel quantization formats, custom attention variants, or specialized samplers that are not yet supported by mainstream tools. The journey of developing this runtime provides a rare, transparent look into the engineering hurdles and performance breakthroughs associated with NVIDIA’s latest sm_90 architecture.
The Architecture of the Annotated Runtime
The annotated-llm-runtime represents a distilled version of a production-grade decoder. Comprising approximately two dozen source files, the project prioritizes transparency and education over raw, competitive throughput. However, its design is deeply rooted in the specific hardware capabilities of the NVIDIA H100.
The system is architected around the Qwen2.5-Coder-7B-Instruct model, a high-performance 7-billion parameter model from Alibaba’s Qwen series. The runtime’s internal dimensions are hardcoded using C++ constexpr to eliminate the overhead of parsing configuration files at runtime. These dimensions include 28 decoder layers, a hidden size of 3,584, and an expansive vocabulary of 152,064 tokens.
A critical design choice in this stack is the implementation of Grouped-Query Attention (GQA). In this specific configuration, 28 query heads share 4 Key-Value (KV) heads, resulting in a 7:1 ratio. This optimization significantly reduces the memory footprint of the KV cache, requiring only 1 KB of storage per token. To align with the H100’s 128-byte L2 cache lines, the system utilizes a paged KV cache mechanism, where tokens are stored in 16-token pages. Each physical page maps to 16 KiB, ensuring optimal memory alignment and hardware utilization.
Performance Benchmarks: A Yardstick for Optimization
In the world of LLM inference, performance is typically measured across three primary axes: Time-To-First-Token (TTFT), Inter-Token Latency (ITL), and overall throughput. The developer of the annotated-llm-runtime provided a transparent comparison against llama.cpp (pinned at commit b3040), which serves as the industry’s gold standard for optimized CPU/GPU inference.
| Metric | Annotated Runtime (H100) | llama.cpp (Q4_K_M) |
|---|---|---|
| TTFT (512 token prompt) | 128 ms | 43 ms |
| Decode ITL (Steady-state) | 16.7 ms/token | 4.95 ms/token |
| Decode Throughput | 60 tokens/s | 200 tokens/s |
While the custom runtime currently lags behind the highly optimized llama.cpp in raw speed, the developer emphasizes that the goal is to understand the "why" behind the performance gap. The discrepancy highlights the massive amount of engineering hours and collective community tuning that goes into production-grade software. However, the custom runtime achieves a respectable 60 tokens per second, which is more than sufficient for real-time human reading and many automated coding tasks.

The CUDA Graph Revolution: Eliminating Driver Overhead
Perhaps the most significant performance revelation in the development of this runtime was the transition from eager kernel execution to CUDA graph-based execution. In a standard eager execution model, every generated token requires approximately 280 individual kernel launches (10 per layer across 28 layers). Each launch involves a round-trip to the CPU driver, creating a massive bottleneck of host-side overhead.
Initially, the runtime’s eager decode performance was measured at 119 ms per token. By capturing the entire steady-state decode sequence into a static CUDA graph, the developer was able to replay the instruction set with a single command to the GPU. This optimization alone dropped the latency from 119 ms to 17 ms per token—a 7x improvement without changing a single line of mathematical logic in the kernels.
The implementation of CUDA graphs in an LLM context is non-trivial because the decode process is not entirely static. Specifically, when a token crosses a memory page boundary, the memory access pattern changes. To solve this, the runtime pre-captures two distinct graphs during a "warmup" phase: one for standard steps and one for page-boundary steps. The system then uses a simple modulo-based logic to select the correct graph for each generation step, effectively bypassing the CPU-to-GPU communication tax.
Technical Hurdles: Lessons from the Kernel
The development of the annotated-llm-runtime was marked by several "war stories" that illustrate the complexities of high-performance GPU programming. These lessons provide a cautionary tale for those attempting to bypass higher-level abstractions.
1. The Perils of Warp Specialization
The runtime utilizes a "warp-specialized" attention kernel, a pattern popularized by NVIDIA’s CUTLASS library. In this model, a "producer" warp manages the Tensor Memory Accelerator (TMA) to move data from Global Memory (HBM) to Shared Memory (SMEM), while multiple "consumer" warps perform the actual computation.
A critical bug was discovered involving the misuse of the __syncthreads() primitive. Because __syncthreads() is a block-wide barrier, placing it inside a branch that only a single warp enters causes the GPU to hang or creates race conditions. In this case, consumer warps were reading from SMEM before the producer had finished the TMA transfer, leading to corrupted softmax calculations. The fix required a transition to mbarriers—Hopper-specific hardware objects that allow for transaction-aware synchronization.
2. The INT8 vs. FP16 Debate
A common assumption in LLM optimization is that reducing precision always leads to higher performance. The developer tested two paths for the model’s projection layers:
- Path A: Quantizing activations to INT8 to use the
__dp4ahardware instruction. - Path B: Keeping activations in FP16 and using the
prmt.b32(byte-permute) instruction to unpack INT4 weights.
Counter-intuitively, Path B outperformed Path A on the H100. Despite INT8 having lower bandwidth requirements, the overhead of the extra quantization kernel and the specific way Hopper handles byte-level permutations made the FP16 activation path more efficient. This discovery reinforces the idea that in modern GPU architecture, theoretical bandwidth wins often disappear when confronted with the realities of kernel launch overhead and SM (Streaming Multiprocessor) occupancy.

Data Preparation and the .nanoqwen Format
To ensure the runtime is as efficient as possible, the project uses a custom binary format called .nanoqwen. Unlike standard GGUF or Safetensors files, which often require complex parsing and metadata handling, a .nanoqwen file is designed to be memory-mapped directly into GPU VRAM.
The file begins with a strict 8-byte magic string ("NANOQWEN") to prevent the accidental loading of corrupt data. The weights are stored in a "ValueShuffle" layout, where INT4 nibbles are interleaved to align with the Hopper architecture’s byte-permute datapath. This specific layout is frozen during the offline packing stage, ensuring that the runtime never has to perform expensive reinterpretation of nibbles during inference.
Broader Impact and Industry Implications
The release of the annotated-llm-runtime comes at a pivotal time for the AI industry. As models like Qwen2.5-Coder-7B demonstrate that smaller, more efficient models can rival much larger predecessors in specific tasks like programming, the focus has shifted toward making these models run as cheaply and quickly as possible on commodity and enterprise hardware.
This project serves as a bridge between high-level AI research and low-level systems engineering. By providing a "readable" version of a high-performance runtime, it lowers the barrier to entry for engineers who wish to contribute to the next generation of inference technology.
Furthermore, the emphasis on the H100’s specific features—such as TMA and mbarriers—highlights the increasing specialization of AI hardware. It suggests that the future of LLM performance lies not in general-purpose code, but in software that is "architecturally aware," designed from the ground up to exploit the nuances of specific silicon.
Chronology of Development
The development of the annotated-llm-runtime followed a logical progression that mirrors the standard lifecycle of systems-level AI projects:
- Architecture Definition: Hardcoding the Qwen2.5-Coder-7B dimensions and defining the 7:1 GQA ratio.
- Quantization Strategy: Selecting symmetric group-wise INT4 for weights while maintaining FP16 for critical components like
rms_normandembed_tokensto ensure numerical stability. - Kernel Implementation: Developing the fused-GEMV and paged-attention kernels, utilizing Hopper’s TMA for memory movement.
- Synchronization Refinement: Debugging the race conditions in warp-specialized kernels and implementing
mbarriers. - Optimization Phase: Transitioning from eager execution to a dual-graph CUDA Graph system, resulting in the 7x latency reduction.
- Validation: Implementing a three-tier testing ladder (unit, layer, and graph tests) to ensure output parity with PyTorch reference models.
Final Analysis
The annotated-llm-runtime is more than a software repository; it is a technical manifesto for the importance of systems ownership. While it may not yet displace llama.cpp in production environments, it provides the blueprint for how developers can "own every barrier" and "capture every graph."
In an era where AI is often treated as a magical black box, projects like this remind the engineering community that at the bottom of every stack is a physical bus, a cache line, and a series of electrical signals. For those building the future of autonomous coding and real-time AI interaction, understanding those signals is the key to unlocking the true potential of the hardware. The project concludes with a simple but profound takeaway: in the world of high-performance computing, the difference between a mediocre system and a world-class one often comes down to how you handle the "gaps" between the math.



