~/bend-docscommunity

st.bend relies on unsafe/foreign

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

bend-safetensors-reader: read .safetensors files into Bend arrays: open a file (its header parsed and checked), find tensors by name, and load a tensor's bytes, or its values widened to F32, into an Array<U32>. import bend-safetensors-reader@<version>/st.bend as ST The header is read with Base's file IO; tensor bytes with bend-raw-cells (foreign C, native builds only), which takes 64-bit offsets, so files past 4 GiB work.

5 imports
import Base
import ./w64.bend as W
import ./header.bend as H
import ./widen.bend as V
import bend-raw-cells@0.1.0.0/raw.bend as R

Types

type St source · line 13 · raw

Data

an open file: its path, where its data section starts (8 + the header's length), and its checked header

Definitions

def St.tensors source · line 16 · raw

@s:St -> List<&2, 0xd8619c297e019f1c512e443a1b7ff426/header.Ent>

def St.meta source · line 21 · raw

@s:St -> List<&2, 0xd8619c297e019f1c512e443a1b7ff426/header.KV>

def find_go source · line 26 · raw

@+name:String -> @es:List<&2, 0xd8619c297e019f1c512e443a1b7ff426/header.Ent> -> Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/header.Ent>

def St.find source · line 34 · raw

@s:St -> @+name:String -> Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/header.Ent>

the tensor named name

def Ent.bytes source · line 38 · raw

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

the tensor's byte count (end - begin)

def Ent.cells source · line 44 · raw

@e:0xd8619c297e019f1c512e443a1b7ff426/header.Ent -> U32

the cells read() fills: ceil(bytes / 4), 4 bytes a cell, little-endian

def Ent.count source · line 48 · raw

@e:0xd8619c297e019f1c512e443a1b7ff426/header.Ent -> U32

the element count (bytes / dtype size)

def fail source · line 55 · raw

@f:File -> @why:String -> IO(Result<&2, &2, String, St>)

def le8 source · line 61 · raw

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

8 bytes, little-endian, as W64

def len_go source · line 68 · raw

@bs:List<&2, U32> -> @+n:U32 -> U32

def size_ok source · line 75 · raw

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

def sized_ok source · line 82 · raw

@ok:Bool -> @+path:String -> @+start:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @h:0xd8619c297e019f1c512e443a1b7ff426/header.Header -> @+bytes:U32 -> @+n:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> Result<&2, &2, String, St>

def sized source · line 90 · raw

@+path:String -> @+start:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @h:0xd8619c297e019f1c512e443a1b7ff426/header.Header -> @sz:Result<&1, &1, Pair(U32, String), U32> -> Result<&2, &2, String, St>

the file's size, when the host can tell it (Base's File.size stops at 4 GiB), must be the data section's end

def closed source · line 97 · raw

@+path:String -> @+start:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @h:0xd8619c297e019f1c512e443a1b7ff426/header.Header -> @fs:Pair(File, Result<&1, &1, Pair(U32, String), U32>) -> IO(Result<&2, &2, String, St>)

def checked source · line 104 · raw

@f:File -> @+path:String -> @+start:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @r:Result<&2, &2, String, 0xd8619c297e019f1c512e443a1b7ff426/header.Header> -> IO(Result<&2, &2, String, St>)

def got_bytes source · line 113 · raw

@ok:Bool -> @f:File -> @+path:String -> @+n:U32 -> @bs:List<&2, U32> -> IO(Result<&2, &2, String, St>)

def got_header source · line 120 · raw

@+path:String -> @+n:U32 -> @r:Pair(File, Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> IO(Result<&2, &2, String, St>)

def got_len3 source · line 128 · raw

@ok:Bool -> @f:File -> @+path:String -> @+n:U32 -> IO(Result<&2, &2, String, St>)

the reference implementation's limit: a header of at most 100,000,000 bytes

def got_len2 source · line 137 · raw

@m:Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64> -> @f:File -> @+path:String -> IO(Result<&2, &2, String, St>)

def got_len source · line 144 · raw

@+path:String -> @r:Pair(File, Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> IO(Result<&2, &2, String, St>)

def opened source · line 151 · raw

@+path:String -> @r:Result<&1, &1, Pair(U32, String), File> -> IO(Result<&2, &2, String, St>)

def St.open source · line 161 · raw

@+path:String -> IO(Result<&2, &2, String, St>)

open a .safetensors file: its header parsed and checked, or what is wrong with it

def wrap source · line 168 · raw

@m:Maybe<&2, 0xd8619c297e019f1c512e443a1b7ff426/w64.W64> -> 0xd8619c297e019f1c512e443a1b7ff426/w64.W64

def tail_set source · line 175 · raw

@+dst:U32 -> @+shift:U32 -> @a:Array<U32> -> @r:Pair(Array<U32>, U32) -> Array<U32>

def tail source · line 181 · raw

@+path:String -> @+endf:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @+rem:U32 -> @+dst:U32 -> @a:Array<U32> -> IO(Array<U32>)

the last 1 to 3 bytes: the 4 bytes ending at the tensor's end, shifted down

def read_tail source · line 187 · raw

@none:Bool -> @+path:String -> @+endf:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @+rem:U32 -> @+dst:U32 -> @a:Array<U32> -> IO(Array<U32>)

def read_go source · line 194 · raw

@+path:String -> @+off:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @+endf:0xd8619c297e019f1c512e443a1b7ff426/w64.W64 -> @+full:U32 -> @+rem:U32 -> @+dst:U32 -> @a:Array<U32> -> IO(Array<U32>)

def St.read source · line 202 · raw

@s:St -> @e:0xd8619c297e019f1c512e443a1b7ff426/header.Ent -> @+dst:U32 -> @a:Array<U32> -> IO(Array<U32>)

tensor e's bytes into a, from cell dst on: 4 bytes a cell, little-endian, Ent.cells(e) cells, the last cell's unused high bytes 0. a must hold dst + Ent.cells(e) cells. Stops the program if the file is shorter than its header says (bend-raw-cells).

def St.can_f32 source · line 212 · raw

@e:0xd8619c297e019f1c512e443a1b7ff426/header.Ent -> Bool

whether read_f32 takes this dtype (F32, BF16, F16)

def depth source · line 224 · raw

@+n:U32 -> Nat

the smallest d with 2^d >= n

def half source · line 227 · raw

@+k:U32 -> @+c:U32 -> U32

Unsafe

unsafe wid source · line 231 · raw

@done:Bool -> @+isbf:Bool -> @+k:U32 -> @+n:U32 -> @+dst:U32 -> @src:Array<U32> -> @out:Array<U32> -> Array<U32>

unsafe wid_get source · line 239 · raw

@+isbf:Bool -> @+k:U32 -> @+n:U32 -> @+dst:U32 -> @out:Array<U32> -> @r:Pair(Array<U32>, U32) -> Array<U32>

unsafe St.widen source · line 247 · raw

@+isbf:Bool -> @+n:U32 -> @src:Array<U32> -> @+dst:U32 -> @out:Array<U32> -> Array<U32>

n 16-bit values, two a cell (low half first), widened to F32 bits into out from cell dst on; one element at a time

unsafe read16 source · line 251 · raw

@+s:St -> @+e:0xd8619c297e019f1c512e443a1b7ff426/header.Ent -> @+isbf:Bool -> @+dst:U32 -> @a:Array<U32> -> IO(Array<U32>)

unsafe St.read_f32 source · line 259 · raw

@+s:St -> @+e:0xd8619c297e019f1c512e443a1b7ff426/header.Ent -> @+dst:U32 -> @a:Array<U32> -> IO(Array<U32>)

tensor e's values as F32 bits into a, from cell dst on, Ent.count(e) cells: F32 as read, BF16 and F16 widened (exactly). Other dtypes stop the program: check St.can_f32 first. a must hold dst + Ent.count(e) cells.