{"slug": "serving-gemma-3-4b-vllm-vs-keras-benchmarked", "title": "Serving Gemma 3 4B: vLLM vs Keras, Benchmarked", "summary": "A Google Developer Expert benchmarked Google's Gemma 3 4B model on two serving stacks and found that at 64 concurrent requests, vLLM serves 26.5x more tokens than Keras Hub's generate(), with the two tying at a single request. The gap stems from continuous batching and paged KV caching, which begin paying off at just 3 concurrent requests. The analysis also shows Gemma 3's interleaved local/global attention cuts KV cache to 2.61 GiB at 128K context versus 17.0 GiB for a full-attention equivalent, letting the model fit long contexts on a single 24 GB card.", "body_md": "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**.\n\nThis 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.\n\nThink 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.\n\nThe 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.\n\nGemma 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**.\n\nThe 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.\n\nThe arithmetic is exact, from the published config (34 layers, 8 query heads, 4 KV heads, head dim 256, bf16):\n\n| Context | Full-attention KV | Gemma 3 KV | Saving | \n|---|---|---|---|\n| 1K | 0.13 GiB | 0.13 GiB | 1.0x | \n| 8K | 1.06 GiB | 0.27 GiB | 3.9x | \n| 32K | 4.25 GiB | 0.74 GiB | 5.8x | \n| 128K | 17.00 GiB | 2.61 GiB | 6.5x | \n\nAt 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.\n\nWhat fits on one 24 GB card, after weights:\n\n| Context per sequence | Max concurrent sequences (Gemma 3) | Full-attention equivalent | \n|---|---|---|\n| 2K | 104 | 60 | \n| 8K | 59 | 15 | \n| 32K | 21 | 3 | \n| 128K | 6 | 0 — doesn't fit | \n\n**Keras** serves Gemma through `keras_hub`:\n\n``` python\nimport os\nos.environ[\"KERAS_BACKEND\"] = \"jax\"   # or \"torch\" / \"tensorflow\"\nimport keras_hub\n\ngemma = keras_hub.models.Gemma3CausalLM.from_preset(\"gemma3_4b_en\")\nprint(gemma.generate(\"Explain attention in one sentence.\", max_length=64))\n```\n\nThat 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.\n\n**vLLM** serves the same weights as a production server:\n\n```\nvllm serve google/gemma-3-4b-it --max-model-len 131072\ncurl http://localhost:8000/v1/chat/completions -d '{\n  \"model\": \"google/gemma-3-4b-it\",\n  \"messages\": [{\"role\": \"user\", \"content\": \"Explain attention in one sentence.\"}]}'\n```\n\nOne 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).\n\nHere 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):\n\n`generate()` lives in this regime too.\nThe 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.\n\nFitting 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:\n\n| Concurrent requests | vLLM (tok/s) | Keras (tok/s) | Ratio | \n|---|---|---|---|\n| 1 | 140 | 140 | 1.0x | \n| 2 | 274 | 140 | 2.0x (1.96x — just under the 2x line) | \n| 3 | 402 | 140 | 2.9x | \n| 8 | 968 | 140 | 6.9x | \n| 16 | 1,677 | 140 | 12.0x | \n| 32 | 2,644 | 140 | 18.9x | \n| 64 | 3,716 | 140 | 26.5x | \n\nThe 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.\n\n|  | Keras `generate()` | vLLM 0.33 | \n|---|---|---|\n| Single-request decode | ~140 tok/s | ~140 tok/s — a tie | \n| Max concurrent 8K sequences, one 24 GB card | 1 (serialized) | 59 (KV-bound) | \n| Output tokens/day, sustained | 12.1M | 321M | \n| HTTP API | Write your own | OpenAI-compatible, built in | \n| Continuous batching | No | Yes | \n| Prefix caching | No | Yes | \n| Structured output | Manual | Guided decoding, built in | \n| TPU serving | Yes, via JAX backend | Yes, but kernels trail GPU | \n| Ops burden | A script | A server to run and monitor | \n| Best for | Local/dev, single-user, fine-tune-then-serve | Multi-user APIs, 3+ concurrent | \n\n**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.\n\n**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.\n\n**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.\n\n*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.*\n\n**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).", "url": "https://wpnews.pro/news/serving-gemma-3-4b-vllm-vs-keras-benchmarked", "canonical_source": "https://dev.to/danielsamfdo/serving-gemma-3-4b-vllm-vs-keras-benchmarked-15mm", "published_at": "2026-12-05 17:09:47+00:00", "updated_at": "2026-12-05 17:13:26.270652+00:00", "lang": "en", "topics": ["large-language-models", "ai-infrastructure", "mlops", "ai-tools", "generative-ai"], "entities": ["Google", "Gemma 3", "vLLM", "Keras", "Keras Hub", "JAX"], "also_reported_by": [], "takeaway": "At 64 concurrent requests vLLM serves 26.5x more tokens than Keras Hub's generate() on identical Gemma 3 4B weights, with the crossover starting at 3 concurrent requests.", "recommendation": {"advice": "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.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/serving-gemma-3-4b-vllm-vs-keras-benchmarked", "markdown": "https://wpnews.pro/news/serving-gemma-3-4b-vllm-vs-keras-benchmarked.md", "text": "https://wpnews.pro/news/serving-gemma-3-4b-vllm-vs-keras-benchmarked.txt", "jsonld": "https://wpnews.pro/news/serving-gemma-3-4b-vllm-vs-keras-benchmarked.jsonld"}}