code wiki / _hdl_build / nx_motion_neural_gate.nx
nx_motion_neural_gate.nx source
↩ module page · 146 lines · 7371 B
1// nx_motion_neural_gate.nx -- prove the neural-motion foundation is LEARNED + GENERATIVE (not the parametric
2// keyword lookup): a codebook trained by VQ/k-means on a real motion corpus, and an n-gram token-LM that
3// GENERATES a novel motion sequence, decoded back to drive the rig -> sovereign video.
4// T1 the codebook LEARNS: trained distortion < initial distortion (k-means reduced quantization error)
5// T2 tokens DISCRIMINATE: different moves -> different token sentences (Hamming > 0 across pairs)
6// T3 round-trip PRESERVES motion: decode(encode(move)) per-frame error bounded (codec keeps the motion)
7// T4 GENERATES novel motion: an LM sample is valid tokens, produces motion (decoded poses vary), and is NOT
8// byte-identical to any single training move (real composition, not playback)
9// Emits knowledge/synth_neuralmove.png (APNG of the GENERATED motion, rendered on the FK figure). license_tier:
10// ORIGINAL expect_exit: 0
11import "nx_syscalls.nx"
12import "nx_skeleton.nx"
13import "nx_figure_render.nx"
14import "nx_motion_neural.nx"
15import "nx_apng.nx"
16
17func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
18func pn(v: i64) -> i64 {
19 let b: *u8 = sys_mmap(32) as *u8
20 var x: i64 = v; var neg: i64 = 0
21 if x < 0 { neg = 1; x = 0 - x }
22 var i: i64 = 31
23 if x == 0 { b[i] = 48 as u8; i = i - 1 }
24 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 }
25 if neg == 1 { b[i] = 45 as u8; i = i - 1 }
26 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i)
27 return 0
28}
29func cksum(fb: *i64, n: i64) -> i64 { var s: i64 = 1469598103; var i: i64 = 0; while i < n { s = (s * 31 + (fb[i] & 0xffffff)) & 0x0fffffffffffffff; i = i + 1 } return s }
30func find4(buf: *u8, n: i64, a: i64, b: i64, c: i64, d: i64) -> i64 {
31 var i: i64 = 0
32 while i + 4 <= n { if (buf[i]&0xff)==a { if (buf[i+1]&0xff)==b { if (buf[i+2]&0xff)==c { if (buf[i+3]&0xff)==d { return i } } } } i = i + 1 }
33 return 0 - 1
34}
35const GW: i64 = 128
36const GH: i64 = 96
37
38func main() -> i64 {
39 hw("=== nx_motion_neural_gate -- LEARNED + GENERATIVE motion (VQ codebook + token-LM), sovereign ===\n" as *u8)
40 let sk: i64 = sys_mmap(sk_bytes()) as i64
41 rig_build(sk)
42 let corpus: *i64 = sys_mmap(MN_N * MN_D * 8) as *i64
43 mn_build_corpus(sk, corpus)
44 let cb: *i64 = sys_mmap(MN_K * MN_D * 8) as *i64
45 mn_init_cb(corpus, cb)
46 let dist_init: i64 = mn_distortion(corpus, cb)
47 let dist_train: i64 = mn_train(corpus, cb, 15)
48 hw(" distortion: init=" as *u8); pn(dist_init); hw(" -> trained=" as *u8); pn(dist_train); hw(" (15 k-means iters)\n" as *u8)
49
50 let tokens: *i64 = sys_mmap(MN_N * 8) as *i64
51 mn_tokenize(corpus, cb, tokens)
52 let trans: *i64 = sys_mmap(MN_K * MN_K * 8) as *i64
53 mn_bigram(tokens, trans)
54
55 var fails: i64 = 0
56 // T1 learning reduced distortion
57 if dist_train < dist_init { hw("T1 PASS codebook LEARNED (distortion "); pn(dist_init); hw(" -> "); pn(dist_train); hw(", k-means reduced it)\n" as *u8) }
58 else { fails = fails + 1; hw("T1 FAIL distortion not reduced\n" as *u8) }
59
60 // T2 different moves -> different token sentences. wave=move0 vs kick=move3 vs sway=move2.
61 var diff02: i64 = 0
62 var diff03: i64 = 0
63 var f: i64 = 0
64 while f < MN_FR {
65 if tokens[0 * MN_FR + f] != tokens[2 * MN_FR + f] { diff02 = diff02 + 1 }
66 if tokens[0 * MN_FR + f] != tokens[3 * MN_FR + f] { diff03 = diff03 + 1 }
67 f = f + 1
68 }
69 if diff02 > 0 { if diff03 > 0 { hw("T2 PASS tokens DISCRIMINATE (wave!=sway "); pn(diff02); hw("/16, wave!=kick "); pn(diff03); hw("/16 tokens differ)\n" as *u8) } else { fails = fails + 1; hw("T2 FAIL wave==kick\n" as *u8) } }
70 else { fails = fails + 1; hw("T2 FAIL wave==sway\n" as *u8) }
71
72 // T3 round-trip: decode move 0's tokens -> centroids -> compare to originals; max per-frame dist bounded.
73 var maxrt: i64 = 0
74 f = 0
75 while f < MN_FR {
76 let tk: i64 = tokens[0 * MN_FR + f]
77 let d: i64 = mn_dist(corpus, (0 * MN_FR + f) * MN_D, cb, tk * MN_D)
78 if d > maxrt { maxrt = d }
79 f = f + 1
80 }
81 let bound: i64 = dist_init / MN_N + dist_init / MN_N // 2x the initial mean = generous, still meaningful
82 if maxrt < bound { hw("T3 PASS round-trip PRESERVES motion (max frame err "); pn(maxrt); hw(" < bound "); pn(bound); hw(")\n" as *u8) }
83 else { fails = fails + 1; hw("T3 FAIL round-trip err "); pn(maxrt); hw(" >= "); pn(bound); hw("\n" as *u8) }
84
85 // T4 GENERATE a novel motion-token sequence
86 let st: *i64 = sys_mmap(8) as *i64; st[0] = 20260706
87 let gen: *i64 = sys_mmap(MN_FR * 8) as *i64
88 mn_generate(trans, tokens[0], st, gen, MN_FR)
89 hw(" generated tokens:" as *u8); var gi: i64 = 0; while gi < MN_FR { hw(" "); pn(gen[gi]); gi = gi + 1 } hw("\n" as *u8)
90 var t4: i64 = 1
91 var valid: i64 = 1
92 gi = 0
93 while gi < MN_FR { if gen[gi] < 0 { valid = 0 } if gen[gi] >= MN_K { valid = 0 } gi = gi + 1 }
94 if valid == 0 { t4 = 0 }
95 // produces motion: decoded successive poses differ somewhere
96 var moves: i64 = 0
97 gi = 0
98 while gi < MN_FR - 1 {
99 if mn_dist(cb, gen[gi] * MN_D, cb, gen[gi+1] * MN_D) > 0 { moves = moves + 1 }
100 gi = gi + 1
101 }
102 if moves < 2 { t4 = 0 }
103 // NOVEL: not byte-identical to any single training move's token sequence
104 var novel: i64 = 1
105 var m: i64 = 0
106 while m < MN_MOVES {
107 var same: i64 = 1
108 var ff: i64 = 0
109 while ff < MN_FR { if gen[ff] != tokens[m * MN_FR + ff] { same = 0; ff = MN_FR } else { ff = ff + 1 } }
110 if same == 1 { novel = 0 }
111 m = m + 1
112 }
113 if novel == 0 { t4 = 0 }
114 if t4 == 1 { hw("T4 PASS GENERATED novel motion (valid tokens, "); pn(moves); hw(" transitions move, not identical to any training move)\n" as *u8) }
115 else { fails = fails + 1; hw("T4 FAIL valid="); pn(valid); hw(" moves="); pn(moves); hw(" novel="); pn(novel); hw("\n" as *u8) }
116
117 // render the GENERATED motion -> APNG (the payoff: a dance the model composed)
118 let fb: *i64 = sys_mmap(GW * GH * 8) as *i64
119 let zb: *i64 = sys_mmap(GW * GH * 8) as *i64
120 let apbuf: *u8 = sys_mmap(8 * 1024 * 1024)
121 let seq: *i64 = sys_mmap(8) as *i64; seq[0] = 0
122 let vec: *i64 = sys_mmap(MN_D * 8) as *i64
123 var o: i64 = apng_open(apbuf, GW, GH, MN_FR)
124 var gf: i64 = 0
125 while gf < MN_FR {
126 let tk: i64 = gen[gf]
127 var d: i64 = 0
128 while d < MN_D { vec[d] = cb[tk * MN_D + d]; d = d + 1 }
129 pose_apply(sk, vec)
130 sk_update(sk)
131 rig_draw(sk, fb, zb, GW, GH, md_deg(15), 86)
132 var first: i64 = 0
133 if gf == 0 { first = 1 }
134 o = apng_frame(apbuf, o, seq, fb, GW, GH, 8, first)
135 gf = gf + 1
136 }
137 o = apng_close(apbuf, o)
138 let fd: i64 = sys_openat_wr("knowledge/synth_neuralmove.png" as *u8, 0x1a4)
139 if fd >= 0 { sys_write(fd, apbuf, o); sys_close(fd) }
140 hw(" wrote knowledge/synth_neuralmove.png bytes=" as *u8); pn(o); hw("\n" as *u8)
141 if find4(apbuf, o, 97, 99, 84, 76) < 0 { fails = fails + 1; hw(" (APNG invalid)\n" as *u8) }
142
143 if fails == 0 { hw("MOTION-NEURAL-GATE 4/4 GREEN -- LEARNED VQ codebook + generative token-LM composes novel motion, sovereign (no LLM load)\n" as *u8); sys_exit(0); return 0 }
144 hw("MOTION-NEURAL-GATE RED fails=" as *u8); pn(fails); hw("\n" as *u8)
145 sys_exit(1); return 1
146}