Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/algebra/realize_bitplane.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / algebra / realize_bitplane.c` 405 lines ยท 18053 bytes Raw Blame History 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 R2: packed ternary bitplanes (2 bits per weight). 18 * 19 * R2_bitplane: per row, chunks of 128 weights stored as two 16-byte planes 20 * P (pos) and N (neg). Bit k of byte j <-> weight (chunk + 16k + j), so the 21 * 16 byte masks for weights chunk+16k .. chunk+16k+15 come from one 22 * vtstq_u8(P, 1<> 8 (int8 in [-2,1]); T = 256*hi + lo exactly. 31 * Weights: per row and group one byte (pos nibble | neg nibble << 4), i.e. 32 * 2 bits per weight. y = sum_g T_g[pos] - T_g[neg]. 33 * Kernel: 4 groups (16 activations) x 4 rows per vqtbl4q lookup (64-byte 34 * tables = 4 groups); USDOT/SDOT with +1/-1 vectors sum each row's 4 lanes. 35 * 16 rows per table load. Table build (depends on x) is part of every run. 36 * 37 * R2c_crumb (below): 2-bit two's-complement codes, shift-pair decode. 38 */ 39 #include "algebra/realize_common.h" 40 41 #include 42 #include 43 #include 44 45 /* ================= R2_bitplane ================= */ 46 #define BP_CHUNK 128u 47 48 static int pack_bitplane(oma_rz_plan *p, const int8_t *w, size_t m, size_t n) { 49 int rc = oma_rz_check_shape(m, n, OMA_RZ_MAX_N); 50 if (rc) return rc; 51 if (!p || !w) return OMA_RZ_E_ARG; 52 rc = oma_rz_validate(w, m, n); 53 if (rc) return rc; 54 memset(p, 0, sizeof *p); 55 size_t chunks = (n + BP_CHUNK - 1) / BP_CHUNK; 56 size_t row_bytes = chunks * 32u; 57 uint8_t *buf = oma_rz_alloc(m * row_bytes); 58 if (!buf) return OMA_RZ_E_NOMEM; 59 uint64_t nnz = 0; 60 for (size_t i = 0; i < m; i++) { 61 const int8_t *row = w + i * n; 62 uint8_t *dst = buf + i * row_bytes; 63 for (size_t c = 0; c < chunks; c++) { 64 uint8_t *P = dst + c * 32u, *N = P + 16; 65 for (unsigned k = 0; k < 8; k++) 66 for (unsigned j = 0; j < 16; j++) { 67 size_t col = c * BP_CHUNK + 16u * k + j; 68 if (col >= n) continue; 69 int8_t v = row[col]; 70 if (v > 0) P[j] |= (uint8_t)(1u << k); 71 else if (v < 0) N[j] |= (uint8_t)(1u << k); 72 nnz += (v != 0); 73 } 74 } 75 } 76 p->m = m; 77 p->n = n; 78 p->mem = buf; 79 p->weight_bytes = m * row_bytes; 80 p->footprint_bytes = m * row_bytes; 81 p->nnz = nnz; 82 return OMA_RZ_OK; 83 } 84 85 #define BP_STEP(k) \ 86 do { \ 87 const uint8x16_t bit = vdupq_n_u8((uint8_t)(1u << (k))); \ 88 const int8x16_t xv = X[k]; \ 89 a0 = vdotq_s32(a0, vsubq_s8(vreinterpretq_s8_u8(vtstq_u8(N0, bit)), \ 90 vreinterpretq_s8_u8(vtstq_u8(P0, bit))), xv); \ 91 a1 = vdotq_s32(a1, vsubq_s8(vreinterpretq_s8_u8(vtstq_u8(N1, bit)), \ 92 vreinterpretq_s8_u8(vtstq_u8(P1, bit))), xv); \ 93 a2 = vdotq_s32(a2, vsubq_s8(vreinterpretq_s8_u8(vtstq_u8(N2, bit)), \ 94 vreinterpretq_s8_u8(vtstq_u8(P2, bit))), xv); \ 95 a3 = vdotq_s32(a3, vsubq_s8(vreinterpretq_s8_u8(vtstq_u8(N3, bit)), \ 96 vreinterpretq_s8_u8(vtstq_u8(P3, bit))), xv); \ 97 } while (0) 98 99 static inline void load_xchunk(const int8_t *x, size_t n, size_t c0, int8x16_t X[8]) { 100 if (c0 + BP_CHUNK <= n) { 101 for (int k = 0; k < 8; k++) X[k] = vld1q_s8(x + c0 + 16u * (unsigned)k); 102 } else { 103 int8_t t[BP_CHUNK]; 104 memset(t, 0, sizeof t); 105 memcpy(t, x + c0, n - c0); 106 for (int k = 0; k < 8; k++) X[k] = vld1q_s8(t + 16 * k); 107 } 108 } 109 110 static int run_bitplane(const oma_rz_plan *p, const int8_t *x, int32_t *y) { 111 if (!p || !p->mem || !x || !y) return OMA_RZ_E_ARG; 112 const uint8_t *w = p->mem; 113 size_t m = p->m, n = p->n; 114 size_t chunks = (n + BP_CHUNK - 1) / BP_CHUNK, row_bytes = chunks * 32u; 115 size_t i = 0; 116 for (; i + 4 <= m; i += 4) { 117 const uint8_t *r0 = w + i * row_bytes, *r1 = r0 + row_bytes; 118 const uint8_t *r2 = r1 + row_bytes, *r3 = r2 + row_bytes; 119 int32x4_t a0 = vdupq_n_s32(0), a1 = a0, a2 = a0, a3 = a0; 120 for (size_t c = 0; c < chunks; c++) { 121 int8x16_t X[8]; 122 load_xchunk(x, n, c * BP_CHUNK, X); 123 size_t o = c * 32u; 124 uint8x16_t P0 = vld1q_u8(r0 + o), N0 = vld1q_u8(r0 + o + 16); 125 uint8x16_t P1 = vld1q_u8(r1 + o), N1 = vld1q_u8(r1 + o + 16); 126 uint8x16_t P2 = vld1q_u8(r2 + o), N2 = vld1q_u8(r2 + o + 16); 127 uint8x16_t P3 = vld1q_u8(r3 + o), N3 = vld1q_u8(r3 + o + 16); 128 BP_STEP(0); BP_STEP(1); BP_STEP(2); BP_STEP(3); 129 BP_STEP(4); BP_STEP(5); BP_STEP(6); BP_STEP(7); 130 } 131 y[i] = vaddvq_s32(a0); 132 y[i + 1] = vaddvq_s32(a1); 133 y[i + 2] = vaddvq_s32(a2); 134 y[i + 3] = vaddvq_s32(a3); 135 } 136 for (; i < m; i++) { 137 const uint8_t *r0 = w + i * row_bytes; 138 int32x4_t a0 = vdupq_n_s32(0), b0 = a0; 139 for (size_t c = 0; c < chunks; c++) { 140 int8x16_t X[8]; 141 load_xchunk(x, n, c * BP_CHUNK, X); 142 uint8x16_t P0 = vld1q_u8(r0 + c * 32u), N0 = vld1q_u8(r0 + c * 32u + 16); 143 for (unsigned k = 0; k < 8; k += 2) { 144 uint8x16_t bit = vdupq_n_u8((uint8_t)(1u << k)); 145 uint8x16_t bit2 = vdupq_n_u8((uint8_t)(1u << (k + 1))); 146 a0 = vdotq_s32(a0, vsubq_s8(vreinterpretq_s8_u8(vtstq_u8(N0, bit)), 147 vreinterpretq_s8_u8(vtstq_u8(P0, bit))), X[k]); 148 b0 = vdotq_s32(b0, vsubq_s8(vreinterpretq_s8_u8(vtstq_u8(N0, bit2)), 149 vreinterpretq_s8_u8(vtstq_u8(P0, bit2))), X[k + 1]); 150 } 151 } 152 y[i] = vaddvq_s32(vaddq_s32(a0, b0)); 153 } 154 return OMA_RZ_OK; 155 } 156 157 /* ================= R2b_lut ================= */ 158 /* Weight layout: row blocks of 16 rows; per block and super-group G (16 159 * activations = 4 groups): 64 bytes = 4 tiles x (4 rows x 4 groups). 160 * Byte for (row r, group j) = pos_nibble | neg_nibble << 4, where bit b of a 161 * nibble is weight 16G + 4j + b. Tables: per super-group 128 bytes: 162 * lo[64] (entry 16j + s) then hi[64]. */ 163 #define LUT_ROWS 16u 164 165 static int pack_lut(oma_rz_plan *p, const int8_t *w, size_t m, size_t n) { 166 int rc = oma_rz_check_shape(m, n, OMA_RZ_MAX_N); 167 if (rc) return rc; 168 if (!p || !w) return OMA_RZ_E_ARG; 169 rc = oma_rz_validate(w, m, n); 170 if (rc) return rc; 171 memset(p, 0, sizeof *p); 172 size_t sg = (n + 15u) / 16u; /* super-groups */ 173 size_t mb = (m + LUT_ROWS - 1) / LUT_ROWS; /* row blocks */ 174 size_t wbytes = mb * sg * 64u; 175 uint8_t *buf = oma_rz_alloc(wbytes); 176 uint8_t *tab = oma_rz_alloc(sg * 128u); 177 if (!buf || !tab) { 178 free(buf); 179 free(tab); 180 return OMA_RZ_E_NOMEM; 181 } 182 uint64_t nnz = 0; 183 for (size_t b = 0; b < mb; b++) 184 for (size_t G = 0; G < sg; G++) { 185 uint8_t *dst = buf + (b * sg + G) * 64u; 186 for (unsigned r = 0; r < LUT_ROWS; r++) { 187 size_t row = b * LUT_ROWS + r; 188 for (unsigned j = 0; j < 4; j++) { 189 unsigned pn = 0, nn = 0; 190 for (unsigned q = 0; q < 4; q++) { 191 size_t col = G * 16u + 4u * j + q; 192 if (row >= m || col >= n) continue; 193 int8_t v = w[row * n + col]; 194 if (v > 0) pn |= 1u << q; 195 else if (v < 0) nn |= 1u << q; 196 nnz += (v != 0); 197 } 198 /* tile = r/4, lane in tile = (r%4)*4 + j */ 199 dst[(r / 4u) * 16u + (r % 4u) * 4u + j] = (uint8_t)(pn | (nn << 4)); 200 } 201 } 202 } 203 p->m = m; 204 p->n = n; 205 p->mem = buf; 206 p->scratch = tab; 207 p->weight_bytes = wbytes; 208 p->scratch_bytes = sg * 128u; 209 p->footprint_bytes = wbytes + sg * 128u; 210 p->nnz = nnz; 211 return OMA_RZ_OK; 212 } 213 214 /* subset-sum bit patterns for s = 0..15, bit b of s */ 215 static const int16_t k_bit[4][16] = { 216 {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, 217 {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1}, 218 {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1}, 219 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}, 220 }; 221 222 static void build_tables(const int8_t *x, size_t n, uint8_t *tab, size_t sg) { 223 const int16x8_t b0l = vld1q_s16(k_bit[0]), b0h = vld1q_s16(k_bit[0] + 8); 224 const int16x8_t b1l = vld1q_s16(k_bit[1]), b1h = vld1q_s16(k_bit[1] + 8); 225 const int16x8_t b2l = vld1q_s16(k_bit[2]), b2h = vld1q_s16(k_bit[2] + 8); 226 const int16x8_t b3l = vld1q_s16(k_bit[3]), b3h = vld1q_s16(k_bit[3] + 8); 227 for (size_t G = 0; G < sg; G++) { 228 int8_t xs[16]; 229 const int8_t *xp; 230 if (G * 16u + 16u <= n) { 231 xp = x + G * 16u; 232 } else { 233 memset(xs, 0, sizeof xs); 234 memcpy(xs, x + G * 16u, n - G * 16u); 235 xp = xs; 236 } 237 uint8_t *lo = tab + G * 128u, *hi = lo + 64; 238 for (unsigned j = 0; j < 4; j++) { 239 int16_t a = xp[4 * j], b = xp[4 * j + 1], c = xp[4 * j + 2], d = xp[4 * j + 3]; 240 int16x8_t tl = vmulq_n_s16(b0l, a), th = vmulq_n_s16(b0h, a); 241 tl = vmlaq_n_s16(tl, b1l, b); th = vmlaq_n_s16(th, b1h, b); 242 tl = vmlaq_n_s16(tl, b2l, c); th = vmlaq_n_s16(th, b2h, c); 243 tl = vmlaq_n_s16(tl, b3l, d); th = vmlaq_n_s16(th, b3h, d); 244 int8x16_t lov = vcombine_s8(vmovn_s16(tl), vmovn_s16(th)); 245 int8x16_t hiv = vcombine_s8(vshrn_n_s16(tl, 8), vshrn_n_s16(th, 8)); 246 vst1q_u8(lo + 16u * j, vreinterpretq_u8_s8(lov)); 247 vst1q_s8((int8_t *)hi + 16u * j, hiv); 248 } 249 } 250 } 251 252 static int run_lut(const oma_rz_plan *p, const int8_t *x, int32_t *y) { 253 if (!p || !p->mem || !p->scratch || !x || !y) return OMA_RZ_E_ARG; 254 size_t m = p->m, n = p->n; 255 size_t sg = (n + 15u) / 16u, mb = (m + LUT_ROWS - 1) / LUT_ROWS; 256 uint8_t *tab = p->scratch; /* per-call scratch, rebuilt from x every run */ 257 build_tables(x, n, tab, sg); 258 const uint8_t *w = p->mem; 259 static const uint8_t k_offs[16] = {0, 16, 32, 48, 0, 16, 32, 48, 0, 16, 32, 48, 0, 16, 32, 48}; 260 const uint8x16_t offs = vld1q_u8(k_offs), lowm = vdupq_n_u8(0x0F); 261 const int8x16_t one = vdupq_n_s8(1), mone = vdupq_n_s8(-1); 262 for (size_t b = 0; b < mb; b++) { 263 int32x4_t L0 = vdupq_n_s32(0), L1 = L0, L2 = L0, L3 = L0; 264 int32x4_t H0 = L0, H1 = L0, H2 = L0, H3 = L0; 265 const uint8_t *wb = w + b * sg * 64u; 266 for (size_t G = 0; G < sg; G++) { 267 const uint8_t *t = tab + G * 128u; 268 uint8x16x4_t LO = vld1q_u8_x4(t); 269 int8x16x4_t HI = vld1q_s8_x4((const int8_t *)t + 64); 270 const uint8_t *wg = wb + G * 64u; 271 #define LUT_TILE(T, Lacc, Hacc) \ 272 do { \ 273 uint8x16_t wv = vld1q_u8(wg + 16u * (T)); \ 274 uint8x16_t pi = vorrq_u8(vandq_u8(wv, lowm), offs); \ 275 uint8x16_t ni = vorrq_u8(vshrq_n_u8(wv, 4), offs); \ 276 Lacc = vusdotq_s32(Lacc, vqtbl4q_u8(LO, pi), one); \ 277 Lacc = vusdotq_s32(Lacc, vqtbl4q_u8(LO, ni), mone); \ 278 Hacc = vdotq_s32(Hacc, vqtbl4q_s8(HI, pi), one); \ 279 Hacc = vdotq_s32(Hacc, vqtbl4q_s8(HI, ni), mone); \ 280 } while (0) 281 LUT_TILE(0, L0, H0); 282 LUT_TILE(1, L1, H1); 283 LUT_TILE(2, L2, H2); 284 LUT_TILE(3, L3, H3); 285 #undef LUT_TILE 286 } 287 int32_t out[16]; 288 vst1q_s32(out, vaddq_s32(vshlq_n_s32(H0, 8), L0)); 289 vst1q_s32(out + 4, vaddq_s32(vshlq_n_s32(H1, 8), L1)); 290 vst1q_s32(out + 8, vaddq_s32(vshlq_n_s32(H2, 8), L2)); 291 vst1q_s32(out + 12, vaddq_s32(vshlq_n_s32(H3, 8), L3)); 292 size_t r0 = b * LUT_ROWS, cnt = m - r0 < LUT_ROWS ? m - r0 : LUT_ROWS; 293 memcpy(y + r0, out, cnt * sizeof(int32_t)); 294 } 295 return OMA_RZ_OK; 296 } 297 298 const oma_rz_impl oma_rz_r2_bitplane = { 299 "R2_bitplane", "pos/neg bitplanes (2 b/w), vtst byte masks -> SDOT", "bitplane", 1, 0, 300 OMA_RZ_MAX_N, pack_bitplane, run_bitplane}; 301 const oma_rz_impl oma_rz_r2b_lut = { 302 "R2b_lut", "T-MAC style 4-activation subset-sum LUT (2 b/w), vqtbl4q + USDOT", "lut", 1, 0, 303 OMA_RZ_MAX_N, pack_lut, run_lut}; 304 305 /* ================= R2c_crumb ================= 306 * 2-bit two's-complement "crumbs" (2 bits per weight, same footprint as the 307 * bitplanes, different code): +1 = 01, 0 = 00, -1 = 11 (10 never written). 308 * Per row, chunks of 64 weights in 16 bytes; bits 2k..2k+1 of byte j hold 309 * weight chunk + 16k + j. Decode is a sign-extending shift pair: 310 * w_k = (int8)(b << (6 - 2k)) >> 6 (k = 3: one shift) 311 * so 7 shifts + 4 SDOT per 64 weights per row (2.75 SIMD ops per 16 312 * weights, against 4 for the H1 bitplanes). Conversion from the H1 code is 313 * part of pack. */ 314 static int pack_crumb(oma_rz_plan *p, const int8_t *w, size_t m, size_t n) { 315 int rc = oma_rz_check_shape(m, n, OMA_RZ_MAX_N); 316 if (rc) return rc; 317 if (!p || !w) return OMA_RZ_E_ARG; 318 rc = oma_rz_validate(w, m, n); 319 if (rc) return rc; 320 memset(p, 0, sizeof *p); 321 size_t chunks = (n + 63u) / 64u, row_bytes = chunks * 16u; 322 uint8_t *buf = oma_rz_alloc(m * row_bytes); 323 if (!buf) return OMA_RZ_E_NOMEM; 324 uint64_t nnz = 0; 325 for (size_t i = 0; i < m; i++) 326 for (size_t c = 0; c < chunks; c++) 327 for (unsigned j = 0; j < 16; j++) { 328 unsigned byte = 0; 329 for (unsigned k = 0; k < 4; k++) { 330 size_t col = c * 64u + 16u * k + j; 331 int v = col < n ? w[i * n + col] : 0; 332 nnz += (v != 0); 333 byte |= ((unsigned)v & 3u) << (2u * k); 334 } 335 buf[i * row_bytes + c * 16u + j] = (uint8_t)byte; 336 } 337 p->m = m; 338 p->n = n; 339 p->mem = buf; 340 p->weight_bytes = m * row_bytes; 341 p->footprint_bytes = m * row_bytes; 342 p->nnz = nnz; 343 return OMA_RZ_OK; 344 } 345 346 static inline void load_x64(const int8_t *x, size_t n, size_t c0, int8x16_t X[4]) { 347 if (c0 + 64u <= n) { 348 for (int k = 0; k < 4; k++) X[k] = vld1q_s8(x + c0 + 16u * (unsigned)k); 349 } else { 350 int8_t t[64]; 351 memset(t, 0, sizeof t); 352 memcpy(t, x + c0, n - c0); 353 for (int k = 0; k < 4; k++) X[k] = vld1q_s8(t + 16 * k); 354 } 355 } 356 357 #define CRUMB_ROW(bv, A, B) \ 358 do { \ 359 A = vdotq_s32(A, vshrq_n_s8(vshlq_n_s8(bv, 6), 6), X[0]); \ 360 B = vdotq_s32(B, vshrq_n_s8(vshlq_n_s8(bv, 4), 6), X[1]); \ 361 A = vdotq_s32(A, vshrq_n_s8(vshlq_n_s8(bv, 2), 6), X[2]); \ 362 B = vdotq_s32(B, vshrq_n_s8(bv, 6), X[3]); \ 363 } while (0) 364 365 static int run_crumb(const oma_rz_plan *p, const int8_t *x, int32_t *y) { 366 if (!p || !p->mem || !x || !y) return OMA_RZ_E_ARG; 367 const int8_t *w = p->mem; 368 size_t m = p->m, n = p->n; 369 size_t chunks = (n + 63u) / 64u, row_bytes = chunks * 16u; 370 size_t i = 0; 371 for (; i + 4 <= m; i += 4) { 372 const int8_t *r0 = w + i * row_bytes; 373 int32x4_t a0 = vdupq_n_s32(0), a1 = a0, a2 = a0, a3 = a0, b0 = a0, b1 = a0, b2 = a0, b3 = a0; 374 for (size_t c = 0; c < chunks; c++) { 375 int8x16_t X[4]; 376 load_x64(x, n, c * 64u, X); 377 size_t o = c * 16u; 378 int8x16_t w0 = vld1q_s8(r0 + o), w1 = vld1q_s8(r0 + row_bytes + o); 379 int8x16_t w2 = vld1q_s8(r0 + 2 * row_bytes + o), w3 = vld1q_s8(r0 + 3 * row_bytes + o); 380 CRUMB_ROW(w0, a0, b0); 381 CRUMB_ROW(w1, a1, b1); 382 CRUMB_ROW(w2, a2, b2); 383 CRUMB_ROW(w3, a3, b3); 384 } 385 int32x4_t s01 = vpaddq_s32(vaddq_s32(a0, b0), vaddq_s32(a1, b1)); 386 int32x4_t s23 = vpaddq_s32(vaddq_s32(a2, b2), vaddq_s32(a3, b3)); 387 vst1q_s32(y + i, vpaddq_s32(s01, s23)); 388 } 389 for (; i < m; i++) { 390 const int8_t *r0 = w + i * row_bytes; 391 int32x4_t a0 = vdupq_n_s32(0), b0 = a0; 392 for (size_t c = 0; c < chunks; c++) { 393 int8x16_t X[4]; 394 load_x64(x, n, c * 64u, X); 395 int8x16_t w0 = vld1q_s8(r0 + c * 16u); 396 CRUMB_ROW(w0, a0, b0); 397 } 398 y[i] = vaddvq_s32(vaddq_s32(a0, b0)); 399 } 400 return OMA_RZ_OK; 401 } 402 403 const oma_rz_impl oma_rz_r2c_crumb = { 404 "R2c_crumb", "2-bit two's-complement crumbs (2 b/w), shift-pair decode -> SDOT", "crumb2", 1, 0, 405 OMA_RZ_MAX_N, pack_crumb, run_crumb};