code wiki / (root) / nx_gen_lora_verify.nx

nx_gen_lora_verify.nx source

↩ module page · 122 lines · 5460 B

1// nx_gen_lora_verify.nx -- prove the sovereign LoRA fold against an EXACT f64 reference. 2// 3// ★★★★★ WHY NOT GRADE AGAINST sd.cpp. We tried, and it cannot answer the question. sd.cpp applies 4// this adapter at RUNTIME (`apply lora at runtime` -> forward_with_lora), so its weight tap yields 5// the BASE weight and only its activations carry the adapter. Grading our fold against those 6// activations measured a LoRA delta of 0.86% of the signal through an oracle whose own Q8_0 error 7// is 0.6% median. A least-squares fit for the scale recovered 0.1725 against a true alpha/rank of 8// 0.1667 -- consistent, but the confidence interval swallows the answer. 9// ⇒ THE SAME REMEDY AS EVERY OTHER SCALE THIS SESSION: build the third, higher-precision reference. 10// exact = h @ (W + (alpha/rank) * up @ down)^T in f64, from the SAME dequantized bytes. 11// Now the only difference between us and the reference is OUR arithmetic, which is the thing under 12// test. sd.cpp is no longer a judge here -- it is a second defendant. 13// 14// Usage: nx_gen_lora_verify <lora.safetensors> [multiplier_milli] [ref_fixture] 15// 16// ⚠THE REFERENCE IS AN ARGUMENT ON PURPOSE. A fold that ignored the multiplier entirely would pass 17// the mult=1.0 case perfectly and look green forever. ★ A GATE THAT ONLY TESTS THE EASY SIDE OF A 18// SWITCH DEFENDS NEITHER SIDE -- so the multiplier is graded against its OWN exact reference at 19// 0.5 and at -1.0, and each must fail the others. 20// license_tier: ORIGINAL 21 22import "nx_syscalls.nx" 23import "nx_le.nx" 24import "nx_f32.nx" 25import "nx_f32_div.nx" 26import "nx_f32_cvt.nx" 27import "nx_f16.nx" 28import "nx_strconv.nx" 29import "nx_genfix.nx" 30import "nx_genver.nx" 31import "nx_genweights.nx" 32import "nx_genblock.nx" 33import "nx_genlora.nx" 34 35const LV_IN: i64 = 3840 36const LV_OUT: i64 = 11520 37const LV_TOK: i64 = 768 38 39func lv_puts(s: *u8) -> i64 { 40 var n: i64 = 0 41 while s[n] != (0 as u8) { n = n + 1 } 42 return sys_write(1, s, n) 43} 44 45func main(argc: i64, argv: *i64) -> i64 { 46 if argc < 2 { 47 lv_puts("usage: nx_gen_lora_verify <lora.safetensors> [multiplier_milli]\n" as *u8) 48 return 2 49 } 50 var mult_milli: i64 = 1000 51 if argc > 2 { mult_milli = nx_strconv_parse_i64(argv[2] as *u8, sys_mmap(64) as *i64) } 52 let mult: i64 = nx_f32_div(nx_i32_to_f32(mult_milli), nx_i32_to_f32(1000)) 53 54 let model: *u8 = "lora_probe" as *u8 55 let n_w: *u8 = "model.diffusion_model.layers.0.attention.qkv.weight" as *u8 56 let n_h: *u8 = "layers.0.h_attn" as *u8 57 var n_r: *u8 = "qkv_lora.exact" as *u8 58 if argc > 3 { n_r = argv[3] as *u8 } 59 60 lv_puts("-- loading base weight (f32, dequantized from the Q8_0 DiT)\n" as *u8) 61 let W: *u8 = nx_genfix_load(model, n_w, br_strlen(n_w), LV_IN * LV_OUT) 62 if (W as i64) == 0 { lv_puts("FAIL base weight fixture missing\n" as *u8); return 10 } 63 64 let h: *u8 = nx_genfix_load(model, n_h, br_strlen(n_h), LV_TOK * LV_IN) 65 if (h as i64) == 0 { lv_puts("FAIL h_attn fixture missing\n" as *u8); return 11 } 66 67 let ref: *u8 = nx_genfix_load(model, n_r, br_strlen(n_r), LV_TOK * LV_OUT) 68 if (ref as i64) == 0 { lv_puts("FAIL exact reference missing\n" as *u8); return 12 } 69 70 let lw: *i64 = nx_gw_open(argv[1] as *u8) 71 if (lw as i64) == 0 { lv_puts("FAIL adapter unreadable\n" as *u8); return 13 } 72 73 // ---- the thing under test ------------------------------------------------------- 74 let t0: i64 = sys_now_us() 75 let rc: i64 = nx_lora_fold(lw, n_w, W, LV_IN, LV_OUT, mult) 76 let t1: i64 = sys_now_us() 77 nx_genver_emit("fold_rc" as *u8, rc) 78 nx_genver_emit("fold_us" as *u8, t1 - t0) 79 if rc != LORA_APPLIED { 80 // An adapter that does not cover this layer is not a pass -- the verify would then be 81 // grading the unmodified base weight and reporting green for having done nothing. 82 lv_puts("FAIL adapter did not apply to this tensor (0=absent, negative=malformed)\n" as *u8) 83 return 14 84 } 85 86 // ---- y = h @ W'^T --------------------------------------------------------------- 87 let y: *u8 = sys_mmap(LV_TOK * LV_OUT * 4 + 64) 88 let acc: *u8 = sys_mmap(64) 89 let nch: i64 = LV_IN / 8 90 let t2: i64 = sys_now_us() 91 var t: i64 = 0 92 while t < LV_TOK { 93 let hrow: i64 = (h as i64) + t * LV_IN * 4 94 var o: i64 = 0 95 while o < LV_OUT { 96 var z: i64 = 0 97 while z < 8 { nx_le_write_u32(acc, z * 4, 0); z = z + 1 } 98 let wrow: i64 = (W as i64) + o * LV_IN * 4 99 var ch: i64 = 0 100 while ch < nch { 101 __f32x8_fma(acc, (hrow + ch * 32) as *u8, (wrow + ch * 32) as *u8) 102 ch = ch + 1 103 } 104 nx_le_write_u32(y, (t * LV_OUT + o) * 4, __f32x8_hsum(acc)) 105 o = o + 1 106 } 107 t = t + 1 108 } 109 nx_genver_emit("matmul_us" as *u8, sys_now_us() - t2) 110 111 // ---- grade ---------------------------------------------------------------------- 112 let tol: *i64 = sys_mmap(64) as *i64 113 nx_genver_tols(tol) 114 let c: *i64 = sys_mmap(128) as *i64 115 nx_genver_init(c, 3) // declare the 1e-4 band as the pass bar 116 var i: i64 = 0 117 while i < LV_TOK * LV_OUT { 118 nx_genver_tally(c, tol, nx_le_read_u32(y, i * 4), nx_le_read_u32(ref, i * 4), i) 119 i = i + 1 120 } 121 return nx_genver_report(c) 122}