nx_nn.nx source
↩ module page · 28 lines · 1697 B
1// nx_nn.nx -- sovereign FIXED-POINT NEURAL INFERENCE substrate, the foundation under the neural-audio moonshot. The
2// research established that neural audio is phone-CPU-viable BECAUSE it runs INT-quantised (DeepFilterNet RTF 0.04, Lyra
3// 0.57ms/frame on a Pixel 6) -- not float, not GPU. So the sovereign path is integer matmul + bias + activation in a
4// Q-scale, which is exactly what a learned PLC / RVQ-codebook net needs for its forward pass. This rung is the inference
5// machinery (a linear layer + ReLU/tanh); the LEARNED weights come from the training pipeline (the next moonshot rung).
6// Pure integer, no FPU, no syscalls -> phone-friendly + wasm-friendly. license_tier: ORIGINAL
7
8// linear (dense) layer: out[O] = ((W[O x I] . in[I]) >> Qshift) + b[O]. W,in are Q-scale fixed-point; >>Qshift descales
9// the products back to the Q-scale; b is in the output Q-scale. The core op of every MLP/GRU/codebook-predictor.
10func nn_linear(inp: *i64, I: i64, W: *i64, b: *i64, O: i64, Qshift: i64, out: *i64) -> i64 {
11 var o: i64 = 0
12 while o < O {
13 var acc: i64 = 0
14 var i: i64 = 0
15 while i < I { acc = acc + W[o*I + i] * inp[i]; i = i + 1 }
16 out[o] = (acc >> Qshift) + b[o]
17 o = o + 1
18 }
19 return 0
20}
21// ReLU activation in place
22func nn_relu(x: *i64, n: i64) -> i64 { var i: i64 = 0; while i < n { if x[i] < 0 { x[i] = 0 } i = i + 1 } return 0 }
23// saturating tanh-ish clamp to [-1,1] in the given Q-scale (a cheap bounded activation for the codebook predictor)
24func nn_clamp(x: *i64, n: i64, one: i64) -> i64 {
25 var i: i64 = 0
26 while i < n { if x[i] > one { x[i] = one } if x[i] < 0 - one { x[i] = 0 - one } i = i + 1 }
27 return 0
28}