code wiki / _hdl_build / nx_nofloat_k3block.nx
nx_nofloat_k3block.nx source
↩ module page · 68 lines · 3066 B
1// nx_nofloat_k3block.nx -- LOGICALLY-INTEGRATED K3-style transformer BLOCK in no-float (operator 2026-07-19
2// "logically integrated"). Proves the scattered K3 primitives COMPOSE into one deterministic block, not just
3// exist as separate gates. Minimal K3 block = the KDA/linear-attention sublayer + a SwiGLU-ish FFN sublayer,
4// both with residual connections (the DeepSeek-V3/Moonlight skeleton the real K3 uses):
5// attn_in = x ; q,k,v = x.Wq, x.Wk, x.Wv ; a = linattn(q,k,v) ; h = x + a.Wo (KDA sublayer + residual)
6// f = relu(h.W1).W2 ; out = h + f (FFN sublayer + residual)
7// All PURE INTEGER Q16 (matvec accumulate-then-shift + the proven linear-attn kernel) = bit-exact
8// deterministic K3-style block. Composes nx_nofloat_linattn (the KDA foundation). Grounded arXiv 2510.26692.
9// license_tier: ORIGINAL No hw writes (Rule 26).
10import "nx_nofloat_linattn.nx"
11import "nx_syscalls.nx"
12
13const K3_QBITS: i64 = 16
14const K3_DMAX: i64 = 16
15const K3_FFMAX: i64 = 32
16
17// Y[T x n] = X[T x d] . W[d x n] (Q16, accumulate i64 >>16)
18func k3_matmul(x: *i64, w: *i64, y: *i64, t: i64, d: i64, n: i64) -> i64 {
19 var ti: i64 = 0
20 while ti < t {
21 var j: i64 = 0
22 while j < n {
23 var acc: i64 = 0
24 var kk: i64 = 0
25 while kk < d { acc = acc + x[ti*d+kk] * w[kk*n+j]; kk = kk + 1 }
26 y[ti*n+j] = acc >> K3_QBITS
27 j = j + 1
28 }
29 ti = ti + 1
30 }
31 return 0
32}
33// out = a + b (T x d), elementwise
34func k3_add(a: *i64, b: *i64, out: *i64, t: i64, d: i64) -> i64 {
35 var i: i64 = 0
36 while i < t*d { out[i] = a[i] + b[i]; i = i + 1 }
37 return 0
38}
39// ReLU in place (T x d)
40func k3_relu(a: *i64, t: i64, d: i64) -> i64 {
41 var i: i64 = 0
42 while i < t*d { if a[i] < 0 { a[i] = 0 } i = i + 1 }
43 return 0
44}
45// one K3-style block. x: T x d (Q16). weights: Wq,Wk,Wv,Wo [d x d], W1 [d x dff], W2 [dff x d]. out: T x d.
46func k3_block(x: *i64, wq: *i64, wk: *i64, wv: *i64, wo: *i64, w1: *i64, w2: *i64, out: *i64, t: i64, d: i64, dff: i64) -> i64 {
47 let q: *i64 = sys_mmap(K3_DMAX*K3_DMAX*8) as *i64
48 let k: *i64 = sys_mmap(K3_DMAX*K3_DMAX*8) as *i64
49 let v: *i64 = sys_mmap(K3_DMAX*K3_DMAX*8) as *i64
50 let a: *i64 = sys_mmap(K3_DMAX*K3_DMAX*8) as *i64
51 let ao: *i64 = sys_mmap(K3_DMAX*K3_DMAX*8) as *i64
52 let h: *i64 = sys_mmap(K3_DMAX*K3_DMAX*8) as *i64
53 let f1: *i64 = sys_mmap(K3_DMAX*K3_FFMAX*8) as *i64
54 let f2: *i64 = sys_mmap(K3_DMAX*K3_DMAX*8) as *i64
55 // KDA sublayer
56 k3_matmul(x, wq, q, t, d, d)
57 k3_matmul(x, wk, k, t, d, d)
58 k3_matmul(x, wv, v, t, d, d)
59 la_forward(q, k, v, a, t, d) // linear attention (KDA kernel)
60 k3_matmul(a, wo, ao, t, d, d) // output projection
61 k3_add(x, ao, h, t, d) // residual: h = x + attn
62 // FFN sublayer
63 k3_matmul(h, w1, f1, t, d, dff)
64 k3_relu(f1, t, dff)
65 k3_matmul(f1, w2, f2, t, dff, d)
66 k3_add(h, f2, out, t, d) // residual: out = h + ffn
67 return 0
68}