jon1012 commited on
Commit
2e573a9
·
verified ·
1 Parent(s): b9f4f77

Final card and build scripts: measured numbers, Engram caveat, pruning analysis

Browse files
Files changed (1) hide show
  1. test_engram_shard.py +70 -0
test_engram_shard.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Check the hand-written safetensors container round-trips before it meets a 94.6 GiB table.
3
+
4
+ Builds a shard shaped like the real ones (one Engram table plus the small q/k/wkv tensors),
5
+ runs write_engram_shard, then reopens the result with safetensors and verifies:
6
+ * it parses at all (the header length, padding and offsets are right)
7
+ * every tensor is present with the right dtype and shape
8
+ * pass-through tensors are byte-identical
9
+ * the packed table dequantizes to the same values the in-RAM path produced
10
+ """
11
+ import os, sys, tempfile
12
+ import torch
13
+ from safetensors.torch import save_file
14
+ from safetensors import safe_open
15
+
16
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
17
+ import dsv41_fp4_stream as Q
18
+
19
+ ROWS, COLS = 250_000, 256
20
+ NBLK = COLS // Q.SRC_BLOCK
21
+ torch.manual_seed(1)
22
+
23
+ raw = (torch.randn(ROWS, COLS) * torch.empty(ROWS, 1).uniform_(0.05, 4.0)).clamp(-448, 448)
24
+ tensors = {
25
+ "layers.1.engram.embed.weight": raw.to(torch.float8_e4m3fn),
26
+ "layers.1.engram.embed.scale": torch.randint(110, 140, (ROWS, NBLK), dtype=torch.uint8).view(torch.float8_e8m0fnu),
27
+ "layers.1.engram.q_weight": torch.randn(4, 5120).bfloat16(),
28
+ "layers.1.engram.k_weight": torch.randn(4, 5120).bfloat16(),
29
+ "layers.1.engram.wkv.weight": torch.randn(256, 6144).to(torch.float8_e4m3fn),
30
+ "layers.1.engram.wkv.scale": torch.randint(120, 132, (8, 192), dtype=torch.uint8).view(torch.float8_e8m0fnu),
31
+ }
32
+
33
+ DEVS = ["cpu"] + (["cuda"] if torch.cuda.is_available() else [])
34
+ with tempfile.TemporaryDirectory() as td:
35
+ src = os.path.join(td, "s.safetensors")
36
+ save_file(tensors, src, metadata={"format": "pt"})
37
+
38
+ for dev in DEVS:
39
+ dst = os.path.join(td, f"o-{dev}.safetensors")
40
+ st = Q.write_engram_shard(src, dst, device=dev, chunk_rows=100_000)
41
+ print(f"\n--- device={dev} --- engram={st['engram']} pass={st['pass']} "
42
+ f"cos={st['cos_sum']/st['cos_n']:.6f}")
43
+
44
+ with safe_open(dst, framework="pt") as g:
45
+ got = set(g.keys())
46
+ want = set(tensors)
47
+ assert got == want, f"key mismatch\n got {sorted(got)}\n want {sorted(want)}"
48
+ w = g.get_tensor("layers.1.engram.embed.weight")
49
+ sc = g.get_tensor("layers.1.engram.embed.scale")
50
+ assert w.dtype == torch.uint8 and tuple(w.shape) == (ROWS, COLS // 2), (w.dtype, w.shape)
51
+ assert sc.dtype == torch.float8_e8m0fnu and tuple(sc.shape) == (ROWS, NBLK)
52
+ for k in ["layers.1.engram.q_weight", "layers.1.engram.k_weight",
53
+ "layers.1.engram.wkv.weight", "layers.1.engram.wkv.scale"]:
54
+ a, b = tensors[k], g.get_tensor(k)
55
+ assert a.dtype == b.dtype and a.shape == b.shape, (k, a.dtype, b.dtype)
56
+ assert bool((a.view(torch.uint8) == b.view(torch.uint8)).all()), f"{k} changed"
57
+ print(f" keys/dtypes/shapes OK; pass-through tensors byte-identical")
58
+
59
+ # compare against the in-RAM path
60
+ with safe_open(src, framework="pt") as f2:
61
+ p2, s2, _, _ = Q.quantize_engram_to_fp4(
62
+ f2, "layers.1.engram.embed.weight", "layers.1.engram.embed.scale",
63
+ ROWS, COLS, chunk_rows=100_000, device=dev)
64
+ assert bool((p2 == w).all()), "streamed weights differ from the in-RAM path"
65
+ assert bool((s2.view(torch.uint8) == sc.view(torch.uint8)).all()), "scales differ"
66
+ print(f" matches the in-RAM path bit-for-bit")
67
+
68
+ print(f" file {os.path.getsize(dst):,} B (source {os.path.getsize(src):,} B)")
69
+
70
+ print("\nSTREAMED ENGRAM SHARD OK")