#!/usr/bin/env python3 """tools/dflash2_pack.py reads a .safetensors file with numpy alone (the person's PC has neither torch nor the safetensors package) and writes the same runtime file the torch-based version wrote. python tools/test_dflash2_pack.py """ import importlib.util import json import struct import subprocess import sys import tempfile import unittest from pathlib import Path import numpy as np HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) import dflash2_pack as new # noqa: E402 L, H, INTER, HEADS, KV, HD, RANK, VOCAB = 2, 64, 96, 2, 1, 32, 32, 128 CFG = {"num_hidden_layers": L, "hidden_size": H, "num_attention_heads": HEADS, "num_key_value_heads": KV, "head_dim": HD, "intermediate_size": INTER, "sliding_window": 128, "rope_parameters": {"rope_theta": 1000000.0}, "rms_norm_eps": 1e-6, "vocab_size": VOCAB, "dflash_config": {"block_size": 8, "conv_group_size": 16, "conv_kernel_size": 2, "mask_token_id": 7, "selector_rank": RANK, "selector_top_k": 4, "target_layer_ids": [1, 2, 3]}} def bf16(a): """float32 -> BF16 bytes by truncation (what torch's .to(bfloat16) does for these values up to rounding; the test only needs a valid BF16 file).""" return (np.ascontiguousarray(a, np.float32).view(np.uint32) >> 16).astype(np.uint16) def tensors(): rng = np.random.default_rng(7) r = lambda *shape: rng.standard_normal(shape).astype(np.float32) * 0.1 t = {"fc.weight": r(H, H * 3), "hidden_norm.weight": r(H), "norm.weight": r(H)} for l in range(L): p = "layers.%d." % l t[p + "self_attn.q_proj.weight"] = r(HEADS * HD, H) t[p + "self_attn.k_proj.weight"] = r(KV * HD, H) t[p + "self_attn.v_proj.weight"] = r(KV * HD, H) t[p + "self_attn.o_proj.weight"] = r(H, HEADS * HD) t[p + "mlp.gate_proj.weight"] = r(INTER, H) t[p + "mlp.up_proj.weight"] = r(INTER, H) t[p + "mlp.down_proj.weight"] = r(H, INTER) t[p + "attention_conv.kernel_projection.weight"] = r(32, H) t[p + "mlp_conv.kernel_projection.weight"] = r(32, H) for n in ("input_layernorm", "post_attention_layernorm", "self_attn.q_norm", "self_attn.k_norm"): t[p + n + ".weight"] = r(HD if "_norm" in n else H) t[p + "attention_conv.base_kernel"] = r(2, 16) t[p + "mlp_conv.base_kernel"] = r(2, 16) t["candidate_selector.hidden_projection.weight"] = r(RANK, H) t["candidate_selector.predecessor_codebook"] = r(VOCAB, RANK) t["candidate_selector.successor_codebook"] = r(VOCAB, RANK) return t def write_safetensors(path, arrays): """A BF16 safetensors file written by hand (the format is documented: u64 header length, JSON header, raw data).""" header, blobs, at = {}, [], 0 for name, a in arrays.items(): data = bf16(a).tobytes() header[name] = {"dtype": "BF16", "shape": list(a.shape), "data_offsets": [at, at + len(data)]} blobs.append(data) at += len(data) js = json.dumps(header).encode() js += b" " * ((8 - len(js) % 8) % 8) with open(path, "wb") as fh: fh.write(struct.pack("