Gemma 3 is the model Google designed to run on your hardware: 1B, 4B, 12B, and 27B sizes, a 128K context window, and — the part nobody quotes — a sliding-window attention design that makes the KV cache nearly flat as context grows. Keras 3 is the way in: one from_preset call loads any size, and the same code runs on JAX, TensorFlow, or PyTorch. This is GDE post #12, and it answers the only question that matters before you download 54 GB of weights: what fits on my machine, and how fast will it go?
The numbers below are measured arithmetic, not quoted: every figure comes from the companion notebook, computed from Gemma 3's published config and cross-checked against an independent memory figure (27B KV cache at 128K: 11.2 GB computed here vs 11.4 GB reported). The headline: 4B in bf16 needs 9.4 GB total at 32K context — it fits a 12 GB card — and 27B in 4-bit needs 16.7 GB, so it fits a 24 GB card. The 27B in full bf16 needs 57.5 GB and does not fit any single consumer GPU. That split is the whole post.
The ELI5 version
Running a model locally is three numbers added together: the weights (the model itself, fixed size), the KV cache (the model's working memory for the conversation, grows with context length), and a little overhead. Weights dominate. A 4B model in bf16 is 8.6 GB — 4.3 billion parameters times 2 bytes each — and that number does not change no matter how long you chat. The KV cache starts at zero and grows one entry per token per layer, which is why long conversations eventually run out of memory even when the weights fit comfortably.
Keras 3 is the loading dock. keras_hub.models.Gemma3CausalLM.from_preset("gemma3_instruct_1b") downloads the weights, builds the model, and hands you a generate() method — three lines from zero to a working chatbot. The preset names are the sizes: gemma3_1b, gemma3_4b_text, gemma3_instruct_4b_text, plus a 270M model for phones and a 300M embedding model. And Keras 3 has one more trick: keras.config.set_backend("jax") switches the compute engine under the same model code, so the notebook from yesterday's JAX post and today's Gemma post are the same stack.
Quantization is the lever that moves models down a tier. bf16 is 2 bytes per parameter; int8 is 1 byte; 4-bit is half a byte. A 27B model goes from 54.4 GB to 13.6 GB, which is the difference between "needs an H100" and "fits a 4090." Google publishes QAT (quantization-aware trained) builds of Gemma 3, trained so they lose less accuracy at 4-bit than a model quantized after the fact — if you run 4-bit, use the QAT build.
The weight ladder: size times precision

The ladder is linear — double the parameters, double the bytes — so the decision is which rung your hardware reaches. A 12 GB card holds 4B in bf16 with room to spare, or 12B in 4-bit. A 24 GB card holds 12B in bf16, or 27B in 4-bit. Nothing consumer-grade holds 27B in bf16: 54.4 GB of weights alone, before a single token of cache.
The 270M model deserves a mention because it is the one that runs anywhere: 0.54 GB in bf16, designed for task-specific fine-tuning on a phone or a Raspberry Pi. It is the same architecture, the same Keras preset pattern (gemma3_270m), and the right answer when the task is narrow — classification, extraction, a single skill — rather than open-ended chat.
The KV cache trick: 5 local layers, 1 global layer
Here is the number that changes the fit math. Gemma 3 uses interleaved sliding-window attention: five layers look back only 1,024 tokens, and every sixth layer attends to the full context. At 128K context, 52 of the 27B model's 62 layers stop growing after 1,024 tokens — only 10 layers pay the full-context price.

The arithmetic: each token costs 2 (K+V) × 16 KV heads × 128 head-dim × 2 bytes = 8,192 bytes per layer for 27B. Ten global layers at 128K is 10.7 GB; the 52 local layers cap out at 0.44 GB. Total 11.2 GB — and the independent figure is 11.4 GB, a 2% cross-check that the config math is right. Without the sliding window it would be 66.6 GB, a 6x penalty. For 4B the same math gives 2.8 GB at 128K, which is why the 4B model barely notices long conversations: weights 8.6 GB, cache 2.8 GB, total 11.4 GB at maximum context.
This is the design lesson worth carrying into the TPU post on Dec 2: the cache is a choice, not a law. Gemma 3 trades a little long-range recall in 5 of 6 layers for a cache that grows 6x slower. When you size a deployment, the cache curve matters as much as the weight size — and most models publish only the weights.
How fast: decode is memory-bound
Text generation is a memory problem, not a compute problem. Each generated token reads the entire weight matrix once, so tokens per second is roughly memory bandwidth divided by weight bytes. The notebook computes that roofline — an upper bound, not a promise — for four machines:

Two reads. First, the 4B model on an RTX 4090 roofs at ~117 tokens/s — faster than anyone reads, which is why 4B feels instant on a gaming GPU. Second, the 27B in 4-bit roofs at ~74 tokens/s on the same card: still faster than reading speed, which is the honest answer to "can my laptop run a 27B model" — yes, quantized, at usable speed, if it fits in VRAM. On a laptop CPU the same 4B roofs at ~5 tokens/s: usable for a demo, painful for a conversation. The roofline is an upper bound — real speed is lower once attention, kernels, and overhead are counted — so treat these as the ceiling and measure your own with the generate() timing code in the notebook.
What fits where

The fit table, weights plus KV cache at 32K context:
- 1B bf16, 2.3 GB total — fits anything, including a 4 GB card and a Raspberry Pi 5 (which runs it at 12–19 tokens/s in 4-bit, per independent measurements).
- 4B bf16, 9.4 GB total — fits a 12 GB card with headroom. This is the sweet spot for local development.
- 12B bf16, 26.9 GB total — needs a 32 GB card or a 40 GB datacenter GPU; in 4-bit (8.6 GB) it fits a 12 GB card.
- 27B bf16, 57.5 GB total — needs an 80 GB H100 or two 40 GB cards; in 4-bit (16.7 GB) it fits a 24 GB card.
The pattern to remember: quantization moves a model down one hardware tier, and the sliding-window cache means context length almost never changes the tier. Pick the size by quality, pick the precision by your GPU, and the cache takes care of itself.
The code: three lines to a chatbot
import keras
import keras_hub
keras.config.set_backend("jax") # or "tensorflow" / "torch"
lm = keras_hub.models.Gemma3CausalLM.from_preset("gemma3_instruct_1b")
print(lm.generate("Explain gravity in one sentence.", max_length=64))
Three things to know before you run it. First, Gemma weights need a one-time Kaggle authentication — from_preset downloads from Kaggle Models, ~2 GB for 1B, ~54 GB for 27B in bf16. Second, the backend line is optional but worth setting deliberately: JAX is the fastest path on TPU and the backend the rest of this lane assumes. Third, generate() takes max_length, temperature, and top_p — and the notebook's timing loop (time a generate() call, divide tokens by seconds) is how you replace the roofline estimate above with your real number.
When Keras loses
Two situations. First, throughput serving: Keras loads one model and generates one sequence at a time, which is perfect for local development and hopeless for a production endpoint — that is what vLLM (Lane A) is for. Second, the biggest models: Keras will happily try to load 27B bf16 on a 24 GB card and fail with an out-of-memory error, where a serving engine would have told you the fit math first. Keras is the lab; vLLM is the factory. Use each where it wins.
Takeaways
- 4B bf16 fits a 12 GB card (9.4 GB total at 32K); 27B 4-bit fits a 24 GB card (16.7 GB). 27B bf16 needs 57.5 GB — an 80 GB GPU or two cards.
- The KV cache is nearly flat. Gemma 3's 5:1 sliding window holds 27B cache to 11.2 GB at 128K, versus 66.6 GB for dense attention — a 6x saving, cross-checked against the independent 11.4 GB figure.
- Decode speed is bandwidth divided by weights. Roofline: 4B bf16 ~117 tokens/s on an RTX 4090, ~14 on an Apple M4, ~5 on a laptop CPU. Measure your own with the notebook's timing loop.
- Keras 3 is three lines to a chatbot, one backend switch (
jax/tensorflow/torch), and presets for every size from 270M to 27B. Use QAT builds at 4-bit. - Keras is the lab, vLLM is the factory. Local runs and fine-tuning here; batched serving in Lane A.
Honesty note: weight and KV-cache figures are computed in the notebook from Gemma 3's published config (layers, KV heads, head dim, 5:1 sliding window of 1,024 tokens) and cross-checked against the independent 27B KV figure of ~11.4 GB at 128K (computed 11.2 GB). Roofline tokens/s is a memory-bandwidth upper bound, not a measured benchmark — real speed is lower. Raspberry Pi 12–19 tokens/s is an independent measurement, not this notebook's. Gemma 3 sizes are the published 1B/4B/12B/27B (plus 270M); parameter counts are the published totals. No Meta content per the standing exclusion.
Companion notebook: gemma3_keras_local.ipynb — weight-memory ladder, sliding-window KV-cache arithmetic with the 11.4 GB cross-check, fit table, roofline decode model, and the real Keras from_preset + generate code, all exec-tested.