nx_gen_embed_verify.nx source
↩ module page · 115 lines · 4401 B
1// nx_gen_embed_verify.nx -- SOVEREIGN F16 linear-with-bias (an embedder), verified vs the oracle.
2//
3// y[t][o] = sum_i x[t][i] * W[o][i] + b[o]
4//
5// The DiT's front-end projections -- x_embedder (patchified latent -> hidden) and cap_embedder.1
6// (text-encoder output -> hidden) -- are stored F16 WITH A BIAS, unlike the Q8_0, bias-free
7// projections inside the blocks. Two differences, either of which silently produces a finite
8// wrong tensor if assumed away, so this is its own organ rather than a flag on the block linear.
9//
10// Usage: nx_gen_embed_verify <model_id> <gguf> <x_name> <w_tensor> <bias_tensor|-> <y_name> [rows]
11// pass "-" for <bias_tensor> when the projection has none
12//
13// Weights come from the GGUF, activations and the reference from the oracle fixtures: this grades
14// ONE op, so its inputs should be the oracle's, exactly as the per-op bench does.
15// license_tier: ORIGINAL
16
17import "nx_syscalls.nx"
18import "nx_le.nx"
19import "nx_f32.nx"
20import "nx_f32_div.nx"
21import "nx_f32_cvt.nx"
22import "nx_f16.nx"
23import "nx_f32_exp.nx"
24import "nx_f32_activations.nx"
25import "nx_strconv.nx"
26import "nx_genfix.nx"
27import "nx_genver.nx"
28import "nx_genweights.nx"
29import "nx_genblock.nx"
30
31func ev_puts(s: *u8) -> i64 {
32 var n: i64 = 0
33 while s[n] != (0 as u8) { n = n + 1 }
34 return sys_write(1, s, n)
35}
36
37func main(argc: i64, argv: *i64) -> i64 {
38 if argc < 7 {
39 ev_puts("usage: nx_gen_embed_verify <model> <gguf> <x> <w_tensor> <bias|-> <y> [rows]\n" as *u8)
40 return 2
41 }
42 let M: *u8 = argv[1] as *u8
43 let GP: *u8 = argv[2] as *u8
44 let xn: *u8 = argv[3] as *u8
45 let wn: *u8 = argv[4] as *u8
46 let bn: *u8 = argv[5] as *u8
47 let yn: *u8 = argv[6] as *u8
48 var rows: i64 = 4
49 if argc >= 8 {
50 let ep: *i64 = sys_mmap(32) as *i64
51 ep[0] = 0
52 rows = nx_strconv_parse_i64(argv[7] as *u8, ep)
53 if ep[0] != 0 { rows = 4 }
54 }
55
56 let gw: *i64 = nx_gw_open(GP)
57 if (gw as i64) == 0 { ev_puts("gguf open failed\n" as *u8); return 20 }
58
59 let wi: i64 = nx_gw_find(gw, wn, br_strlen(wn))
60 if wi < 0 { ev_puts("weight tensor not in gguf\n" as *u8); return 21 }
61 let in_dim: i64 = nx_gw_dim0(gw, wi)
62 let out_dim: i64 = nx_gw_dim1(gw, wi)
63
64 let ne: *i64 = sys_mmap(64) as *i64
65 let c_x: i64 = nx_genfix_dims(M, xn, br_strlen(xn), ne)
66 if c_x < 0 { ev_puts("missing x fixture\n" as *u8); return 30 }
67 if ne[0] != in_dim { nx_genver_emit("x_d0_ne_weight_in_dim" as *u8, ne[0]); return 31 }
68 let n_tok: i64 = ne[1]
69 let c_y: i64 = nx_genfix_dims(M, yn, br_strlen(yn), ne)
70 if c_y < 0 { ev_puts("missing y fixture\n" as *u8); return 32 }
71 if ne[0] != out_dim { nx_genver_emit("y_d0_ne_weight_out_dim" as *u8, ne[0]); return 33 }
72
73 let x: *u8 = nx_genfix_load(M, xn, br_strlen(xn), c_x)
74 if (x as i64) == 0 { ev_puts("load x failed\n" as *u8); return 40 }
75 let y: *u8 = nx_genfix_load(M, yn, br_strlen(yn), c_y)
76 if (y as i64) == 0 { ev_puts("load y failed\n" as *u8); return 41 }
77 // materialize the F16 weight once as packed f32
78 let W: *u8 = br_gw_f32(gw, wn, in_dim * out_dim)
79 if (W as i64) == 0 { ev_puts("weight dequant failed\n" as *u8); return 42 }
80
81 var bias: *u8 = 0 as *u8
82 if bn[0] != (0x2D as u8) {
83 bias = br_gw_f32(gw, bn, out_dim)
84 if (bias as i64) == 0 { ev_puts("bias dequant failed\n" as *u8); return 43 }
85 }
86
87 if n_tok < rows { rows = n_tok }
88 nx_genver_emit("in_dim" as *u8, in_dim)
89 nx_genver_emit("out_dim" as *u8, out_dim)
90 nx_genver_emit("tokens_total" as *u8, n_tok)
91 nx_genver_emit("tokens_checked" as *u8, rows)
92 if (bias as i64) == 0 { nx_genver_emit("bias" as *u8, 0) } else { nx_genver_emit("bias" as *u8, 1) }
93
94 let got: *u8 = sys_mmap_shared(rows * out_dim * 4 + 64)
95 br_matmul_f32(W, x, got, rows, in_dim, out_dim, 16)
96
97 let tol: *i64 = sys_mmap(64) as *i64
98 nx_genver_tols(tol)
99 let c: *i64 = sys_mmap(128) as *i64
100 nx_genver_init(c, 3)
101
102 var t: i64 = 0
103 while t < rows {
104 var o: i64 = 0
105 while o < out_dim {
106 let f: i64 = t * out_dim + o
107 var v: i64 = nx_le_read_u32(got, f * 4)
108 if (bias as i64) != 0 { v = __f32_add(v, nx_le_read_u32(bias, o * 4)) }
109 nx_genver_tally(c, tol, v, nx_le_read_u32(y, f * 4), f)
110 o = o + 1
111 }
112 t = t + 1
113 }
114 return nx_genver_report(c)
115}