|
| 1 | +# Gemma 4 31B-IT |
| 2 | + |
| 3 | +Text-only export of Google's Gemma 4 31B-IT to ExecuTorch with INT4/INT8 |
| 4 | +weight quantization. Currently supports the CUDA backend. |
| 5 | + |
| 6 | +For architecture and design notes see [model.md](model.md). |
| 7 | + |
| 8 | +## When to use which script |
| 9 | + |
| 10 | +The full bf16 weights for 31B (~62 GB) often don't fit in available RAM. The |
| 11 | +recommended flow is to quantize once and reuse the quantized checkpoint for |
| 12 | +both export and eager inference: |
| 13 | + |
| 14 | +| Script | Purpose | Peak memory | |
| 15 | +|---|---|---| |
| 16 | +| `quantize_and_save.py` | bf16 HF checkpoint → quantized checkpoint (one-time) | ~30 GB CPU | |
| 17 | +| `export.py --prequantized <dir>` | quantized checkpoint → `model.pte` + `model.ptd` | ~24 GB CPU + CUDA for packing | |
| 18 | +| `inference.py --prequantized <dir>` | quantized checkpoint → eager generation under `torch.compile` | ~24 GB GPU | |
| 19 | +| `export.py --model-dir <hf>` | one-shot bf16 → quantize → export (no intermediate file) | ~30 GB CPU + CUDA for packing | |
| 20 | + |
| 21 | +The quantized checkpoint is a safetensors file with int values + per-group |
| 22 | +scales and a JSON header describing each weight's `QuantConfig`. No tensor |
| 23 | +subclass or backend-specific packing — packing for the target backend happens |
| 24 | +at load time via `quant.pack_model()`. |
| 25 | + |
| 26 | +## Quantization recipes |
| 27 | + |
| 28 | +Two built-in recipes (see `quantize_and_save.py`): |
| 29 | + |
| 30 | +| Recipe | Description | |
| 31 | +|---|---| |
| 32 | +| `default` | INT4 min_max linears, INT8 per-axis embedding | |
| 33 | +| `sensitive` | INT8 for edge-layer v_proj/down_proj, INT4 hqq elsewhere, INT8 per-axis embedding | |
| 34 | + |
| 35 | +## Quantize once |
| 36 | + |
| 37 | +```bash |
| 38 | +python examples/models/gemma4_31b/quantize_and_save.py \ |
| 39 | + --model-dir ~/local/scripts/models/gemma-4-31B-it \ |
| 40 | + --output ./gemma4_31b_int4 \ |
| 41 | + --quant-recipe default |
| 42 | +``` |
| 43 | + |
| 44 | +Writes `model.safetensors`, `config.json`, and |
| 45 | +`tokenizer.json` into `--output`. |
| 46 | + |
| 47 | +## Export to ExecuTorch |
| 48 | + |
| 49 | +```bash |
| 50 | +python examples/models/gemma4_31b/export.py \ |
| 51 | + --prequantized ./gemma4_31b_int4 \ |
| 52 | + --output-dir ./gemma4_31b_exports \ |
| 53 | + --max-seq-len 4096 \ |
| 54 | + --backend cuda |
| 55 | +``` |
| 56 | + |
| 57 | +Writes `model.pte` and `model.ptd` into `--output-dir`. |
| 58 | + |
| 59 | +## Eager inference |
| 60 | + |
| 61 | +```bash |
| 62 | +python examples/models/gemma4_31b/inference.py \ |
| 63 | + --prequantized ./gemma4_31b_int4 \ |
| 64 | + --prompt "Write a short joke about saving RAM." \ |
| 65 | + --max-new-tokens 128 \ |
| 66 | + --temperature 0.8 |
| 67 | +``` |
| 68 | + |
| 69 | +Useful before spending the export+lowering time to confirm the quantized |
| 70 | +model produces sensible text. |
| 71 | + |
| 72 | +## Build the runner |
| 73 | + |
| 74 | +```bash |
| 75 | +make gemma4_31b-cuda |
| 76 | +``` |
| 77 | + |
| 78 | +The binary lands at `cmake-out/examples/models/gemma4_31b/gemma4_31b_runner`. |
| 79 | + |
| 80 | +## Run the .pte |
| 81 | + |
| 82 | +```bash |
| 83 | +./gemma4_31b_runner \ |
| 84 | + --model_path ./gemma4_31b_exports/model.pte \ |
| 85 | + --data_path ./gemma4_31b_exports/aoti_cuda_blob.ptd \ |
| 86 | + --tokenizer_path ./gemma4_31b_int4/tokenizer.json \ |
| 87 | + --prompt "Write a short joke about saving RAM." \ |
| 88 | + --max_new_tokens 128 \ |
| 89 | + --temperature 0.8 |
| 90 | +``` |
| 91 | + |
| 92 | +For benchmarking, add `--cuda_graph` to capture the decode method in a CUDA |
| 93 | +graph (decode is fully static — `T=1`). |
0 commit comments