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 / calibration / docs / CODER_SPEC.md

359 lines · 16167 bytes Raw Blame History Source

EXP-001 coder specification (lane B)

Status: implemented and tested on branch feat/turing-exp001-b. Sections 1 to 8 are normative. Section 9 records the measured envelope; its rule is the profile field coder_envelope and freezes with the profile.

Implementation: src/turing/tc_pstream.[ch] (formats), src/turing/tc_range.[ch] (coder A), src/turing/tc_rans.[ch] (coder B), src/turing/tc_produce.[ch] (predictor side), src/turing/tc_tool.c (CLI turing-coder). Tests: tests/turing/test_tc.c, tests/turing/test_tc_produce.c, make target test-turing-exp001-coders in mk/turing_exp001_b.mk.

1. Scope and roles

A predictor (a TYM0 model walking a CTR1 trace) emits a probability stream (TPS1) and the observed symbols (TSY1) before any coding happens. Two reference coders then turn (TPS1, TSY1) into a coded file, and decode (TPS1, coded file) back to symbols. The coders never see the model: the coder sources do not include ty_model.h, the make target greps for this, and test_tc is linked without ty_model.c.

The TPS1 depends on the observed data (context keys come from past symbols), so decoding with a TPS1 in hand checks the coder arithmetic and the bitstream, not the causality of the predictor. Causality is a property of the producer (section 7), which uses only past symbols of the same crumb.

Conventions: all integers little-endian. SHA-256 as in src/sha256.c. "Domain digest" D(tag, bytes) = SHA-256(tag || 0x00 || bytes).

2. Probability stream TPS1

File = header (128 bytes) || count records || trailer (32 bytes).

OffsetSizeFieldValue
04magicTPS1
42version1
61Kalphabet size, 2..16
71qbits16
84flags0
128countnumber of records n
204record_bytes24 + 2K
244reserved0
2832profile_digestthe profile digest as 32 raw bytes: SHA-256 of the profile TOML file bytes, which is the 64-hex value on the sidecar line (DATA_FORMAT.md section 2), not the hash of the sidecar file
6032model_digestty_model_digest = SHA-256("turing.ymodel.v0" 0x00 || .tym bytes)
9232dataset_digestplain SHA-256 of the CTR1 file bytes
1244reserved0

Record t (t = 0..n-1), 24 + 2K bytes:

OffsetSizeField
08observation_index, must equal t
88context_key (section 7)
164crumb ordinal in the dataset file (0-based, section 7)
204norm_sum, must equal 65536
242Kq[0..K-1], u16 each

Probability of symbol x at step t is q[x] / 65536. Every entry must be at least 1 (zero-probability policy: refuse) and the K entries must sum to exactly 65536 (normalization policy: exact). The float vector and the quantized vector are the same object: TYM0 models are quantized tables.

Trailer = D("turing.tc.pstream.v1", header || records). This value is "the TPS1 digest"; coded files bind to it.

A reader refuses, in this order: size below 160 bytes or wrong magic (FORMAT); trailer mismatch (DIGEST); version, qbits, flags, reserved or K out of range (FORMAT); record_bytes or file size disagreeing with count (FORMAT); then per record: observation_index != t (INDEX), any q = 0 (ZERO), sum or norm_sum != 65536 (NORM).

3. Symbol file TSY1

File = header (56 bytes) || n symbol bytes || trailer (32 bytes).

OffsetSizeFieldValue
04magicTSY1
42version1
61K2..16
71reserved0
88countn
168reserved0
2432dataset_digestas in TPS1

Trailer = D("turing.tc.symbols.v1", header || symbols). Every symbol must be below K. A (TPS1, TSY1) pair is valid only when K, count and dataset_digest agree (else COUNT or DATASET).

4. Coded file header (both coders)

Coded file = header (56 bytes) || payload.

OffsetSizeField
04magic: TCR1 (range) or TCA1 (rANS)
41version = 1
51coder_id: 1 = range, 2 = rANS
62reserved = 0
88count = TPS1 count
1632TPS1 digest (section 2 trailer)
488payload_bytes

The decoder checks, in this order: length below 56 or wrong magic (HEADER); version, coder_id or reserved (HEADER); TPS1 digest (BINDING); count (COUNT); file shorter than 56 + payload_bytes (TRUNC); file longer (TRAIL).

Accounting rule. Coded length in bits = 8 x (every byte of the coded file), header and flush included. Overhead = coded bits minus the ideal length (section 8). The 56-byte header alone is 448 bits.

5. Coder A: range coder (TCR1, id 1)

State: low u64 (only 33 bits used), range u32, cache u8, have_cache flag, pending count of deferred 0xFF bytes. Constant TOP = 2^24. All products below fit in u32 because range < 2^32 and r = range >> 16 < 2^16, q < 2^16.

For each record t, let q = row t, s = symbol t, cum = q[0] + ... + q[s-1].

Encoder:

low = 0; range = 0xFFFFFFFF; have_cache = 0; pending = 0
for t in 0..n-1:
    r = range >> 16
    low += r * cum
    range = (s == K-1) ? range - r*cum : r * q[s]     # last symbol takes the slack
    while range < TOP: range <<= 8; shift_low()
repeat 5 times: shift_low()

shift_low():
    if (low mod 2^32) < 0xFF000000 or (low >> 32) != 0:
        carry = low >> 32                               # 0 or 1
        if have_cache: emit(cache + carry)
        while pending > 0: emit(0xFF + carry mod 256); pending -= 1
        cache = (low >> 24) & 0xFF; have_cache = 1
    else:
        pending += 1
    low = (low & 0x00FFFFFF) << 8

There is no leading dummy byte: the first shift_low that resolves records the cache without emitting. A carry while have_cache = 0 cannot occur and is refused (CORRUPT).

Decoder (payload bytes b[0..L-1]):

if L < 4: refuse TRUNC
code = b[0..3] big-endian; pos = 4; range = 0xFFFFFFFF
for t in 0..n-1:
    if code >= range: refuse CORRUPT
    r = range >> 16
    s = smallest x in 0..K-2 with code < r * (q[0]+...+q[x]); else s = K-1
    code -= r * cum(s)
    range = (s == K-1) ? range - r*cum(s) : r * q[s]
    while range < TOP:
        if pos == L: refuse TRUNC
        code = (code << 8) | b[pos++]; range <<= 8
termination: code must be 0 (else CORRUPT); pos must equal L (else TRAIL)

6. Coder B: rANS (TCA1, id 2)

Constants: L = 2^23, scale 16 (M = 65536), byte renormalization. State x is u32 in [L, 2^31) between steps.

Encoder (symbols processed in reverse, bytes written back to front):

x = L
for t in n-1 down to 0:
    f = q[s]; c = cum(s)
    x_max = ((L >> 16) << 8) * f        # = 32768 * f
    while x >= x_max: push_front(x & 0xFF); x >>= 8
    x = ((x / f) << 16) + (x mod f) + c
push_front the 4 bytes of x, little-endian (so the payload starts x0 x1 x2 x3)

Decoder:

if L_bytes < 4: refuse TRUNC
x = b[0..3] little-endian; pos = 4
if x < 2^23 or x >= 2^31: refuse CORRUPT
for t in 0..n-1:
    m = x mod 65536
    s = the x with cum(x) <= m < cum(x) + q[x]
    x = q[s] * (x >> 16) + m - cum(s)
    while x < L:
        if pos == L_bytes: refuse TRUNC
        x = (x << 8) | b[pos++]
termination: x must equal L (else CORRUPT); pos must equal L_bytes (else TRAIL)

7. Producer context rule (predictor side)

tc_produce reimplements the static key walk of ty_model.c and cross-checks that its ideal length equals ty_model_score exactly (else CORRUPT).

8. Ideal length

Ideal(t0..t1) = sum over t of ub(q_t[x_t]) micro-bits (ty_ubits_q16, the same integer method the TY-2 receipts use). ub(q) for q in 1..65536 is this exact rule and no other (profile ideal_codelength_method, EVALUATOR.md section 6.2):

  1. ip = index of the highest set bit of q; m = q << (62 - ip); frac = 0.
  2. For i = 1..32: m = (m * m) >> 62 (128-bit product, truncated); if m >= 2^63, set bit (32 - i) of frac and m = m >> 1.
  3. log2 = (ip << 32) | frac; x = (16 << 32) - log2; ub(q) = (x * 1000000 + 2^31) >> 32.

Test values: ub(1) = 16000000, ub(32768) = 1000000, ub(65536) = 0 (defined for completeness; a coded symbol never has q = 65536, since every other entry of its row is at least 1), ub(43481) = 591903, ub(46819) = 485194. The last two are one micro-bit above the correctly rounded -log2(q/65536) x 10^6 (591902, 485193); every other q agrees, so each symbol is within 0.501 ub of the exact value. The rule, not correct rounding, is the definition. Bits = ub / 10^6.

9. Envelope (frozen with the profile, coder_envelope)

Overhead = coded bits (whole file, section 4) minus ideal bits. N = symbols in the unit. Measured with make turing-exp001-envelope on dev seeds 1 to 7 only (seeds 8 to 10 untouched), profile digest all zeros (the numbers do not depend on it), three models: uniform K=9 (83c04b...), TY-2 baseline (59ae93...), TY-2 candidate (64a57b...). Units: 21 whole files (N = 1,945,549 to 2,172,776) and 3,894 crumbs (1,298 per model, N = 403 to 20,004), each crumb coded as its own file with its own 56-byte header.

Per-crumb coding (the S4 per-crumb envelope and sum checks) builds no new TPS1 file and no new digest. For the crumb with symbols [lo, hi) the evaluator takes rows lo..hi-1 of the one parsed file TPS1 (already checked, section 2) and symbols lo..hi-1 of the parsed TSY1, runs both coders on that slice from a fresh coder state (the section 5 and 6 payloads, no header written), decodes the payload with the same rows and compares it with the symbols, and counts the crumb as payload bytes plus 56, the size of a section 4 header. A crumb header would bind the file TPS1 digest, which the file-level BINDING check (S3) already covers. Per-crumb coded bytes are recomputed, not stored, and are reported in ideal_lengths.json (crumbs list) and the envelope result (tools/turing_cal_eval.c, per-crumb loop).

Per file, overhead in bits (min / mean / max over 7 seeds):

ModelRangerANS
uniform1425 / 1479 / 1543-641 / -570 / -509
baseline1341 / 1391 / 1433473 / 477 / 482
candidate1457 / 1528 / 1582474 / 477 / 481

Per-symbol rate after removing the 448-bit header, (ovh - 448) / N: range 4.5e-4 to 5.4e-4; rANS 1.1e-5 to 1.7e-5 on the TY-2 models and -5.0e-4 to -4.9e-4 on uniform.

Per crumb, (ovh - 448) in bits (min / mean / max, all models): range 19.4 / 33.0 / 42.9; rANS 13.7 / 26.2 / 32.2.

Where the overhead comes from:

The envelope (one two-sided band for both coders, any unit; the profile value):

| overhead - 448 | <= 64 + 1.0e-3 x N   bits

What N counts in each scope (from tools/turing_cal_eval.c, env_band(n) = 64e6 + 1000 x n in micro-bits):

ScopeNOverhead compared with the bandFailure
per codernot a separate N: the same band and the same N are applied once for the range coder and once for the rANS codercoded bytes x 8 minus ideal length, per codereither coder outside the band on any unit fails S4
per fileN = the number of symbols in the whole checked file (s.n, all crumbs of that TPS1/TSY1 file)coded file bytes including the 56-byte headerthat file fails S4
per crumbN = the number of symbols in that one crumb (hi - lo of its row range)crumb coded bytes plus the 56-byte header (no file is written)that crumb fails S4

A coder whose overhead leaves this band on any unit of a frozen run fails S4 for that unit. There is no alternative bound: the single two-sided band above is the only envelope (a one-sided range bound was considered and not adopted).

Measured on all seven candidates (CAL-0 review Q4; make turing-exp001-envelope, calibration/scripts/envelope_summary.sh): each candidate over the 7 dev files of seeds 1-7 and their 1,298 crumbs, 1,305 units per coder. Largest |overhead - 448| in bits over all units, and violations:

CandidateRangerANSViolations
B0_uniform1094.61089.40
B1_order0953.532.70
B2_order1984.733.50
B3_heuristic1217.8790.20
M_candidate1133.833.40
M_mem1134.5569.50
M_mem_seed1547.871.20

The closest unit of every candidate and coder is about 32 bits inside the bound. The dev models were fit in-sample on seeds 1-7; the numbers measure the coders on these streams, not model quality.

Throughput on the Spark (single thread, -O2, millions of symbols per second, whole dev files): range encode 95 to 104, decode 137 to 143; rANS encode 103 to 120, decode 126 to 131.

10. Refusal codes

CodeNameMeaning
-101ARGbad argument
-102IOfile or allocation failure
-103FORMATbad magic, version, reserved field, size, K or qbits
-104DIGESTtrailer digest does not match
-105NORMrow sum or norm_sum is not 65536
-106ZEROa probability entry is 0
-107PROFILEprofile digest differs from expected
-108MODELmodel digest differs from expected
-109DATASETdataset digest differs (expected, or TPS1 vs TSY1)
-110COUNTcount mismatch
-111HEADERcoded header missing or altered
-112BINDINGcoded file bound to another TPS1
-113TRUNCbitstream too short
-114TRAILunconsumed bytes after the last symbol
-115CORRUPTdecoder state invalid / termination check failed
-116SYMBOLsymbol outside 0..K-1, or decoded symbols differ
-117INDEXobservation_index is not the record position

Every refusal also returns a one-line reason.

11. CLI (turing-coder)

Exit 0 = accepted, 2 = refused (reason on stderr), 1 = usage.