Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/flash_expert/reference.py # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / flash_expert / reference.py` 129 lines · 6817 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 """Reads ONE routed expert of ONE layer of the Qwen3.8-Flash-Next FP8 checkpoint from disk and writes the raw bytes plus an independent 4 reference computation. Host only; an outside reference instrument (Python float64), not part of CUDA++. 5 6 reference.py MODEL_DIR OUT_DIR [LAYER EXPERT] 7 8 Writes to OUT_DIR: gate_w.bin up_w.bin down_w.bin (E4M3 bytes), gate_s.bin up_s.bin down_s.bin (BF16 weight_scale_inv, 128 x 128 blocks), 9 x.f32 (the input row, 2560 floats), gu_ref.f64 and gu_mag.f64 (y = [gate; up] x and sum |terms|, 1280 values each), a.f32 (silu(gate) * up 10 rounded to float32, 640 values: the down projection's input), dn_ref.f64 and dn_mag.f64 (2560 values), and manifest.txt (shard file, tensor 11 name, dtype, shape, byte range and SHA-256 of every byte read). Only shard files that have no .aria2 file beside them are read. 12 """ 13 import hashlib 14 import json 15 import struct 16 import sys 17 from pathlib import Path 18 19 import numpy as np 20 21 model, out = Path(sys.argv[1]), Path(sys.argv[2]) 22 layer = int(sys.argv[3]) if len(sys.argv) > 3 else 0 23 expert = int(sys.argv[4]) if len(sys.argv) > 4 else 3 24 out.mkdir(parents=True, exist_ok=True) 25 prefix = f"model.language_model.layers.{layer}.mlp.experts.{expert}." 26 want = {name: prefix + name for name in ( 27 "gate_proj.weight", "gate_proj.weight_scale_inv", "up_proj.weight", "up_proj.weight_scale_inv", 28 "down_proj.weight", "down_proj.weight_scale_inv")} 29 30 found = {} 31 for shard in sorted(model.glob("model-*.safetensors")): 32 if (shard.parent / (shard.name + ".aria2")).exists(): 33 continue # a shard still downloading is never read 34 with open(shard, "rb") as f: 35 n = struct.unpack("> 7, (b >> 3) & 15, b & 7 68 v = np.nan if (e == 15 and m == 7) else (m / 8.0) * 2.0 ** -6 if e == 0 else (1 + m / 8.0) * 2.0 ** (e - 7) 69 lut[b] = -v if s else v 70 71 72 def weights(name, rows, cols): 73 return lut[np.frombuffer(found[name][6], dtype=np.uint8).reshape(rows, cols)] 74 75 76 def scales(name, rows, cols): 77 bits = np.frombuffer(found[name][6], dtype=" 1.1e-6) & (small < 2.99e-5) & (large > 3.1e-5) 107 if eligible[100, 88]: 108 row, col = 100, 600 109 else: 110 matches = np.argwhere(eligible) 111 if not len(matches): 112 raise ValueError("no tail weight meets both sensitivity controls") 113 row, offset = matches[0] 114 col = 512 + offset 115 (out / "control.u32").write_bytes(struct.pack("