~/bend-docscommunity

LAWS.bend open laws/TODOs

raw source on the hub · import bend-safetensors-reader@0.1.0.0/LAWS.bend as LAWS

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.

3 imports
import Base
import ./w64.bend as W
import ./header.bend as H

Laws

law tile_agrees provedin PROOF.bendsource · line 33 · raw

@+es:List<&2, 0xd8619c297e019f1c512e443a1b7ff426/header.Ent> -> @+pos:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> {Ok(0xd8619c297e019f1c512e443a1b7ff426/header.tile(es, pos)) == 0xd8619c297e019f1c512e443a1b7ff426/header.Ends(es, pos) : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}

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 parse_sound provedin PROOF.bendsource · line 42 · raw

@bs:List<&2, U32> -> Sound(0xd8619c297e019f1c512e443a1b7ff426/header.parse(bs))

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 w64_edges provedin PROOF.bendsource · line 55 · raw

Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.add(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 4294967295}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64.of(1)) == Some{0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1, 0}} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.add(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{4294967294, 4294967295}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64.of(1)) == Some{0xd8619c297e019f1c512e443a1b7ff426/w64.W64{4294967295, 0}} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.add(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{4294967295, 4294967295}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64.of(1)) == None{} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.mul32(4294967295, 4294967295) == 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{4294967294, 1} : 0xd8619c297e019f1c512e443a1b7ff426/w64.W64}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.mul(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1, 2}, 2147483648) == Some{0xd8619c297e019f1c512e443a1b7ff426/w64.W64{2147483649, 0}} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.mul(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{2147483648, 0}, 2) == None{} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.digit(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{429496729, 2576980377}, 5) == Some{0xd8619c297e019f1c512e443a1b7ff426/w64.W64{4294967295, 4294967295}} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.digit(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{429496729, 2576980377}, 6) == None{} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.digit(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 2714116104}, 0) == Some{0xd8619c297e019f1c512e443a1b7ff426/w64.W64{6, 1371357264}} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.cells(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1, 1}) == 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 1073741825} : 0xd8619c297e019f1c512e443a1b7ff426/w64.W64}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.cells(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{4294967295, 4294967293}) == 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1073741824, 0} : 0xd8619c297e019f1c512e443a1b7ff426/w64.W64}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.sub(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1, 0}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64.of(1)) == 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 4294967295} : 0xd8619c297e019f1c512e443a1b7ff426/w64.W64}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.is_lt(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 4294967295}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1, 0}) == True{} : Bool}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.is_le(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1, 0}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 4294967295}) == False{} : Bool}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.add(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{2147483648, 0}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{2147483648, 0}) == None{} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.mul(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1431655765, 4294967295}, 3) == None{} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.cells(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 8}) == 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 2} : 0xd8619c297e019f1c512e443a1b7ff426/w64.W64}, Pair({0xd8619c297e019f1c512e443a1b7ff426/w64.W64.is_eq(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1, 2}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1, 2}) == True{} : Bool}, {0xd8619c297e019f1c512e443a1b7ff426/w64.W64.is_eq(0xd8619c297e019f1c512e443a1b7ff426/w64.W64{1, 2}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{2, 1}) == False{} : Bool}))))))))))))))))))

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 nbytes_edges provedin PROOF.bendsource · line 64 · raw

Pair({0xd8619c297e019f1c512e443a1b7ff426/header.nbytes(0xd8619c297e019f1c512e443a1b7ff426/header.DBF16{}, [5376, 21504]) == Some{0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 231211008}} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/header.nbytes(0xd8619c297e019f1c512e443a1b7ff426/header.DF32{}, [0, 4]) == Some{0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 0}} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/header.nbytes(0xd8619c297e019f1c512e443a1b7ff426/header.DU8{}, []) == Some{0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 1}} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/header.nbytes(0xd8619c297e019f1c512e443a1b7ff426/header.DI64{}, [2147483648, 536870912]) == Some{0xd8619c297e019f1c512e443a1b7ff426/w64.W64{2147483648, 0}} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/header.nbytes(0xd8619c297e019f1c512e443a1b7ff426/header.DI64{}, [2147483648, 1073741824]) == None{} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, Pair({0xd8619c297e019f1c512e443a1b7ff426/header.nbytes(0xd8619c297e019f1c512e443a1b7ff426/header.DF64{}, [4294967295, 4294967295]) == None{} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}, {0xd8619c297e019f1c512e443a1b7ff426/header.nbytes(0xd8619c297e019f1c512e443a1b7ff426/header.DF64{}, [2147483648, 2147483648, 8]) == None{} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>}))))))

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 dtype_names provedin PROOF.bendsource · line 71 · raw

@+d:0xd8619c297e019f1c512e443a1b7ff426/header.DType -> {0xd8619c297e019f1c512e443a1b7ff426/header.DType.of(0xd8619c297e019f1c512e443a1b7ff426/header.DType.name(d)) == Some{d} : Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/header.DType>}

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_sizes provedin PROOF.bendsource · line 75 · raw

{[0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DBool{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DU8{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DI8{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DF8E5M2{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DF8E4M3{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DI16{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DU16{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DF16{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DBF16{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DI32{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DU32{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DF32{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DF64{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DI64{}), 0xd8619c297e019f1c512e443a1b7ff426/header.DType.size(0xd8619c297e019f1c512e443a1b7ff426/header.DU64{})] == [1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 8, 8, 8] : List<&2, U32>}

law parse_example provedin PROOF.bendsource · line 92 · raw

{0xd8619c297e019f1c512e443a1b7ff426/header.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{0xd8619c297e019f1c512e443a1b7ff426/header.Header{[0xd8619c297e019f1c512e443a1b7ff426/header.Ent{"a", 0xd8619c297e019f1c512e443a1b7ff426/header.DF32{}, [1], 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 0}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 4}}, 0xd8619c297e019f1c512e443a1b7ff426/header.Ent{"bé", 0xd8619c297e019f1c512e443a1b7ff426/header.DBF16{}, [2, 3], 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 4}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 16}}, 0xd8619c297e019f1c512e443a1b7ff426/header.Ent{"z", 0xd8619c297e019f1c512e443a1b7ff426/header.DU8{}, [0], 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 16}, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 16}}], [0xd8619c297e019f1c512e443a1b7ff426/header.KV{"format", "pt"}], 0xd8619c297e019f1c512e443a1b7ff426/w64.W64{0, 16}}} : Result<&2, &2, String, 0xd8619c297e019f1c512e443a1b7ff426/header.Header>}

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).

Definitions

def Ok source · line 11 · raw

@r:Result<&2, &2, String, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64> -> Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>

a check's answer as a Maybe: the end it reached, or None for a refusal

def Sound source · line 20 · raw

@r:Result<&2, &2, String, 0xd8619c297e019f1c512e443a1b7ff426/header.Header> -> Type

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 ascii source · line 79 · raw

@s:String -> List<&2, U32>

a String's code points as bytes (for an ASCII string, its bytes)