OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
# Copyright (C) 2018-2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
"""Generate small GGUF models and logits from real llama.cpp CPU, never from OpenVINO.
Usage: PYTHONPATH=<llama.cpp>/gguf-py python gen_arch_accuracy.py --oracle <architecture_oracle>
Build architecture_oracle.cpp against a CPU-only llama.cpp (include/, ggml/include/,
link libllama, libggml, libggml-base). The generated NPZs are consumed without llama.cpp.
"""
import argparse
from pathlib import Path
import subprocess
import tempfile
import gguf
import numpy as np
from mmproj_fixtures import finish, save_npz
CASES = {
"afmoe": {"qk": True, "moe": True, "shared": True, "lead": 1, "post": True,
"swa": True, "gate": True, "selection_bias": True, "sigmoid": True, "layers": 4},
"lfm2": {},
"lfm2-tied": {"architecture": "lfm2", "tied": True},
"lfm2-swa": {"architecture": "lfm2", "swa": True, "kernel": 4},
"lfm2-attention-first": {"architecture": "lfm2", "attention_first": True, "fused": True},
"lfm2moe": {"moe": True, "lead": 1},
"lfm2moe-all-experts": {"architecture": "lfm2moe", "moe": True, "sigmoid": True, "expert_scale": 2.5},
"lfm3": {"tied": True, "post": True},
"lfm3-plain": {"architecture": "lfm3"},
"lfm3-attention-first": {"architecture": "lfm3", "attention_first": True, "fused": True, "post": True},
"lfm3-wide-query": {"architecture": "lfm3", "heads": 6, "post": True},
"qwen35": {}, "qwen35moe": {},
"qwen3next": {}, "qwen3next-legacy": {"architecture": "qwen3next", "legacy": True},
"qwen3vl": {"qk": True, "mrope": True},
"qwen3tts": {"qk": True, "mrope": True, "vocab": 3072, "output_vocab": 3072},
"qwen3vlmoe": {"qk": True, "mrope": True, "moe": True},
"qwen3vl-deepstack": {"architecture": "qwen3vl", "qk": True, "mrope": True, "layers": 4, "deepstack": 2},
"cohere2": {"layer_norm": True, "swa": True, "layers": 4, "tied": True},
"gemma3n": {},
"eagle3": {"layers": 1},
"eagle3-fc-q4_1": {"architecture": "eagle3", "layers": 1,
"quantized": {"fc.weight": gguf.GGMLQuantizationType.Q4_1},
"q4_1_fc_exact": True, "q8_hidden_exact": True},
"eagle3-mapped": {"architecture": "eagle3", "layers": 1, "output_vocab": 16, "norm_before_fc": True,
"norm_before_residual": True, "d2t": True},
"dflash": {"layers": 2, "qk": True},
"dflash-fc-q4_1": {"architecture": "dflash", "layers": 2, "qk": True,
"quantized": {"fc.weight": gguf.GGMLQuantizationType.Q4_1},
"q4_1_fc_exact": True, "q8_hidden_exact": True},
"dflash-post": {"architecture": "dflash", "layers": 2, "qk": True, "post": True,
"layer_scale": True, "fc_scale": True, "value_scale": 1.5, "hidden_act": "gelu_pytorch_tanh"},
"dflash-shared": {"architecture": "dflash", "layers": 2, "qk": True, "shared_v": True},
"dflash-mapped": {"architecture": "dflash", "layers": 2, "qk": True, "output_vocab": 16, "d2t": True},
"qwen35-zero-scales": {"architecture": "qwen35", "zero_scales": True},
"qwen35moe-fused": {"architecture": "qwen35moe", "fused_experts": True},
# Real checkpoints quantize attn_gate apart from ssm_beta/ssm_alpha: F16 here selects the
# frontend's beta+alpha-only merge, and an F16 beta in layer 2 selects separate projections.
"qwen35-mixed": {"architecture": "qwen35", "f16": ("attn_gate", "blk.2.ssm_beta")},
"bonsai2-hadamard": {"architecture": "qwen35", "hadamard": True},
"nemotron_h": {},
"mamba2": {}, "mamba2-tied": {"architecture": "mamba2", "tied": True},
"llama": {},
"llama-embed": {"embedding_output": True, "tied": True},
"llama-embed-noncausal": {"architecture": "llama-embed", "embedding_output": True, "tied": True, "causal": False},
"llama-embed-mean": {"architecture": "llama-embed", "embedding_output": True, "tied": True, "pooling": 1},
"llama-embed-cls": {"architecture": "llama-embed", "embedding_output": True, "tied": True, "pooling": 2},
"llama-embed-last": {"architecture": "llama-embed", "embedding_output": True, "tied": True, "pooling": 3},
"exaone-moe": {"qk": True, "moe": True, "shared": True, "lead": 1, "swa": True, "layers": 4},
"exaone-moe-nextn": {"architecture": "exaone-moe", "qk": True, "moe": True, "shared": True, "lead": 1, "swa": True, "layers": 4, "nextn": 1, "sigmoid": True, "selection_bias": True, "expert_scale": 2.5},
"glm4moe": {"qk": True, "moe": True, "shared": True, "lead": 1, "selection_bias": True, "sigmoid": True, "ffn_post_attn": True},
"jais2": {"layer_norm": True, "relu_squared": True, "bias": True},
"minimax-m2": {"moe": True, "full_qk": True, "selection_bias": True, "rope_dims": 4, "sigmoid": True},
"plamo3": {"qk": True, "fused": True, "fused_ffn": True, "post": True, "bare_post": True, "swa": True}, "qwen2": {"bias": True}, "qwen3": {"qk": True},
"phi3": {"fused": True, "fused_ffn": True}, "minicpm": {},
"minicpm-default": {"architecture": "minicpm", "default_logit_scale": True},
"olmoe": {"moe": True, "full_qk": True},
"hunyuan-dense": {"qk": True}, "hunyuan-moe": {"qk": True, "moe": True, "shared": True},
"qwen3moe": {"qk": True, "moe": True},
"gemma4-mqa": {"architecture": "gemma4"},
"gemma4-moe": {"architecture": "gemma4", "moe": True},
"gemma4-ple": {"architecture": "gemma4", "per_layer": 8, "layers": 4, "shared_kv": 2},
"gemma": {"mqa": True, "tied": True},
"gemma2": {"post": True, "swa": True, "tied": True, "softcap": True},
"gemma3": {"post": True, "swa": True, "tied": True, "qk": True, "linear": 8.0},
"exaone4": {"qk": True, "post_only": True},
"ernie4_5-moe": {"moe": True, "lead": 1, "selection_bias": True, "shared": True},
"bailingmoe2": {"qk": True, "fused": True, "moe": True, "lead": 1,
"selection_bias": True, "shared": True, "sigmoid": True},
"maincoder": {"qk": True}, "mistral3": {}, "smollm3": {"layers": 4},
"mellum": {"qk": True, "moe": True},
"muse-glimmer": {"qk": True, "post": True, "swa": True, "gate": True},
"deepseek2-ocr": {"moe": True, "lead": 1, "shared": True},
"devstral-small": {"architecture": "llama", "embedding": 40},
"devstral-small2": {"architecture": "mistral3", "embedding": 40, "yarn": 48.0,
"yarn_log_mul": 1.0, "temperature": 0.1, "original_context": 2},
"devstral2": {"architecture": "mistral3", "yarn": 64.0, "yarn_beta_fast": 4.0,
"yarn_log_mul": 0.0, "original_context": 128},
}
def tensor_writer(writer, rng, f16=(), quantized=None, exact_q4_1=False):
"""Uniform random F32 tensors; norm weights are centered at 1. Names containing an `f16`
entry are stored as F16."""
quantized = quantized or {}
def tensor(name, shape, norm=False):
values = rng.uniform(-1, 1, shape).astype(np.float32)
values = 1 + values * .3 if norm else values * .2
if exact_q4_1 and name == "fc.weight":
codes = (np.arange(shape[0])[:, None] * 5 + np.arange(shape[1])[None, :] * 7) % 16
values = -1.0 + codes.astype(np.float32) * np.float32(0.125)
quantization = quantized.get(name)
if quantization is not None:
writer.add_tensor(name, gguf.quants.quantize(values, quantization), raw_dtype=quantization)
else:
writer.add_tensor(name, values.astype(np.float16) if any(s in name for s in f16) else values)
return tensor
def write_mamba2_model(path, opts, arch="mamba2"):
w = gguf.GGUFWriter(path, arch)
hybrid = arch == "nemotron_h"
d, inner, heads, groups, state, kernel, vocab = 32, 64, 4, 2, 8, 4, 32
w.add_context_length(128)
w.add_embedding_length(d)
w.add_block_count(4 if hybrid else 2)
w.add_head_count([4, 4, 0, 0] if hybrid else 0)
if hybrid:
w.add_layer_norm_eps(1e-5)
w.add_head_count_kv([0, 2, 0, 0])
w.add_feed_forward_length([0, 0, 48, 0])
w.add_key_length(8)
w.add_value_length(8)
w.add_layer_norm_rms_eps(1e-5)
w.add_vocab_size(vocab)
w.add_tokenizer_model("none")
for key, value in {"inner_size": inner, "time_step_rank": heads, "group_count": groups,
"state_size": state, "conv_kernel": kernel}.items():
w.add_uint32(arch + ".ssm." + key, value)
rng = np.random.default_rng(20260915)
tensor = tensor_writer(w, rng)
tensor("token_embd.weight", (vocab, d))
tensor("output_norm.weight", (d,), True)
if opts.get("layer_norm"):
tensor("output_norm.bias", (d,))
if not opts.get("tied"):
tensor("output.weight", (vocab, d))
conv_dim = inner + 2 * groups * state
for layer in range(4 if hybrid else 2):
p = f"blk.{layer}."
tensor(p + "attn_norm.weight", (d,), True)
if hybrid and layer == 1:
for name, width in (("q", 32), ("k", 16), ("v", 16)):
tensor(p + f"attn_{name}.weight", (width, 2 * d if arch == "eagle3" else d))
tensor(p + f"attn_{name}.bias", (width,))
tensor(p + "attn_output.weight", (d, 32))
tensor(p + "attn_output.bias", (d,))
continue
if hybrid and layer == 2:
tensor(p + "ffn_up.weight", (48, d))
tensor(p + "ffn_up.bias", (48,))
tensor(p + "ffn_down.weight", (d, 48))
tensor(p + "ffn_down.bias", (d,))
continue
tensor(p + "ssm_in.weight", (inner + conv_dim + heads, d))
tensor(p + "ssm_conv1d.weight", (conv_dim, kernel))
tensor(p + "ssm_conv1d.bias", (conv_dim,))
tensor(p + "ssm_dt.bias", (heads,))
w.add_tensor(p + "ssm_a", -rng.uniform(.1, 1, (heads, 1)).astype(np.float32))
tensor(p + "ssm_d", (heads, 1))
tensor(p + "ssm_norm.weight", (groups, inner // groups), True)
tensor(p + "ssm_out.weight", (d, inner))
finish(w)
def write_qwen35_model(path, arch, fused_experts=False, f16=(), hadamard=False, legacy=False, zero_scales=False):
w = gguf.GGUFWriter(path, arch)
d, head, heads, kv, ff, vocab = 32, 16, 4, 2, 48, 32
state, groups, vheads, kernel = 8, 2, 4, 4
inner, conv = state * vheads, state * (2 * groups + vheads)
w.add_context_length(128)
w.add_embedding_length(d)
w.add_block_count(4)
w.add_feed_forward_length(ff)
w.add_head_count(heads)
w.add_head_count_kv(kv)
w.add_key_length(head)
w.add_value_length(head)
w.add_rope_dimension_count(8)
w.add_rope_freq_base(10000.)
if arch != "qwen3next":
w.add_array(arch + ".rope.dimension_sections", [1, 1, 2, 0])
w.add_layer_norm_rms_eps(1e-5)
w.add_vocab_size(vocab)
w.add_tokenizer_model("none")
for key, value in {"inner_size": inner, "time_step_rank": vheads, "group_count": groups,
"state_size": state, "conv_kernel": kernel}.items():
w.add_uint32(arch + ".ssm." + key, value)
w.add_uint32(arch + ".full_attention_interval", 4)
if zero_scales:
w.add_embedding_scale(0.0)
w.add_residual_scale(0.0)
if hadamard:
names = ["output.weight"]
for layer in range(4):
p = f"blk.{layer}."
kinds = ("attn_qkv", "attn_gate", "ssm_out") if layer < 3 else ("attn_q", "attn_k", "attn_v", "attn_output")
names.extend(p + kind + ".weight" for kind in (*kinds, "ffn_gate", "ffn_up", "ffn_down"))
w.add_uint32("prism.hadamard.version", 1)
w.add_uint32("prism.hadamard.block_size", 8)
w.add_string("prism.hadamard.transform", "normalized-sylvester-walsh-hadamard")
w.add_string("prism.hadamard.axis", "input-last-dimension")
w.add_string("prism.hadamard.sign_mode", "explicit")
w.add_array("prism.hadamard.weight_names", names)
w.add_array("prism.hadamard.inverse_weight_names", ["token_embd.weight"])
w.add_key_value("prism.hadamard.sign_widths", [32, 48, 64], gguf.GGUFValueType.ARRAY, gguf.GGUFValueType.INT32)
w.add_key_value("prism.hadamard.sign_values", [(-1 if i % 3 else 1) for i in range(144)], gguf.GGUFValueType.ARRAY, gguf.GGUFValueType.INT32)
w.add_bool("prism.hadamard.gdn_v_grouped", True)
moe = arch in ("qwen35moe", "qwen3next")
if moe:
w.add_expert_count(4)
w.add_expert_used_count(2)
w.add_expert_feed_forward_length(ff)
w.add_expert_shared_feed_forward_length(ff)
rng = np.random.default_rng(20260922)
tensor = tensor_writer(w, rng, f16)
tensor("token_embd.weight", (vocab, d))
tensor("output_norm.weight", (d,), True)
tensor("output.weight", (vocab, d))
for layer in range(4):
p = f"blk.{layer}."
tensor(p + "attn_norm.weight", (d,), True)
tensor(p + "post_attention_norm.weight", (d,), True)
if layer < 3:
if legacy:
tensor(p + "ssm_in.weight", (conv + inner, d))
else:
tensor(p + "attn_qkv.weight", (conv, d))
tensor(p + "attn_gate.weight", (inner, d))
tensor(p + "ssm_conv1d.weight", (conv, kernel))
tensor(p + "ssm_dt.bias", (vheads,))
w.add_tensor(p + "ssm_a", -rng.uniform(.5, 1.5, vheads).astype(np.float32))
if arch == "qwen3next":
tensor(p + "ssm_ba.weight", (2 * vheads, d))
else:
for name in ("beta", "alpha"):
tensor(p + f"ssm_{name}.weight", (vheads, d))
tensor(p + "ssm_norm.weight", (state,), True)
tensor(p + "ssm_out.weight", (d, inner))
else:
for name, width in (("q", 2 * heads * head), ("k", kv * head), ("v", kv * head)):
tensor(p + f"attn_{name}.weight", (width, 2 * d if arch == "eagle3" else d))
for name in ("q", "k"):
tensor(p + f"attn_{name}_norm.weight", (head,), True)
tensor(p + "attn_output.weight", (d, heads * head))
if moe:
tensor(p + "ffn_gate_inp.weight", (4, d))
if fused_experts:
tensor(p + "ffn_gate_up_exps.weight", (4, 2 * ff, d))
else:
for name in ("gate", "up"):
tensor(p + f"ffn_{name}_exps.weight", (4, ff, d))
tensor(p + "ffn_down_exps.weight", (4, d, ff))
tensor(p + "ffn_gate_inp_shexp.weight", (d,))
for name, shape in (("gate", (ff, d)), ("up", (ff, d)), ("down", (d, ff))):
tensor(p + f"ffn_{name}" + ("_shexp" if moe else "") + ".weight", shape)
finish(w)
def write_gemma4_model(path, opts):
"""Layers alternate sliding/full attention; E2B/E4B-style options add per-layer
embeddings (`per_layer`) and reuse earlier KV in the last `shared_kv` layers, which omit
their unused K/V tensors."""
w = gguf.GGUFWriter(path, "gemma4")
d, heads, layers, vocab, ff = 32, 4, opts.get("layers", 2), 32, 48
per_layer, shared = opts.get("per_layer", 0), opts.get("shared_kv", 0)
w.add_context_length(128)
w.add_embedding_length(d)
w.add_block_count(layers)
w.add_head_count(heads)
w.add_head_count_kv([2, 1] * (layers // 2))
w.add_feed_forward_length(ff)
w.add_key_length(16)
w.add_value_length(16)
w.add_layer_norm_rms_eps(1e-6)
w.add_vocab_size(vocab)
w.add_tokenizer_model("none")
for key, value in {"attention.key_length_swa": 8, "attention.value_length_swa": 8,
"attention.sliding_window": 2, "attention.shared_kv_layers": shared,
"embedding_length_per_layer_input": per_layer, "rope.dimension_count": 16,
"rope.dimension_count_swa": 8}.items():
w.add_uint32("gemma4." + key, value)
w.add_array("gemma4.attention.sliding_window_pattern", [True, False] * (layers // 2))
w.add_float32("gemma4.rope.freq_base", 1000000.)
w.add_float32("gemma4.rope.freq_base_swa", 10000.)
w.add_float32("gemma4.final_logit_softcapping", 30.)
if opts.get("moe"):
w.add_expert_count(4)
w.add_expert_used_count(2)
w.add_uint32("gemma4.expert_feed_forward_length", 24)
rng = np.random.default_rng(20260922)
tensor = tensor_writer(w, rng)
tensor("token_embd.weight", (vocab, d))
tensor("output_norm.weight", (d,), True)
tensor("rope_freqs.weight", (8,), True)
if per_layer:
tensor("per_layer_token_embd.weight", (vocab, per_layer * layers))
tensor("per_layer_model_proj.weight", (per_layer * layers, d))
tensor("per_layer_proj_norm.weight", (per_layer,), True)
for layer in range(layers):
p = f"blk.{layer}."
swa = layer % 2 == 0
head, kv = (8, 2) if swa else (16, 1)
for name in ("attn_norm", "ffn_norm", "post_attention_norm", "post_ffw_norm"):
tensor(p + name + ".weight", (d,), True)
tensor(p + "attn_q.weight", (heads * head, d))
if layer < layers - shared:
tensor(p + "attn_k.weight", (kv * head, d))
if swa:
tensor(p + "attn_v.weight", (kv * head, d))
for name in ("q", "k"):
tensor(p + f"attn_{name}_norm.weight", (head,), True)
tensor(p + "attn_output.weight", (d, heads * head))
for name, shape in (("gate", (ff, d)), ("up", (ff, d)), ("down", (d, ff))):
tensor(p + f"ffn_{name}.weight", shape)
tensor(p + "layer_output_scale.weight", (1,), True)
if per_layer:
tensor(p + "inp_gate.weight", (per_layer, d))
tensor(p + "proj.weight", (d, per_layer))
tensor(p + "post_norm.weight", (d,), True)
if opts.get("moe"):
for name in ("pre_ffw_norm_2", "post_ffw_norm_1", "post_ffw_norm_2"):
tensor(p + name + ".weight", (d,), True)
tensor(p + "ffn_gate_inp.weight", (4, d))
tensor(p + "ffn_gate_inp.scale", (d,), True)
tensor(p + "ffn_gate_up_exps.weight", (4, 48, d))
tensor(p + "ffn_down_exps.weight", (4, d, 24))
tensor(p + "ffn_down_exps.scale", (4,), True)
finish(w)
def write_gemma3n_model(path):
w = gguf.GGUFWriter(path, "gemma3n")
d, heads, kv, head, ff, vocab, layers, pe = 32, 4, 2, 8, 48, 32, 5, 256
w.add_context_length(128)
w.add_embedding_length(d)
w.add_block_count(layers)
w.add_head_count(heads)
w.add_head_count_kv(kv)
w.add_key_length(head)
w.add_value_length(head)
w.add_feed_forward_length(ff)
w.add_layer_norm_rms_eps(1e-6)
w.add_sliding_window(2)
w.add_sliding_window_pattern(5)
w.add_rope_dimension_count(head)
w.add_rope_freq_base(100.)
w.add_rope_freq_base_swa(100.)
w.add_final_logit_softcapping(30.)
w.add_vocab_size(vocab)
w.add_tokenizer_model("none")
tensor = tensor_writer(w, np.random.default_rng(20261020))
tensor("token_embd.weight", (vocab, d))
tensor("output_norm.weight", (d,), True)
tensor("per_layer_token_embd.weight", (vocab, pe * layers))
tensor("per_layer_model_proj.weight", (pe * layers, d))
tensor("per_layer_proj_norm.weight", (pe,), True)
for name in ("altup_proj", "altup_unembd_proj"):
tensor(name + ".weight", (3, d, d))
for layer in range(layers):
p = f"blk.{layer}."
for name in ("attn_norm", "post_attention_norm", "ffn_norm", "post_ffw_norm", "post_norm",
"altup_correct_scale", "altup_router_norm", "laurel_post_norm"):
tensor(p + name + ".weight", (d,), True)
for name in ("attn_q_norm", "attn_k_norm"):
tensor(p + name + ".weight", (head,), True)
for name, shape in (("attn_q", (heads * head, d)), ("attn_k", (kv * head, d)),
("attn_v", (kv * head, d)), ("attn_output", (d, heads * head)),
("ffn_gate", (ff, d)), ("ffn_up", (ff, d)), ("ffn_down", (d, ff)),
("inp_gate", (pe, d)), ("proj", (d, pe)), ("altup_correct_coef", (4, 4)),
("altup_predict_coef", (16, 4)), ("altup_router", (4, d)),
("laurel_l", (64, d)), ("laurel_r", (d, 64))):
tensor(p + name + ".weight", shape)
finish(w)
def write_lfm_model(path, arch, opts):
w = gguf.GGUFWriter(path, arch)
d, heads, kv, head, ff, vocab = 32, 4, 2, 8, 48, 32
heads = opts.get("heads", heads)
q_width = heads * head
kernel = opts.get("kernel", 3)
kv_layers = [kv, 0, kv, 0] if opts.get("attention_first") else [0, kv, 0, kv]
w.add_context_length(128)
w.add_embedding_length(d)
w.add_block_count(len(kv_layers))
w.add_feed_forward_length(ff)
w.add_head_count(heads)
w.add_head_count_kv(kv_layers)
w.add_key_length(head)
w.add_value_length(head)
w.add_rope_dimension_count(head)
w.add_rope_freq_base(1000000.)
w.add_layer_norm_rms_eps(1e-5)
w.add_uint32(arch + ".shortconv.l_cache", kernel)
w.add_vocab_size(vocab)
w.add_tokenizer_model("none")
if arch == "lfm3":
w.add_sliding_window(2)
w.add_sliding_window_pattern([i == kv_layers.index(kv) for i in range(len(kv_layers))])
w.add_float32(arch + ".rope.freq_base_swa", 10000.)
if opts.get("swa"):
w.add_sliding_window(2)
if opts.get("moe"):
w.add_expert_count(4)
w.add_expert_used_count(2)
w.add_expert_feed_forward_length(ff)
w.add_leading_dense_block_count(opts.get("lead", 0))
w.add_uint32(arch + ".expert_gating_func", 2 if opts.get("sigmoid") else 1)
if opts.get("expert_scale"):
w.add_expert_weights_scale(opts["expert_scale"])
tensor = tensor_writer(w, np.random.default_rng(20261014))
tensor("token_embd.weight", (vocab, d))
tensor("output_norm.weight" if arch == "lfm3" else "token_embd_norm.weight", (d,), True)
if arch == "lfm3":
w.add_tensor("rope_freqs.weight", np.array([1.] * (head // 4) + [1e30] * (head // 4), dtype=np.float32))
if not opts.get("tied"):
tensor("output.weight", (vocab, d))
for layer, layer_kv in enumerate(kv_layers):
p = f"blk.{layer}."
tensor(p + "attn_norm.weight", (d,), True)
tensor(p + "ffn_norm.weight", (d,), True)
if arch == "lfm3" and opts.get("post"):
tensor(p + "post_attention_norm.weight", (d,), True)
tensor(p + "post_ffw_norm.weight", (d,), True)
if layer_kv:
if opts.get("fused"):
tensor(p + "attn_qkv.weight", ((heads + 2 * kv) * head, d))
else:
for name, width in (("q", q_width), ("k", kv * head), ("v", kv * head)):
tensor(p + f"attn_{name}.weight", (width, d))
for name in ("q", "k"):
tensor(p + f"attn_{name}_norm.weight", (head,), True)
tensor(p + "attn_output.weight", (d, q_width))
if arch == "lfm3":
tensor(p + "attn_gate.weight", (heads, d))
else:
tensor(p + "shortconv.in_proj.weight", (3 * d, d))
tensor(p + "shortconv.conv.weight", (d, kernel))
tensor(p + "shortconv.out_proj.weight", (d, d))
if opts.get("moe") and layer >= opts.get("lead", 0):
tensor(p + "ffn_gate_inp.weight", (4, d))
tensor(p + "exp_probs_b.bias", (4,))
for name, shape in (("gate", (4, ff, d)), ("up", (4, ff, d)), ("down", (4, d, ff))):
tensor(p + f"ffn_{name}_exps.weight", shape)
else:
for name, shape in (("gate", (ff, d)), ("up", (ff, d)), ("down", (d, ff))):
tensor(p + f"ffn_{name}.weight", shape)
finish(w)
def write_model(path, arch, opts):
arch = opts.get("architecture", arch)
if arch == "gemma3n":
return write_gemma3n_model(path)
if arch in ("lfm2", "lfm2moe", "lfm3"):
return write_lfm_model(path, arch, opts)
if arch == "gemma4":
return write_gemma4_model(path, opts)
if arch in ("qwen35", "qwen35moe", "qwen3next"):
return write_qwen35_model(path, arch, opts.get("fused_experts", False), opts.get("f16", ()), opts.get("hadamard", False), legacy=opts.get("legacy", False), zero_scales=opts.get("zero_scales", False))
if arch in ("mamba2", "nemotron_h"):
return write_mamba2_model(path, opts, arch)
w = gguf.GGUFWriter(path, arch)
d, heads, head, ff, vocab = opts.get("embedding", 32), 4, 8, 48, opts.get("vocab", 32)
kv = 1 if opts.get("mqa") else heads if opts.get("full_qk") or arch == "deepseek2-ocr" else 2
layers = opts.get("layers", 2)
w.add_context_length(128)
w.add_embedding_length(d)
w.add_block_count(layers + opts.get("nextn", 0))
if opts.get("nextn"):
w.add_nextn_predict_layers(opts["nextn"])
w.add_feed_forward_length(ff)
w.add_head_count(heads)
w.add_head_count_kv(kv)
w.add_key_length(head)
w.add_value_length(head)
w.add_rope_dimension_count(opts.get("rope_dims", head))
w.add_rope_freq_base(10000.0 if arch == "deepseek2-ocr" else 100.0)
if arch in ("eagle3", "dflash"):
w.add_array(arch + ".target_layers", [0, 1, 2])
w.add_uint32(arch + ".target_hidden_size", d)
for name in ("norm_before_fc", "norm_before_residual"):
if opts.get(name):
w.add_bool(arch + "." + name, True)
if opts.get("hidden_act"):
w.add_string(arch + ".hidden_activation", opts["hidden_act"])
if opts.get("value_scale"):
w.add_float32(arch + ".attention.value_scale", opts["value_scale"])
if opts.get("deepstack"):
w.add_uint32(arch + ".n_deepstack_layers", opts["deepstack"])
if opts.get("mrope"):
w.add_array(arch + ".rope.dimension_sections", [1, 1, 2, 0])
if opts.get("layer_norm"):
w.add_layer_norm_eps(1e-5)
else:
w.add_layer_norm_rms_eps(1e-5)
w.add_vocab_size(vocab)
w.add_tokenizer_model("none")
if opts.get("embedding_output"):
w.add_uint32(arch + ".pooling_type", opts.get("pooling", 0))
w.add_bool(arch + ".attention.causal", opts.get("causal", True))
if opts.get("linear"):
w.add_rope_scaling_type(gguf.RopeScalingType.LINEAR)
w.add_rope_scaling_factor(opts["linear"])
w.add_rope_freq_base_swa(100.0)
if opts.get("yarn"):
w.add_rope_scaling_type(gguf.RopeScalingType.YARN)
w.add_rope_scaling_factor(opts["yarn"])
w.add_rope_scaling_orig_ctx_len(opts["original_context"])
w.add_rope_scaling_yarn_beta_fast(opts.get("yarn_beta_fast", 32.0))
w.add_rope_scaling_yarn_beta_slow(1.0)
w.add_rope_scaling_yarn_log_mul(opts["yarn_log_mul"])
w.add_attn_temperature_scale(opts.get("temperature", 0.0))
if opts.get("swa"):
w.add_sliding_window(2)
w.add_sliding_window_pattern(4 if arch in ("cohere2", "afmoe") else 2)
if arch == "plamo3":
w.add_rope_freq_base_swa(100.0)
if opts.get("softcap"):
w.add_attn_logit_softcapping(2.0)
w.add_final_logit_softcapping(3.0)
if arch == "muse-glimmer":
w.add_logit_scale(1.0)
if arch == "cohere2":
w.add_logit_scale(0.3)
if arch == "minicpm" and not opts.get("default_logit_scale"):
w.add_logit_scale(d / 256.0)
if opts.get("moe"):
w.add_expert_count(4)
w.add_expert_used_count(2)
w.add_expert_feed_forward_length(ff)
w.add_leading_dense_block_count(opts.get("lead", 0))
if arch != "ernie4_5-moe":
w.add_expert_shared_count(int(opts.get("shared", False)))
w.add_expert_shared_feed_forward_length(ff if opts.get("shared") else 0)
w.add_uint32(arch + ".expert_gating_func", 2 if opts.get("sigmoid") else 1)
if opts.get("expert_scale"):
w.add_expert_weights_scale(opts["expert_scale"])
if arch not in ("qwen3vlmoe", "hunyuan-moe", "qwen3moe", "ernie4_5-moe", "mellum", "minimax-m2"):
w.add_expert_weights_norm(arch != "olmoe")
if arch == "bailingmoe2":
w.add_expert_group_count(2)
w.add_expert_group_used_count(1)
if arch == "ernie4_5-moe":
w.add_interleave_moe_layer_step(2)
rng = np.random.default_rng(20260907)
tensor = tensor_writer(w, rng, quantized=opts.get("quantized"),
exact_q4_1=opts.get("q4_1_fc_exact", False))
tensor("token_embd.weight", (vocab, d))
tensor("output_norm.weight", (d,), True)
if opts.get("layer_norm") and arch != "cohere2":
tensor("output_norm.bias", (d,))
if not opts.get("tied"):
tensor("output.weight", (opts.get("output_vocab", vocab), d))
if arch == "dflash":
tensor("enc.output_norm.weight", (d,), True)
if arch in ("eagle3", "dflash"):
tensor("fc.weight", (d, 3 * d))
if opts.get("fc_scale"):
tensor("fc.scale", (1,), True)
if arch == "eagle3":
tensor("blk.0.attn_norm_2.weight", (d,), True)
if opts.get("norm_before_fc"):
tensor("enc.output_norm.weight", (3 * d,), True)
if opts.get("d2t"):
w.add_tensor("d2t", np.arange(0, vocab, 2, dtype=np.int64))
for layer in range(layers):
p = f"blk.{layer}."
if not opts.get("post_only"):
norms = ("attn_norm",) if arch == "cohere2" else ("attn_norm", "post_attention_norm" if opts.get("ffn_post_attn") else "ffn_norm")
for name in norms:
tensor(p + name + ".weight", (d,), True)
if opts.get("layer_norm") and arch != "cohere2":
tensor(p + name + ".bias", (d,))
if opts.get("post") or opts.get("post_only"):
for name in ("post_attention_norm", "post_ffw_norm"):
tensor(p + name + ("" if opts.get("bare_post") else ".weight"), (d,), True)
if opts.get("fused"):
tensor(p + "attn_qkv.weight", ((heads + 2 * kv) * head, d))
else:
for name, width in (("q", heads * head), ("k", kv * head), ("v", kv * head)):
if name == "v" and opts.get("shared_v"):
continue
tensor(p + f"attn_{name}.weight", (width, 2 * d if arch == "eagle3" else d))
if opts.get("bias"):
tensor(p + f"attn_{name}.bias", (width,))
tensor(p + "attn_output.weight", (d, heads * head))
if opts.get("layer_norm") and arch != "cohere2":
tensor(p + "attn_output.bias", (d,))
if opts.get("layer_scale"):
tensor(p + "layer_output_scale.weight", (1,), True)
if opts.get("qk") or opts.get("full_qk"):
for name, width in (("q", heads * head), ("k", kv * head)):
tensor(p + f"attn_{name}_norm.weight", (width if opts.get("full_qk") else head,), True)
if opts.get("gate"):
tensor(p + "attn_gate.weight", (heads * head, d))
if opts.get("moe") and layer >= opts.get("lead", 0):
tensor(p + "ffn_gate_inp.weight", (4, d))
for name, shape in (("gate", (4, ff, d)), ("up", (4, ff, d)), ("down", (4, d, ff))):
tensor(p + f"ffn_{name}_exps.weight", shape)
if opts.get("selection_bias"):
tensor(p + "exp_probs_b.bias", (4,))
if opts.get("shared"):
for name, shape in (("gate", (ff, d)), ("up", (ff, d)), ("down", (d, ff))):
tensor(p + f"ffn_{name}_shexp.weight", shape)
elif opts.get("relu_squared"):
for name, shape in (("up", (ff, d)), ("down", (d, ff))):
tensor(p + f"ffn_{name}.weight", shape)
tensor(p + f"ffn_{name}.bias", (shape[0],))
elif opts.get("fused_ffn"):
tensor(p + "ffn_up.weight", (2 * ff, d))
tensor(p + "ffn_down.weight", (d, ff))
else:
for name, shape in (("gate", (ff, d)), ("up", (ff, d)), ("down", (d, ff))):
tensor(p + f"ffn_{name}.weight", shape)
if opts.get("nextn"):
p = f"blk.{layers}."
for name, width in (("q", heads * head), ("k", kv * head), ("v", kv * head)):
tensor(p + f"attn_{name}.weight", (width, 2 * d if arch == "eagle3" else d))
tensor(p + "attn_output.weight", (d, heads * head))
for name in ("attn_norm", "attn_q_norm", "attn_k_norm", "ffn_norm"):
tensor(p + name + ".weight", (head if "_q_" in name or "_k_" in name else d,), True)
for name, shape in (("gate", (ff, d)), ("up", (ff, d)), ("down", (d, ff))):
tensor(p + f"ffn_{name}.weight", shape)
tensor(p + "nextn.eh_proj.weight", (d, 2 * d))
for name in ("enorm", "hnorm"):
tensor(p + "nextn." + name + ".weight", (d,), True)
finish(w)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--oracle", required=True)
parser.add_argument("--embedding-oracle")
parser.add_argument("--out-dir", type=Path, default=Path(__file__).parent / "test_data/arch_accuracy")
parser.add_argument("--architectures", nargs="+", default=list(CASES))
parser.add_argument("--header-dir", type=Path, help="also write header-only conversion fixtures")
args = parser.parse_args()
if not args.embedding_oracle and any(CASES[arch].get("embedding_output") for arch in args.architectures):
parser.error("--embedding-oracle is required to generate embedding fixtures")
args.out_dir.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory() as tmp:
for arch in args.architectures:
model = Path(tmp) / f"{arch}.gguf"
reference = model.with_suffix(".bin")
write_model(model, arch, CASES[arch])
oracle = args.embedding_oracle if CASES[arch].get("embedding_output") else args.oracle
extra = {}
command = [oracle, str(model), str(reference)]
if CASES[arch].get("architecture", arch) in ("eagle3", "dflash"):
feature_shape = (6, 96)
if CASES[arch].get("q8_hidden_exact"):
codes = (np.arange(feature_shape[0])[:, None] * 47 + np.arange(feature_shape[1])[None, :] * 73) % 255 - 127
for block in range(0, feature_shape[1], 32):
codes[:, block] = 127
codes[:, block + 1] = -127
features = codes.astype(np.float32) * np.float32(1 / 512)
else:
features = np.random.default_rng(20261021).uniform(-.2, .2, feature_shape).astype(np.float32)
features.tofile(model.with_suffix(".features"))
command += ["--dflash" if CASES[arch].get("architecture", arch) == "dflash" else "--draft", str(model.with_suffix(".features"))]
extra["hidden_states"] = features
if CASES[arch].get("deepstack"):
reader = gguf.GGUFReader(model)
embedding = next(t.data for t in reader.tensors if t.name == "token_embd.weight")
count = CASES[arch]["deepstack"]
width = embedding.shape[1]
features = np.random.default_rng(20261019).uniform(-.2, .2, (6, count, width)).astype(np.float32)
packed = np.concatenate((embedding[[1, 2, 3, 4, 5, 6]][:, None, :], features), axis=1)
packed.tofile(model.with_suffix(".embeddings"))
command += ["--embeddings", str(model.with_suffix(".embeddings"))]
extra["deepstack"] = features
result = subprocess.run(command, capture_output=True)
if result.returncode:
raise RuntimeError(f"{arch}: oracle failed\n{result.stderr.decode()}")
if CASES[arch].get("embedding_output"):
dim, count = np.fromfile(reference, dtype="<i4", count=2)
values = np.fromfile(reference, dtype="<f4", offset=8 + 4 * count).reshape(count, dim)
pool = CASES[arch].get("pooling", 0)
expected = values.mean(axis=0, keepdims=True) if pool == 1 else values[:1] if pool == 2 else values[-1:] if pool == 3 else values
save_npz(args.out_dir / f"{arch}.npz", {"model": np.fromfile(model, dtype=np.uint8), "embeddings": expected})
print(f"{arch}: embeddings {expected.shape}", flush=True)
continue
vocab = np.fromfile(reference, dtype="<i4", count=1)[0]
logits = np.fromfile(reference, dtype="<f4", offset=4).reshape(3, vocab)
if CASES[arch].get("d2t"):
extra["d2t"] = np.arange(0, vocab, 2, dtype=np.int64)
if CASES[arch].get("architecture", arch) != "dflash":
logits = logits[:, extra["d2t"]]
finite = logits[np.isfinite(logits)]
assert not np.isnan(logits).any() and np.linalg.norm(finite) > 0
save_npz(args.out_dir / f"{arch}.npz", {"model": np.fromfile(model, dtype=np.uint8), "logits": logits, **extra})
if args.header_dir:
from gen_arch_fixtures import strip_to_header
args.header_dir.mkdir(parents=True, exist_ok=True)
filename = f"{arch}.gguf.hdr"
_, data_bytes, _ = strip_to_header(model, args.header_dir / filename)
manifest = args.header_dir / "manifest.txt"
lines = manifest.read_text().splitlines() if manifest.exists() else []
lines = [line for line in lines if not line.startswith(filename + " ")]
lines.append(f"{filename} {data_bytes} convert")
manifest.write_text("\n".join(lines) + "\n")
print(f"{arch}: {model.stat().st_size} bytes, reference logits {logits.shape}", flush=True)
if __name__ == "__main__":
main()