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 / gpu / nvidia / coder / FORMAT.md
| 1 | <!-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception --> |
| 2 | # ERC1: coded E4M3 weight matrix, byte format |
| 3 | |
| 4 | One file holds one matrix of `rows x cols` bytes (any byte values; E4M3 weights are the target). |
| 5 | All integers are unsigned and little-endian. `R` is `rows`. There is no padding and no alignment. |
| 6 | |
| 7 | | Offset | Width | Field | Value | |
| 8 | |---|---|---|---| |
| 9 | | 0 | 4 | magic | bytes `45 52 43 31` (`ERC1`) | |
| 10 | | 4 | 2 | version | 1 | |
| 11 | | 6 | 2 | flags | 0 | |
| 12 | | 8 | 4 | rows | 1 .. 2^32 - 1 | |
| 13 | | 12 | 4 | cols | 1 .. 2^32 - 1 | |
| 14 | | 16 | 4 | prob_bits | 14 | |
| 15 | | 20 | 4 | matrix_crc32 | CRC-32 of the original `rows * cols` bytes, row-major | |
| 16 | | 24 | 8 | total_len | length of this file in bytes | |
| 17 | | 32 | 512 | freq | 256 x u16: frequency of byte value 0 .. 255; the sum is 2^14 | |
| 18 | | 544 | 4 (R + 1) | offset | R + 1 x u32: start of row r's stream, counted from the first stream byte; `offset[R]` is the length of the stream area | |
| 19 | | 548 + 4 R | 2 R | row_crc16 | R x u16: CRC-16 of row r's stream bytes | |
| 20 | | 548 + 6 R | 4 | header_crc32 | CRC-32 of bytes 0 .. 548 + 6 R - 1 (everything above) | |
| 21 | | 552 + 6 R | offset[R] | streams | row 0's stream, row 1's stream, ... | |
| 22 | |
| 23 | Header size is `552 + 6 R` bytes; the file is that plus `offset[R]` bytes. |
| 24 | |
| 25 | ## One row stream |
| 26 | |
| 27 | Static rANS over the model in `freq`, 14-bit probabilities, state in `[2^23, 2^31)`, byte-wise renormalisation. |
| 28 | Slot table: symbol `s` owns slots `cum[s] .. cum[s] + freq[s] - 1`, where `cum[s]` is the sum of `freq` below `s`. |
| 29 | |
| 30 | * The stream is a u32 (little-endian) initial state `x`, then renormalisation bytes in the order the decoder reads them. |
| 31 | * Decode `cols` symbols: `k = x & 16383`, `s` = the symbol that owns slot `k`, output `s`, |
| 32 | `x = freq[s] * (x >> 14) + (k - cum[s])`, then while `x < 2^23` read the next byte `b` and set `x = (x << 8) | b`. |
| 33 | * The encoder runs the same model backwards from `x = 2^23` and writes the final state first. |
| 34 | * The frequencies come from the matrix: `f = floor(count * 2^14 / n)`, raised to 1 when the symbol is present and `f` is 0, |
| 35 | then 1 added to or taken from the first largest symbol until the sum is 2^14. This is the rule of the measurement prototype |
| 36 | `evidence/FLASH-NEXT-WEIGHT-WASTE-20261118/rowrans.c`, so the stream bytes equal the prototype's. A decoder does not need the rule; it reads `freq`. |
| 37 | |
| 38 | Rows are independent: a row is decoded from its own stream, the table and `cols`. No state passes between rows. |
| 39 | |
| 40 | ## What the decoder checks, in this order |
| 41 | |
| 42 | Each refusal prints `erc: refused NAME` (and ` row N` for a row) on stderr and exits 3. |
| 43 | |
| 44 | | Name | Check | |
| 45 | |---|---| |
| 46 | | TRUNC | fewer than 32 bytes, or fewer bytes than the header size | |
| 47 | | MAGIC | bytes 0 .. 3 are not `ERC1` | |
| 48 | | VERSION | version is not 1 or flags is not 0 | |
| 49 | | FIELD | prob_bits is not 14, or rows or cols is 0 | |
| 50 | | LENGTH | total_len differs from the file length | |
| 51 | | HDRCRC | header_crc32 differs from the CRC-32 of the header, frequency table, offsets and row CRCs | |
| 52 | | FREQ | the 256 frequencies do not sum to 2^14 | |
| 53 | | OFFSET | `offset[0]` is not 0, a stream is shorter than 4 bytes, an offset decreases, or `offset[R]` is not the stream area length | |
| 54 | | ROWCRC | the CRC-16 of the row's stream bytes differs from `row_crc16[r]` (checked before the row is decoded) | |
| 55 | | STATE | the initial state is below 2^23 or at least 2^31 | |
| 56 | | EXHAUST | the decoder needs a byte past the end of the row's stream | |
| 57 | | LEFTOVER | the row is decoded and bytes of its stream are unread | |
| 58 | | FINAL | the row is decoded, every byte is read, and the state is not 2^23 (the encoder's start state) | |
| 59 | | MATCRC | after every row is decoded, the CRC-32 of the output differs from matrix_crc32 (only whole-matrix decode) | |
| 60 | |
| 61 | The checks from TRUNC to OFFSET run once when the file is opened (an `info`, `decode`, `decode-row` or `bench` call). |
| 62 | Decoding one row alone checks the header, the table, the offsets and that row; it cannot check matrix_crc32. |
| 63 | The STATE, EXHAUST, LEFTOVER and FINAL checks cost no bytes: a valid stream ends exactly at state 2^23 with no byte left. |
| 64 | They alone let some single-bit stream flips through (the flip scan receipt counts them on real expert rows), which is why each row has a CRC-16. |
| 65 | CRC-16 detects every single-bit flip in a stream of any length, and every burst of 16 bits or fewer. |
| 66 | |
| 67 | ## Checksums |
| 68 | |
| 69 | * CRC-32: IEEE 802.3, reflected, polynomial `0xEDB88320`, initial value `0xFFFFFFFF`, final xor `0xFFFFFFFF`. `123456789` gives `0xCBF43926`; `matrix_crc32` equals the trailer CRC-32 of `gzip` on the raw matrix. |
| 70 | * CRC-16: CRC-16/CCITT-FALSE, polynomial `0x1021`, initial value `0xFFFF`, most significant bit first, no final xor. `123456789` gives `0x29B1`. |
| 71 | |
| 72 | ## Size |
| 73 | |
| 74 | `file bytes = 552 + 6 R + sum of stream bytes`. The measurement prototype counts `512 + 4 R + sum of stream bytes` and no header, |
| 75 | row CRCs or matrix CRC, so an ERC1 file is larger than the prototype's count by `40 + 2 R` bytes for the same matrix |
| 76 | (32 header, 4 for the extra offset, 4 header CRC, `2 R` row CRCs; the 512 frequency bytes and the streams are identical). |
| 77 | |
| 78 | ## Limits |
| 79 | |
| 80 | The width of a field is the limit: rows and cols are u32, the stream area is at most 2^32 - 1 bytes (the encoder refuses a larger one, |
| 81 | message `the stream area is longer than a u32 offset can address`). The program itself sizes its buffers from the input. |