diff --git a/README.md b/README.md index b31cc68f..594c02e5 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ To support the development of radiance: [Patreon](https://patreon.com/StillDeadC | MiniMax-Music3 bf16: songs from lyrics and a description | `minimax-music3-bf16.rad` (20 GiB) | [download](https://huggingface.co/StillDeadcode/minimax-music3-bf16) | `minimax-music3.yaml` | | Breeze-TTS-2 bf16: speech, whole, streamed or live, one card | `breeze-tts2-bf16.rad` (5.5 GiB) | [download](https://huggingface.co/StillDeadcode/radiance-breeze-tts2-bf16) | `breeze-tts2.yaml` | | Qwen3.8-27B ParoQuant int4, int5 or MXFP4 (learned rotations), DFlash2 drafter, vision | `qwen3.8-27b-paro-int4.rad` (19 GiB; int5 22 GiB) | [convert](#convert-a-checkpoint) | `qwen3.8-27b-paro.yaml` | +| TranslateGemma 27B MXFP4 (GPTQ), translation; optional DFlash2 drafter | `translategemma-27b-mxfp4-gptq.rad` (17 GiB) | [convert](#convert-a-checkpoint) | `translategemma-27b.yaml` | | Qwen3.8-27B bf16 | `qwen3.8-27b-bf16.rad` (50 GiB) | [convert](#convert-a-checkpoint) | `qwen3.8-27b-bf16.yaml` | | Qwen3.8-Flash-Next bf16, a disk-streamed reference | `qwen3.8-flash-next-bf16.rad` (335 GiB) | [convert](#convert-a-checkpoint) | `flashnext-bf16.yaml` | | EmbeddingGemma 2 bf16: text, image, video and audio embeddings, one card | `embeddinggemma2-bf16.rad` (1.4 GiB) | [convert](#convert-a-checkpoint) | `embeddinggemma2.yaml` | @@ -54,6 +55,15 @@ with a scale search, bf16 lm_head); see [Convert a checkpoint](#convert-a-checkp that container decodes at 103 tok/s on prose and 141 on code, against 112 and 150 for keptn42's, and accepts 0.46 of its drafts on prose against 0.50. +TranslateGemma 27B is converted from Google's bf16 checkpoint in three steps: an int8 container to +calibrate in, its Gram matrices over translation prompts, then MXFP4 with GPTQ +(`data/recipes/translategemma-27b-*.recipe`, the steps in docs/ARCHITECTURES.md). On two R9700s it +decodes 56 tok/s a stream (205 at four) and scores mean KL 0.042 against the bf16 model on held-out +translations; with a DFlash2 drafter, 152 a stream. Its chat template takes structured turns +naming the two languages and refuses the plain turn the server renders at startup, so +`translategemma-27b.yaml` serves a plain Gemma 3 template (`deploy/compose/gemma3.jinja`) and a +client sends on `/v1/completions` the prompt the checkpoint's template renders. + The ParoQuant containers keep the trunk exactly as the ParoQuant releases ship it, with the learned Givens rotations it was trained in. Convert int4 from `z-lab/Qwen3.8-27B-PARO`, int5 from `Launch80/Qwen3.8-27B-PARO-int5` and MXFP4 from `MisterSnrub/Qwen3.8-27B-PARO-MXFP4-v2`, each with diff --git a/data/recipes/translategemma-27b-i8a8.recipe b/data/recipes/translategemma-27b-i8a8.recipe new file mode 100644 index 00000000..3a0b7030 --- /dev/null +++ b/data/recipes/translategemma-27b-i8a8.recipe @@ -0,0 +1,14 @@ +# translategemma-27b-i8a8.recipe -- TranslateGemma 27B (arch/gemma3) for calibration: every linear +# int8 with a bf16 scale a 128 columns, served at i8a8 over an int8 activation scaled per (row, 128). +# KL 0.003 against the bf16 model, and it fits two 32 GB cards with room for the tap's Grams. +# +# rad-convert --recipe data/recipes/translategemma-27b-i8a8.recipe \ +# -o translategemma-27b-i8a8.rad + +blk.*.attn_q.weight rtn codes=i8 group=128 scale=bf16 clamp=sym +blk.*.attn_k.weight rtn codes=i8 group=128 scale=bf16 clamp=sym +blk.*.attn_v.weight rtn codes=i8 group=128 scale=bf16 clamp=sym +blk.*.attn_o.weight rtn codes=i8 group=128 scale=bf16 clamp=sym +blk.*.ffn_down.weight rtn codes=i8 group=128 scale=bf16 clamp=sym +blk.*.ffn_gate_up.weight rtn codes=i8 group=128 scale=bf16 clamp=sym +output.weight rtn codes=i8 group=128 scale=bf16 clamp=sym diff --git a/data/recipes/translategemma-27b-mxfp4-gptq-df2.recipe b/data/recipes/translategemma-27b-mxfp4-gptq-df2.recipe new file mode 100644 index 00000000..a88ca9d8 --- /dev/null +++ b/data/recipes/translategemma-27b-mxfp4-gptq-df2.recipe @@ -0,0 +1,25 @@ +# translategemma-27b-mxfp4-gptq-df2.recipe -- translategemma-27b-mxfp4-gptq.recipe with a DFlash2 +# drafter (rad-convert --draft-model). The drafter's linears are MXFP4 (read whole every draft pass; +# drafts are verified, so this changes only what is proposed) and its head a 2-bit copy of the tied +# embedding, rescored against output.weight, as q38-27b-quark-mxfp4-df2.recipe makes Qwen's. +# `--reuse` of a container made by the recipe above copies its trunk. +# +# CALIB= rad-convert --draft-model \ +# --recipe data/recipes/translategemma-27b-mxfp4-gptq-df2.recipe -o translategemma-27b-mxfp4-gptq-df2.rad + +blk.*.attn_q.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +blk.*.attn_k.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +blk.*.attn_v.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +blk.*.attn_o.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +blk.*.ffn_down.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +blk.*.ffn_gate_up.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +output.weight rtn codes=i8 group=128 scale=bf16 clamp=sym + +dflash.draft_head.weight rtn codes=u2 zero=u8 group=128 scale=f16 rule=search +dflash.fc.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_q.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_k.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_v.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_output.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.ffn_gate_up.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.ffn_down.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 diff --git a/data/recipes/translategemma-27b-mxfp4-gptq.recipe b/data/recipes/translategemma-27b-mxfp4-gptq.recipe new file mode 100644 index 00000000..db76594b --- /dev/null +++ b/data/recipes/translategemma-27b-mxfp4-gptq.recipe @@ -0,0 +1,16 @@ +# translategemma-27b-mxfp4-gptq.recipe -- TranslateGemma 27B (arch/gemma3): every linear MXFP4 by +# GPTQ against the Grams a calibration run wrote (CALIB, see docs/ARCHITECTURES.md), served at +# mxfp4a8 over an E4M3 activation scaled per (row, 128). The tied lm_head is int8 a 128 columns: +# block fp8 cannot be split at --tp 2, where a rank holds 131104 vocabulary rows. Norms and the +# embedding stay bf16. KL 0.042 against the bf16 model. +# +# CALIB= rad-convert \ +# --recipe data/recipes/translategemma-27b-mxfp4-gptq.recipe -o translategemma-27b-mxfp4-gptq.rad + +blk.*.attn_q.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +blk.*.attn_k.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +blk.*.attn_v.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +blk.*.attn_o.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +blk.*.ffn_down.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +blk.*.ffn_gate_up.weight gptq codes=fp4_e2m1 group=32 scale=e8m0 calib=$CALIB +output.weight rtn codes=i8 group=128 scale=bf16 clamp=sym diff --git a/deploy/compose/gemma3.jinja b/deploy/compose/gemma3.jinja new file mode 100644 index 00000000..e882ddba --- /dev/null +++ b/deploy/compose/gemma3.jinja @@ -0,0 +1,4 @@ +{{ bos_token }}{% for message in messages %}{% set role = "model" if message["role"] == "assistant" else "user" %}{{ role }} +{{ message["content"] | trim }} +{% endfor %}{% if add_generation_prompt %}model +{% endif %} diff --git a/deploy/compose/translategemma-27b.yaml b/deploy/compose/translategemma-27b.yaml new file mode 100644 index 00000000..261440b3 --- /dev/null +++ b/deploy/compose/translategemma-27b.yaml @@ -0,0 +1,20 @@ +# TranslateGemma 27B, MXFP4 by GPTQ (data/recipes/translategemma-27b-mxfp4-gptq.recipe; the -df2 +# recipe adds a DFlash2 drafter, whose depth the server takes from the container). +# +# The checkpoint's chat template takes structured turns naming the source and target languages and +# refuses the plain turn the server renders at startup (a user turn must read +# <<>>en<<>>cs<<>>...), so a plain Gemma 3 template (gemma3.jinja, beside this +# file) stands in, and a client sends on /v1/completions the prompt the checkpoint's template renders +# ("You are a professional English (en) to Czech (cs) translator. ..."; its README shows it whole). +name: radiance-translategemma-27b +services: + radiance: + extends: { file: common.yaml, service: radiance } + container_name: radiance-translategemma-27b + volumes: + - ./gemma3.jinja:/templates/gemma3.jinja:ro + command: > + --model /models/translategemma-27b-mxfp4-gptq.rad + --host :: --port ${PORT:-8000} --api-key ${RADIANCE_API_KEY:?set RADIANCE_API_KEY in .env} + --tp 2 --max-num-seqs 8 --max-model-len 16384 --kv-cache-dtype fp8 + --override-chat-template /templates/gemma3.jinja diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index cec30f76..07f9ee9c 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -17,6 +17,7 @@ plugin claims. | `arch/lfm2_bf16` | `lfm2` | *(empty)* | LiquidAI's LFM2 and LFM2-VL, bf16, one rank | | `arch/embgemma2` | `embeddinggemma2` | *(empty)* | EmbeddingGemma 2: a bidirectional encoder with Gemma 4's vision tower and audio conformer, pooled to one vector | | `arch/gemma4_bf16` | `gemma4` | `compressed-tensors` | Gemma 4 31B dense with its vision tower: sliding-window and proportional-RoPE global (`attention_k_eq_v`) attention, GELU ffn, softcapped logits; int4 codes with an f16 scale per 128 from Google's CT-INT4 (QAT) release, served w4a8. Optional draft head: Google's assistant (`rad-convert --draft-model`), with no cache of its own (`rad_drafter_shared_kv`) | +| `arch/gemma3` | `gemma3` | *(empty)* | Gemma 3 dense text (TranslateGemma 27B) from the bf16 checkpoint: sliding-window and global attention at one head geometry, GemmaRMSNorm, GELU ffn; linears bf16, or int8, MXFP4 or asymmetric int4 over an activation scaled per (row, 128), as the recipe stores them. Optional DFlash2 drafter (`rad-convert --draft-model`) | | `arch/gemma4` | `gemma4` | *(empty)* | Gemma 4 text from Google's bf16 checkpoint (the 26B-A4B MoE and the dense sizes without per-layer embeddings) with z-lab's DFlash drafter; routed experts bf16 or block fp8 | | `arch/qwenimage21` | `qwenimage21` | *(empty)* | Qwen-Image 2.1 and 2.1-Turbo, an image generator, from the diffusers pipeline directory; DiT and text encoder bf16 or block fp8 | | `arch/qwen3asr_bf16` | `qwen3asr` | *(empty)* | Qwen3-ASR: a Whisper-style audio encoder over a dense Qwen3 decoder, speech to text | @@ -559,6 +560,101 @@ under a tenth of the weight bytes, and the dense arm's 2112 is not whole 128-blo --- +# `gemma3`: Gemma 3 text (TranslateGemma) + +`arch/gemma3` serves the language model of a `Gemma3ForConditionalGeneration` checkpoint (the +vision tower and projector are not mapped, so never converted): TranslateGemma 27B, and the Gemma 3 +text sizes with its config keys. It is `gemma4_bf16`'s block structure with Gemma 3's differences; +the reference is transformers' `modeling_gemma3` (4.57). + +``` +TranslateGemma 27B (google/translategemma-27b-it) + 62 layers, hidden 5376, vocab 262208 tied to the embedding, rms_norm_eps 1e-6 + layer_types: five sliding_attention then one full_attention (without it, every + _sliding_window_pattern-th layer is global: (l + 1) % 6 == 0) + sliding 32 heads / 16 kv x 128, rope theta 1e4 (rope_local_base_freq), unscaled, window 1024 + global 32 heads / 16 kv x 128, rope theta 1e6, linear scaling factor 8 + scores scaled by query_pre_attn_scalar^-0.5 = 1/sqrt(168); q and k RMS-normed per head, v not + every norm GemmaRMSNorm, x_hat * (1 + w); sandwich norms around attention and feed-forward + feed-forward gate|up 2 x 21504 (gelu-tanh) -> down + embedding * sqrt(5376) held in bf16 (73.5); final norm; tied head; no soft-capping +``` + +Traps: + +- **The rotary scaling is the global class's only.** HF applies `rope_scaling` (linear, 8) to the + full-attention layers; the sliding ones rotate at `rope_local_base_freq` unscaled. transformers + 5.x reads TranslateGemma's flat `rope_parameters` as unscaled global layers, and llama.cpp's + current converter misses the factor too; a reference made either way is wrong by KL 0.024. +- **The scale is query_pre_attn_scalar's**, not 1/sqrt(head_dim): 1/sqrt(128) costs KL 0.013. +- **The embedding scale is sqrt(hidden) held in bf16**: 73.5, not 73.32, as HF's buffer is. +- **One int8 scale a row does not fit Gemma 3's activations**: a few channels run hundreds of times + the rest. `gemma4_bf16`'s w8a8 linears cost KL 0.097 against bf16 on TranslateGemma, w4a8 0.225, + where int8 weights alone cost 0.003. The quantised linears here scale the activation per (row, + 128). +- **Soft-capping and YaRN are refused**, not ignored: Gemma 2's caps are absent from Gemma 3, and + these kernels cannot apply them. + +Checked against transformers' float32 forward on a tiny random Gemma 3 with TranslateGemma's head +geometry, a 16-token window and the real tokenizer: KL 0.00016 on the host backend and on the cards, +at prefill and at 16-token chunks. The wrong attention scale, the missing linear scaling, a window of +15 or 17 and no window score 80 to 1600 times worse. + +**KV groups.** `kv_sliding` (`RAD_KV_WINDOW`, the 1024-token window) and `kv_global` +(`RAD_KV_FULL`), 16 heads of 128 each, 8 a rank at two. The sliding layers' group holds the window, +not the context: at a 16k context a sequence's sliding layers keep a sixteenth of what a full group +would. The cost is the prefix cache: a window group is not reused across requests (see `gemma4` +above), which matters little for translation prompts, whose shared prefix is a line. + +**Linears.** Each weight is served as its encoding stores it: + +| recipe rule | served | activation | +|---|---|---| +| none (bf16) | `gemm_nt` | bf16 | +| `codes=i4\|i8 group=128 scale=f16` | `gemm_nt_q` w4a8 / w8a8, as `gemma4_bf16` | int8, one scale a row | +| `codes=i8 group=128 scale=bf16` | `gemm_nt_q` i8a8 | int8, an f32 scale per (row, 128) (`quant_act_i8g`) | +| `codes=fp4_e2m1 group=32 scale=e8m0` (MXFP4) | `gemm_nt_q` mxfp4a8 | E4M3, a scale per (row, 128) (`quant_act_fp8`) | +| `codes=u4 zero=u8 group=128 scale=f16` | `gemm_nt_q` w4a8i8_asym | int8, a scale per (row, 128) | + +A linear reading the same activation as the one before it (k and v after q, up after gate) reuses +its codes. On an MXFP4 or int8 trunk the q and k norms and both rotaries are one `qkv_norm_rope` at +`v_norm` 0, and each junction (the branch's post-norm, the residual add, the next norm and the next +linear's quantiser) is one `ar_rmsnorm_post_quant_fp8` at two ranks on the exact wire, the branch's +all-reduce in the same launch, or a post-norm and `rmsnorm_quant_fp8` otherwise. Where a row is +missing the plugin declares the chain, and the startup notes say which form each took. + +**Calibration.** Serve a calibration container (int8, `translategemma-27b-i8a8.recipe`: KL 0.003 +against bf16, and it fits two 32 GB cards) with `RADIANCE_CALIB_DIR` set, push a corpus of the +prompts you will serve through prefill, and each linear's input Gram is written once +`RADIANCE_CALIB_TOKENS` have passed. `RADIANCE_G3_CALIB_LAYERS=lo:hi` limits the tap to a layer +slice, so the 21504-wide down projections of 62 layers can be taken in passes. At two ranks the +column-split linears (o, down) see their rank's columns only and write `.part`; libquant +assembles the parts block diagonally (docs/TOOLS.md). Give every prompt a distinct first line, or +prefix-cache hits keep tokens from the tap. + +**Measured** on two R9700s, against the bf16 model on 32 held-out translation texts (chunked at +2048 tokens), decode on an 800-token translation: + +| container | mean KL | top-1 | decode, 1 / 2 / 4 streams | +|---|---|---|---| +| int8 (calibration) | 0.003 | 98.0% | | +| **MXFP4 + GPTQ** | **0.042** | **93.2%** | **56 / 108 / 205 tok/s** | +| asymmetric int4 + GPTQ | 0.043 | 93.4% | 22 tok/s (the tiled kernel) | +| MXFP4, round to nearest | 0.142 | 87.5% | | +| w4a8, round to nearest | 0.225 | 84.1% | | + +**DFlash2.** No public drafter exists for Gemma 3. One trained with NeMo AutoModel's +`TrainDFlash2Recipe` on the target's own translations (a Qwen3-shaped draft config: plain RoPE 1e6 +and SiLU; target layers 1, 16, 30, 44, 59; mask token ``, 6; block 8; window 2048; +`dflash_config.input_embedding_scale` 73.5) is merged with `--draft-model` and served as +`qwen35_fp8` serves Qwen's: the trunk copies the residual stream after each tapped layer's junction +into `dflash_src`, the probe fixes the depth at block - 1, and `rad_block_dflash2.h` applies the +embedding scale through `embed_lookup`'s `mult` (only when it is not 1, so other drafters' graphs are +unchanged). On held-out translation prompts it accepts 3.3 tokens a verify step: 152 tok/s a stream +and 364 at four, against 56 and 175 without it. + +--- + # `qwenimage21`: Qwen-Image 2.1 and 2.1-Turbo, an image generator `arch/qwenimage21` declares a generator (`rad_declare_generator`), which makes the program a diff --git a/docs/GUIDE.md b/docs/GUIDE.md index c7d00abf..5aec839e 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -471,6 +471,14 @@ rad-convert /srv/models/MisterSnrub/Qwen3.8-27B-PARO-MXFP4-v2 --draft-model /srv rad-convert /srv/models/Launch80/Qwen3.8-27B-PARO-int5 --draft-model /srv/models/z-lab/Qwen3.8-27B-DFlash2 \ --recipe data/recipes/q38-27b-paro-mxfp4-df2.recipe -o q38-27b-paro-int5-df2.rad +# TranslateGemma 27B from Google's bf16 checkpoint: an int8 container to calibrate in, its Grams +# (serve it with RADIANCE_CALIB_DIR set and push translation prompts through prefill, see +# docs/ARCHITECTURES.md), then MXFP4 + GPTQ; with a DFlash2 drafter, the -df2 recipe and --draft-model +rad-convert /srv/models/google/translategemma-27b-it \ + --recipe data/recipes/translategemma-27b-i8a8.recipe -o translategemma-27b-i8a8.rad +CALIB=/srv/calib/translategemma rad-convert /srv/models/google/translategemma-27b-it \ + --recipe data/recipes/translategemma-27b-mxfp4-gptq.recipe -o translategemma-27b-mxfp4-gptq.rad + # unsloth's NVFP4 Qwen3.8-27B with the DFlash2 drafter: the NVFP4 MLPs and the FP8 linears beside # them made MXFP4 by one recipe, the head block fp8 rad-convert /srv/models/unsloth/Qwen3.8-27B-NVFP4 --draft-model /srv/models/z-lab/Qwen3.8-27B-DFlash2 \ @@ -2674,6 +2682,7 @@ reductions running on fewer blocks: Qwen3.8-27B's 41k-token prefill 9.5 s uncapp | `RADIANCE_TUNE` | pin a kernel's tunable axes instead of reading the tune cache: `op:key=value[,key=value][;op:...]`. For comparing tunings, not for deployment | | `RADIANCE_CALIB_DIR` | where an engine run writes calibration data for a recipe's `gptq calib=DIR` | | `RADIANCE_CALIB_TOKENS` | with `RADIANCE_CALIB_DIR`, the tokens the calibration tap accumulates before it writes and switches itself off (default 65536) | +| `RADIANCE_G3_CALIB_LAYERS` | `lo:hi`: `arch/gemma3`'s calibration tap over layers lo to hi - 1 only, to take a large model's Grams in passes (docs/ARCHITECTURES.md) | | `RADIANCE_PROFILE_EVERY` | with `--profile-ops`, dump the per-op table every N steps (default 32) | | `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (§4.5) | | `RADIANCE_AR8_SHARD_WEIGHTS`, `RADIANCE_AR8_PIECES` | the eight-rank two-shot's shard weights (`auto`, the default, reads the PCIe links; `off`; or eight integers) and piece count, with `RADIANCE_AR8_SHARD_WEIGHTS_MIN_ELEMS` and `RADIANCE_AR8_PIECES_MIN_ELEMS` for the message size each starts at (§4.7) |