interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / algebra / realize_sparse.c

93 lines · 3399 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 R3: sparse ternary. Per row, a list of +1 column indices followed by a
18 * list of -1 column indices (uint16, so n <= 65536); row offsets uint32.
19 * y_i = sum x[pos idx] - sum x[neg idx], scalar gather with 4 accumulators
20 * (NEON has no gather; SVE gather at VL=128 is 4 lanes and not faster than
21 * scalar loads here). Weight bytes read per call = 2 * nnz + 4 * (2m + 1). */
22#include "algebra/realize_common.h"
23
24#include <stdlib.h>
25#include <string.h>
26
27#define SP_MAX_N ((size_t)65536u)
28
29static int pack_sparse(oma_rz_plan *p, const int8_t *w, size_t m, size_t n) {
30 int rc = oma_rz_check_shape(m, n, SP_MAX_N);
31 if (rc) return rc;
32 if (!p || !w) return OMA_RZ_E_ARG;
33 rc = oma_rz_validate(w, m, n);
34 if (rc) return rc;
35 memset(p, 0, sizeof *p);
36 uint64_t nnz = 0;
37 for (size_t i = 0; i < m * n; i++) nnz += (w[i] != 0);
38 if (nnz > UINT32_MAX) return OMA_RZ_E_OVERFLOW;
39 uint16_t *idx = oma_rz_alloc((size_t)nnz * sizeof(uint16_t));
40 uint32_t *off = oma_rz_alloc((2u * m + 1u) * sizeof(uint32_t));
41 if (!idx || !off) {
42 free(idx);
43 free(off);
44 return OMA_RZ_E_NOMEM;
45 }
46 uint32_t k = 0;
47 for (size_t i = 0; i < m; i++) {
48 const int8_t *row = w + i * n;
49 off[2 * i] = k;
50 for (size_t j = 0; j < n; j++)
51 if (row[j] > 0) idx[k++] = (uint16_t)j;
52 off[2 * i + 1] = k;
53 for (size_t j = 0; j < n; j++)
54 if (row[j] < 0) idx[k++] = (uint16_t)j;
55 }
56 off[2 * m] = k;
57 p->m = m;
58 p->n = n;
59 p->mem = idx;
60 p->aux = off;
61 p->nnz = nnz;
62 p->weight_bytes = (size_t)nnz * 2u + (2u * m + 1u) * 4u;
63 p->footprint_bytes = p->weight_bytes;
64 return OMA_RZ_OK;
65}
66
67static inline int32_t gather_sum(const int8_t *x, const uint16_t *ix, uint32_t cnt) {
68 int32_t s0 = 0, s1 = 0, s2 = 0, s3 = 0;
69 uint32_t k = 0;
70 for (; k + 8 <= cnt; k += 8) {
71 s0 += x[ix[k]] + x[ix[k + 4]];
72 s1 += x[ix[k + 1]] + x[ix[k + 5]];
73 s2 += x[ix[k + 2]] + x[ix[k + 6]];
74 s3 += x[ix[k + 3]] + x[ix[k + 7]];
75 }
76 for (; k < cnt; k++) s0 += x[ix[k]];
77 return (s0 + s1) + (s2 + s3);
78}
79
80static int run_sparse(const oma_rz_plan *p, const int8_t *x, int32_t *y) {
81 if (!p || !p->mem || !p->aux || !x || !y) return OMA_RZ_E_ARG;
82 const uint16_t *idx = p->mem;
83 const uint32_t *off = p->aux;
84 for (size_t i = 0; i < p->m; i++) {
85 uint32_t a = off[2 * i], b = off[2 * i + 1], c = off[2 * i + 2];
86 y[i] = gather_sum(x, idx + a, b - a) - gather_sum(x, idx + b, c - b);
87 }
88 return OMA_RZ_OK;
89}
90
91const oma_rz_impl oma_rz_r3_sparse = {
92 "R3_sparse", "uint16 +1/-1 index lists per row, scalar gather add/sub", "sparse", 1, 0,
93 SP_MAX_N, pack_sparse, run_sparse};