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_dense.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 R5: dense 5 trits per byte (1.6 bits per weight), decode then SDOT. |
| 18 | * Byte rule is the reference dense rule (oma_pack.h): byte = sum_{i<5} |
| 19 | * (t_i + 1) * 3^i, valid bytes 0..242. Only the assignment of trits to bytes |
| 20 | * differs ("strided dense"): per row, chunks of 80 weights in 16 bytes; byte j |
| 21 | * of a chunk holds weights chunk + 16 i + j for i = 0..4. Digit plane i |
| 22 | * (d_i = t_i + 1 in {0,1,2}) is then directly the 16-lane weight vector for |
| 23 | * x[chunk + 16 i ..]. Decode: q = b / 3 = (b * 171) >> 9 (exact for b < 256), |
| 24 | * d = b - 3 q, repeated; d_4 = b / 81. Since d = t + 1: |
| 25 | * y = sum_i SDOT(d_i, x_i) - sum_j x_j (sum x computed once per call; |
| 26 | * padding trits are 0 -> d = 1 and meet zero-padded x). */ |
| 27 | #include "algebra/realize_common.h" |
| 28 | |
| 29 | #include <arm_neon.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | #define D5_CHUNK 80u |
| 33 | |
| 34 | static int pack_dense5(oma_rz_plan *p, const int8_t *w, size_t m, size_t n) { |
| 35 | int rc = oma_rz_check_shape(m, n, OMA_RZ_MAX_N); |
| 36 | if (rc) return rc; |
| 37 | if (!p || !w) return OMA_RZ_E_ARG; |
| 38 | rc = oma_rz_validate(w, m, n); |
| 39 | if (rc) return rc; |
| 40 | memset(p, 0, sizeof *p); |
| 41 | size_t chunks = (n + D5_CHUNK - 1) / D5_CHUNK, row_bytes = chunks * 16u; |
| 42 | uint8_t *buf = oma_rz_alloc(m * row_bytes); |
| 43 | if (!buf) return OMA_RZ_E_NOMEM; |
| 44 | uint64_t nnz = 0; |
| 45 | for (size_t r = 0; r < m; r++) |
| 46 | for (size_t c = 0; c < chunks; c++) |
| 47 | for (unsigned j = 0; j < 16; j++) { |
| 48 | unsigned byte = 0, pw = 1; |
| 49 | for (unsigned i = 0; i < 5; i++, pw *= 3u) { |
| 50 | size_t col = c * D5_CHUNK + 16u * i + j; |
| 51 | int v = col < n ? w[r * n + col] : 0; |
| 52 | nnz += (v != 0); |
| 53 | byte += (unsigned)(v + 1) * pw; |
| 54 | } |
| 55 | buf[r * row_bytes + c * 16u + j] = (uint8_t)byte; |
| 56 | } |
| 57 | p->m = m; |
| 58 | p->n = n; |
| 59 | p->mem = buf; |
| 60 | p->weight_bytes = m * row_bytes; |
| 61 | p->footprint_bytes = m * row_bytes; |
| 62 | p->nnz = nnz; |
| 63 | return OMA_RZ_OK; |
| 64 | } |
| 65 | |
| 66 | static inline uint8x16_t div3(uint8x16_t b) { |
| 67 | const uint8x16_t k171 = vdupq_n_u8(171); |
| 68 | uint16x8_t lo = vmull_u8(vget_low_u8(b), vget_low_u8(k171)); |
| 69 | uint16x8_t hi = vmull_high_u8(b, k171); |
| 70 | uint8x16_t h8 = vuzp2q_u8(vreinterpretq_u8_u16(lo), vreinterpretq_u8_u16(hi)); /* >> 8 */ |
| 71 | return vshrq_n_u8(h8, 1); /* >> 9 */ |
| 72 | } |
| 73 | |
| 74 | static int run_dense5(const oma_rz_plan *p, const int8_t *x, int32_t *y) { |
| 75 | if (!p || !p->mem || !x || !y) return OMA_RZ_E_ARG; |
| 76 | size_t m = p->m, n = p->n; |
| 77 | size_t chunks = (n + D5_CHUNK - 1) / D5_CHUNK, row_bytes = chunks * 16u; |
| 78 | size_t nfull = (n / D5_CHUNK) * D5_CHUNK; |
| 79 | int8_t xt[D5_CHUNK]; |
| 80 | memset(xt, 0, sizeof xt); |
| 81 | if (nfull < n) memcpy(xt, x + nfull, n - nfull); |
| 82 | int32_t sumx = 0; |
| 83 | for (size_t j = 0; j < n; j++) sumx += x[j]; |
| 84 | const uint8_t *w = p->mem; |
| 85 | const uint8x16_t three = vdupq_n_u8(3); |
| 86 | for (size_t r = 0; r < m; r++) { |
| 87 | const uint8_t *row = w + r * row_bytes; |
| 88 | int32x4_t a0 = vdupq_n_s32(0), a1 = a0; |
| 89 | for (size_t c = 0; c < chunks; c++) { |
| 90 | const int8_t *xc = (c * D5_CHUNK < nfull) ? x + c * D5_CHUNK : xt; |
| 91 | uint8x16_t b = vld1q_u8(row + c * 16u); |
| 92 | uint8x16_t q1 = div3(b); |
| 93 | uint8x16_t d0 = vmlsq_u8(b, q1, three); |
| 94 | uint8x16_t q2 = div3(q1); |
| 95 | uint8x16_t d1 = vmlsq_u8(q1, q2, three); |
| 96 | uint8x16_t q3 = div3(q2); |
| 97 | uint8x16_t d2 = vmlsq_u8(q2, q3, three); |
| 98 | uint8x16_t q4 = div3(q3); |
| 99 | uint8x16_t d3 = vmlsq_u8(q3, q4, three); |
| 100 | a0 = vdotq_s32(a0, vreinterpretq_s8_u8(d0), vld1q_s8(xc)); |
| 101 | a1 = vdotq_s32(a1, vreinterpretq_s8_u8(d1), vld1q_s8(xc + 16)); |
| 102 | a0 = vdotq_s32(a0, vreinterpretq_s8_u8(d2), vld1q_s8(xc + 32)); |
| 103 | a1 = vdotq_s32(a1, vreinterpretq_s8_u8(d3), vld1q_s8(xc + 48)); |
| 104 | a0 = vdotq_s32(a0, vreinterpretq_s8_u8(q4), vld1q_s8(xc + 64)); |
| 105 | } |
| 106 | y[r] = vaddvq_s32(vaddq_s32(a0, a1)) - sumx; |
| 107 | } |
| 108 | return OMA_RZ_OK; |
| 109 | } |
| 110 | |
| 111 | const oma_rz_impl oma_rz_r5_dense5 = { |
| 112 | "R5_dense5", "5 trits/byte (1.6 b/w), NEON divide-by-3 decode then SDOT", "dense5", 1, 0, |
| 113 | OMA_RZ_MAX_N, pack_dense5, run_dense5}; |