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 / turing / ty_model.h
| 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 | /* Turing Yield: context-table predictors and their exact model code. |
| 18 | * |
| 19 | * A model is a table of 16-bit quantized distributions over K outcome symbols: |
| 20 | * one default row plus one row per stored context key. P(x_t | ctx_t) = |
| 21 | * row(ctx_t)[x_t] / 65536, or default[x_t] / 65536 when ctx_t has no row. |
| 22 | * Every entry is >= 1 (the floor, 2^-16) and each row sums to exactly 65536. |
| 23 | * |
| 24 | * L(M) is the exact bit length of the model code written by ty_model_encode |
| 25 | * (MSB-first bitstream): |
| 26 | * "TYM0" 32 | version 8 (=0) | K 8 | feature mask 8 | qbits 8 (=16) | |
| 27 | * key bits 8 | row count 32 | default row: K-1 entries x 16 | |
| 28 | * per row, keys strictly increasing: key (key bits) | K-1 entries x 16. |
| 29 | * The K-th entry of each row is implied (65536 minus the others). The decoder |
| 30 | * (ty_model_decode) rebuilds the identical table, and scoring always runs on a |
| 31 | * decoded table, so the model that is scored is the model that is paid for. |
| 32 | */ |
| 33 | #ifndef TURING_TY_MODEL_H |
| 34 | #define TURING_TY_MODEL_H |
| 35 | |
| 36 | #include <stddef.h> |
| 37 | #include <stdint.h> |
| 38 | |
| 39 | #include "turing/ty_ctr1.h" |
| 40 | #include "turing/ty_math.h" |
| 41 | |
| 42 | #define TY_MODEL_VERSION 0 |
| 43 | #define TY_MODEL_DOMAIN "turing.ymodel.v0" |
| 44 | #define TY_MODEL_HEADER_BITS 104 |
| 45 | |
| 46 | /* Context features (mixed-radix key, in this order, low to high). */ |
| 47 | enum { |
| 48 | TY_F_OP = 1, /* op feature, radix 16 */ |
| 49 | TY_F_DEPTH = 2, /* clipped depth, radix 8 */ |
| 50 | TY_F_PREV1 = 4, /* previous outcome in the crumb or start-of-crumb, radix K+1 */ |
| 51 | TY_F_PREV2 = 8, |
| 52 | TY_F_PREV3 = 16, |
| 53 | TY_F_POS = 32, /* (crumb ordinal, event_index): a position lookup key, radix 2^36 */ |
| 54 | TY_F_PREV4 = 64, |
| 55 | TY_F_PREV5 = 128, |
| 56 | TY_F_ALL = 255 |
| 57 | }; |
| 58 | |
| 59 | typedef struct { |
| 60 | uint64_t key; |
| 61 | uint32_t q[TY_KMAX]; |
| 62 | } ty_row; |
| 63 | |
| 64 | typedef struct { |
| 65 | unsigned K; |
| 66 | unsigned mask; |
| 67 | unsigned keybits; |
| 68 | uint32_t def[TY_KMAX]; |
| 69 | size_t nrows; |
| 70 | ty_row *rows; |
| 71 | } ty_model; |
| 72 | |
| 73 | /* Fit procedures (declared in the profile). */ |
| 74 | enum { TY_FIT_KEEP_ALL = 0, TY_FIT_MDL_PRUNE = 1 }; |
| 75 | |
| 76 | void ty_model_free(ty_model *m); |
| 77 | /* Radix product of the mask's features, and ceil(log2) of it. */ |
| 78 | int ty_key_space(unsigned mask, unsigned K, uint64_t *space, unsigned *bits); |
| 79 | /* Fit on streams with KT smoothing + 16-bit quantization; rows kept per rule. */ |
| 80 | int ty_model_fit(ty_model *m, const ty_stream *const *streams, size_t ns, unsigned mask, unsigned K, int rule); |
| 81 | /* Uniform-as-quantized model (no counts, no rows). */ |
| 82 | int ty_model_uniform(ty_model *m, unsigned K); |
| 83 | /* Model code. *bits = L(M). Caller frees *buf. */ |
| 84 | int ty_model_encode(const ty_model *m, uint8_t **buf, size_t *nbytes, uint64_t *bits); |
| 85 | int ty_model_decode(const uint8_t *buf, size_t nbytes, ty_model *m, uint64_t *bits, char *why, size_t whylen); |
| 86 | /* L(D|M) in micro-bits over a stream; *nsym = events scored. */ |
| 87 | int ty_model_score(const ty_model *m, const ty_stream *s, int64_t *ub, uint64_t *nsym); |
| 88 | /* SHA-256(TY_MODEL_DOMAIN || 0x00 || code bytes). */ |
| 89 | void ty_model_digest(const uint8_t *buf, size_t nbytes, uint8_t out[32]); |
| 90 | |
| 91 | #endif /* TURING_TY_MODEL_H */ |