Work / Research
Inference Foundry
A narrow transformer serving runtime: a KV-cached GPT-2 decoder, batched prefill and decode, static and continuous scheduling, and a loopback streaming service with cancellation and backpressure, validated against a reference and benchmarked on one laptop GPU.
- Research prototype
- Recorded run

Case study

Problem
A language model generates text one token at a time, and each token needs a full pass through the network. Serving many users at once is therefore a scheduling problem as much as a numerical one. When does a new request start? How do sequences of different lengths share one GPU call? What happens when a client cancels, disconnects or reads too slowly? Inference Foundry is a narrow serving runtime built to answer those questions with code and measurements, for one architecture (GPT-2) on one laptop GPU.
Approach
- Own the engine, borrow the kernels. PyTorch supplies matrix multiply, softmax and layer norm. The attention, key/value cache, token-step interface, batching, scheduler, lifecycle, telemetry and benchmarks are this project's code. No library
generate()runs the serving loop. - Correctness first, on a seeded tiny model. A 2-layer random-weight decoder proves the cached path computes exactly what full recomputation computes, before anything is batched or timed.
- A written contract. Request states, a cache layout, limits, and a trace format with a checker that every recorded run must pass: no slot held twice, every slot released, no illegal transition.
- Only then a real model. GPT-2 small, pinned and hash-verified, validated against an independent implementation, then benchmarked.
Implementation
- Decoder: GPT-2 layout as plain tensor operations. One cached operation,
extend_batch, appends any number of tokens to any set of cache slots, each from its own position. It serves batched prefill (prompts right-padded) and batched decode (one token per slot). Stale cache rows are both masked and zeroed, because a zero attention weight times a stale NaN is still NaN. - Cache: one contiguous region per slot,
[layers, slots, heads, 1024, 64]. This is not a paged cache; every slot reserves the full context. - Scheduler: FIFO admission with token reservations and a bounded queue, two policies (static and continuous), cancellation at step boundaries, and release of slot and reservation on every path.
- Service: a loopback-only HTTP API. One worker thread owns the model; connection threads only validate, enqueue and stream NDJSON events. It handles client cancel, disconnect, a timeout enforced in the worker, slow consumers (bounded buffers, never blocking the worker), overload (429) and faults. Nothing is retried behind a client's back.
- Checkpoint: GPT-2 small (
openai-community/gpt2at607a30d783df), read from safetensors by a short reader; no pickle is ever loaded. Every file's size and published hash is checked on every load.
Validation
- Cached vs full, tiny model: worst difference 7.6e-06 on CPU and 7.6e-06 on CUDA, within the stated tolerance at every position up to the context limit, with stale cache rows poisoned with NaN (runs
20261109T190933Z-0b139b,20261109T190938Z-1a60a4). - Batching and scheduling (tiny model): 13 requests under both policies. Every request's tokens equal its isolated run, and continuous refilled a freed slot 6 times while another request was still running (
20261109T202311Z-8d2202). - Reliability (tiny model, real HTTP): concurrent burst, client cancel, disconnect, timeout, worker error. All passed, and the service ended with every slot free and nothing reserved (
20261110T082213Z-afbd12). - GPT-2 against
transformers(dev-only reference,20261110T085143Z-5448b4):- tokenization identical on 7/7 strings;
- logits within 4.8e-04 (CPU) and 6.4e-04 (CUDA), with 0 argmax disagreements;
- greedy output identical on 4/4 prompts.
- GPT-2 tolerance: the tiny model's tolerance fails on GPT-2 by about 25-28×, because its relative term gives almost nothing on near-zero logits. GPT-2 has its own bound, stated as set after measuring.
A sample, to show the weights are wired correctly:
Generated by GPT-2 small (OpenAI, 2019): greedy decoding, 32 tokens max, unedited. Prompt: "The capital of France is". Output: "the capital of the French Republic, and the capital of the French Republic is the capital of the French Republic.
The French Republic is the capital of the…"
Results: continuous batching, measured
The setup:
- Workload: 40 requests per run (34 short, 6 long). EOS was ignored, so every configuration generated exactly the same tokens.
- Arrivals: a Poisson schedule fixed before each run, sent by real HTTP clients.
- Hardware: RTX 3050 Ti Laptop GPU, float32, two repetitions.
- Token mismatches across configurations: 0 in 30 runs.
| Configuration | Throughput at 8 req/s (tok/s) | First token p50 at 4 req/s (s) | Inter-token p50 at 8 req/s (s) | Peak GPU memory at 8 req/s (MiB) |
|---|---|---|---|---|
| One at a time | 51 / 66 | 11.872 / 9.464 | 0.017 / 0.014 | 735 / 735 |
| Static batches of 8 | 127 / 120 | 3.181 / 1.408 | 0.023 / 0.022 | 1971 / 1971 |
| Continuous, 8 slots | 180 / 167 | 0.055 / 0.051 | 0.032 / 0.029 | 1341 / 1257 |
Each cell gives repetition 1 / repetition 2.
- Below saturation, every configuration delivers the offered load. What differs is waiting: continuous starts a new request on the next step, static waits for the current batch, and one-at-a-time waits for everyone ahead.
- At 8 req/s all three are saturated, and continuous delivers 2.5-3.5× the one-at-a-time throughput and 1.3-1.5× static (range over repetition pairings). Static leaves slots idle while its longest request finishes.
- The cost: each running stream gets its next token more slowly, because steps carry bigger batches and new arrivals' prefills run in between.
- Fairness: short requests suffer most from waiting behind long ones, and continuous narrows that gap the most. No request was starved.
- Cold vs warm: model load took 1.16 s (hash check, read, copy to the GPU), and measured runs are warm.
Limitations
See the list below the case study. In short:
- one laptop, one model, float32, one workload mix and two repetitions;
- an unoptimised Python engine, so the absolute tokens/s are not a statement about GPT-2 on this GPU;
- one large spread between repetitions that wasn't explained;
- a contiguous, unpaged cache.
Next work
- Chunked prefill: mix prompt chunks into decode steps to cut the inter-token spikes that new arrivals cause.
extend_batchalready accepts mixed chunk lengths. - A paged cache: allocate cache memory in blocks instead of a full 1024 positions per slot.
- More measurement: half precision, and repeated runs on a machine that isn't also driving a desktop.
Limitations
- One architecture (GPT-2 small, 124M parameters), float32, greedy decoding only.
- Benchmarks come from one laptop GPU (RTX 3050 Ti, 4 GiB) that also drives the desktop: one workload mix, two repetitions per point, client and server in one Python process.
- The engine is unoptimised Python over PyTorch kernels (per-slot cache writes, a K/V gather per layer), so absolute tokens/s say little about GPT-2 on this GPU. The configurations are compared fairly because they share those costs.
- Sequential throughput at saturation varied 51 vs 66 tokens/s between repetitions; the cause was not investigated.
- The KV cache is contiguous per slot (every slot reserves 1024 positions). It is not a paged cache, and there is no prefix sharing or chunked prefill.
- Benchmark prompts are synthetic token ids; cost depends on length, not content.
- M1-M3 evidence uses a seeded random-weight tiny model, and the M3 reliability run adds an artificial 20 ms delay per step.
- The service is loopback-only with no authentication; it was tested on Windows only.
- The demo is recorded: it replays traces and calls no model or GPU.
Evidence
| Run | Recorded | Backend | Kind | Code revision |
|---|---|---|---|---|
| 20261110T085257Z-ac6624 | 2026-11-10 08:52 UTC | PyTorch 2.15.0+cu132 (CUDA 13.2) on NVIDIA GeForce RTX 3050 Ti Laptop GPU, driver 610.88; AMD Ryzen 7 5800H (8 cores, 16 threads); Windows-11-10.0.26200-SP0 | Measured | 6e8d346+uncommitted |
| 20261110T083743Z-ab176b | 2026-11-10 08:37 UTC | PyTorch 2.15.0+cu132 (CUDA 13.2) on NVIDIA GeForce RTX 3050 Ti Laptop GPU, driver 610.88; AMD Ryzen 7 5800H (8 cores, 16 threads); Windows-11-10.0.26200-SP0 | Measured | 6e8d346+uncommitted |
| 20261110T085143Z-5448b4 | 2026-11-10 08:51 UTC | PyTorch 2.15.0+cu132 (CUDA 13.2) on NVIDIA GeForce RTX 3050 Ti Laptop GPU, driver 610.88; AMD Ryzen 7 5800H (8 cores, 16 threads); Windows-11-10.0.26200-SP0 | Measured | 6e8d346+uncommitted |
| 20261110T082213Z-afbd12 | 2026-11-10 08:22 UTC | PyTorch 2.15.0+cu132 on CPU; AMD Ryzen 7 5800H (8 cores, 16 threads); Windows-11-10.0.26200-SP0 | Measured | 0c85df9+uncommitted |
| 20261109T202311Z-8d2202 | 2026-11-09 20:23 UTC | PyTorch 2.15.0+cu132 on CPU; AMD Ryzen 7 5800H (8 cores, 16 threads); Windows-11-10.0.26200-SP0 | Measured | uncommitted (no prior commit) |
| 20261109T190933Z-0b139b | 2026-11-09 19:09 UTC | PyTorch 2.15.0+cu132 on CPU; AMD Ryzen 7 5800H (8 cores, 16 threads); Windows-11-10.0.26200-SP0 | Measured | uncommitted (no prior commit) |
- 20261110T085257Z-ac6624: M4 benchmark, saturation: 4 and 8 req/s, sequential vs static vs continuous on GPT-2 small, 2 repetitions, 0 token mismatches. Work committed as e858f7d.
- 20261110T083743Z-ab176b: M4 benchmark: 0.5, 1 and 2 req/s, sequential vs static vs continuous on GPT-2 small, 2 repetitions, 0 token mismatches. Work committed as e858f7d.
- 20261110T085143Z-5448b4: M4 validation of GPT-2 small against transformers on CPU and CUDA: tokenization, logits, cached vs full, greedy tokens. PASS. Work committed as e858f7d.
- 20261110T082213Z-afbd12: M3 reliability over loopback HTTP on the seeded random-weight tiny model (CPU), with an artificial 20 ms step delay. PASS. Work committed as 6e8d346.
- 20261109T202311Z-8d2202: M2 scheduling scenario on the seeded random-weight tiny model (CPU): step counts, no timings. PASS. Work committed as 0c85df9.
- 20261109T190933Z-0b139b: M1 cached vs full logits on the seeded random-weight tiny model (CPU). PASS. Work committed as 0c85df9.