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/calibration/docs/CODER_SPEC.md # 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 / calibration / docs / CODER_SPEC.md` 359 lines ยท 16167 bytes Raw Blame History Rendered 1 # EXP-001 coder specification (lane B) 2 3 Status: implemented and tested on branch `feat/turing-exp001-b`. Sections 1 to 8 4 are normative. Section 9 records the measured envelope; its rule is the profile field 5 `coder_envelope` and freezes with the profile. 6 7 Implementation: `src/turing/tc_pstream.[ch]` (formats), `src/turing/tc_range.[ch]` 8 (coder A), `src/turing/tc_rans.[ch]` (coder B), `src/turing/tc_produce.[ch]` 9 (predictor side), `src/turing/tc_tool.c` (CLI `turing-coder`). Tests: 10 `tests/turing/test_tc.c`, `tests/turing/test_tc_produce.c`, make target 11 `test-turing-exp001-coders` in `mk/turing_exp001_b.mk`. 12 13 ## 1. Scope and roles 14 15 A predictor (a TYM0 model walking a CTR1 trace) emits a probability stream 16 (TPS1) and the observed symbols (TSY1) before any coding happens. Two reference 17 coders then turn (TPS1, TSY1) into a coded file, and decode (TPS1, coded file) 18 back to symbols. The coders never see the model: the coder sources do not 19 include `ty_model.h`, the make target greps for this, and `test_tc` is linked 20 without `ty_model.c`. 21 22 The TPS1 depends on the observed data (context keys come from past symbols), so 23 decoding with a TPS1 in hand checks the coder arithmetic and the bitstream, not 24 the causality of the predictor. Causality is a property of the producer 25 (section 7), which uses only past symbols of the same crumb. 26 27 Conventions: all integers little-endian. SHA-256 as in `src/sha256.c`. 28 "Domain digest" D(tag, bytes) = SHA-256(tag || 0x00 || bytes). 29 30 ## 2. Probability stream TPS1 31 32 File = header (128 bytes) || count records || trailer (32 bytes). 33 34 | Offset | Size | Field | Value | 35 |---|---|---|---| 36 | 0 | 4 | magic | `TPS1` | 37 | 4 | 2 | version | 1 | 38 | 6 | 1 | K | alphabet size, 2..16 | 39 | 7 | 1 | qbits | 16 | 40 | 8 | 4 | flags | 0 | 41 | 12 | 8 | count | number of records n | 42 | 20 | 4 | record_bytes | 24 + 2K | 43 | 24 | 4 | reserved | 0 | 44 | 28 | 32 | profile_digest | the 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 | 45 | 60 | 32 | model_digest | `ty_model_digest` = SHA-256("turing.ymodel.v0" 0x00 \|\| .tym bytes) | 46 | 92 | 32 | dataset_digest | plain SHA-256 of the CTR1 file bytes | 47 | 124 | 4 | reserved | 0 | 48 49 Record t (t = 0..n-1), 24 + 2K bytes: 50 51 | Offset | Size | Field | 52 |---|---|---| 53 | 0 | 8 | observation_index, must equal t | 54 | 8 | 8 | context_key (section 7) | 55 | 16 | 4 | crumb ordinal in the dataset file (0-based, section 7) | 56 | 20 | 4 | norm_sum, must equal 65536 | 57 | 24 | 2K | q[0..K-1], u16 each | 58 59 Probability of symbol x at step t is q[x] / 65536. Every entry must be at least 60 1 (zero-probability policy: refuse) and the K entries must sum to exactly 65536 61 (normalization policy: exact). The float vector and the quantized vector are the 62 same object: TYM0 models are quantized tables. 63 64 Trailer = D("turing.tc.pstream.v1", header || records). This value is "the TPS1 65 digest"; coded files bind to it. 66 67 A reader refuses, in this order: size below 160 bytes or wrong magic (FORMAT); 68 trailer mismatch (DIGEST); version, qbits, flags, reserved or K out of range 69 (FORMAT); record_bytes or file size disagreeing with count (FORMAT); then per 70 record: observation_index != t (INDEX), any q = 0 (ZERO), sum or norm_sum != 65536 71 (NORM). 72 73 ## 3. Symbol file TSY1 74 75 File = header (56 bytes) || n symbol bytes || trailer (32 bytes). 76 77 | Offset | Size | Field | Value | 78 |---|---|---|---| 79 | 0 | 4 | magic | `TSY1` | 80 | 4 | 2 | version | 1 | 81 | 6 | 1 | K | 2..16 | 82 | 7 | 1 | reserved | 0 | 83 | 8 | 8 | count | n | 84 | 16 | 8 | reserved | 0 | 85 | 24 | 32 | dataset_digest | as in TPS1 | 86 87 Trailer = D("turing.tc.symbols.v1", header || symbols). Every symbol must be 88 below K. A (TPS1, TSY1) pair is valid only when K, count and dataset_digest agree 89 (else COUNT or DATASET). 90 91 ## 4. Coded file header (both coders) 92 93 Coded file = header (56 bytes) || payload. 94 95 | Offset | Size | Field | 96 |---|---|---| 97 | 0 | 4 | magic: `TCR1` (range) or `TCA1` (rANS) | 98 | 4 | 1 | version = 1 | 99 | 5 | 1 | coder_id: 1 = range, 2 = rANS | 100 | 6 | 2 | reserved = 0 | 101 | 8 | 8 | count = TPS1 count | 102 | 16 | 32 | TPS1 digest (section 2 trailer) | 103 | 48 | 8 | payload_bytes | 104 105 The decoder checks, in this order: length below 56 or wrong magic (HEADER); 106 version, coder_id or reserved (HEADER); TPS1 digest (BINDING); count (COUNT); 107 file shorter than 56 + payload_bytes (TRUNC); file longer (TRAIL). 108 109 **Accounting rule.** Coded length in bits = 8 x (every byte of the coded file), 110 header and flush included. Overhead = coded bits minus the ideal length 111 (section 8). The 56-byte header alone is 448 bits. 112 113 ## 5. Coder A: range coder (`TCR1`, id 1) 114 115 State: `low` u64 (only 33 bits used), `range` u32, `cache` u8, `have_cache` 116 flag, `pending` count of deferred 0xFF bytes. Constant TOP = 2^24. All products 117 below fit in u32 because range < 2^32 and r = range >> 16 < 2^16, q < 2^16. 118 119 For each record t, let q = row t, s = symbol t, cum = q[0] + ... + q[s-1]. 120 121 Encoder: 122 123 ``` 124 low = 0; range = 0xFFFFFFFF; have_cache = 0; pending = 0 125 for t in 0..n-1: 126 r = range >> 16 127 low += r * cum 128 range = (s == K-1) ? range - r*cum : r * q[s] # last symbol takes the slack 129 while range < TOP: range <<= 8; shift_low() 130 repeat 5 times: shift_low() 131 132 shift_low(): 133 if (low mod 2^32) < 0xFF000000 or (low >> 32) != 0: 134 carry = low >> 32 # 0 or 1 135 if have_cache: emit(cache + carry) 136 while pending > 0: emit(0xFF + carry mod 256); pending -= 1 137 cache = (low >> 24) & 0xFF; have_cache = 1 138 else: 139 pending += 1 140 low = (low & 0x00FFFFFF) << 8 141 ``` 142 143 There is no leading dummy byte: the first `shift_low` that resolves records the 144 cache without emitting. A carry while `have_cache` = 0 cannot occur and is 145 refused (CORRUPT). 146 147 Decoder (payload bytes b[0..L-1]): 148 149 ``` 150 if L < 4: refuse TRUNC 151 code = b[0..3] big-endian; pos = 4; range = 0xFFFFFFFF 152 for t in 0..n-1: 153 if code >= range: refuse CORRUPT 154 r = range >> 16 155 s = smallest x in 0..K-2 with code < r * (q[0]+...+q[x]); else s = K-1 156 code -= r * cum(s) 157 range = (s == K-1) ? range - r*cum(s) : r * q[s] 158 while range < TOP: 159 if pos == L: refuse TRUNC 160 code = (code << 8) | b[pos++]; range <<= 8 161 termination: code must be 0 (else CORRUPT); pos must equal L (else TRAIL) 162 ``` 163 164 ## 6. Coder B: rANS (`TCA1`, id 2) 165 166 Constants: L = 2^23, scale 16 (M = 65536), byte renormalization. State x is u32 167 in [L, 2^31) between steps. 168 169 Encoder (symbols processed in reverse, bytes written back to front): 170 171 ``` 172 x = L 173 for t in n-1 down to 0: 174 f = q[s]; c = cum(s) 175 x_max = ((L >> 16) << 8) * f # = 32768 * f 176 while x >= x_max: push_front(x & 0xFF); x >>= 8 177 x = ((x / f) << 16) + (x mod f) + c 178 push_front the 4 bytes of x, little-endian (so the payload starts x0 x1 x2 x3) 179 ``` 180 181 Decoder: 182 183 ``` 184 if L_bytes < 4: refuse TRUNC 185 x = b[0..3] little-endian; pos = 4 186 if x < 2^23 or x >= 2^31: refuse CORRUPT 187 for t in 0..n-1: 188 m = x mod 65536 189 s = the x with cum(x) <= m < cum(x) + q[x] 190 x = q[s] * (x >> 16) + m - cum(s) 191 while x < L: 192 if pos == L_bytes: refuse TRUNC 193 x = (x << 8) | b[pos++] 194 termination: x must equal L (else CORRUPT); pos must equal L_bytes (else TRAIL) 195 ``` 196 197 ## 7. Producer context rule (predictor side) 198 199 `tc_produce` reimplements the static key walk of `ty_model.c` and cross-checks 200 that its ideal length equals `ty_model_score` exactly (else CORRUPT). 201 202 - Events are the CTR1 events in file order. The crumb ordinal starts at 0 and 203 increments at every event with `first` set, except event 0. `first` means 204 CTR1 event_index = 0 (DATA_FORMAT.md section 1.2; the file must start with one). 205 - History p[0..4] (PREV1..PREV5) resets to K (the "none" value) at every `first` 206 event, then after the event p shifts and p[0] = symbol. 207 - Key = mixed radix, low digit first, over the fields in the model mask: 208 OP (radix 16), DEPTH (radix TY_DEPTH_CLIP + 1 = 8), PREV1..PREV5 (radix K+1 209 each), and finally POS = (crumb << 20) | min(idx, 2^20 - 1) as the top digit. 210 - Row = the model row whose key matches (binary search over sorted keys), else 211 the model's default row. Every entry must lie in 1..65535 (else ZERO). 212 213 ## 8. Ideal length 214 215 Ideal(t0..t1) = sum over t of `ub(q_t[x_t])` micro-bits (`ty_ubits_q16`, the same 216 integer method the TY-2 receipts use). ub(q) for q in 1..65536 is this exact rule and no 217 other (profile ideal_codelength_method, EVALUATOR.md section 6.2): 218 219 1. ip = index of the highest set bit of q; m = q << (62 - ip); frac = 0. 220 2. For i = 1..32: m = (m * m) >> 62 (128-bit product, truncated); if m >= 2^63, 221 set bit (32 - i) of frac and m = m >> 1. 222 3. log2 = (ip << 32) | frac; x = (16 << 32) - log2; ub(q) = (x * `1000000` + 2^31) >> 32. 223 224 Test values: ub(1) = `16000000`, ub(32768) = `1000000`, ub(65536) = 0 (defined for completeness; a coded 225 symbol never has q = 65536, since every other entry of its row is at least 1), ub(43481) = 591903, 226 ub(46819) = 485194. The last two are one micro-bit above the correctly rounded 227 -log2(q/65536) x 10^6 (591902, 485193); every other q agrees, so each symbol is within 228 0.501 ub of the exact value. The rule, not correct rounding, is the definition. Bits = ub / 10^6. 229 230 ## 9. Envelope (frozen with the profile, coder_envelope) 231 232 Overhead = coded bits (whole file, section 4) minus ideal bits. N = symbols in 233 the unit. Measured with `make turing-exp001-envelope` on dev seeds 1 to 7 only 234 (seeds 8 to 10 untouched), profile digest all zeros (the numbers do not depend 235 on it), three models: uniform K=9 (`83c04b...`), TY-2 baseline (`59ae93...`), 236 TY-2 candidate (`64a57b...`). Units: 21 whole files (N = 1,945,549 to 2,172,776) 237 and 3,894 crumbs (1,298 per model, N = 403 to 20,004), each crumb coded as its 238 own file with its own 56-byte header. 239 240 Per-crumb coding (the S4 per-crumb envelope and sum checks) builds no new TPS1 file and no new 241 digest. For the crumb with symbols [lo, hi) the evaluator takes rows lo..hi-1 of the one parsed file 242 TPS1 (already checked, section 2) and symbols lo..hi-1 of the parsed TSY1, runs both coders on that 243 slice from a fresh coder state (the section 5 and 6 payloads, no header written), decodes the payload 244 with the same rows and compares it with the symbols, and counts the crumb as payload bytes plus 56, 245 the size of a section 4 header. A crumb header would bind the file TPS1 digest, which the file-level 246 BINDING check (S3) already covers. Per-crumb coded bytes are recomputed, not stored, and are reported 247 in ideal_lengths.json (crumbs list) and the envelope result (`tools/turing_cal_eval.c`, per-crumb loop). 248 249 Per file, overhead in bits (min / mean / max over 7 seeds): 250 251 | Model | Range | rANS | 252 |---|---|---| 253 | uniform | 1425 / 1479 / 1543 | -641 / -570 / -509 | 254 | baseline | 1341 / 1391 / 1433 | 473 / 477 / 482 | 255 | candidate | 1457 / 1528 / 1582 | 474 / 477 / 481 | 256 257 Per-symbol rate after removing the 448-bit header, (ovh - 448) / N: 258 range 4.5e-4 to 5.4e-4; rANS 1.1e-5 to 1.7e-5 on the TY-2 models and 259 -5.0e-4 to -4.9e-4 on uniform. 260 261 Per crumb, (ovh - 448) in bits (min / mean / max, all models): range 262 19.4 / 33.0 / 42.9; rANS 13.7 / 26.2 / 32.2. 263 264 Where the overhead comes from: 265 - Range: the truncation r = range >> 16 loses up to 2^-8 of the interval per 266 symbol in the worst case (range at 2^24) and about 5e-4 bits per symbol on 267 this data; flush adds up to 40 bits. 268 - rANS: rounding of x / f moves each step slightly up or down. The effect is 269 signed and favours low-index symbols; the uniform model on this data (skewed 270 toward low symbols) codes about 5e-4 bits per symbol BELOW ideal. The 4-byte 271 final state adds 32 bits. So the envelope must be two-sided. 272 273 **The envelope (one two-sided band for both coders, any unit; the profile value):** 274 275 | overhead - 448 | <= 64 + 1.0e-3 x N bits 276 277 - a = 448 bits header (exact, counted) plus 64 bits constant margin 278 (observed per-crumb constant part at most 43 bits). 279 - b = 1.0e-3 bits per symbol, about 1.9x the largest observed magnitude 280 (5.4e-4). Empirical for K = 9 and these models, not proven: adversarial 281 sequences can reach about 5.6e-3 (range, interval pinned near 2^24) and 2.8e-3 282 (rANS, f near M/2 with x near L) bits per symbol. 283 - Every one of the 21 files and 3,894 crumbs fits. Closest per-crumb approach: 284 32 bits inside the bound (N = 410). Closest per-file: 973 bits inside. 285 - Observed maxima to record next to the bound: range file 1582 bits, rANS file 286 482 bits, rANS file minimum -641 bits; crumb max 491 (range) and 480 (rANS) 287 bits including the header. 288 289 What N counts in each scope (from `tools/turing_cal_eval.c`, `env_band(n)` = 64e6 + 1000 x n in micro-bits): 290 291 | Scope | N | Overhead compared with the band | Failure | 292 |---|---|---|---| 293 | per coder | not a separate N: the same band and the same N are applied once for the range coder and once for the rANS coder | coded bytes x 8 minus ideal length, per coder | either coder outside the band on any unit fails S4 | 294 | per file | N = 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 header | that file fails S4 | 295 | per crumb | N = 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 | 296 297 A coder whose overhead leaves this band on any unit of a frozen run fails S4 for 298 that unit. There is no alternative bound: the single two-sided band above is the 299 only envelope (a one-sided range bound was considered and not adopted). 300 301 Measured on all seven candidates (CAL-0 review Q4; `make turing-exp001-envelope`, 302 `calibration/scripts/envelope_summary.sh`): each candidate over the 7 dev files of 303 seeds 1-7 and their 1,298 crumbs, 1,305 units per coder. Largest |overhead - 448| 304 in bits over all units, and violations: 305 306 | Candidate | Range | rANS | Violations | 307 |---|---:|---:|---:| 308 | B0_uniform | 1094.6 | 1089.4 | 0 | 309 | B1_order0 | 953.5 | 32.7 | 0 | 310 | B2_order1 | 984.7 | 33.5 | 0 | 311 | B3_heuristic | 1217.8 | 790.2 | 0 | 312 | M_candidate | 1133.8 | 33.4 | 0 | 313 | M_mem | 1134.5 | 569.5 | 0 | 314 | M_mem_seed1 | 547.8 | 71.2 | 0 | 315 316 The closest unit of every candidate and coder is about 32 bits inside the bound. 317 The dev models were fit in-sample on seeds 1-7; the numbers measure the coders on 318 these streams, not model quality. 319 320 Throughput on the Spark (single thread, -O2, millions of symbols per second, 321 whole dev files): range encode 95 to 104, decode 137 to 143; rANS encode 103 to 322 120, decode 126 to 131. 323 324 ## 10. Refusal codes 325 326 | Code | Name | Meaning | 327 |---|---|---| 328 | -101 | ARG | bad argument | 329 | -102 | IO | file or allocation failure | 330 | -103 | FORMAT | bad magic, version, reserved field, size, K or qbits | 331 | -104 | DIGEST | trailer digest does not match | 332 | -105 | NORM | row sum or norm_sum is not 65536 | 333 | -106 | ZERO | a probability entry is 0 | 334 | -107 | PROFILE | profile digest differs from expected | 335 | -108 | MODEL | model digest differs from expected | 336 | -109 | DATASET | dataset digest differs (expected, or TPS1 vs TSY1) | 337 | -110 | COUNT | count mismatch | 338 | -111 | HEADER | coded header missing or altered | 339 | -112 | BINDING | coded file bound to another TPS1 | 340 | -113 | TRUNC | bitstream too short | 341 | -114 | TRAIL | unconsumed bytes after the last symbol | 342 | -115 | CORRUPT | decoder state invalid / termination check failed | 343 | -116 | SYMBOL | symbol outside 0..K-1, or decoded symbols differ | 344 | -117 | INDEX | observation_index is not the record position | 345 346 Every refusal also returns a one-line reason. 347 348 ## 11. CLI (`turing-coder`) 349 350 Exit 0 = accepted, 2 = refused (reason on stderr), 1 = usage. 351 352 - `pstream ` 353 - `encode ` 354 - `decode ` 355 - `verify ` 356 - encode, decode and verify accept `--profile HEX`, `--model HEX`, `--dataset HEX` 357 and refuse unless the TPS1 header carries those digests 358 - `envelope ` (CSV columns: unit, 359 model, index, n, ideal_ub, range_bytes, rans_bytes; bytes include the header)