~/bend-docscommunity

nn.bend source

nn.bend on the hub · documented module

# Neural-network building blocks on Bend arrays (F32 as bits in Array<U32>): int8 quantization helpers, activations,# and data-parallel kernels in the form of bang.bend.import Baseimport ./bang.bend as Bimport ./lanes.bend as L# an int32 in a U32 as F32 (round to nearest, as torch's int32 .float())def i2f(+x: U32) -> F32:  Bool.pick(F32, U32.is_lt(x, 2147483648), U32.to_f32(x), F32.neg(U32.to_f32((0 - x : U32))))# round half to even, as torch.rounddef rne3(+y: F32, +f: F32) -> F32:  +d = (y - f : F32)  Bool.pick(F32, F32.is_gt(d, 0.5), (f + 1.0 : F32),    Bool.pick(F32, F32.is_lt(d, 0.5), f, Bool.pick(F32, F32.is_eq(F32.mod(f, 2.0), 0.0), f, (f + 1.0 : F32))))def rne(+y: F32) -> F32:  rne3(y, F32.floor(y))# int8 byte (0..255) of v / scale, rounded half to even and clamped to -128..127def q8(+v: F32, +s: F32) -> U32:  U32.and(F32.to_u32((F32.clamp(rne((v / s : F32)), F32.neg(128.0), 127.0) + 256.0 : F32)), 255)# a GEMM output cell: an int32 times row scale times weight scale, or (isf) an F32 value read as is@unsafedef dq(isf: Bool, +cv: U32, +xsv: F32, +wv: U32) -> F32:  match isf:    case True{}:      B.fv(cv)    case False{}:      (i2f(cv) * (xsv * B.fv(wv) : F32) : F32)def silu(+g: F32) -> F32:  (g / (1.0 + F32.exp(F32.neg(g)) : F32) : F32)def wr_if(ok: Bool, a: Array<U32>, +i: U32, +v: U32) -> Array<U32>:  match ok:    case True{}:      Array.set(U32, a, i, v)    case False{}:      a# ---- small dense layer: out[i] = b[i] + sum_{j < k} w[i * k + j] * x[j]; arrays w, b, x, out; item = output cell ----type ELin is Data:  ELin{k: U32}@unsafedef lin_dot(cnt: Nat, +wb: U32, +j: U32, acc: F32, rw: Array<U32> & U32, rx: Array<U32> & U32) -> Array<U32> & Array<U32> & F32:  match cnt rw rx:    case 0n Tuple{w, a} Tuple{x, c}:      (w, (x, acc))    case 1n+q Tuple{w, +a} Tuple{x, +c}:      +n = (j + 1 : U32)      lin_dot(q, wb, n, (acc + (B.fv(a) * B.fv(c) : F32) : F32), Array.get(U32, w, (wb + n : U32)), Array.get(U32, x, n))@unsafedef lin_fin(+i: U32, o: Array<U32>, x4: Array<U32>, x5: Array<U32>, rb: Array<U32> & U32, rd: Array<U32> & Array<U32> & F32) -> B.AR:  match rb rd:    case Tuple{b, +bv} Tuple{w, Tuple{x, acc}}:      B.AR{w, b, x, Array.set(U32, o, i, B.uv((acc + B.fv(bv) : F32))), x4, x5}@unsafedef lin_item(+i: U32, +e: ELin, ar: B.AR) -> B.AR:  match e ar:    case ELin{+k} B.AR{w, b, x, o, x4, x5}:      +wb = L.cell(i, k)      lin_fin(i, o, x4, x5, Array.get(U32, b, i), lin_dot(U32.to_nat(k), wb, 0, 0.0, Array.get(U32, w, wb), Array.get(U32, x, 0)))# ---- Row segments. A row of d = seg * nseg cells is nseg segments of seg cells; item i is row i / nseg, segment# i % nseg (lanes.bend), so a kernel over rows x nseg items keeps all lanes busy, and item i owns cells i * seg ..# i * seg + seg - 1. Row reductions go through per-segment partials (ps). ----@unsafedef sq_loop(cnt: Nat, +base: U32, +j: U32, acc: F32, rx: Array<U32> & U32) -> Array<U32> & F32:  match cnt rx:    case 0n Tuple{x, v}:      (x, acc)    case 1n+q Tuple{x, +v}:      +f = B.fv(v)      +k = (j + 1 : U32)      sq_loop(q, base, k, (acc + (f * f : F32) : F32), Array.get(U32, x, (base + k : U32)))# sum of cnt cells from base@unsafedef sum_loop(cnt: Nat, +base: U32, +j: U32, acc: F32, rx: Array<U32> & U32) -> Array<U32> & F32:  match cnt rx:    case 0n Tuple{x, v}:      (x, acc)    case 1n+q Tuple{x, +v}:      +k = (j + 1 : U32)      sum_loop(q, base, k, (acc + B.fv(v) : F32), Array.get(U32, x, (base + k : U32)))# largest of cnt cells from base@unsafedef max_loop(cnt: Nat, +base: U32, +j: U32, acc: F32, rx: Array<U32> & U32) -> Array<U32> & F32:  match cnt rx:    case 0n Tuple{x, v}:      (x, acc)    case 1n+q Tuple{x, +v}:      +k = (j + 1 : U32)      max_loop(q, base, k, F32.max(acc, B.fv(v)), Array.get(U32, x, (base + k : U32)))# largest |x| of cnt cells from base@unsafedef amax_loop(cnt: Nat, +base: U32, +j: U32, acc: F32, rx: Array<U32> & U32) -> Array<U32> & F32:  match cnt rx:    case 0n Tuple{x, v}:      (x, acc)    case 1n+q Tuple{x, +v}:      +k = (j + 1 : U32)      amax_loop(q, base, k, F32.max(acc, F32.abs(B.fv(v))), Array.get(U32, x, (base + k : U32)))# ---- ps[i] = sum of squares of segment i; arrays x, ps ----type ESeg is Data:  ESeg{seg: U32, nseg: U32}@unsafedef ss_fin(+i: U32, ps: Array<U32>, x2: Array<U32>, x3: Array<U32>, x4: Array<U32>, x5: Array<U32>, r: Array<U32> & F32) -> B.AR:  match r:    case Tuple{x, s}:      B.AR{x, Array.set(U32, ps, i, B.uv(s)), x2, x3, x4, x5}@unsafedef ss_item(+i: U32, +e: ESeg, ar: B.AR) -> B.AR:  match e ar:    case ESeg{+seg, +nseg} B.AR{x, ps, x2, x3, x4, x5}:      +base = L.cell(i, seg)      ss_fin(i, ps, x2, x3, x4, x5, sq_loop(U32.to_nat(seg), base, 0, 0.0, Array.get(U32, x, base)))# ---- RMSNorm with modulation over row segments:# h[r, c] = x[r, c] * inv(r) * p[woff + c] * (1 + m[soff + t(r) + c]) + m[hoff + t(r) + c],# inv(r) = 1 / sqrt(sum_k ps[r * nseg + k] / d + eps), t(r) = ts for rows r < tt, else 0.# arrays x, p (norm weight), m (scale and shift), h, ps; item = r * nseg + segment ----type ENs is Data:  ENs{seg: U32, nseg: U32, tt: U32, eps: F32, woff: U32, soff: U32, hoff: U32, ts: U32}@unsafedef ns_loop(cnt: Nat, +base: U32, +wb: U32, +sb: U32, +hb: U32, +j: U32, +inv: F32, rx: Array<U32> & U32, rw: Array<U32> & U32, gm: B.G2, h: Array<U32>, ps: Array<U32>) -> B.AR:  match cnt rx rw gm:    case 0n Tuple{x, a} Tuple{p, b} B.G2{m, c, d}:      B.AR{x, p, m, h, ps, B.one(Unit{})}    case 1n+q Tuple{x, +xv} Tuple{p, +wv} B.G2{m, +scv, +shv}:      +val = ((((B.fv(xv) * inv : F32) * B.fv(wv) : F32) * (1.0 + B.fv(scv) : F32) : F32) + B.fv(shv) : F32)      +k = (j + 1 : U32)      ns_loop(q, base, wb, sb, hb, k, inv, Array.get(U32, x, (base + k : U32)), Array.get(U32, p, (wb + k : U32)), B.g2(m, (sb + k : U32), (hb + k : U32)), Array.set(U32, h, (base + j : U32), B.uv(val)), ps)@unsafedef ns_mid(+i: U32, +e: ENs, x: Array<U32>, p: Array<U32>, m: Array<U32>, h: Array<U32>, rs: Array<U32> & F32) -> B.AR:  match e rs:    case ENs{+seg, +nseg, +tt, +eps, +woff, +soff, +hoff, +ts} Tuple{ps, +ss}:      +inv = (1.0 / F32.sqrt(((ss / U32.to_f32(L.width(seg, nseg)) : F32) + eps : F32)) : F32)      +c0 = L.col(i, seg, nseg)      +t = Bool.pick(U32, U32.is_lt(L.row(i, nseg), tt), ts, 0)      +base = L.cell(i, seg)      +wb = (woff + c0 : U32)      +sb = ((soff + t : U32) + c0 : U32)      +hb = ((hoff + t : U32) + c0 : U32)      ns_loop(U32.to_nat(seg), base, wb, sb, hb, 0, inv, Array.get(U32, x, base), Array.get(U32, p, wb), B.g2(m, sb, hb), h, ps)@unsafedef ns_item(+i: U32, +e: ENs, ar: B.AR) -> B.AR:  match e ar:    case ENs{+seg, +nseg, +tt, +eps, +woff, +soff, +hoff, +ts} B.AR{x, p, m, h, ps, x5}:      +pb = L.first(i, nseg)      ns_mid(i, e, x, p, m, h, sum_loop(U32.to_nat(nseg), pb, 0, 0.0, Array.get(U32, ps, pb)))# ---- gated residual over row segments, with the new rows' partial sums of squares (for the next RMSNorm):# x[r, c] += m[goff + t(r) + c] * dq(cc[r, c], xs[r], p[woff + c]); ps[i] = sum over the segment of the new x^2.# arrays x, cc (GEMM output, d a row), p (weight scales), xs, m (gate), ps; item = r * nseg + segment ----type ERz is Data:  ERz{seg: U32, nseg: U32, tt: U32, isf: Bool, woff: U32, goff: U32, ts: U32}@unsafedef rz_loop(cnt: Nat, +isf: Bool, +base: U32, +wb: U32, +gb: U32, +j: U32, +xsv: F32, acc: F32, rx: Array<U32> & U32, rc: Array<U32> & U32, rw: Array<U32> & U32, rg: Array<U32> & U32) -> B.AR & F32:  match cnt rx rc rw rg:    case 0n Tuple{x, a} Tuple{c, b} Tuple{p, d} Tuple{m, f}:      (B.AR{x, c, p, B.one(Unit{}), m, B.one(Unit{})}, acc)    case 1n+q Tuple{x, +xv} Tuple{c, +cv} Tuple{p, +wv} Tuple{m, +gv}:      +val = (B.fv(xv) + (dq(isf, cv, xsv, wv) * B.fv(gv) : F32) : F32)      +k = (j + 1 : U32)      rz_loop(q, isf, base, wb, gb, k, xsv, (acc + (val * val : F32) : F32), Array.get(U32, Array.set(U32, x, (base + j : U32), B.uv(val)), (base + k : U32)), Array.get(U32, c, (base + k : U32)), Array.get(U32, p, (wb + k : U32)), Array.get(U32, m, (gb + k : U32)))@unsafedef rz_fin(+i: U32, xs: Array<U32>, ps: Array<U32>, r: B.AR & F32) -> B.AR:  match r:    case Tuple{B.AR{x, c, p, z3, m, z5}, s}:      B.AR{x, c, p, xs, m, Array.set(U32, ps, i, B.uv(s))}@unsafedef rz_mid(+i: U32, +e: ERz, x: Array<U32>, c: Array<U32>, p: Array<U32>, m: Array<U32>, ps: Array<U32>, rx: Array<U32> & U32) -> B.AR:  match e rx:    case ERz{+seg, +nseg, +tt, +isf, +woff, +goff, +ts} Tuple{xs, +xv}:      +c0 = L.col(i, seg, nseg)      +base = L.cell(i, seg)      +wb = (woff + c0 : U32)      +gb = ((goff + Bool.pick(U32, U32.is_lt(L.row(i, nseg), tt), ts, 0) : U32) + c0 : U32)      rz_fin(i, xs, ps, rz_loop(U32.to_nat(seg), isf, base, wb, gb, 0, B.fv(xv), 0.0, Array.get(U32, x, base), Array.get(U32, c, base), Array.get(U32, p, wb), Array.get(U32, m, gb)))@unsafedef rz_item(+i: U32, +e: ERz, ar: B.AR) -> B.AR:  match e ar:    case ERz{+seg, +nseg, +tt, +isf, +woff, +goff, +ts} B.AR{x, c, p, xs, m, ps}:      rz_mid(i, e, x, c, p, m, ps, Array.get(U32, xs, L.row(i, nseg)))# ---- per-row int8 quantization over row segments, in two bangs:# am_item: pam[i] = largest |r| in segment i (arrays r, pam);# qs_item: row scale s = max(largest pam of the row / 127, 1e-30), segment i of the row to int8 (round half to even),# packed 4 a word little-endian into q8 (seg / 4 words a segment; seg must be a multiple of 4), s into xs[row]# (arrays r, q8, xs, pam). ----@unsafedef am_fin(+i: U32, pam: Array<U32>, x2: Array<U32>, x3: Array<U32>, x4: Array<U32>, x5: Array<U32>, r: Array<U32> & F32) -> B.AR:  match r:    case Tuple{x, a}:      B.AR{x, Array.set(U32, pam, i, B.uv(a)), x2, x3, x4, x5}@unsafedef am_item(+i: U32, +e: ESeg, ar: B.AR) -> B.AR:  match e ar:    case ESeg{+seg, +nseg} B.AR{x, pam, x2, x3, x4, x5}:      +base = L.cell(i, seg)      am_fin(i, pam, x2, x3, x4, x5, amax_loop(U32.to_nat(seg), base, 0, 0.0, Array.get(U32, x, base)))@unsafedef qs_loop(cnt: Nat, +base: U32, +qbase: U32, +j: U32, +s: F32, +word: U32, +mul: U32, rx: Array<U32> & U32, q: Array<U32>) -> Array<U32> & Array<U32>:  match cnt rx:    case 0n Tuple{x, v}:      (x, q)    case 1n+q2 Tuple{x, +v}:      +w2 = (word + (q8(B.fv(v), s) * mul : U32) : U32)      +last = U32.is_eq(U32.and(j, 3), 3)      qs_loop(q2, base, qbase, (j + 1 : U32), s, Bool.pick(U32, last, 0, w2), Bool.pick(U32, last, 1, (mul * 256 : U32)), Array.get(U32, x, (base + (j + 1 : U32) : U32)), wr_if(last, q, (qbase + (j / 4 : U32) : U32), w2))@unsafedef qs_fin(+i: U32, +nseg: U32, +s: F32, xs: Array<U32>, pam: Array<U32>, rq: Array<U32> & Array<U32>) -> B.AR:  match rq:    case Tuple{x, q}:      B.AR{x, q, wr_if(L.leads(i, nseg), xs, L.row(i, nseg), B.uv(s)), pam, B.one(Unit{}), B.one(Unit{})}@unsafedef qs_mid(+i: U32, +e: ESeg, x: Array<U32>, q: Array<U32>, xs: Array<U32>, ra: Array<U32> & F32) -> B.AR:  match e ra:    case ESeg{+seg, +nseg} Tuple{pam, +am}:      +s = F32.max((am / 127.0 : F32), 0.000000000000000000000000000001)      +base = L.cell(i, seg)      +qbase = L.cell(i, (seg / 4 : U32))      qs_fin(i, nseg, s, xs, pam, qs_loop(U32.to_nat(seg), base, qbase, 0, s, 0, 1, Array.get(U32, x, base), q))@unsafedef qs_item(+i: U32, +e: ESeg, ar: B.AR) -> B.AR:  match e ar:    case ESeg{+seg, +nseg} B.AR{x, q, xs, pam, x4, x5}:      +pb = L.first(i, nseg)      qs_mid(i, e, x, q, xs, max_loop(U32.to_nat(nseg), pb, 0, 0.0, Array.get(U32, pam, pb)))# ---- SwiGLU over row segments: a[r, c] = silu(dq(cc[r, c])) * dq(cc[r, f + c]) with f = seg * nseg, cc 2f a row,# weight scales at p[woff + c] (gate) and p[woff + f + c] (up); arrays cc, p, xs, a; item = r * nseg + segment ----type ESg is Data:  ESg{seg: U32, nseg: U32, isf: Bool, woff: U32}@unsafedef sg_loop(cnt: Nat, +isf: Bool, +cb: U32, +wb: U32, +ab: U32, +f: U32, +j: U32, +xsv: F32, gc: B.G2, gw: B.G2, a: Array<U32>) -> Array<U32> & Array<U32> & Array<U32>:  match cnt gc gw:    case 0n B.G2{c, x, y} B.G2{p, u, v}:      (c, (p, a))    case 1n+q B.G2{c, +cg, +cu} B.G2{p, +wg, +wu}:      +k = (j + 1 : U32)      sg_loop(q, isf, cb, wb, ab, f, k, xsv, B.g2(c, (cb + k : U32), ((cb + f : U32) + k : U32)), B.g2(p, (wb + k : U32), ((wb + f : U32) + k : U32)), Array.set(U32, a, (ab + j : U32), B.uv((silu(dq(isf, cg, xsv, wg)) * dq(isf, cu, xsv, wu) : F32))))@unsafedef sg_fin(xs: Array<U32>, r: Array<U32> & Array<U32> & Array<U32>) -> B.AR:  match r:    case Tuple{c, Tuple{p, a}}:      B.AR{c, p, xs, a, B.one(Unit{}), B.one(Unit{})}@unsafedef sg_mid(+i: U32, +e: ESg, c: Array<U32>, p: Array<U32>, a: Array<U32>, rx: Array<U32> & U32) -> B.AR:  match e rx:    case ESg{+seg, +nseg, +isf, +woff} Tuple{xs, +xv}:      +f = L.width(seg, nseg)      +c0 = L.col(i, seg, nseg)      +cb = ((L.row(i, nseg) * (2 * f : U32) : U32) + c0 : U32)      +wb = (woff + c0 : U32)      sg_fin(xs, sg_loop(U32.to_nat(seg), isf, cb, wb, L.cell(i, seg), f, 0, B.fv(xv), B.g2(c, cb, (cb + f : U32)), B.g2(p, wb, (wb + f : U32)), a))@unsafedef sg_item(+i: U32, +e: ESg, ar: B.AR) -> B.AR:  match e ar:    case ESg{+seg, +nseg, +isf, +woff} B.AR{c, p, xs, a, x4, x5}:      sg_mid(i, e, c, p, a, Array.get(U32, xs, L.row(i, nseg)))# ---- softmax of each n-wide row in place, normalized: pass 1 keeps a running max m and sum l of exp(s * scale - m)# (rescaling l when m grows), pass 2 writes exp(s * scale - m) / l. arrays sc; item = row ----type ESn is Data:  ESn{n: U32, scale: F32}@unsafedef on_loop(cnt: Nat, +base: U32, +j: U32, +sc: F32, +m: F32, +l: F32, rx: Array<U32> & U32) -> Array<U32> & F32 & F32:  match cnt rx:    case 0n Tuple{x, v}:      (x, (m, l))    case 1n+q Tuple{x, +v}:      +z = (B.fv(v) * sc : F32)      +big = F32.is_gt(z, m)      +m2 = Bool.pick(F32, big, z, m)      +l2 = Bool.pick(F32, big, ((l * F32.exp((m - z : F32)) : F32) + 1.0 : F32), (l + F32.exp((z - m : F32)) : F32))      +k = (j + 1 : U32)      on_loop(q, base, k, sc, m2, l2, Array.get(U32, x, (base + k : U32)))@unsafedef nw_loop(cnt: Nat, +base: U32, +j: U32, +sc: F32, +m: F32, +inv: F32, rx: Array<U32> & U32) -> Array<U32>:  match cnt rx:    case 0n Tuple{x, v}:      x    case 1n+q Tuple{x, +v}:      +k = (j + 1 : U32)      nw_loop(q, base, k, sc, m, inv, Array.get(U32, Array.set(U32, x, (base + j : U32), B.uv((F32.exp(((B.fv(v) * sc : F32) - m : F32)) * inv : F32))), (base + k : U32)))@unsafedef sn_go(+n: U32, +base: U32, +sc: F32, x: Array<U32>, x1: Array<U32>, x2: Array<U32>, x3: Array<U32>, x4: Array<U32>, x5: Array<U32>, ml: F32 & F32) -> B.AR:  match ml:    case Tuple{+m, +l}:      B.AR{nw_loop(U32.to_nat(n), base, 0, sc, m, (1.0 / l : F32), Array.get(U32, x, base)), x1, x2, x3, x4, x5}@unsafedef sn_mid(+n: U32, +base: U32, +sc: F32, x1: Array<U32>, x2: Array<U32>, x3: Array<U32>, x4: Array<U32>, x5: Array<U32>, r: Array<U32> & F32 & F32) -> B.AR:  match r:    case Tuple{x, ml}:      sn_go(n, base, sc, x, x1, x2, x3, x4, x5, ml)@unsafedef sn_item(+i: U32, +e: ESn, ar: B.AR) -> B.AR:  match e ar:    case ESn{+n, +sc} B.AR{x, x1, x2, x3, x4, x5}:      +base = L.cell(i, n)      sn_mid(n, base, sc, x1, x2, x3, x4, x5, on_loop(U32.to_nat(n), base, 0, sc, F32.neg(340282346638528859811704183484516925440.0), 0.0, Array.get(U32, x, base)))