code wiki / _hdl_build / nx_intfp_dpo_gradcheck_gate.nx
nx_intfp_dpo_gradcheck_gate.nx
buildroot/runtime/_hdl_build/nx_intfp_dpo_gradcheck_gate.nx
about
nx_intfp_dpo_gradcheck_gate.nx -- DPO preference alignment (the RLHF/DPO axis) in Q20 INTEGER, gradchecked, no
float. Uses the just-built fp_log (softplus) + fp_exp (sigmoid). DPO math is clean in integer: for the SAME x the
logsumexp cancels, so margin = beta*[(l_c - l_r) - (lref_c - lref_r)] (pure logit differences). Loss =
softplus(-margin) = log(1+exp(-margin)); dL/dmargin = -sigmoid(-margin). Policy W trainable, reference Wref
FROZEN. Gradcheck the policy W. license_tier: ORIGINAL
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 12 | const S: i64 = 1048576 |
| 13 | const T: i64 = 3 // preference pairs |
| 14 | const DM: i64 = 4 |
| 15 | const V: i64 = 5 // vocab |
| 16 | const BETA: i64 = 1048576 // beta = 1.0 |
functions
| 8 | func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 9 | func wn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(24); var k: i64=0; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let o: *u8=sys_mmap(24); var q: i64=k-1; var i: i64=0; while q>=0{o[i]=t[q];i=i+1;q=q-1} sys_write(1,o,i); return 0 } |
| 10 | func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v } |
| 18 | func fp_exp(xq: i64) -> i64 { let y: i64=(xq*1512776)/S; var yi: i64=0; if y>=0 { yi=y/S } else { yi=0-(((0-y)+S-1)/S) } let yf: i64=y-yi*S; var p: i64=10085; p=58197+(p*yf)/S; p=251882+(p*yf)/S; p=726817+(p*yf)/S; p=S+(p*yf)/S; if yi>=0 { if yi>=31 { return 2000000000 } return p*(1<<yi) } let k: i64=0-yi; if k>=31 { return 0 } return p/(1<<k) } |
| 20 | func fp_log(xq: i64) -> i64 { if xq<=0 { return 0-2000000000 } var e: i64=0; var t: i64=xq; while t>=2*S { t=t/2; e=e+1 } while t<S { t=t*2; e=e-1 } let s: i64=((t-S)*S)/(t+S); let s2: i64=(s*s)/S; let poly: i64=S+s2/3+((s2*s2)/S)/5; let logm: i64=(2*s*poly)/S; return e*726817+logm } called by 1: softplus |
| 21 | func sigmoid(z: i64) -> i64 { let e: i64=fp_exp(0-z); return (S*S)/(S+e) } |
| 23 | func softplus(z: i64) -> i64 { var mx: i64=0; if z>0 { mx=z } let e: i64=fp_exp(0-iabs(z)); return mx + fp_log(S+e) } |
| 26 | func dpo_loss(P: *i64) -> i64 |
| 43 | func dpo_bwd(P: *i64, dW: *i64) -> i64 |
| 57 | func gcheck(name: *u8, P: *i64, Wt: *i64, dW: *i64, ncell: i64) -> i64 |
| 74 | func main() -> i64 |