okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
1"""Strata P0.S9 - build the pack-layout expert arena from the real GGUF. 2 3The architecture (§3.2) defines one expert blob as 1,382,400 bytes: 4 5gate_up.codes 2 x 640 rows x 640 B rows interleaved g0,u0,g1,u1,... (share input x) 6down.codes 2,560 rows x 160 B 7gate_up.scales 2 x 640 x 40 x FP16 8down.scales 2,560 x 10 x FP16 9 10and that totals exactly 819,200 + 409,600 + 102,400 + 51,200 = 1,382,400 B, matching the artifact's 11Q2_0 expert size to the byte. The conversion is a pure relayout: Q2_0 codes and their FP16 block 12scale are copied unchanged, so dequant(pack) == dequant(ggml) bit-exactly. 13 14Why build this at all rather than read the GGUF in place: the GGUF stores gate, up and down as three 15separate tensors, so a fused gate/up pass would read two streams from three arrays. The pack 16interleaves gate and up ROWS so one pass over the activation vector serves both, which is the whole 17point of the blob layout. 18 19Expert index is the LAST axis of [in, out, experts] (P0.S6), and ne[0] varies fastest, so expert `e` 20is one CONTIGUOUS 460,800-byte run inside each role tensor. Extraction is a strided copy. 21 22Run: python tools/pack_layer.py [n_layers] (default 1; 48 = the full 34 GB arena) 23""" 24from __future__import annotations 25 26import json 27import pathlib 28import sys 29 30import numpy as np 31 32sys .path .insert (0 ,str (pathlib .Path (__file__ ).resolve ().parent )) 33from gguf_reader import GGUFFile # noqa: E402 34 35REPO = pathlib .Path (__file__ ).resolve ().parents [1 ] 36STRATA = REPO .parents [min (1 ,len (REPO .parents )- 1 )]# the dev layout's default shards (setup.py passes --gguf) 37SHARD1 = STRATA / "Q2_0" / "Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00001-of-00002.gguf" 38 39H ,FF ,NE = 2560 ,640 ,512 40QK ,BLOCK_BYTES = 64 ,18 # Q2_0 41ROW_BYTES_GU = H * 2 // 8 # 640 B of codes for one 2,560-wide row 42ROW_BYTES_D = FF * 2 // 8 # 160 B of codes for one 640-wide row 43SCALES_GU = H // QK # 40 blocks per gate/up row 44SCALES_D = FF // QK # 10 blocks per down row 45ROLE_BYTES = H * FF * BLOCK_BYTES // QK # 460,800 B per role per expert 46BLOB_BYTES = 3 * ROLE_BYTES # 1,382,400 47 48assert BLOB_BYTES == 1_382_400 ,BLOB_BYTES 49assert 2 * 640 * ROW_BYTES_GU + 2560 * ROW_BYTES_D + 2 * 640 * SCALES_GU * 2 \ 50+ 2560 * SCALES_D * 2 == BLOB_BYTES 51 52 53def blob_offsets ()-> dict [str ,int ]: 54gu_codes = 0 55d_codes = gu_codes + 2 * FF * ROW_BYTES_GU 56gu_scales = d_codes + H * ROW_BYTES_D 57d_scales = gu_scales + 2 * FF * SCALES_GU * 2 58end = d_scales + H * SCALES_D * 2 59assert end == BLOB_BYTES ,end 60return {"gate_up_codes" :gu_codes ,"down_codes" :d_codes , 61"gate_up_scales" :gu_scales ,"down_scales" :d_scales } 62 63 64OFF = blob_offsets () 65 66 67def build (n_layers :int ,out_path :pathlib .Path )-> dict : 68g = GGUFFile (SHARD1 ) 69by_name = {t .name :t for t in g .tensors } 70src = open (SHARD1 ,"rb" ) 71manifest = {"blob_bytes" :BLOB_BYTES ,"offsets" :OFF ,"layers" : [],"n_layers" :n_layers } 72total = n_layers * NE * BLOB_BYTES 73out_path .parent .mkdir (parents = True ,exist_ok = True ) 74 75with out_path .open ("wb" )as out : 76for layer in range (n_layers ): 77roles = {} 78for role in ("gate" ,"up" ,"down" ): 79t = by_name [f"blk. { layer } .ffn_ { role } _exps.weight" ] 80roles [role ]= t 81assert t .type_name == "Q2_0" ,t .type_name 82# expert e is a contiguous run inside the role tensor 83per_expert = t .elements // NE 84assert per_expert * BLOCK_BYTES // QK == ROLE_BYTES ,per_expert 85layer_off = out .tell () 86# vectorized over the layer's 512 experts (the same bytes the per-block loop wrote; tools/strata_pack.py 87# verify checks them): each role is (512 experts, rows, blocks per row, 18 bytes) 88def role_blocks (role ,rows ,per_row ): 89t = roles [role ] 90n = NE * ROLE_BYTES 91src .seek (g .data_start + t .offset ) 92a = np .frombuffer (src .read (n ),dtype = np .uint8 ) 93assert a .size == n , (role ,a .size ) 94return a .reshape (NE ,rows ,per_row ,BLOCK_BYTES ) 95gate = role_blocks ("gate" ,FF ,SCALES_GU ) 96up = role_blocks ("up" ,FF ,SCALES_GU ) 97down = role_blocks ("down" ,H ,SCALES_D ) 98gu = np .stack ([gate ,up ],axis = 2 ).reshape (NE ,2 * FF ,SCALES_GU ,BLOCK_BYTES )# rows g0,u0,g1,u1,... 99blobs = np .concatenate ([ 100gu [:, :, :,2 :].reshape (NE ,- 1 ),# gate/up codes 101down [:, :, :,2 :].reshape (NE ,- 1 ),# down codes 102gu [:, :, :, :2 ].reshape (NE ,- 1 ),# gate/up fp16 scales 103down [:, :, :, :2 ].reshape (NE ,- 1 ),# down fp16 scales 104 ],axis = 1 ) 105assert blobs .shape == (NE ,BLOB_BYTES ),blobs .shape 106out .write (blobs .tobytes ()) 107manifest ["layers" ].append ({"layer" :layer ,"offset" :layer_off , 108"role_offsets" : {r :by_name [f"blk. { layer } .ffn_ { r } _exps.weight" ].offset 109for r in ("gate" ,"up" ,"down" )}}) 110f" layer { layer } : wrote { NE } blobs at { layer_off } " ) 111src .close () 112manifest ["total_bytes" ]= total 113 (out_path .with_suffix (".json" )).write_text (json .dumps (manifest ,indent = 2 ),encoding = "utf-8" ) 114return manifest 115 116 117if __name__ == "__main__" : 118n = int (sys .argv [1 ])if len (sys .argv )> 1 else 1 119dest = REPO / "pack" / ("layer0.bin" if n == 1 else f"experts- { n } l.bin" ) 120f"building { n } layer(s) -> { dest } ( { ( n * NE * BLOB_BYTES ) / 2 ** 30 :.2f } GiB)" ) 121m = build (n ,dest ) 122f"done: { m [ 'total_bytes' ] / 2 ** 30 :.2f } GiB, { n * NE } blobs" )