~/bend-docscommunity

header.bend checks

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

A safetensors header, from its bytes to a checked table of tensors. Pure: no IO, no @unsafe. The header is a JSON object: each key a tensor name whose value is {"dtype": .., "shape": [..], "data_offsets": [begin, end]}, plus an optional "__metadata__" object of strings. A header is accepted only if, sorted by offset, the tensors tile the data section from 0 with no gap or overlap, each tensor's bytes are its element count times its dtype's size, and no two tensors share a name (the checks of the reference Rust implementation).

3 imports
import Base
import ./w64.bend as W
import ./lex.bend as X

Types

type DType source · line 10 · raw

Data

type Ent source · line 79 · raw

Data

one tensor: data_offsets are bytes from the start of the data section, which follows the header

type KV source · line 121 · raw

Data

a metadata entry

type Part source · line 124 · raw

Data

type Acc source · line 127 · raw

Data

type Ps source · line 130 · raw

Data

Definitions

def DType.size source · line 28 · raw

@d:DType -> U32

bytes an element

def DType.name source · line 47 · raw

@d:DType -> String

the name a header uses

def dtypes source · line 65 · raw

List<&2, DType>

def dtype_go source · line 68 · raw

@+s:String -> @ds:List<&2, DType> -> Maybe<&2, DType>

def DType.of source · line 75 · raw

@+s:String -> Maybe<&2, DType>

def Ent.name source · line 82 · raw

@e:Ent -> String

def Ent.begin source · line 87 · raw

@e:Ent -> 0xd8619c297e019f1c512e443a1b7ff426/w64.W64

def Ent.end source · line 92 · raw

@e:Ent -> 0xd8619c297e019f1c512e443a1b7ff426/w64.W64

def mul_m source · line 99 · raw

@acc:Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64> -> @+k:U32 -> Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>

def count source · line 107 · raw

@sh:List<&2, U32> -> @acc:Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64> -> Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>

the product of the dimensions (1 for a scalar), or None past 2^64 - 1

def nbytes source · line 115 · raw

@d:DType -> @sh:List<&2, U32> -> Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>

the bytes a tensor of this dtype and shape holds, or None past 2^64 - 1

def tok_show source · line 149 · raw

@t:0xd8619c297e019f1c512e443a1b7ff426/lex.Tok -> String

def meta_done source · line 161 · raw

@a:Acc -> @m:List<&2, KV> -> Ps

def val_open source · line 168 · raw

@meta:Bool -> @a:Acc -> @k:String -> Ps

def dims source · line 176 · raw

@xs:List<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64> -> @acc:List<&2, U32> -> Maybe<&2, List<&2, U32>>

the dimensions as U32 (xs arrives reversed, so collecting onto acc restores the order), or None past 2^32 - 1

def set_dt source · line 183 · raw

@r:Maybe<&2, DType> -> @a:Acc -> @k:String -> @p:Part -> @+v:String -> Ps

def set_sh source · line 192 · raw

@r:Maybe<&2, List<&2, U32>> -> @a:Acc -> @k:String -> @p:Part -> Ps

def set_of source · line 201 · raw

@a:Acc -> @k:String -> @p:Part -> @xs:List<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64> -> Ps

def field_str source · line 208 · raw

@isdt:Bool -> @a:Acc -> @k:String -> @p:Part -> @+f:String -> @+v:String -> Ps

def field_list source · line 215 · raw

@issh:Bool -> @isof:Bool -> @a:Acc -> @k:String -> @p:Part -> @+f:String -> @xs:List<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64> -> Ps

def field_key source · line 224 · raw

@known:Bool -> @a:Acc -> @k:String -> @p:Part -> @+f:String -> Ps

def tensor_done source · line 231 · raw

@a:Acc -> @k:String -> @p:Part -> Ps

def is_field source · line 240 · raw

@+f:String -> Bool

def pstep source · line 243 · raw

@st:Ps -> @t:0xd8619c297e019f1c512e443a1b7ff426/lex.Tok -> Ps

def run source · line 302 · raw

@ts:List<&2, 0xd8619c297e019f1c512e443a1b7ff426/lex.Tok> -> @st:Ps -> Ps

def le_be source · line 315 · raw

@+ab:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @+ae:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @+bb:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @+be:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> Bool

by begin, then end, so an empty tensor sorts before the tensor that starts where it is

def le_begin source · line 318 · raw

@a:Ent -> @b:Ent -> Bool

def le_name source · line 323 · raw

@a:Ent -> @b:Ent -> Bool

def dup source · line 327 · raw

@es:List<&2, Ent> -> Maybe<&2, String>

the first name that two neighbours of a name-sorted list share

def same source · line 338 · raw

@r:Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64> -> @+end:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> Bool

def end_of source · line 345 · raw

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

def fits source · line 353 · raw

@e:Ent -> @+pos:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> Bool

whether e starts at pos and its byte range holds exactly its element count times its dtype's size

def ends_if source · line 358 · raw

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

def Ends source · line 367 · raw

@es:List<&2, Ent> -> @+pos:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>

the spec: where the tensors, in order, end if each starts where the one before it ends (the first at pos) and spans its element count times its dtype size; None if they do not

def why_size source · line 376 · raw

@ok:Bool -> @+nm:String -> Maybe<&2, String>

def why_at source · line 383 · raw

@ok:Bool -> @+nm:String -> @+b:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @+pos:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @size_ok:Bool -> Maybe<&2, String>

def check source · line 390 · raw

@e:Ent -> @+pos:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> Maybe<&2, String>

def tile_join source · line 395 · raw

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

def tile source · line 403 · raw

@es:List<&2, Ent> -> @+pos:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> Result<&2, &2, String, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64>

the end of the tiling from pos, or the first problem

def meta_of source · line 412 · raw

@m:Maybe<&2, List<&2, KV>> -> List<&2, KV>

def finish source · line 419 · raw

@es:List<&2, Ent> -> @m:List<&2, KV> -> @r:Result<&2, &2, String, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64> -> Result<&2, &2, String, Header>

def unique source · line 426 · raw

@d:Maybe<&2, String> -> @+es:List<&2, Ent> -> @m:List<&2, KV> -> Result<&2, &2, String, Header>

def accept source · line 433 · raw

@a:Acc -> Result<&2, &2, String, Header>

def parsed source · line 439 · raw

@st:Ps -> Result<&2, &2, String, Header>

def parse source · line 451 · raw

@bs:List<&2, U32> -> Result<&2, &2, String, Header>

the header's tensors, sorted by begin, its metadata, and the size of its data section; or what is wrong with it