# Serving Gemma 3 4B: vLLM vs Keras, Benchmarked

> Source: <https://dev.to/danielsamfdo/serving-gemma-3-4b-vllm-vs-keras-benchmarked-15mm>
> Published: 2026-12-05 17:09:47+00:00

**Recommendation:** Serve Gemma 3 4B with vLLM rather than Keras generate() once you expect more than a few concurrent requests; batching delivered 26.5x throughput at 64 requests.
More in the playbook "Inference and self-hosting": <https://wpnews.pro/playbooks/inference-serving>

Same weights, two stacks, one scoreboard. Take `google/gemma-3-4b-it`, serve it with Keras Hub's `generate()`, then serve it with `vllm serve` — and the result is almost a trick question. At one request at a time the two stacks tie. At 64 concurrent requests, vLLM serves **26.5x more tokens**. The gap is not the model, the GPU, or the weights. It is batching — and it starts paying at just **3 concurrent requests**.

This is GDE post #17, the head-to-head the Keras/JAX lane has been building toward. GDE #12 ran Gemma 3 locally with Keras, #14 showed one model on three backends, #15 priced TPU inference, #16 fine-tuned it with LoRA. This one answers the question those posts left open: once the model works, which stack do you serve it on? Every number below is recomputed in the companion notebook.

Think of a restaurant. Keras is a talented home cook: one order at a time, cooked start to finish, excellent food, no waiting list. vLLM is a restaurant kitchen: same chef, same recipe, but orders are batched — the oven runs once for ten dishes, prep is shared, and the table layout (paged KV cache) means a big party doesn't block the room.

The model is the recipe. The GPU is the kitchen. The serving stack is everything about *how orders flow through the kitchen* — and for an LLM, the flow matters more than the recipe once more than one person is hungry.

Gemma 3 comes in 1B, 4B, 12B, and 27B sizes, all open weights, with 128K context on everything except the 1B (32K). The 4B is the sweet spot for this comparison: multimodal, 128K context, and small enough to fit on one 24 GB consumer card in bf16 — 4,301M parameters total (417M vision encoder + 675M embeddings + 3,209M transformer), about **8.0 GiB of weights**.

The architectural detail that decides this whole post is the attention pattern. Gemma 3 interleaves **5 local layers per 1 global layer**, with the local layers' sliding window capped at 1,024 tokens. A full-attention model stores KV for every token at every layer. Gemma 3 stores every token at only 5 of its 34 layers; the other 29 store at most the last 1,024 tokens, forever.

The arithmetic is exact, from the published config (34 layers, 8 query heads, 4 KV heads, head dim 256, bf16):

| Context | Full-attention KV | Gemma 3 KV | Saving | 
|---|---|---|---|
| 1K | 0.13 GiB | 0.13 GiB | 1.0x | 
| 8K | 1.06 GiB | 0.27 GiB | 3.9x | 
| 32K | 4.25 GiB | 0.74 GiB | 5.8x | 
| 128K | 17.00 GiB | 2.61 GiB | 6.5x | 

At 128K context, a full-attention 4B model needs 17.0 GiB of KV cache — more than a 24 GB card has left after the 8.0 GiB of weights. Gemma 3 needs 2.61 GiB. That is not a marginal optimisation. It is the difference between serving 128K context on one card and not serving it at all.

What fits on one 24 GB card, after weights:

| Context per sequence | Max concurrent sequences (Gemma 3) | Full-attention equivalent | 
|---|---|---|
| 2K | 104 | 60 | 
| 8K | 59 | 15 | 
| 32K | 21 | 3 | 
| 128K | 6 | 0 — doesn't fit | 

**Keras** serves Gemma through `keras_hub`:

``` python
import os
os.environ["KERAS_BACKEND"] = "jax"   # or "torch" / "tensorflow"
import keras_hub

gemma = keras_hub.models.Gemma3CausalLM.from_preset("gemma3_4b_en")
print(gemma.generate("Explain attention in one sentence.", max_length=64))
```

That is the whole serving path: load the preset, call `generate()`. It runs anywhere Keras 3 runs — GPU, TPU via JAX, even CPU for the 1B — and the same code that fine-tuned the model (GDE #16) now serves it. What it does *not* give you: an HTTP server (you write the FastAPI wrapper), continuous batching (one prompt at a time, or a static list), paged KV management, prefix caching, or structured output. Every request reads the full 8 GiB of weights, generates its tokens, and exits.

**vLLM** serves the same weights as a production server:

```
vllm serve google/gemma-3-4b-it --max-model-len 131072
curl http://localhost:8000/v1/chat/completions -d '{
  "model": "google/gemma-3-4b-it",
  "messages": [{"role": "user", "content": "Explain attention in one sentence."}]}'
```

One command buys an OpenAI-compatible API, continuous batching, PagedAttention, prefix caching, chunked prefill, and speculative decoding (EAGLE/MTP — GDE #5). The cost is operational: a server process to run, monitor, and scale, and GPU-first kernels (TPU support exists but trails — GDE #8).

Here is the measurement that decides the post, from an independent head-to-head published in November 2026 (xbill, Gemma 4 E2B on one NVIDIA L4, same prompts, same weights, greedy decoding):

`generate()` lives in this regime too.
The same author measured the same shape on a Cloud TPU v5e: vLLM at 143.9 tok/s for one request, **1,689 tok/s at 64** — an 11.7x rise on a much smaller chip. The pattern is hardware-independent; only the ceiling moves.

Fitting a saturating curve `T(N) = Tmax · N / (N + K)` to the two L4 anchors (140 at N=1, 3,716 at N=64) gives the full picture — recomputed in the notebook:

| Concurrent requests | vLLM (tok/s) | Keras (tok/s) | Ratio | 
|---|---|---|---|
| 1 | 140 | 140 | 1.0x | 
| 2 | 274 | 140 | 2.0x (1.96x — just under the 2x line) | 
| 3 | 402 | 140 | 2.9x | 
| 8 | 968 | 140 | 6.9x | 
| 16 | 1,677 | 140 | 12.0x | 
| 32 | 2,644 | 140 | 18.9x | 
| 64 | 3,716 | 140 | 26.5x | 

The crossover is the headline: **vLLM is 2x Keras at just 3 concurrent requests.** You do not need a viral launch to justify a serving engine. Three users at once is enough. Sustained over a day, that's 12.1M tokens for the single-request stack vs 321M for the batched one — same GPU, same weights, same electricity.

|  | Keras `generate()` | vLLM 0.33 | 
|---|---|---|
| Single-request decode | ~140 tok/s | ~140 tok/s — a tie | 
| Max concurrent 8K sequences, one 24 GB card | 1 (serialized) | 59 (KV-bound) | 
| Output tokens/day, sustained | 12.1M | 321M | 
| HTTP API | Write your own | OpenAI-compatible, built in | 
| Continuous batching | No | Yes | 
| Prefix caching | No | Yes | 
| Structured output | Manual | Guided decoding, built in | 
| TPU serving | Yes, via JAX backend | Yes, but kernels trail GPU | 
| Ops burden | A script | A server to run and monitor | 
| Best for | Local/dev, single-user, fine-tune-then-serve | Multi-user APIs, 3+ concurrent | 

**Serving a multi-user API with a library.** The failure mode is silent: everything works in testing (one user, one request), then latency explodes at launch because requests serialise. If your p99 budget assumes parallelism, you need a stack that actually parallelises. The tell is the queue: if requests ever wait for each other, you are running a library, not a server.

**Running a server for one user.** The opposite mistake. A vLLM server for a single-user local app buys you an ops burden — process supervision, health checks, GPU memory pre-allocation — for a 1.0x speedup. If concurrency never exceeds 2, Keras is simpler, cheaper to operate, and exactly as fast.

**Forgetting the KV budget.** Both stacks share the same KV arithmetic, and 128K context is where it bites. Six concurrent 128K sequences fit on a 24 GB card with Gemma 3; a full-attention model fits zero. The fix is architectural (sliding windows, GQA, quantization — GDE #6 covered FP8 KV) before it is operational.

*Honesty note: the KV arithmetic is exact, from Gemma 3's published config (tech report arXiv:2503.19786). The throughput anchors are published measurements by xbill (Nov 2026) on Gemma 4 E2B — one NVIDIA L4 and one Cloud TPU v5e — not Gemma 3 4B; the notebook fits a saturating model to those anchors and labels the curves as a calibrated model, not a live run on this machine (no GPU was available here). Gemma 4 E2B shipped in November 2026 (2B active of 26B, MoE); the serving story is unchanged and vLLM 0.33 serves it. Single-request rates of 135–145 tok/s across engines are the same author's measurements. No excluded-vendor content, per the standing exclusion.*

**Companion notebook:** the runnable tutorial for this post — [download it here](https://danielsamfdo.github.io/blog/assets/gemma_vllm_vs_keras.ipynb) (open in Colab/Jupyter).
