# bend-safetensors-reader: the laws about the pure core (header.bend, w64.bend). # tile_agrees and parse_sound hold for every input; w64_edges and nbytes_edges check the arithmetic at literal # values, since Base has no theory of U32 arithmetic to prove it for every input. # Laws: tile_agrees, parse_sound, w64_edges, nbytes_edges, dtype_names, parse_example. import Base import ./w64.bend as W import ./header.bend as H # a check's answer as a Maybe: the end it reached, or None for a refusal def Ok(r: Result<&2, &2, String, W.W64>) -> Maybe<&2, W.W64>: match r: case Fail{why}: None{} case Done{n}: Some{n} # what an accepted header promises: its tensors, in the order given, tile the data section from 0 to its size, and # no two neighbours in name order share a name; a refusal promises nothing def Sound(r: Result<&2, &2, String, H.Header>) -> Type: match r: case Fail{why}: Unit case Done{H.Header{es, m, n}}: {H.Ends(es, W.W64.zero()) == Some{n} : Maybe<&2, W.W64>} & {H.dup(List.sort(~H.Ent, ~H.le_name, es)) == None{} : Maybe<&2, String>} # Guarantees: for any list of tensors and any start, the check tile reaches the same end as the spec Ends: the end of # the last tensor when each tensor starts where the one before it ends (the first at the start) and spans exactly # its element count times its dtype size, with no overflow; and it refuses exactly when Ends says None. # Why it matters: tile is what the reader runs, with error messages; Ends is the rule a reader of the code can check # at a glance. If they disagreed, a file with a gap, an overlap or a wrong size would load, and a tensor would be # read from bytes that belong to another tensor. law tile_agrees: for +es: List<&2, H.Ent> for +pos: W.W64 {Ok(H.tile(es, pos)) == H.Ends(es, pos) : Maybe<&2, W.W64>} # Guarantees: for any bytes, if parse accepts them as a header, the tensors it returns (sorted by offset) tile the # data section from byte 0 to exactly the size it returns, and the names sorted no two equal neighbours. # Why it matters: every read trusts these offsets: St.read seeks to start + begin and reads end - begin bytes. A # header that parse accepted without them could make two tensors share bytes, or a read run past the data. law parse_sound: for bs: List<&2, U32> Sound(H.parse(bs)) # Guarantees: 64-bit offsets carry and overflow correctly at the half boundaries: 2^32 - 1 + 1 carries into the high # half; 2^64 - 2^32 + 1 does too; 2^64 - 1 + 1 is refused; the full product of two 2^32 - 1 is 2^64 - 2^33 + 1; # (2^32 + 2) * 2^31 is 2^63 + 2^32 and 2^63 * 2 is refused; parsing 18446744073709551615 digit by digit gives # 2^64 - 1 and one more digit is refused; the real offset 27,141,161,040 parses to 6 * 2^32 + 1,371,357,264; # cells(2^32 + 1) is 2^30 + 1, cells(2^64 - 3) is 2^62 and cells(8) is 2; 2^32 - 1 borrows; order compares the # high half first; equality looks at both halves; and overflow is caught when only the high halves overflow (2^63 + # 2^63) and when the low product carries into a full high half ((0x55555555 * 2^32 + 2^32 - 1) * 3). # Why it matters: offsets in real checkpoints pass 4 GiB (27 GB here); a lost carry or a missed overflow would read # the wrong bytes, or accept a header whose numbers wrapped. law w64_edges: {W.W64.add(W.W64{0, 4294967295}, W.W64.of(1)) == Some{W.W64{1, 0}} : Maybe<&2, W.W64>} & {W.W64.add(W.W64{4294967294, 4294967295}, W.W64.of(1)) == Some{W.W64{4294967295, 0}} : Maybe<&2, W.W64>} & {W.W64.add(W.W64{4294967295, 4294967295}, W.W64.of(1)) == None{} : Maybe<&2, W.W64>} & {W.W64.mul32(4294967295, 4294967295) == W.W64{4294967294, 1} : W.W64} & {W.W64.mul(W.W64{1, 2}, 2147483648) == Some{W.W64{2147483649, 0}} : Maybe<&2, W.W64>} & {W.W64.mul(W.W64{2147483648, 0}, 2) == None{} : Maybe<&2, W.W64>} & {W.W64.digit(W.W64{429496729, 2576980377}, 5) == Some{W.W64{4294967295, 4294967295}} : Maybe<&2, W.W64>} & {W.W64.digit(W.W64{429496729, 2576980377}, 6) == None{} : Maybe<&2, W.W64>} & {W.W64.digit(W.W64{0, 2714116104}, 0) == Some{W.W64{6, 1371357264}} : Maybe<&2, W.W64>} & {W.W64.cells(W.W64{1, 1}) == W.W64{0, 1073741825} : W.W64} & {W.W64.cells(W.W64{4294967295, 4294967293}) == W.W64{1073741824, 0} : W.W64} & {W.W64.sub(W.W64{1, 0}, W.W64.of(1)) == W.W64{0, 4294967295} : W.W64} & {W.W64.is_lt(W.W64{0, 4294967295}, W.W64{1, 0}) == True{} : Bool} & {W.W64.is_le(W.W64{1, 0}, W.W64{0, 4294967295}) == False{} : Bool} & {W.W64.add(W.W64{2147483648, 0}, W.W64{2147483648, 0}) == None{} : Maybe<&2, W.W64>} & {W.W64.mul(W.W64{1431655765, 4294967295}, 3) == None{} : Maybe<&2, W.W64>} & {W.W64.cells(W.W64{0, 8}) == W.W64{0, 2} : W.W64} & {W.W64.is_eq(W.W64{1, 2}, W.W64{1, 2}) == True{} : Bool} & {W.W64.is_eq(W.W64{1, 2}, W.W64{2, 1}) == False{} : Bool} # Guarantees: a tensor's byte count is its element count times its dtype size: a BF16 5,376 x 21,504 weight is # 231,211,008 bytes; an F32 0 x 4 tensor is 0; a U8 scalar (shape []) is 1; an I64 2^31 x 2^29 tensor is 2^63; # and counts past 2^64 - 1 are refused: I64 2^31 x 2^30 (exactly 2^64), F64 (2^32 - 1) x (2^32 - 1), and F64 # 2^31 x 2^31 x 8. # Why it matters: tile and the spec Ends both measure tensors with nbytes, so tile_agrees cannot catch a bug inside # it; a wrong count, or a product that wrapped, would let a header claim the wrong bytes for a tensor. law nbytes_edges: {H.nbytes(H.DBF16{}, [5376, 21504]) == Some{W.W64{0, 231211008}} : Maybe<&2, W.W64>} & {H.nbytes(H.DF32{}, [0, 4]) == Some{W.W64{0, 0}} : Maybe<&2, W.W64>} & {H.nbytes(H.DU8{}, []) == Some{W.W64{0, 1}} : Maybe<&2, W.W64>} & {H.nbytes(H.DI64{}, [2147483648, 536870912]) == Some{W.W64{2147483648, 0}} : Maybe<&2, W.W64>} & {H.nbytes(H.DI64{}, [2147483648, 1073741824]) == None{} : Maybe<&2, W.W64>} & {H.nbytes(H.DF64{}, [4294967295, 4294967295]) == None{} : Maybe<&2, W.W64>} & {H.nbytes(H.DF64{}, [2147483648, 2147483648, 8]) == None{} : Maybe<&2, W.W64>} # Guarantees: every dtype's name, read back, is that dtype (for every dtype); and (dtype_sizes) the byte sizes are BOOL, U8, I8, # F8_E5M2, F8_E4M3: 1; I16, U16, F16, BF16: 2; I32, U32, F32: 4; F64, I64, U64: 8. # Why it matters: a header names its dtypes as text; a name mapped to the wrong dtype, or a wrong size in the table, # would accept tensors with the wrong byte count, or read F16 as BF16. law dtype_names: for +d: H.DType {H.DType.of(H.DType.name(d)) == Some{d} : Maybe<&2, H.DType>} law dtype_sizes: {[H.DType.size(H.DBool{}), H.DType.size(H.DU8{}), H.DType.size(H.DI8{}), H.DType.size(H.DF8E5M2{}), H.DType.size(H.DF8E4M3{}), H.DType.size(H.DI16{}), H.DType.size(H.DU16{}), H.DType.size(H.DF16{}), H.DType.size(H.DBF16{}), H.DType.size(H.DI32{}), H.DType.size(H.DU32{}), H.DType.size(H.DF32{}), H.DType.size(H.DF64{}), H.DType.size(H.DI64{}), H.DType.size(H.DU64{})] == [1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 8, 8, 8] : List<&2, U32>} # a String's code points as bytes (for an ASCII string, its bytes) def ascii(s: String) -> List<&2, U32>: match s: case SNil{}: [] case SCon{Chr{c}, t}: c <> ascii(t) # Guarantees: one header, read in full: three tensors listed out of order (one with a \u-escaped name, one empty) and # a __metadata__ entry come back as exactly these tensors sorted by offset (a F32 [1] at 0..4, "b\u00e9" BF16 [2, 3] # at 4..16, z U8 [0] at 16..16), that metadata entry, and a data size of 16. # Why it matters: parse_sound says an accepted header is consistent, not that it says what the file says; a parser # that dropped a tensor, swapped names or dtypes, or reversed a shape would still be consistent. This pins the # whole path from bytes to header on one example (the tests compare thousands more with Python). law parse_example: {H.parse(ascii("{\"__metadata__\":{\"format\":\"pt\"},\"b\\u00e9\":{\"dtype\":\"BF16\",\"shape\":[2,3],\"data_offsets\":[4,16]},\"a\":{\"dtype\":\"F32\",\"shape\":[1],\"data_offsets\":[0,4]},\"z\":{\"dtype\":\"U8\",\"shape\":[0],\"data_offsets\":[16,16]}}")) == Done{H.Header{[H.Ent{"a", H.DF32{}, [1], W.W64{0, 0}, W.W64{0, 4}}, H.Ent{SCon{Chr{98}, SCon{Chr{233}, SNil{}}}, H.DBF16{}, [2, 3], W.W64{0, 4}, W.W64{0, 16}}, H.Ent{"z", H.DU8{}, [0], W.W64{0, 16}, W.W64{0, 16}}], [H.KV{"format", "pt"}], W.W64{0, 16}}} : Result<&2, &2, String, H.Header>}