Skip to main content

Gemma-4

Introduction​

This implementation currently supports batch-one text generation for the following Gemma4 variants:

VariantHuggingFace card
E2Bgoogle/gemma-4-E2B-it
E4Bgoogle/gemma-4-E4B-it
E4B W8A8lsm0729/gemma-4-E4B-it-W8A8-INT8-GPTQ

Multimodal input, logical batch sizes greater than one, MoE variants, speculative decoding, and the vLLM adapter are outside the current scope.

Environment setup​

From $TT_METAL_HOME:

source env_set.sh gemma4

Set HF_MODEL to a supported HuggingFace model ID or local checkpoint path.

Run the pytest demo​

The text demo is parameterized with the batch-one ID:

HF_MODEL=google/gemma-4-E2B-it \
pytest models/bos_model/gemma4/demo/text_demo.py -k "batch-one"

For E4B or quantized E4B:

HF_MODEL=google/gemma-4-E4B-it \
pytest models/bos_model/gemma4/demo/text_demo.py -k "batch-one"

HF_MODEL=lsm0729/gemma-4-E4B-it-W8A8-INT8-GPTQ \
pytest models/bos_model/gemma4/demo/text_demo.py -k "batch-one"

The default prompt is models/bos_model/gemma4/demo/sample_prompts/text_only.json.

Run the model runner​

The model runner streams generated tokens, prints compile/prefill/decode metrics for each request, and prints all inputs and outputs after every request has completed.

Run the default sample prompt:

HF_MODEL=google/gemma-4-E2B-it \
python models/bos_model/gemma4/run_gemma4.py

Run a single query:

HF_MODEL=google/gemma-4-E2B-it \
python models/bos_model/gemma4/run_gemma4.py \
--queries "Explain Rayleigh scattering in simple terms." \
--max_seq_len 1024 \
-g 128

Run prompts from a JSON or text file:

HF_MODEL=google/gemma-4-E2B-it \
python models/bos_model/gemma4/run_gemma4.py \
--queries models/bos_model/gemma4/demo/sample_prompts/text_only.json \
-n 1 \
--max_seq_len 1024 \
--max_generated_tokens 256

JSON input must contain a list of objects with a prompt field. Text input uses one non-empty, non-comment line per prompt. --num_iters/-n limits the number of requests and repeats the supplied prompts when necessary.

Runner options​

OptionDefaultDescription
--device_idall selected mesh devicesDevice ID override
--batch_size1Batch size; only 1 is currently supported
-n, --num_iters1Number of prompts to process
-g, --max_generated_tokens256Maximum generated tokens per prompt
--max_seq_len1024Prompt and generation token budget
--queriessample JSONJSON file, text file, or one query string
--output_pathunsetWrite inputs, outputs, and measurements as JSON
--no_tracedisabledDisable decode trace capture/replay

--no_trace affects decode trace only. Prefill and model kernels are still compiled normally.

Per-token decode timing logs are hidden during normal token streaming so they do not interrupt generated text. Set GEMMA4_DECODE_DEBUG=1 to enable detailed iteration timing logs for debugging; these logs may interleave with streamed tokens.

Quantized E4B notes​

The quantized path keeps one INT8, six-bank DRAM width-sharded copy of each quantized linear weight. Fused global QKV and MLP gate_proj/up_proj use minimal qlinear for supported prefill shapes; the remaining projections use regular 2D qlinear. The three PLI projections (per_layer_input_gate, layer per_layer_projection, and model per_layer_model_projection) remain BF16 because the checkpoint excludes them from quantization.

Prefix-cache throughput​

Prefix caching is available through text_demo.py::run_generation and the lm_benchmark throughput runner:

Gemma4 KV sharing across chunked prefill calls is currently experimental. The command below validates end-to-end execution and reports performance; it does not validate generated-text accuracy.

python models/bos_model/lm_benchmark/run_throughput.py \
--category t2t \
--model google/gemma-4-E4B-it \
--prefix-cache-len 9024 \
--input-sequence-length 10000 \
--output-sequence-length 100 \
--prompt models/bos_model/lm_benchmark/prompts/text_demo_10k.json

For quantized E4B, replace --model with lsm0729/gemma-4-E4B-it-W8A8-INT8-GPTQ.