diff --git a/src/program/generate.cpp b/src/program/generate.cpp index d5b2459dd6..d29bc4bad8 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -1406,7 +1406,7 @@ void drive_pool(void* user, const float* x_f, const int32_t* ids, const float* w // unaffected - it is a per-layer split, and shifting every layer by one preserves both metrics - but // anything keyed on the layer index, which is exactly what a cache profile is, would have been wrong. const int32_t layer_idx = (int32_t) (t->d.layers - 1); - if (layer_idx < 0 || layer_idx >= 48) { + if (layer_idx < 0 || layer_idx >= strata::core::kCompiledGeometry.n_layers) { std::fprintf(stderr, "strata generate: the routing trace saw layer %d, outside 0..47\n", layer_idx); return; } @@ -1430,7 +1430,7 @@ void drive_pool_multi_impl(void* user, const float* x_f, const int32_t* ids, int // one record per token. The multi dispatch fuses the router weights into the kernel and does not surface // them, so records carry unit weights: tools/make_profile.py ranks pairs by routed frequency, which is the // signal that matters; a one-shot --dump-routing run records true weights if a weighted ranking is wanted. - if (t->routing != nullptr && layer >= 0 && layer < 48) { + if (t->routing != nullptr && layer >= 0 && layer < strata::core::kCompiledGeometry.n_layers) { for (int64_t tok = 0; tok < n_tok; ++tok) { const int32_t rec[2] = {(int32_t) layer, (int32_t) k}; std::fwrite(rec, sizeof rec, 1, t->routing); diff --git a/tools/q4geo_synth.py b/tools/q4geo_synth.py new file mode 100644 index 0000000000..d08e95a5b2 --- /dev/null +++ b/tools/q4geo_synth.py @@ -0,0 +1,364 @@ +#!/usr/bin/env python3 +"""Synthetic qwen4exp GGUF files with random weights, at any shape, in the per-tensor type mix of an Unsloth UD file. + +Why: the engine is compiled for one model shape (include/strata/core/geometry.hpp). To check that a build for another shape +loads and runs, there has to be a file of that shape. The writer in gguf_writer.py holds every tensor in memory and knows +only F32, F16 and Q2_0; this one streams, and writes the types the UD files use (Q8_0, Q4_K, Q5_K, Q6_K, Q5_0, Q5_1, IQ4_NL, +IQ4_XS, IQ3_S, BF16, F32). The weights are random bytes with the scale fields of every block set to a fixed small number, so +the dequantized values stay in a few hundredths and the activations stay finite; nothing here quantizes anything. + + python tools/q4geo_synth.py --shape q4_lite --layers 8 --vocab 8192 --recipe ud-q4k --out /data/synth/lite.gguf + python tools/q4geo_synth.py --shape q4_lite --layers 8 --header out.hpp # the geometry header for that file + python tools/q4geo_synth.py --from-gguf some.gguf --header out.hpp # day one: a geometry header from a real file + python tools/q4geo_synth.py --list + +The layout (tensor names and shapes) is core/family_layout.hpp's for qwen4exp. The PLE / n-gram tables of Flash-Next are NOT +written (their hash constants are compiled in and the table is 28 GB): run the engine with --no-ple on these files. + +The geometry header written by --header is what -DSTRATA_GEOMETRY_FILE= takes. +""" +from __future__ import annotations + +import argparse +import pathlib +import struct +import sys + +import numpy as np + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) +from gguf_writer import GGUFWriter, GGUF_MAGIC, DEFAULT_ALIGNMENT # noqa: E402 + +# ggml type id, elements per block, bytes per block, fields to set: (byte offset, fp16 value) +QTYPES = { + "F32": (0, 1, 4, []), + "F16": (1, 1, 2, []), + "BF16": (30, 1, 2, []), + "Q5_0": (6, 32, 22, [(0, 0.0024)]), + "Q5_1": (7, 32, 24, [(0, 0.0012), (2, -0.018)]), + "Q8_0": (8, 32, 34, [(0, 0.0003)]), + "Q4_K": (12, 256, 144, [(0, 3e-5), (2, 3e-5)]), + "Q5_K": (13, 256, 176, [(0, 1.5e-5), (2, 1.5e-5)]), + "Q6_K": (14, 256, 210, [(208, 5e-6)]), + "IQ4_NL": (20, 32, 18, [(0, 0.0003)]), + "IQ3_S": (21, 256, 110, [(0, 1e-4)]), + "IQ4_XS": (23, 256, 136, [(0, 1e-5)]), +} + +# Flash-Next's values for every field a shape does not name +FLASH = dict(n_embd=2560, n_layers=48, n_head=24, n_head_kv=2, head_dim=256, n_rot=64, ssm_state=128, ssm_k_heads=16, + ssm_v_heads=48, ssm_d_conv=4, idx_q_heads=4, idx_key_dim=128, idx_top_k=2048, idx_block=4, hc=4, hc_lr=320, + n_expert=512, n_expert_used=10, n_ff=640, qsa_interval=4) +SHAPES = { + "flash_next": {}, + "q4_deep": dict(n_layers=64), + "q4_lite": dict(n_embd=2048, n_layers=40, n_head=16, ssm_v_heads=32, hc_lr=256, n_expert=256, n_expert_used=8, n_ff=512), + "q4_mid": dict(n_embd=3072, n_layers=48, n_head=32, ssm_v_heads=64, hc_lr=384, n_expert=256, n_expert_used=8, n_ff=1024), + "q4_big": dict(n_embd=4096, n_layers=60, n_head=32, ssm_v_heads=64, hc_lr=512, n_expert=512, n_expert_used=10, n_ff=1024), +} + + +def shape(name: str, layers: int | None = None, **over) -> dict: + s = dict(FLASH) + s.update(SHAPES[name]) + if layers: + s["n_layers"] = layers + s.update(over) + s["conv_ch"] = 2 * s["ssm_state"] * s["ssm_k_heads"] + s["ssm_state"] * s["ssm_v_heads"] + s["value_dim"] = s["ssm_state"] * s["ssm_v_heads"] + return s + + +# ------------------------------------------------------------------ layout (core/family_layout.hpp) +def layer_tensors(s: dict, layer: int): + """(name, shape, role) of one layer.""" + N, NE, FF = s["n_embd"], s["n_expert"], s["n_ff"] + H = s["hc"] * N + t = [("ffn_gate_inp.weight", [N, NE], "router"), ("ffn_gate_inp_shexp.weight", [N], "small"), + ("ffn_gate_shexp.weight", [N, FF], "shexp"), ("ffn_up_shexp.weight", [N, FF], "shexp"), + ("ffn_down_shexp.weight", [FF, N], "shexp"), + ("ffn_gate_exps.weight", [N, FF, NE], "gate"), ("ffn_up_exps.weight", [N, FF, NE], "up"), + ("ffn_down_exps.weight", [FF, N, NE], "down")] + for blk in ("attn", "ffn"): + t += [(f"hc_{blk}_down.weight", [H, s["hc_lr"]], "hc"), (f"hc_{blk}_up.weight", [s["hc_lr"], H], "hc"), + (f"hc_{blk}_inject.weight", [H, s["hc"]], "inject"), (f"hc_{blk}_norm.weight", [H], "norm")] + if (layer % s["qsa_interval"]) != s["qsa_interval"] - 1: + t += [("attn_qkv.weight", [N, s["conv_ch"]], "dense"), ("attn_gate.weight", [N, s["value_dim"]], "dense"), + ("ssm_out.weight", [s["value_dim"], N], "dense"), ("ssm_conv1d.weight", [s["ssm_d_conv"], s["conv_ch"]], "conv"), + ("ssm_alpha.weight", [N, s["ssm_v_heads"]], "small_mat"), ("ssm_beta.weight", [N, s["ssm_v_heads"]], "small_mat"), + ("ssm_a", [s["ssm_v_heads"]], "ssm_a"), ("ssm_dt.bias", [s["ssm_v_heads"]], "bias"), + ("ssm_norm.weight", [s["ssm_state"]], "norm")] + else: + hd, nh, nkv = s["head_dim"], s["n_head"], s["n_head_kv"] + t += [("attn_q.weight", [N, 2 * nh * hd], "dense"), ("attn_k.weight", [N, nkv * hd], "dense"), + ("attn_v.weight", [N, nkv * hd], "dense"), ("attn_output.weight", [nh * hd, N], "dense"), + ("attn_q_norm.weight", [hd], "norm"), ("attn_k_norm.weight", [hd], "norm"), + ("indexer.q_proj.weight", [N, s["idx_q_heads"] * s["idx_key_dim"]], "indexer"), + ("indexer.k_proj.weight", [N, s["idx_key_dim"]], "indexer"), + ("indexer.q_norm.weight", [s["idx_key_dim"]], "norm"), ("indexer.k_norm.weight", [s["idx_key_dim"]], "norm")] + return t + + +def all_tensors(s: dict, vocab: int): + N = s["n_embd"] + H = s["hc"] * N + out = [("token_embd.weight", [N, vocab], "embd"), ("output.weight", [N, vocab], "output"), + ("output_hc_down.weight", [H, s["hc_lr"]], "hc"), ("output_hc_up.weight", [s["hc_lr"], H], "hc"), + ("output_hc_norm.weight", [H], "norm")] + for l in range(s["n_layers"]): + out += [(f"blk.{l}.{n}", sh, r) for n, sh, r in layer_tensors(s, l)] + return out + + +# ------------------------------------------------------------------ recipes (the type per role, as Unsloth's UD files) +def recipe_type(recipe: str, role: str, layer: int, s: dict) -> str: + ff256 = s["n_ff"] % 256 == 0 + if role in ("router", "small", "norm", "inject", "small_mat", "conv", "ssm_a", "bias"): + return "F32" + if role == "indexer": + return "BF16" + if role in ("dense", "shexp", "hc"): + return "Q8_0" + if role == "embd": + return "Q8_0" + if role == "output": + return "Q6_K" if recipe == "ud-iq4xs" else "Q8_0" + first = layer == 0 + if recipe == "ud-q4k": # UD-Q4_K_XL: Q4_K gate/up; down Q5_K, or Q5_1 where llama.cpp falls back (n_ff % 256); Q8_0 in a few + if role in ("gate", "up"): + return "Q5_K" if first else "Q4_K" + return "Q8_0" if first else ("Q5_K" if ff256 else "Q5_1") + if recipe == "ud-q4k-q6": # as ud-q4k with Q6_K down on every 8th layer (Unsloth's Qwen3.6 mix: 38 x Q5_K, 3 x Q6_K) + if role in ("gate", "up"): + return "Q5_K" if first else "Q4_K" + if first: + return "Q8_0" + return ("Q6_K" if layer % 8 == 1 else "Q5_K") if ff256 else "Q5_1" + if recipe == "ud-iq4xs": # UD-IQ4_XS: IQ3_S gate/up (IQ4_XS in one layer), IQ4_XS down or IQ4_NL by the fallback + if role in ("gate", "up"): + return "IQ4_XS" if first else "IQ3_S" + return "Q8_0" if first else ("IQ4_XS" if ff256 else "IQ4_NL") + if recipe == "ud-q5_0down": # the v31 mix: Q5_0 down layers (what llama.cpp makes of Q4_K down at a non-256 width) + if role in ("gate", "up"): + return "Q4_K" + return "Q5_0" + raise SystemExit(f"unknown recipe {recipe}") + + +RECIPES = ["ud-q4k", "ud-q4k-q6", "ud-iq4xs", "ud-q5_0down"] + + +def row_ok(ty: str, ne0: int) -> bool: + return ne0 % QTYPES[ty][1] == 0 + + +# ------------------------------------------------------------------ data +def small_f32(role: str, shape: list[int], rng: np.random.Generator) -> np.ndarray: + n = int(np.prod(shape)) + if role == "norm": + return (1.0 + 0.05 * rng.standard_normal(n)).astype(" int: + _, blk, tsz, _ = QTYPES[ty] + n = int(np.prod(shape)) + assert n % blk == 0, (ty, shape) + return n // blk * tsz + + +def emit_data(fh, ty: str, role: str, shape: list[int], rng: np.random.Generator) -> int: + tid, blk, tsz, fields = QTYPES[ty] + total = tensor_bytes(ty, shape) + if ty == "F32": + fh.write(small_f32(role, shape, rng).tobytes()) + return total + if ty == "BF16": + a = (0.02 * rng.standard_normal(int(np.prod(shape)))).astype("> 16).astype(" GGUFWriter: + w = GGUFWriter() + p = "qwen4exp." + w.add("general.architecture", "qwen4exp") + w.add("general.type", "model") + w.add("general.name", name) + w.add("general.description", "synthetic random weights (tools/q4geo_synth.py), not a model") + w.add(p + "block_count", s["n_layers"], "u32") + w.add(p + "context_length", 262144, "u32") + w.add(p + "embedding_length", s["n_embd"], "u32") + w.add(p + "attention.head_count", s["n_head"], "u32") + w.add(p + "attention.head_count_kv", s["n_head_kv"], "u32") + w.add(p + "rope.dimension_sections", [11, 11, 10, 0], "array:i32") + w.add(p + "rope.freq_base", 10000000.0, "f32") + w.add(p + "attention.layer_norm_rms_epsilon", 1e-6, "f32") + w.add(p + "expert_count", s["n_expert"], "u32") + w.add(p + "expert_used_count", s["n_expert_used"], "u32") + w.add(p + "attention.key_length", s["head_dim"], "u32") + w.add(p + "attention.value_length", s["head_dim"], "u32") + w.add(p + "expert_feed_forward_length", s["n_ff"], "u32") + w.add(p + "expert_shared_feed_forward_length", s["n_ff"], "u32") + w.add(p + "ssm.conv_kernel", s["ssm_d_conv"], "u32") + w.add(p + "ssm.state_size", s["ssm_state"], "u32") + w.add(p + "ssm.group_count", s["ssm_k_heads"], "u32") + w.add(p + "ssm.time_step_rank", s["ssm_v_heads"], "u32") + w.add(p + "ssm.inner_size", s["value_dim"], "u32") + w.add(p + "full_attention_interval", s["qsa_interval"], "u32") + w.add(p + "rope.dimension_count", s["n_rot"], "u32") + w.add(p + "hyper_connection.count", s["hc"], "u32") + w.add(p + "hyper_connection.low_rank", s["hc_lr"], "u32") + w.add(p + "attention.indexer.head_count", s["idx_q_heads"], "u32") + w.add(p + "attention.indexer.key_length", s["idx_key_dim"], "u32") + w.add(p + "attention.indexer.top_k", s["idx_top_k"], "u32") + w.add(p + "attention.compress_ratios", + [s["idx_block"] if l % s["qsa_interval"] == s["qsa_interval"] - 1 else 0 for l in range(s["n_layers"])], "array:i32") + w.add("tokenizer.ggml.model", "gpt2") + w.add("tokenizer.ggml.pre", "qwen35") + w.add("tokenizer.ggml.tokens", [f"" for i in range(vocab)], "array:string") + w.add("tokenizer.ggml.token_type", [1] * vocab, "array:i32") + w.add("tokenizer.ggml.merges", [], "array:string") if False else None + w.add("tokenizer.ggml.eos_token_id", vocab - 2, "u32") + w.add("tokenizer.ggml.padding_token_id", vocab - 1, "u32") + w.add("tokenizer.ggml.bos_token_id", vocab - 1, "u32") + w.add("general.quantization_version", 2, "u32") + return w + + +def write_gguf(path: pathlib.Path, s: dict, vocab: int, recipe: str, seed: int, name: str) -> dict: + rng = np.random.default_rng(seed) + w = metadata(s, vocab, name) + tl = all_tensors(s, vocab) + plan = [] + for n, sh, role in tl: + layer = int(n.split(".")[1]) if n.startswith("blk.") else -1 + ty = recipe_type(recipe, role, layer, s) + if not row_ok(ty, sh[0]): + raise SystemExit(f"{n} {sh}: row length {sh[0]} is not a multiple of {QTYPES[ty][1]} ({ty})") + plan.append((n, sh, role, ty)) + body = bytearray() + body += struct.pack(" str: + f = FLASH + lines = [f"// {label}: written by tools/q4geo_synth.py from {source}.", "// Take it with -DSTRATA_GEOMETRY_FILE=.", + "#pragma once", "", "namespace strata::core {", "", + f'inline constexpr const char* kGeometryName = "{label}";', + "inline constexpr ModelGeometry kCompiledGeometry = [] {", " ModelGeometry g;"] + m = [("n_embd", "n_embd"), ("n_layers", "n_layers"), ("qsa_interval", "qsa_interval"), ("n_head", "n_head"), + ("n_head_kv", "n_head_kv"), ("head_dim", "head_dim"), ("n_rot", "n_rot"), ("ssm_state", "ssm_state_size"), + ("ssm_k_heads", "ssm_k_heads"), ("ssm_v_heads", "ssm_v_heads"), ("ssm_d_conv", "ssm_d_conv"), + ("idx_q_heads", "idx_q_heads"), ("idx_key_dim", "idx_key_dim"), ("idx_block", "idx_block"), ("idx_top_k", "idx_top_k"), + ("hc", "hc"), ("hc_lr", "hc_lr"), ("n_expert", "n_expert"), ("n_expert_used", "n_expert_used"), ("n_ff", "n_ff")] + for k, c in m: + lines.append(f" g.{c} = {s[k]};") + lines += [" g.n_ff_shexp = g.n_ff;", + " g.ssm_conv_channels = 2 * g.ssm_state_size * g.ssm_k_heads + g.ssm_state_size * g.ssm_v_heads;", + " g.ssm_value_dim = g.ssm_state_size * g.ssm_v_heads;", " return g;", "}();", "", "} // namespace strata::core", ""] + return "\n".join(lines) + + +def shape_from_gguf(path: str) -> dict: + from gguf_reader import GGUFFile + g = GGUFFile(pathlib.Path(path)) + md = g.metadata + p = "qwen4exp." + gi = lambda k, d=None: int(md[p + k]) if (p + k) in md else d + s = dict(FLASH) + s.update(n_embd=gi("embedding_length"), n_layers=gi("block_count"), n_head=gi("attention.head_count"), + n_head_kv=gi("attention.head_count_kv"), head_dim=gi("attention.key_length", 256), n_rot=gi("rope.dimension_count", 64), + ssm_state=gi("ssm.state_size", 128), ssm_k_heads=gi("ssm.group_count", 16), ssm_v_heads=gi("ssm.time_step_rank", 48), + ssm_d_conv=gi("ssm.conv_kernel", 4), idx_q_heads=gi("attention.indexer.head_count", 4), + idx_key_dim=gi("attention.indexer.key_length", 128), idx_top_k=gi("attention.indexer.top_k", 2048), + hc=gi("hyper_connection.count", 4), hc_lr=gi("hyper_connection.low_rank", 320), n_expert=gi("expert_count"), + n_expert_used=gi("expert_used_count"), n_ff=gi("expert_feed_forward_length"), + qsa_interval=gi("full_attention_interval", 4)) + cr = md.get(p + "attention.compress_ratios") + if cr: + nz = [int(x) for x in cr if int(x)] + if nz: + s["idx_block"] = nz[0] + s["conv_ch"] = 2 * s["ssm_state"] * s["ssm_k_heads"] + s["ssm_state"] * s["ssm_v_heads"] + s["value_dim"] = s["ssm_state"] * s["ssm_v_heads"] + return s + + +def main() -> int: + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("--shape", choices=sorted(SHAPES), default="flash_next") + ap.add_argument("--layers", type=int, help="block_count of the file (a build for it needs a header with the same count)") + ap.add_argument("--vocab", type=int, default=8192) + ap.add_argument("--recipe", choices=RECIPES, default="ud-q4k") + ap.add_argument("--seed", type=int, default=1) + ap.add_argument("--out", help="the GGUF to write") + ap.add_argument("--header", help="write the geometry header for the shape (or --from-gguf) here") + ap.add_argument("--label", help="kGeometryName in the header (default: the shape, plus _l)") + ap.add_argument("--from-gguf", help="read the shape from this file's metadata instead of --shape") + ap.add_argument("--list", action="store_true") + a = ap.parse_args() + if a.list: + for n in sorted(SHAPES): + s = shape(n) + print(n, {k: s[k] for k in ("n_embd", "n_layers", "n_head", "n_head_kv", "ssm_v_heads", "hc_lr", "n_expert", "n_expert_used", "n_ff")}) + return 0 + if a.from_gguf: + s = shape_from_gguf(a.from_gguf) + label = a.label or pathlib.Path(a.from_gguf).stem.replace("-", "_").lower() + src = a.from_gguf + else: + s = shape(a.shape, a.layers) + label = a.label or (a.shape + (f"_{a.layers}l" if a.layers else "")) + src = f"shape {a.shape}" + (f", {a.layers} layers" if a.layers else "") + if a.header: + pathlib.Path(a.header).write_text(header_text(s, label, src), encoding="utf8", newline="\n") + print(f"wrote {a.header} ({label})") + if a.out: + if a.from_gguf: + raise SystemExit("--from-gguf is for --header") + info = write_gguf(pathlib.Path(a.out), s, a.vocab, a.recipe, a.seed, f"synthetic {label} ({a.recipe})") + print(f"wrote {a.out}: {info['tensors']} tensors, {info['bytes'] / 1e9:.2f} GB, types {info['types']}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())