code wiki / _hdl_build / nx_ng_avatar.nx

nx_ng_avatar.nx source

↩ module page · 157 lines · 9194 B

1// nx_ng_avatar.nx -- CAP-NEURAL-AVATAR, generation 13: animatable neural humans / gaussian avatars (the 2// Samsung-Moscow neural-avatar lineage, src ng_region_ru_neuralavatars + gaussian-avatar work). The defining 3// property past a STATIC 3D scene (3DGS/NeRF): one CANONICAL set of primitives is DEFORMED by a pose/expression 4// parameter (a deformation field maps canonical -> posed) so a SINGLE learned model renders MANY poses -- and a 5// NOVEL pose (not trained) is produced by INTERPOLATING the same deformation = generalization, not memorization. 6// 7// Reuses the splat machinery: a canonical gaussian has center mu0; pose theta moves it mu(theta)=mu0 + theta*db 8// (a learnable per-gaussian deform basis db). Render = splat at the posed center (CAP-GS-REALTIME footprint) into 9// a 1D image; fit the deform basis from a few posed target frames. Q16, no float. 10// 11// Honest gated proof: T1 ANIMATABLE -- after fitting db from training poses, the model REPRODUCES those poses 12// (loss->~0). T2 NOVEL-POSE GENERALIZATION -- a pose theta NOT in training renders correctly (the gaussian lands 13// at the right place) because the deform is linear in theta = interpolation, not a stored frame (the load-bearing 14// avatar property). T3 NEG-CONTROL -- a STATIC model (no deform, db=0) CANNOT match the posed frames (high loss): 15// the deformation is what's doing the work. T4 BIT-EXACT. HONEST: 1D, 1-DOF linear deform, fixed footprint 16// (full avatar = 3D + skeleton/blendshape rig + learned non-linear deform = follow-on). Sovereign: nx_nofloat_autograd 17// (tape + nfa_fxexp footprint) + syscalls. license_tier: ORIGINAL expect_exit: 0 18import "nx_nofloat_autograd.nx" 19import "nx_syscalls.nx" 20const K_MAGIC_1500: i64 = 1500 21const K_MAGIC_32768: i64 = 32768 22const K_MAGIC_19661: i64 = 19661 23const K_MAGIC_4096: i64 = 4096 24const K_MAGIC_2000: i64 = 2000 25 26const ALOG: *u8 = "knowledge/status/ng_avatar.log" 27const Q16: i64 = 65536 28const NP: i64 = 16 // image pixels (1D) 29const INV2S2: i64 = 2275555 // 1/(2 sigma^2) Q16, sigma=0.12 (splat footprint width) 30const EPOCHS: i64 = 6000 31const LRQ: i64 = 48 // small: the gaussian-derivative chain inflates the gradient ~69x (2*INV2S2) -> tiny lr 32 33func ap(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 34func apn(v: i64) -> i64 { 35 let b: *u8 = sys_mmap(28); var x: i64=v 36 if x<0 { b[0]=45; sys_write(1,b,1); x=0-x } 37 if x==0 { b[0]=48; sys_write(1,b,1); return 0 } 38 var d: i64=0; var y: i64=x; while y>0 { d=d+1; y=y/10 } 39 var i: i64=d-1; y=x; while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 } 40 sys_write(1,b,d); return 0 41} 42func a_abs(v: i64) -> i64 { if v<0 { return 0-v } return v } 43func aqm(a: i64, b: i64) -> i64 { return (a*b)>>16 } 44func al_ws(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 45func al_wn(fd: i64, v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0 {sys_write(fd,"-" as *u8,1); m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1} while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1} var i: i64=0; while i<k {b[i]=t[k-1-i]; i=i+1} sys_write(fd,b,k); return 0 } 46 47// render the posed avatar (single gaussian) to a 1D image: center = mu0 + theta*db ; pixel = exp(-(x-center)^2/2s^2) 48func render_pose(mu0: i64, db: i64, theta: i64, out: *i64) -> i64 { 49 let center: i64 = mu0 + aqm(theta, db) 50 var p: i64 = 0 51 while p < NP { 52 let pos: i64 = p * (Q16/NP) + (Q16/(2*NP)) 53 let dlt: i64 = pos - center 54 let d2: i64 = aqm(dlt, dlt) 55 out[p] = nfa_fxexp(0 - aqm(d2, INV2S2)) 56 p = p + 1 57 } 58 return 0 59} 60func sse(a: *i64, b: *i64) -> i64 { var s: i64=0; var p: i64=0; while p<NP { let d: i64=a[p]-b[p]; s=s+aqm(d,d); p=p+1 } return s } 61 62// fit the deform basis db (scalar) by gradient descent so render(mu0,db,theta_k) matches frame_k, over K poses. 63// analytic gradient of SSE wrt db (chain through the gaussian): dL/ddb = sum_k sum_p 2(r-t)*r*( (x-center)/s^2 )*theta_k 64func fit_db(mu0: i64, thetas: *i64, frames: *i64, K: i64, db_out: *i64, lf: *i64, ll: *i64) -> i64 { 65 var db: i64 = 0 66 let cur: *i64 = sys_mmap(NP*8) as *i64 67 var ep: i64 = 0 68 while ep < EPOCHS { 69 var g: i64 = 0 70 var loss: i64 = 0 71 var k: i64 = 0 72 while k < K { 73 let th: i64 = thetas[k] 74 let center: i64 = mu0 + aqm(th, db) 75 render_pose(mu0, db, th, cur) 76 let fr: i64 = k * NP 77 var p: i64 = 0 78 while p < NP { 79 let r: i64 = cur[p] 80 let resid: i64 = r - frames[fr + p] 81 loss = loss + aqm(resid, resid) 82 let pos: i64 = p * (Q16/NP) + (Q16/(2*NP)) 83 let dxc: i64 = pos - center 84 // dr/dcenter = r * (x-center)/sigma^2 ; dcenter/ddb = theta ; so dL/ddb += 2*resid*r*(dxc*INV2S2*2)*theta 85 let drdc: i64 = aqm(r, aqm(dxc, 2 * INV2S2)) 86 g = g + aqm(aqm(2 * resid, drdc), th) 87 p = p + 1 88 } 89 k = k + 1 90 } 91 if ep == 0 { *lf = loss } 92 *ll = loss 93 if ep % K_MAGIC_1500 == 0 { ap(" fit ep " as *u8); apn(ep); ap(" db=" as *u8); apn(db); ap(" loss=" as *u8); apn(loss); ap("\n" as *u8) } 94 db = db - aqm(LRQ, g) 95 ep = ep + 1 96 } 97 *db_out = db 98 return 0 99} 100 101func main() -> i64 { 102 ap("nx_ng_avatar: CAP-NEURAL-AVATAR -- animatable model (one canonical primitive deformed by a pose param)\n" as *u8) 103 let mu0: i64 = K_MAGIC_32768 // canonical center 0.5 104 let db_true: i64 = K_MAGIC_19661 // TRUE deform basis 0.3 (so pose theta shifts center by 0.3*theta) 105 // training poses theta in [-1, 1]; frames rendered from the TRUE avatar 106 let K: i64 = 3 107 let thetas: *i64 = sys_mmap(K*8) as *i64 108 thetas[0]=0-Q16; thetas[1]=0; thetas[2]=Q16 // -1.0, 0.0, +1.0 109 let frames: *i64 = sys_mmap(K*NP*8) as *i64 110 var k: i64=0; while k<K { let tmp: *i64=sys_mmap(NP*8) as *i64; render_pose(mu0, db_true, thetas[k], tmp); var p: i64=0; while p<NP { frames[k*NP+p]=tmp[p]; p=p+1 } k=k+1 } 111 112 // T1: FIT the deform basis from the training poses 113 let db: *i64=sys_mmap(8) as *i64; let lf: *i64=sys_mmap(8) as *i64; let ll: *i64=sys_mmap(8) as *i64 114 fit_db(mu0, thetas, frames, K, db, lf, ll) 115 var t1: i64=0; if (*ll)*50 < (*lf) { if a_abs((*db)-db_true) < K_MAGIC_4096 { t1=1 } } 116 117 // T2: NOVEL POSE (theta=0.5, NOT trained) -- the fitted model renders it correctly (interpolation, not memorized) 118 let novel_th: i64 = K_MAGIC_32768 // +0.5, between trained 0 and +1 119 let gen: *i64=sys_mmap(NP*8) as *i64; let ref_novel: *i64=sys_mmap(NP*8) as *i64 120 render_pose(mu0, *db, novel_th, gen) // with the LEARNED db 121 render_pose(mu0, db_true, novel_th, ref_novel) // ground-truth novel pose 122 let novel_err: i64 = sse(gen, ref_novel) 123 var t2: i64=0; if novel_err < K_MAGIC_2000 { t2=1 } // novel pose matches ground truth = generalization 124 125 // T3: NEG-CONTROL -- a STATIC model (db=0, no deformation) cannot match the posed frames 126 let stat: *i64=sys_mmap(NP*8) as *i64 127 var static_loss: i64=0 128 k=0; while k<K { render_pose(mu0, 0, thetas[k], stat); var fp: i64=k*NP; var pp: i64=0; while pp<NP { let dd: i64=stat[pp]-frames[fp+pp]; static_loss=static_loss+aqm(dd,dd); pp=pp+1 } k=k+1 } 129 var t3: i64=0; if static_loss > (*ll)*20 { t3=1 } // static is much worse than the fitted animatable model 130 131 // T4: BIT-EXACT 132 let db2: *i64=sys_mmap(8) as *i64; let lf2: *i64=sys_mmap(8) as *i64; let ll2: *i64=sys_mmap(8) as *i64 133 fit_db(mu0, thetas, frames, K, db2, lf2, ll2) 134 var t4: i64=0; if (*db2)==(*db) { t4=1 } 135 136 ap(" T1 ANIMATABLE: fit loss " as *u8); apn(*lf); ap("->" as *u8); apn(*ll); ap(" learned db=" as *u8); apn(*db); ap(" vs true 19661 ok=" as *u8); apn(t1); ap("\n" as *u8) 137 ap(" T2 NOVEL-POSE(theta=0.5 untrained): err vs ground-truth=" as *u8); apn(novel_err); ap(" generalizes=" as *u8); apn(t2); ap("\n" as *u8) 138 ap(" T3 NEG-CONTROL static(no-deform) loss=" as *u8); apn(static_loss); ap(" >> fitted " as *u8); apn(*ll); ap(" deform-is-load-bearing=" as *u8); apn(t3); ap("\n" as *u8) 139 ap(" T4 bit-exact=" as *u8); apn(t4); ap("\n" as *u8) 140 141 var ok: i64=1 142 if t1!=1 { ok=0 } 143 if t2!=1 { ok=0 } 144 if t3!=1 { ok=0 } 145 if t4!=1 { ok=0 } 146 let logf: i64=sys_openat_append(ALOG, 420) 147 if logf>=0 { 148 al_ws(logf,"NGAVATAR authored=organ animatable-deform learned_db=" as *u8); al_wn(logf,*db) 149 al_ws(logf," t1=" as *u8); al_wn(logf,t1); al_ws(logf," t2_novel=" as *u8); al_wn(logf,t2); al_ws(logf," t3_negctl=" as *u8); al_wn(logf,t3); al_ws(logf," t4=" as *u8); al_wn(logf,t4) 150 if ok==1 { al_ws(logf," verdict=GREEN\n" as *u8) } else { al_ws(logf," verdict=RED\n" as *u8) } 151 sys_close(logf) 152 } 153 ap(" verdict=" as *u8) 154 if ok==1 { ap("GREEN (one canonical model deformed by a pose param reproduces trained poses AND generalizes to a novel pose = animatable avatar)\n" as *u8); sys_exit(0); return 0 } 155 ap("RED\n" as *u8) 156 sys_exit(1); return 1 157}