VTE (Vector Tensor Engine): Building an LLM Inference Engine from Scratch for AMD GPUs on Windows

What do you do when you want to run an LLM on a consumer AMD card, but you don’t want to rely on heavy frameworks? You build the entire stack yourself.

VTE (Vector Tensor Engine) is an inference engine built directly on top of the AMD HIP runtime. There is no llama.cpp, no ONNX Runtime, no vLLM, and no PyTorch underneath it. VTE parses the GGUF file, generates the HIP C++ kernels on the fly, compiles them with hipcc at runtime, and drives amdhip64.dll through a hand-written Python ctypes bridge.

The entire project was developed, tuned, and verified on a single consumer desktop card: an AMD Radeon RX 7600 (RDNA3, gfx1102, 8GB VRAM), running Qwen2.5-1.5B-Instruct in Q4_K_M quantization.

Why build from scratch?

The goal wasn’t to compete with enterprise data-center runners, but to gain complete, granular control over every single byte moving between VRAM and the ALUs, and study about what AMD can reach. On a consumer card with tighter memory bandwidth and limited Compute Units (CUs), enterprise defaults don’t always make sense.

Single-Sequence Decode (Batch = 1)

Stage Throughput What Changed
Naive per-thread GEMV ~18.8 tok/s Baseline: One thread per output element, no coalescing.
Coalesced GEMV ~31.5 tok/s Threads in a block cooperate on one output neuron; coalesced weight reads.
In-kernel Dequantization ~37.9 tok/s FFN weights dequantized inline during GEMV (No-Sync Direct Unpack) instead of a separate pass.
QKV Two-Pass Split-K Fusion ~41.0 tok/s Fills all 32 CUs instead of leaving most idle.

Batched Decode Throughput

Using lockstep generation for same-length prompts, VTE scales up smoothly until hitting physical hardware limits:

  • Batch 1: 110.7 tok/s (Prefill/Base)

  • Batch 2: 165.3 tok/s (75% efficiency vs ideal linear scaling)

  • Batch 4: 204.2 tok/s (Peak performance)

  • Batch 8: 193.5 tok/s (Regression due to 32MB Infinity Cache thrashing)

The Infinity Cache Bottleneck: At batch=8, performance drops because the FFN weights (~27.5MB per layer) no longer fit comfortably inside the RX 7600’s 32MB Infinity Cache alongside the rest of the operational data.

How It Fits Together

The codebase is split cleanly into three main layers:

  • vte/bridge: The driver interface. Houses a custom ctypes wrapper for amdhip64.dll, a slab allocator to bypass repeated hipMalloc overhead, a Kernel Watchdog to prevent Windows TDR hangs, and a userspace duty-cycle limiter to keep your desktop responsive by capping sustained GPU utilization at 95%.

  • vte/compiler: The translation layer. Features GGUF parsers, byte-exact Q4_K/Q6_K dequantizers, an IR graph builder that analyzes RDNA3 VGPR/LDS limits for safe kernel fusion, and a from-scratch byte-level BPE tokenizer mirroring the tiktoken algorithm without external dependencies.

  • vte/core: The orchestrator. Captures the entire per-token decode path into a HIP Graph once and replays it, completely wiping out Python-side dispatch overhead.

Hunting Silent Data Corruptions

When building an engine from scratch, the hardest bugs don’t crash the code—they give you highly fluent, beautifully formatted garbage. A few notable bugs caught by layer-by-layer NumPy diffing included:

  1. The SwiGLU Black Hole: The SwiGLU kernel expected total_elements (batch * seq_len * intermediate_size), but the argument builder passed them as three separate values. The kernel read total_elements = 1, gracefully processed exactly one out of 8,960 elements, and discarded the rest.

  2. The Hidden 16-bit Swapping: The matmul kernel outputted FP16, but the host buffer read it back as FP32. Adjacent pairs of FP16 values were reinterpreted as single FP32 floats, creating massive logit magnitudes (~1e7 instead of ±40) from a perfectly healthy hidden state.

  3. The Overlapping KV Cache: An early layout mistake caused every layer’s V-cache to overwrite the previous layer’s K-cache space. The model ran perfectly smoothly, but completely lost its mind across layer transitions.

(Github Repository)

Hello, thanks a lot for sharing your project, this is seriously cool, writing GGUF parsing, HIP kernel gen, and a BPE tokenizer from scratch is a big deal, and the debugging war stories (the FP16/FP32 reinterpret, SwiGLU reading 1 of 8,960 elements) are gold for anyone learning GPU internals. The Infinity Cache thrash analysis at batch=8 is a sharp catch. One question: you nailed the CU/cache story on a 1.5B, what breaks first when you scale up? A 7B won’t fit 8GB, so does the architecture hold once you’re bandwidth-bound and streaming weights, or does the from-scratch approach hit a wall? That’s the real test of whether this is a learning gem or a viable path.

Thanks!

Honest answer: VRAM isn’t actually the first wall — a 7B Q4_K_M would roughly fit in 8GB. What breaks first is that attention weights (Q/K/V) stay in FP16 today instead of being quantized like the FFN, cheap at 1.5B’s hidden size, but that cost scales with hidden_size² and would likely blow the budget at 7B. Known gap, not a discovered wall.

After that, bandwidth-bound decode is bandwidth-bound decode, bigger weights just mean proportionally lower tok/s, same physics as llama.cpp on this card. That part holds fine.

What I’d actually expect to break is the batching win (41→204 tok/s): it works because a 1.5B’s per-layer FFN weights (~27.5MB) fit alongside this GPU’s 32MB Infinity Cache, so concurrent sequences reuse hot weights. A 7B’s per-layer weights blow way past that, so I’d expect that specific multiplier to shrink or vanish.

1 Like