Picture this: your H100s are sitting at 65% utilization under peak load. The model is fine. The network is fine. You add more threads — utilization barely moves. You dig into the profiler and find the culprit: a Jinja2 template loop. Python code so unassuming that nobody had thought to benchmark it.
This is not a hypothetical. It is a failure mode I set out to reproduce and quantify. Through four controlled benchmarks, I profile the preprocessing pipeline that every LLM inference server runs — templating, tokenization, and collation — and trace exactly how Python’s GIL converts what looks like a 7% overhead in single-request profiling into a hard throughput ceiling at production concurrency. The numbers are worth understanding before you hit them in production.
All benchmarks were run on a 12-core Apple M3 Pro. The full benchmarking suite is on GitHub.
The Three-Stage Pipeline
Before a single float reaches the GPU, raw text must pass through three CPU-bound transformations. Each stage has a different runtime, a different scaling characteristic, and — crucially — a different relationship with Python’s GIL.
Templating → Tokenization → Collation: three stages, three runtimes, three bottleneck profiles
Stage 1: Templating (Python — GIL-bound)
The server receives a list of JSON messages and compiles them into a single formatted string using a model-specific chat template (e.g., ChatML). The dominant implementation is Jinja2 — pure Python, fully GIL-bound.
Input:
| |
Output:
| |
Stage 2: Tokenization (Rust — GIL-free)
The templated string is encoded into integer token IDs. The standard implementations — HuggingFace Tokenizers and tiktoken — are Rust-backed and release the GIL during execution, enabling true multi-threaded parallelism.
Input: The formatted string above
Output:
| |
Stage 3: Collation (Python/C++ — partially GIL-bound)
Token ID lists are converted into padded PyTorch tensors and pinned for GPU transfer. This stage involves both Python orchestration and C++ tensor operations.
Input:
| |
Output:
| |
Baseline: A Single Request Looks Fast
How expensive is this pipeline for a single request? To establish a baseline, I profiled one Llama-3.1-8B-Instruct request at ~1,000 tokens (experiment code).
| Stage | Latency | Share |
|---|---|---|
| Tokenization (Rust) | 0.82 ms | 68% |
| Collation (PyTorch) | 0.30 ms | 25% |
| Templating (Jinja) | 0.08 ms | 7% |
| Total | ~1.2 ms | 100% |
At 1.2 ms total, it’s easy to dismiss preprocessing as negligible — and most teams do. The Rust-based tokenizer handles the bulk of the work efficiently, and Jinja’s 0.08 ms contribution barely registers.
This is precisely the reasoning that breaks down at scale.
Structure Matters More Than Content
Real-world LLM usage is rarely single-turn. RAG pipelines, multi-turn agent loops, and tool-calling workflows generate requests with dozens or hundreds of message turns.
The question: does the structure of a request (number of messages) affect preprocessing cost differently than the content (number of tokens)? To test this, I held the total token count constant at ~100k tokens and varied the number of conversation turns (experiment code).
| Turns | Jinja Latency | Tokenization Latency | Jinja Scaling |
|---|---|---|---|
| 1 turn | 0.07 ms | 56.5 ms | 1× |
| 100 turns | 0.37 ms | 58.7 ms | ~5× Slower |
Jinja templating latency scales with message count (structure), not token count (content)
Tokenization cost scales with content — the number of tokens to encode. Templating cost scales with structure — the number of messages to iterate over in the Jinja template loop. As applications grow more complex (more turns, more RAG chunks, more tool calls), the Python-based templating layer compounds linearly while the Rust tokenizer stays flat.
This asymmetry is invisible in single-request benchmarks.
At Production Concurrency, the GPU Starves
The real bottleneck emerges when the inference server processes batches. Production systems batch 64, 128, or 256 requests per forward pass to saturate GPU memory bandwidth. During batch preparation, the GPU waits.
I simulated a high-throughput server processing 100,000 requests in batches of 64, and measured the per-batch GPU wait time — the wall-clock time the GPU sits idle while the CPU assembles each batch — across different thread counts (experiment code).
| Threads | GPU Wait Time (per batch) | Tokenization | Jinja |
|---|---|---|---|
| 1 thread | 456 ms | 409 ms | 18.6 ms |
| 4 threads | 298 ms | 248 ms | 19.1 ms |
| 16 threads | 178 ms | 126 ms | 20.8 ms |
| 64 threads | 134 ms | 83 ms | 22.3 ms |

At 64 threads, Jinja templating accounts for 16.6% of total GPU wait time — and is still growing
The Rust-based tokenization scales as expected — ~5× speedup (409 ms → 83 ms) by distributing work across cores.
Jinja templating, however, gets slower (18 ms → 22 ms).
At 64 threads, the “trivial” 7% from our baseline now accounts for 16.6% of total GPU wait time. In a system processing thousands of requests per second, this 22 ms per batch becomes a hard ceiling that no amount of GPU optimization can overcome.
The Root Cause: Python’s GIL
Why does adding threads make Jinja slower? To isolate the mechanism, I measured the speedup factor of each component against ideal linear scaling as thread counts increased (experiment code).
| Component | Speedup at 32 Threads | Mechanism |
|---|---|---|
| Tokenization (Rust) | 5.72× | Releases the GIL — true parallelism |
| Templating (Jinja) | 0.84× | GIL-bound — actually gets slower |
Tokenization scales near-linearly with threads. Templating degrades — threads contend for the same GIL lock.
Jinja2 is a pure Python library. Under CPython’s GIL, only one thread can execute Python bytecode at a time. When 64 threads attempt concurrent Jinja rendering, they don’t run in parallel — they serialize on the GIL and pay additional context-switching overhead. More threads, less throughput.
Implications
Single-request profiling is a trap. The numbers look fine — 1.2 ms total, 0.08 ms for templating — until you model what happens at batch concurrency. Three things become clear:
CPU preprocessing is not free. When GPU utilization plateaus at 60–70%, the instinct is to look at model architecture, batch sizes, or memory layout. But the GPU may simply be starving for tensors — blocked on CPU-side preprocessing that hasn’t been profiled.
The community solved half the problem. Rewriting tokenizers in Rust was a massive win — it moved the heaviest preprocessing stage out of the GIL. But the shift to chat-based models introduced Jinja templating as a new serial bottleneck, and that work hasn’t received the same attention.
Concurrency patterns matter as much as algorithms. The difference between a GIL-releasing Rust function and a GIL-bound Python function is invisible at low concurrency and catastrophic at high concurrency. Serving infrastructure must use patterns (multiprocessing, C++ backends, or dedicated preprocessing workers) that can actually saturate available cores.
What’s Next
Identifying the bottleneck is step one. In the next post, we push vLLM and SGLang to saturation to see what happens when this preprocessing bottleneck meets real GPU serving systems:
- The Utilization Illusion — Why
nvidia-smishows 95% utilization while throughput stays flat - The Radix Paradox — Why SGLang’s KV cache optimization cannot reach the GPU at high concurrency
- Decoupled Preprocessing — What happens when you move Jinja and tokenization out of the inference server entirely
Code: The full benchmarking suite — all four experiments, data collection, and plotting scripts — is available on GitHub.