code wiki / _hdl_build / nx_intfp_flashattn_gate.nx

nx_intfp_flashattn_gate.nx

buildroot/runtime/_hdl_build/nx_intfp_flashattn_gate.nx

4865 B67 linesdepth 2pulls 2 transitivereach 0 importersview sourcekind gate/prooftopic intfp
docsdependenciesstructsconstsfunctions

about

nx_intfp_flashattn_gate.nx -- FLASH-ATTENTION (online-softmax, O(T) memory, no T×T matrix) in Q20 INTEGER. modelwright marks flash-attn ABSENT ("ours is a plain score matrix"). Flash processes keys one at a time keeping a running max m, running denominator l, and running output o, rescaling by exp(m_old-m_new) when the max grows -- mathematically identical to full softmax attention but streaming (the key to long-context scale). Here: compute attention BOTH ways (full materialized + flash online) and verify they MATCH. Uses proven fp_exp. license_tier: ORIGINAL

dependencies 1 imports · 0 importers

nx_syscalls.nx nx_intfp_flashattn_gate.nx

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

main w sys_write sys_mmap attn_full score sys_mmap ↻ fp_exp attn_flash sys_mmap ↻ score ↻ fp_exp ↻ iabs wn sys_write ↻ sys_mmap ↻

structs

none

consts

12const S: i64 = 1048576
13const T: i64 = 6
14const DM: i64 = 4
15const SCALE: i64 = 524288 // 1/sqrt(4)

functions

8func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
called by 1: main calls 1: sys_write
9func 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 }
called by 1: main calls 2: sys_writesys_mmap
10func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v }
called by 1: main
17func 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) }
called by 2: attn_fullattn_flash
19func score(Q: *i64, K: *i64, t: i64, s: i64) -> i64 { var dot: i64=0; var i: i64=0; while i<DM { dot=dot+Q[t*DM+i]*K[s*DM+i]; i=i+1 } return ((dot/S)*SCALE)/S }
called by 2: attn_fullattn_flash
22func attn_full(Q: *i64, K: *i64, V: *i64, t: i64, O: *i64) -> i64
called by 1: main calls 3: scoresys_mmapfp_exp
29func attn_flash(Q: *i64, K: *i64, V: *i64, t: i64, O: *i64) -> i64
called by 1: main calls 3: sys_mmapscorefp_exp
47func main() -> i64