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_common.h

98 lines · 4343 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/* Omega mixed algebra MA-2: realizations of one exact semantic operation.
18 * spec/mixed-algebra-ma2.md
19 *
20 * Operation Omega-X ("ternary GEMV"):
21 * y = W . x, W in {-1,0,+1}^(m x n) (row-major int8), x in int8^n,
22 * y in int32^m, EXACT (every realization is bit-identical to the naive
23 * integer oracle).
24 *
25 * Overflow contract: |y_i| <= 128 * n (x may be -128), so n is limited to
26 * OMA_RZ_MAX_N = floor(INT32_MAX / 128) = 16,777,215 (same bound as
27 * oma_dot_tw_i8). Realizations with narrower index types (sparse: uint16
28 * column indices) state their own lower limit through max_n.
29 *
30 * A realization is a pair (pack, run): pack converts the canonical int8
31 * ternary matrix into the realization's own weight form (validating every
32 * weight), run computes y for one x. Pack cost is measured separately so the
33 * benchmark can report "pack once, amortized" and "pack per call".
34 */
35#ifndef OMA_REALIZE_COMMON_H
36#define OMA_REALIZE_COMMON_H
37
38#include <stddef.h>
39#include <stdint.h>
40
41#define OMA_RZ_MAX_N ((size_t)16777215u) /* INT32_MAX / 128 */
42
43enum {
44 OMA_RZ_OK = 0,
45 OMA_RZ_E_ARG = -1, /* NULL, zero-size where not allowed, n above max_n */
46 OMA_RZ_E_TRIT = -2, /* a weight outside {-1,0,+1} */
47 OMA_RZ_E_NOMEM = -3,
48 OMA_RZ_E_OVERFLOW = -4 /* size arithmetic would overflow */
49};
50
51typedef struct {
52 size_t m, n;
53 void *mem; /* owned allocation(s) holding the packed weights */
54 void *aux; /* second owned allocation (index lists, row offsets) */
55 void *scratch; /* per-call scratch (tables, residues), owned */
56 size_t weight_bytes; /* bytes of packed weight data read by one run */
57 size_t footprint_bytes; /* all bytes the plan holds (weights + aux + scratch) */
58 size_t scratch_bytes; /* per-call scratch written/read by one run */
59 uint64_t nnz; /* non-zero weights (informational) */
60 int stride; /* realization-private layout parameter */
61} oma_rz_plan;
62
63typedef struct {
64 const char *id; /* short stable id, e.g. "R1_sdot" */
65 const char *label; /* one-line human description */
66 const char *family; /* "binary", "bitplane", "lut", "crumb2", "sparse", "rns", "dense5" */
67 int exact; /* 1: bit-exact contract; every realization here is exact */
68 int weak_baseline; /* 1: labelled weak baseline, not a champion candidate */
69 size_t max_n; /* largest n the realization accepts */
70 int (*pack)(oma_rz_plan *p, const int8_t *w, size_t m, size_t n);
71 int (*run)(const oma_rz_plan *p, const int8_t *x, int32_t *y);
72} oma_rz_impl;
73
74/* All realizations, in a fixed order. */
75size_t oma_rz_count(void);
76const oma_rz_impl *oma_rz_get(size_t i);
77const oma_rz_impl *oma_rz_find(const char *id);
78
79void oma_rz_free(oma_rz_plan *p);
80const char *oma_rz_strerror(int rc);
81
82/* Shared helpers. */
83int oma_rz_check_shape(size_t m, size_t n, size_t max_n);
84/* OMA_RZ_OK if every entry of w[m*n] is -1, 0 or +1. */
85int oma_rz_validate(const int8_t *w, size_t m, size_t n);
86void *oma_rz_alloc(size_t bytes); /* 64-byte aligned, zeroed; NULL on failure */
87
88/* Naive oracle (plain int32 loop, no vectorization tricks). */
89int oma_rz_oracle(const int8_t *w, size_t m, size_t n, const int8_t *x, int32_t *y);
90
91/* Per-family declarations (each file registers its impls). */
92extern const oma_rz_impl oma_rz_r1_plain, oma_rz_r1_sdot, oma_rz_r1_sdot_il, oma_rz_r1_smmla;
93extern const oma_rz_impl oma_rz_r2_bitplane, oma_rz_r2b_lut, oma_rz_r2c_crumb;
94extern const oma_rz_impl oma_rz_r3_sparse;
95extern const oma_rz_impl oma_rz_r4_rns;
96extern const oma_rz_impl oma_rz_r5_dense5;
97
98#endif /* OMA_REALIZE_COMMON_H */