omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / algebra / realize_rns.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* MA-2 R4: residue number system (RNS). |
| 18 | * Moduli {256, 255, 253} (pairwise coprime), M = 16,515,840. Every centered |
| 19 | * residue fits int8: x mod 256 is x itself, x mod 255 in [-127,127], |
| 20 | * x mod 253 in [-126,126]; ternary weights are their own residues. |
| 21 | * Per call: build the three residue vectors of x (O(n)); per row, load the |
| 22 | * int8 weights ONCE and run three SDOT channels on them (fused channels, the |
| 23 | * cheapest RNS layout on this chip), reduce each channel sum mod p, then |
| 24 | * reconstruct y by mixed-radix CRT (included in the cost): |
| 25 | * a0 = r256, a1 = (r255 - a0) mod 255 (256 == 1 mod 255) |
| 26 | * a2 = (r253 - a0 - 256 a1) * 211 mod 253 (256*255 == 6, 6^-1 == 211) |
| 27 | * Y = a0 + 256 a1 + 65280 a2 in [0, M); y = Y - M if Y >= M/2. |
| 28 | * Exactness needs |y| <= 128 n < M/2, so n <= 64,515 (max_n = 64,512). |
| 29 | * Honest limit: each channel accumulates in int32 lanes (SDOT forces that); |
| 30 | * a narrower channel accumulator is not cheaper on this chip, so RNS pays |
| 31 | * 3x the multiply-accumulate work of R1 plus residue build and CRT. */ |
| 32 | #include "algebra/realize_common.h" |
| 33 | |
| 34 | #include <arm_neon.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #define RNS_MAX_N ((size_t)64512u) |
| 39 | #define RNS_M 16515840 |
| 40 | |
| 41 | static int pack_rns(oma_rz_plan *p, const int8_t *w, size_t m, size_t n) { |
| 42 | int rc = oma_rz_check_shape(m, n, RNS_MAX_N); |
| 43 | if (rc) return rc; |
| 44 | if (!p || !w) return OMA_RZ_E_ARG; |
| 45 | rc = oma_rz_validate(w, m, n); |
| 46 | if (rc) return rc; |
| 47 | memset(p, 0, sizeof *p); |
| 48 | size_t np = (n + 15u) & ~(size_t)15u; |
| 49 | int8_t *buf = oma_rz_alloc(m * np); |
| 50 | int8_t *res = oma_rz_alloc(3u * np); |
| 51 | if (!buf || !res) { |
| 52 | free(buf); |
| 53 | free(res); |
| 54 | return OMA_RZ_E_NOMEM; |
| 55 | } |
| 56 | uint64_t nnz = 0; |
| 57 | for (size_t i = 0; i < m; i++) { |
| 58 | memcpy(buf + i * np, w + i * n, n); |
| 59 | for (size_t j = 0; j < n; j++) nnz += (w[i * n + j] != 0); |
| 60 | } |
| 61 | p->m = m; |
| 62 | p->n = n; |
| 63 | p->mem = buf; |
| 64 | p->scratch = res; |
| 65 | p->weight_bytes = m * np; |
| 66 | p->scratch_bytes = 3u * np; |
| 67 | p->footprint_bytes = m * np + 3u * np; |
| 68 | p->nnz = nnz; |
| 69 | return OMA_RZ_OK; |
| 70 | } |
| 71 | |
| 72 | static inline int32_t modp(int32_t v, int32_t p) { |
| 73 | int32_t r = v % p; |
| 74 | return r < 0 ? r + p : r; |
| 75 | } |
| 76 | |
| 77 | static int run_rns(const oma_rz_plan *p, const int8_t *x, int32_t *y) { |
| 78 | if (!p || !p->mem || !p->scratch || !x || !y) return OMA_RZ_E_ARG; |
| 79 | size_t m = p->m, n = p->n, np = (n + 15u) & ~(size_t)15u; |
| 80 | int8_t *x256 = p->scratch, *x255 = x256 + np, *x253 = x255 + np; |
| 81 | /* residue build (padding lanes stay 0 from pack) */ |
| 82 | for (size_t j = 0; j < n; j++) { |
| 83 | int v = x[j]; |
| 84 | x256[j] = (int8_t)v; |
| 85 | x255[j] = (int8_t)(v == -128 ? 127 : v); |
| 86 | x253[j] = (int8_t)(v > 126 ? v - 253 : (v < -126 ? v + 253 : v)); |
| 87 | } |
| 88 | const int8_t *w = p->mem; |
| 89 | for (size_t i = 0; i < m; i++) { |
| 90 | const int8_t *row = w + i * np; |
| 91 | int32x4_t c0 = vdupq_n_s32(0), c1 = c0, c2 = c0, d0 = c0, d1 = c0, d2 = c0; |
| 92 | size_t j = 0; |
| 93 | for (; j + 32 <= np; j += 32) { |
| 94 | int8x16_t wa = vld1q_s8(row + j), wb = vld1q_s8(row + j + 16); |
| 95 | c0 = vdotq_s32(c0, wa, vld1q_s8(x256 + j)); |
| 96 | c1 = vdotq_s32(c1, wa, vld1q_s8(x255 + j)); |
| 97 | c2 = vdotq_s32(c2, wa, vld1q_s8(x253 + j)); |
| 98 | d0 = vdotq_s32(d0, wb, vld1q_s8(x256 + j + 16)); |
| 99 | d1 = vdotq_s32(d1, wb, vld1q_s8(x255 + j + 16)); |
| 100 | d2 = vdotq_s32(d2, wb, vld1q_s8(x253 + j + 16)); |
| 101 | } |
| 102 | for (; j < np; j += 16) { |
| 103 | int8x16_t wa = vld1q_s8(row + j); |
| 104 | c0 = vdotq_s32(c0, wa, vld1q_s8(x256 + j)); |
| 105 | c1 = vdotq_s32(c1, wa, vld1q_s8(x255 + j)); |
| 106 | c2 = vdotq_s32(c2, wa, vld1q_s8(x253 + j)); |
| 107 | } |
| 108 | int32_t r256 = vaddvq_s32(vaddq_s32(c0, d0)) & 255; |
| 109 | int32_t r255 = modp(vaddvq_s32(vaddq_s32(c1, d1)), 255); |
| 110 | int32_t r253 = modp(vaddvq_s32(vaddq_s32(c2, d2)), 253); |
| 111 | int32_t a0 = r256; |
| 112 | int32_t a1 = modp(r255 - a0, 255); |
| 113 | int32_t a2 = modp((int32_t)(((int64_t)modp(r253 - a0 - 256 * a1, 253) * 211) % 253), 253); |
| 114 | int32_t Y = a0 + 256 * a1 + 65280 * a2; |
| 115 | y[i] = Y >= RNS_M / 2 ? Y - RNS_M : Y; |
| 116 | } |
| 117 | return OMA_RZ_OK; |
| 118 | } |
| 119 | |
| 120 | const oma_rz_impl oma_rz_r4_rns = { |
| 121 | "R4_rns", "RNS {256,255,253}, 3 fused SDOT channels + mixed-radix CRT", "rns", 1, 0, |
| 122 | RNS_MAX_N, pack_rns, run_rns}; |