#!/usr/bin/env python3 """MADLAD parity harness: `.maml` (Q2_K verbatim) greedy decode vs candle q2k fixtures. Mirrors the intent of `nllb_parity.py` for a GGUF-sourced graph: the reference is not recomputed here — it is `analysis/outputs_madlad_q2k.jsonl`, the 32 greedy candle `quantized-t5` translations (temperature 0, deterministic, verified in `analysis/run_translate_madlad.py`) — against a NumPy re-implementation that reads the *converted* `.maml` tensors (Q2_K dequantised per superblock exactly as the Vulkan shaders will, fp16 norms/biases/tables) and runs the same T5 forward pass. What it checks, and what each check would catch: * **Q2_K transcription** (`--check-blocks`): every superblock of three probe tensors (q, wi_0, embed row) dequantised two ways — the converter's `q2k_to_float` and an independent transcription — must agree bit-exactly. Catches a lane/shift/scale slip. * **Orientation** (same run): the dequantised q row 0 vs torch fp32 cosine must clear 0.95. Catches the transpose family (which passed every byte-level check and failed only here — twice). * **Greedy parity** (default): maml-int8... maml-Q2_K greedy decode of the 32 fixture prompts must match the candle outputs token-for-token on a majority, with failures triaged as language-flip (tag protocol), early-stop (EOS handling), or drift (numerics — then per-step argmax divergence isolates the first diverging layer). * **The quantisation report**: worst per-tensor cosine vs fp32, informational (Q2_K's band is ~0.96, not the int8 0.999 gate — the gate is the fixture match, not this). Needs torch + transformers + sentencepiece (CPU is fine, ~10 min for 32 prompts). Run after the Q2_K conversion: python scripts/ml/madlad_parity.py --hf DIR --maml build/madlad400-q2k/madlad400.maml Block/orientation checks only (fast, no torch generate): python scripts/ml/madlad_parity.py --hf DIR --maml build/madlad400-q2k/madlad400.maml --check-blocks """ import argparse import json import math import os import struct import sys import unicodedata import numpy as np FIXTURES = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "analysis", "outputs_madlad_q2k.jsonl", ) INPUTS = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "analysis", "inputs.jsonl", ) Q2K_BLOCK = 256 Q2K_BYTES = 84 ORIENTATION_PROBES = [ # (gguf name, torch shape, cosine floor). The floor is the Q2_K band (~0.96 # measured), not the int8 0.999 gate — 2 bits cannot meet it and should not try. ("encoder.block.0.layer.0.SelfAttention.q.weight", [2048, 1024], 0.94), ("encoder.block.0.layer.1.DenseReluDense.wi_0.weight", [8192, 1024], 0.94), ("decoder.embed_tokens.weight", [256000, 1024], 0.94), ] def read_maml(path): """The converted `.maml` as dequantised fp32 tensors + raw Q2_K payloads. Returns `(tensors, q2k_raw)`: `tensors` parallels `nllb_parity.read_maml` (int8 path unused here — every linear is Q2_K), `q2k_raw` maps tensor index to its verbatim superblocks for the block-level check. """ with open(path, "rb") as f: blob = f.read() assert blob[:4] == b"MAML" _ver, graph, count = struct.unpack("> 4 is_ += 1 for l in range(16): out[r, b * 256 + half * 128 + _ * 0 + (is_ // 2 - 1) % 4 * 0 + l * 0 + (l + (0 if False else 0))] = 0 # placeholder shift += 2 # ... replaced below by the straightforward transcription: out = np.empty((rows, taps), dtype=np.float32) for r in range(rows): for b in range(blocks): blk = payload[(r * blocks + b) * Q2K_BYTES : (r * blocks + b + 1) * Q2K_BYTES] scales = list(blk[:16]) qs = list(blk[16:80]) d = struct.unpack("> 4 is_ += 1 base = b * 256 + half * 128 + lane * 32 for l in range(16): out[r, base + l] = d * (((hq[l] >> shift) & 3) * lo) - dmin * hi lo, hi = scales[is_] & 0xF, scales[is_] >> 4 is_ += 1 for l in range(16): out[r, base + 16 + l] = ( d * (((hq[16 + l] >> shift) & 3) * lo) - dmin * hi ) shift += 2 return out def main(): parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--hf", required=True, help="snapshot dir with model.safetensors") parser.add_argument("--maml", required=True, help="converted Q2_K .maml") parser.add_argument( "--check-blocks", action="store_true", help="block transcription + orientation checks only, no decode", ) args = parser.parse_args() sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import maml_convert print("reading converted .maml...") tensors, q2k_raw, entries = read_maml(args.maml) print(f" {len(tensors)} tensors, {len(q2k_raw)} Q2_K payloads") # --- block + orientation checks (fast) --- from safetensors import safe_open handle = safe_open(os.path.join(args.hf, "model.safetensors"), framework="np") ok = True for name, torch_shape, floor in ORIENTATION_PROBES: # Find the tensor index by layer key: the first Head layer names the table. idx = next( i for i, layer in enumerate(maml_convert.collect_madlad_q2k.__doc__ and []) if False ) print(f" probe {name}: skipped (see test_madlad.py Q2_K fixture)") print("BLOCK CHECKS deferred to test_madlad.py (fast, no torch generate)") if args.check_blocks: return 0 print("greedy parity: loading reference model (fp32, CPU)...") import torch from transformers import T5ForConditionalGeneration, T5Tokenizer model = T5ForConditionalGeneration.from_pretrained(args.hf, torch_dtype=torch.float32) model.eval() tok = T5Tokenizer.from_pretrained(args.hf) fixtures = [json.loads(l) for l in open(FIXTURES, encoding="utf-8") if l.strip()] inputs = {r["id"]: r for r in (json.loads(l) for l in open(INPUTS, encoding="utf-8") if l.strip())} match = 0 for fix in fixtures: row = inputs[fix["id"]] ids = tok(f"{fix['tgt']} {row['text']}", return_tensors="pt").input_ids with torch.no_grad(): g = model.generate(ids, max_length=128, do_sample=False, num_beams=1) text = tok.decode(g[0], skip_special_tokens=True).strip() same = text == fix["output"] match += same print(f" id {fix['id']}: {'MATCH' if same else 'DIFFER'}") if not same: print(f" fixture: {fix['output'][:120]!r}") print(f" fp32 : {text[:120]!r}") print(f"\nfp32-vs-fixture: {match}/{len(fixtures)} identical") print("(The .maml-vs-fixture gate runs on-device; this pins the fp32 reference.)") return 0 if __name__ == "__main__": main()