Gemma 4 12B IT: ONNX q4f16 for Transformers.js / WebGPU (text only)

4-bit ONNX export of the text decoder of google/gemma-4-12B-it (revision 43c9d014b231474fa7212dbb48a762566c2b255d), made to run in the browser with Transformers.js v4 on WebGPU. It powers the β€œMaximum” model of WebGPU Chat.

This is a modified version of the original model (Apache 2.0, see LICENSE and NOTICE below): the weights of the linear layers and the embedding table were quantized to 4 bits, and the vision and audio towers were removed. It is not an official Google release.

Files

File Content
onnx/embed_tokens_q4f16.onnx (+ .onnx_data) token ids β†’ embeddings Γ— √hidden (GatherBlockQuantized, 4-bit)
onnx/decoder_model_merged_q4f16.onnx (+ .onnx_data … _3) 48 decoder layers with KV cache β†’ logits
config.json, generation_config.json, tokenizer*.json, chat_template.jinja from the original repository; config.json adapted for Transformers.js (gemma4, text only)
  • Linear layers: com.microsoft.MatMulNBits, 4 bits, block 32, asymmetric (zero points), fp16 scales, round-to-nearest. The tied LM head is split in 4 chunks (≀ 130 MB per GPU buffer).
  • Activations, attention and KV cache in float16; RMSNorm, the residual stream and softmax in float32.
  • Sliding-window layers keep only the last 1,024 positions in their cache; global layers keep everything.
  • Every external data file is smaller than 2 GB. Total download β‰ˆ 7.1 GB.

Usage (Transformers.js β‰₯ 4.3.2, WebGPU, shader-f16)

import { AutoTokenizer, Gemma4ForCausalLM } from '@huggingface/transformers';

const id = 'AMINOXDEV/gemma-4-12B-it-ONNX';
const tokenizer = await AutoTokenizer.from_pretrained(id);
const model = await Gemma4ForCausalLM.from_pretrained(id, {
  dtype: { embed_tokens: 'q4f16', decoder_model_merged: 'q4f16' },
  device: 'webgpu',
});
const inputs = tokenizer.apply_chat_template([{ role: 'user', content: 'Hello!' }], { add_generation_prompt: true, return_dict: true });
const out = await model.generate({ ...inputs, max_new_tokens: 128 });
console.log(tokenizer.decode(out.slice(null, [inputs.input_ids.dims[1], null]), { skip_special_tokens: true }));

Needs a GPU with roughly 10 GB of free memory.

How it was made and checked

The graph is written node by node by tools/gemma4/export.py from the official safetensors, read tensor by tensor with HTTP range requests (the math follows transformers 5.23 modeling_gemma4_unified.py). verify.py runs this ONNX model with greedy decoding and compares every generated token with the official PyTorch modules (teacher forcing, equivalent to comparing greedy outputs), both against the original bf16 weights and against the same weights after this 4-bit quantization.

Result (parity.json, 6 prompts in Spanish and English, 274 greedy steps):

  • Same q4 weights in PyTorch: 97.8 % of steps agree, and every disagreement is a near-tie (top-1/top-2 margin below 0.7 logits), i.e. float16 rounding, not a graph error. Criterion: β‰₯ 97 % and margins < 1.0 β†’ pass.
  • Original bf16 weights: 92.3 % of steps agree (the effect of 4-bit quantization). Criterion: β‰₯ 90 % β†’ pass.
Prompt Steps Agreement with original bf16 Identical greedy prefix Agreement with same q4 weights Identical greedy prefix Mismatch margins (q4 track)
es-chat 48 91.7 % 10 100.0 % 48 β€”
en-chat 34 82.3 % 21 94.1 % 24 0.68, 0.279
es-code 48 95.8 % 0 97.9 % 15 0.454
en-code 48 93.8 % 8 95.8 % 34 0.187, 0.101
es-reason 48 89.6 % 0 97.9 % 1 0.057
en-reason 48 97.9 % 32 100.0 % 48 β€”

β€œIdentical greedy prefix” is how many tokens the greedy outputs share before the first disagreement.

Limitations

Quantization makes the model somewhat less accurate than the original. Text only (no images or audio). Like any language model it can produce wrong or invented information. See the original model card for intended use and limitations.

NOTICE

Gemma 4 is provided by Google under the Apache License 2.0. Copyright Google LLC. Modifications in this repository (ONNX conversion, 4-bit quantization, removal of the vision and audio towers, Transformers.js configuration) by Amin Tahtah Toumi, also under the Apache License 2.0.

Downloads last month
82
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for AMINOXDEV/gemma-4-12B-it-ONNX

Quantized
(423)
this model