~/bend-docscommunity

main.bend relies on unsafe/foreign

raw source on the hub · import bendtorch-load@2.0.2.0/main.bend as Main

Temporary loading-only exception: bend-ml-binary-read is foreign C/JavaScript. No file with laws imports this module. Replace it with native Bend when the language supports dense reads (Renan, 2026-10-17). The reader is bend-ml-binary-read@0.2.0.1, with 64-bit offsets, BF16 files and reads into a cell offset bend-lint: disable-file bend-lint-rules/performance/eager_branch -- header parsing: 19-digit numbers and rank <= 4 shapes not-expected-because: weights load once bend-lint: disable-file bend-lint-rules/performance/repeated_call -- St.total again only in the error message not-expected-because: weights load once, and the message runs only on a failure

3 imports
import Base
import bend-ml-binary-read@0.2.0.1/main.bend as R
import bendtorch@2.0.2.0/main.bend as T

Types

type W64 source · line 20 · raw

Data

type Entry source · line 235 · raw

Data

one tensor the model expects, in Layer.dump order: its name in the file and its shape (rows, columns, ...)

type Tok source · line 242 · raw

Data

type Lex source · line 250 · raw

Data

the lexer's state between bytes: outside a token, inside a string (its characters reversed), just after a backslash in a string, inside a number

type LexR source · line 257 · raw

Data

a state and the tokens one byte completed, in order

type Rec source · line 325 · raw

Data

one top-level entry of the header: a tensor (or __metadata__, which no layout names)

type Ps source · line 331 · raw

Data

the pass over the tokens: the depth ({ and [ open a level), whether the last token was a colon, the last string, the current entry's name and field, what the entry holds so far, the numbers of the open list, the entries read (last first), and the count of tokens the header may not hold

type Plan source · line 448 · raw

Data

where a tensor is and how to read it, or why it cannot be read

Definitions

def require source · line 10 · raw

@ok:Bool -> @+message:String -> IO(Unit)

def W64.of source · line 23 · raw

@x:U32 -> W64

def W64.of_pair source · line 26 · raw

@p:Pair(U32, U32) -> W64

def W64.bit source · line 30 · raw

@b:Bool -> U32

def W64.eq source · line 35 · raw

@a:W64 -> @b:W64 -> Bool

def W64.add_go source · line 39 · raw

@+ah:U32 -> @+bh:U32 -> @+s:U32 -> @+al:U32 -> W64

def W64.add source · line 43 · raw

@a:W64 -> @b:W64 -> W64

a + b, wrapping at 2^64: the low words' sum carries one when it wrapped (it is then below either term)

def W64.mul_go source · line 47 · raw

@+h:U32 -> @+k:U32 -> @+p0:U32 -> @+p1:U32 -> W64

def W64.mul_small source · line 52 · raw

@a:W64 -> @+k:U32 -> W64

a * k for k < 2^16 (enough for bytes a number and decimal digits), wrapping at 2^64: lo = l1 2^16 + l0, so lo * k = (l1 k) 2^16 + l0 k, where both products fit a U32

def W64.div10_go source · line 56 · raw

@+qh:U32 -> @+r1:U32 -> @+q1:U32 -> @+l:U32 -> Pair(W64, U32)

def W64.div10 source · line 61 · raw

@a:W64 -> Pair(W64, U32)

(a / 10, a % 10), by long division in 16-bit steps so no partial remainder overflows a U32

def W64.is_zero source · line 67 · raw

@a:W64 -> Bool

def W64.show_go source · line 71 · raw

@f:Nat -> @z:Bool -> @qr:Pair(W64, U32) -> @acc:String -> String

def W64.show_if source · line 77 · raw

@z:Bool -> @+a:W64 -> String

def W64.show source · line 83 · raw

@+a:W64 -> String

the decimal digits, as U32.show writes them

def W64.digits source · line 86 · raw

@s:String -> @f:Nat -> Bool

def W64.read_go source · line 92 · raw

@s:String -> @+acc:W64 -> W64

def W64.read_if source · line 97 · raw

@ok:Bool -> @s:String -> Maybe<&2, W64>

def W64.read source · line 103 · raw

@s:String -> Maybe<&2, W64>

A decimal number of 1 to 19 digits (every such number fits 64 bits); anything else is None

def read_done source · line 112 · raw

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

def read_go source · line 121 · raw

@+path:String -> @+n:U32 -> @f:File -> @+size:W64 -> IO(Array<F32>)

def read_sized source · line 127 · raw

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

def read source · line 135 · raw

@+path:String -> @+n:U32 -> IO(Array<F32>)

A raw, exact-size little-endian F32 file, read once into one source array: up to 2^32 - 1 numbers (16 GiB).

def read_f32_at source · line 142 · raw

@+path:String -> @off:W64 -> @+n:U32 -> IO(Array<F32>)

n little-endian F32 numbers at byte off of a file of any size (a tensor inside a larger file)

def read_bf16_at source · line 151 · raw

@+path:String -> @off:W64 -> @+n:U32 -> IO(Array<F32>)

n little-endian BF16 numbers at byte off of a file of any size, each widened exactly to F32 (bf16.to(torch.float32))

def size_done source · line 159 · raw

@r:Pair(File, Result<&1, &1, Pair(U32, String), Pair(U32, U32)>) -> IO(W64)

def size source · line 168 · raw

@+path:String -> IO(W64)

A file's size in bytes, at any size

def weights_done source · line 174 · raw

@-P:Type -> @+n:U32 -> @r:0x126b6a38011ee25cff0be3f38ba4be79/main.PA<P> -> IO(P)

def cells_done source · line 198 · raw

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

def cells_of source · line 207 · raw

@+path:String -> IO(U32)

the number of F32 cells in a file (a packed-moment checkpoint carries its own sizes: its length is the file's)

def St.entry_name source · line 238 · raw

@e:Entry -> String

def St.digit source · line 260 · raw

@+b:U32 -> Bool

def St.space source · line 263 · raw

@+b:U32 -> Bool

def St.pun source · line 267 · raw

@+b:U32 -> Bool

{ } [ ] : ,

def St.out_go source · line 270 · raw

@quote:Bool -> @digit:Bool -> @space:Bool -> @pun:Bool -> @+b:U32 -> LexR

def St.out source · line 278 · raw

@+b:U32 -> LexR

def St.str_go source · line 281 · raw

@quote:Bool -> @slash:Bool -> @+b:U32 -> @acc:String -> LexR

def St.num_end source · line 288 · raw

@+v:W64 -> @r:LexR -> LexR

a number ends at the first byte that is not a digit, and that byte is then read as outside a token

def St.num_go source · line 292 · raw

@digit:Bool -> @+b:U32 -> @+v:W64 -> LexR

def St.step source · line 297 · raw

@st:Lex -> @+b:U32 -> LexR

def St.push source · line 305 · raw

@ts:List<&2, Tok> -> @out:List<&2, Tok> -> List<&2, Tok>

ts onto out, which holds the tokens so far in reverse

def St.fin source · line 311 · raw

@st:Lex -> @out:List<&2, Tok> -> List<&2, Tok>

the end of the header: a number ends there, an unfinished string is malformed

def St.lex source · line 319 · raw

@bs:List<&2, U32> -> @r:LexR -> @out:List<&2, Tok> -> List<&2, Tok>

the header's tokens, in order; r is what the previous byte left

def Sp.str source · line 335 · raw

@colon:Bool -> @is_dt:Bool -> @ps:Ps -> @s:String -> Ps

a string is a value after a colon (the dtype, at depth 2) or else a key, kept until its colon

def Sp.colon source · line 342 · raw

@d1:Bool -> @d2:Bool -> @ps:Ps -> Ps

a colon makes the last string the entry's name (depth 1) or the field's (depth 2)

def Sp.open source · line 349 · raw

@fresh:Bool -> @ps:Ps -> Ps

{ opens a level; at depth 2 an entry starts empty

def Sp.close source · line 355 · raw

@emit:Bool -> @ps:Ps -> Ps

} closes a level; closing depth 2 ends an entry

def Sp.lopen source · line 360 · raw

@ps:Ps -> Ps

def Sp.lclose source · line 365 · raw

@is_sh:Bool -> @is_of:Bool -> @ps:Ps -> Ps

] closes a list; at depth 3 it is the entry's shape or data_offsets

def Sp.comma source · line 371 · raw

@ps:Ps -> Ps

def Sp.num source · line 376 · raw

@in_list:Bool -> @v:W64 -> @ps:Ps -> Ps

a number counts inside a list at depth 3; elsewhere it is not part of a tensor's entry

def Sp.pun source · line 381 · raw

@colon:Bool -> @ob:Bool -> @cb:Bool -> @ol:Bool -> @cl:Bool -> @+d:U32 -> @+f:String -> @ps:Ps -> Ps

def Sp.step source · line 390 · raw

@t:Tok -> @ps:Ps -> Ps

def Sp.parse source · line 401 · raw

@ts:List<&2, Tok> -> @ps:Ps -> Ps

def St.rec_name source · line 406 · raw

@r:Rec -> String

def St.find_put source · line 410 · raw

@r:Rec -> @rest:Maybe<&2, Rec> -> @hit:Bool -> Maybe<&2, Rec>

def St.find source · line 416 · raw

@rs:List<&2, Rec> -> @+name:String -> Maybe<&2, Rec>

the entry named name

def St.prod source · line 421 · raw

@xs:List<&2, U32> -> U32

def St.total source · line 426 · raw

@es:List<&2, Entry> -> U32

def St.shape_eq source · line 431 · raw

@a:List<&2, W64> -> @b:List<&2, U32> -> Bool

def St.show_u source · line 437 · raw

@xs:List<&2, U32> -> @sep:String -> String

def St.show_w source · line 442 · raw

@xs:List<&2, W64> -> @sep:String -> String

def St.plan_span source · line 452 · raw

@ok:Bool -> @+name:String -> @bf16:Bool -> @off:W64 -> @+n:U32 -> Plan

def St.plan_of source · line 458 · raw

@+name:String -> @bf16:Bool -> @+start:W64 -> @+n:U32 -> @+width:U32 -> @of:List<&2, W64> -> Plan

data_offsets must be [begin, end] with end - begin the tensor's bytes

def St.plan_shape source · line 465 · raw

@ok:Bool -> @+name:String -> @f32:Bool -> @+start:W64 -> @+sh:List<&2, W64> -> @+want:List<&2, U32> -> @of:List<&2, W64> -> Plan

def St.plan_dtype source · line 471 · raw

@f32:Bool -> @bf16:Bool -> @+name:String -> @+dt:String -> @+start:W64 -> @+sh:List<&2, W64> -> @+want:List<&2, U32> -> @of:List<&2, W64> -> Plan

def St.plan source · line 477 · raw

@+start:W64 -> @e:Entry -> @mr:Maybe<&2, Rec> -> Plan

def St.read source · line 482 · raw

@bf16:Bool -> @f:File -> @off:W64 -> @+n:U32 -> @a:Array<F32> -> @+at:U32 -> IO(Pair(File, Pair(Array<F32>, Result<&1, &1, Pair(U32, String), U32>)))

def St.got source · line 487 · raw

@+path:String -> @+name:String -> @+n:U32 -> @r:Pair(File, Pair(Array<F32>, Result<&1, &1, Pair(U32, String), U32>)) -> @k:(@_:File -> @_:Array<F32> -> @_:U32 -> IO(Array<F32>)) -> IO(Array<F32>)

def St.then source · line 496 · raw

@+path:String -> @p:Plan -> @f:File -> @a:Array<F32> -> @+at:U32 -> @k:(@_:File -> @_:Array<F32> -> @_:U32 -> IO(Array<F32>)) -> IO(Array<F32>)

reads one planned tensor into cells at .., then goes on with k

def St.fill source · line 503 · raw

@es:List<&2, Entry> -> @+path:String -> @+start:W64 -> @+recs:List<&2, Rec> -> @f:File -> @a:Array<F32> -> @+at:U32 -> IO(Array<F32>)

every tensor of es, in order, into consecutive cells from at

def St.le32 source · line 512 · raw

@+a:U32 -> @+b:U32 -> @+c:U32 -> @+d:U32 -> U32

def St.le64 source · line 516 · raw

@bs:List<&2, U32> -> Maybe<&2, W64>

the header's length: the file's first 8 bytes, little-endian

def St.count source · line 523 · raw

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

def St.parsed source · line 534 · raw

@+path:String -> @+start:W64 -> @ps:Ps -> IO(Header)

def St.header source · line 541 · raw

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

def St.length source · line 551 · raw

@f:File -> @+path:String -> @h:Maybe<&2, W64> -> IO(Header)

the format caps the header at 100 MB

def St.first8 source · line 560 · raw

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

def tensors_from source · line 575 · raw

@+path:String -> @h:Header -> @+n:U32 -> @+layout:List<&2, Entry> -> IO(Array<F32>)

The tensors layout names, read from the file whose header is h, in the layout's order, into one array of n numbers

def tensors source · line 586 · raw

@+path:String -> @+n:U32 -> @+layout:List<&2, Entry> -> IO(Array<F32>)

The tensors layout names, read from a safetensors file in its order into one array of n numbers (F32 as they are, BF16 widened exactly). Each must be in the file with the shape given, as F32 or BF16, and the shapes must hold n numbers in all; otherwise the program stops with a message naming the tensor. Other tensors in the file are ignored.

def two source · line 601 · raw

@-P1:Type -> @-P2:Type -> @p:0x126b6a38011ee25cff0be3f38ba4be79/main.Two<P1, P2> -> @k1:(@_:P1 -> IO(P1)) -> @k2:(@_:P2 -> IO(P2)) -> IO(0x126b6a38011ee25cff0be3f38ba4be79/main.Two<P1, P2>)

A pair of parameters filled one after the other: the first piece's source array is released before the second is read

Templates

template weights source · line 182 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:0x126b6a38011ee25cff0be3f38ba4be79/main.Layer<P, X, Y, S> -> @+path:String -> @p:P -> IO(P)

All parameters in Layer.dump order. The source array is released after the model's arrays are filled.

template train_read source · line 191 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:0x126b6a38011ee25cff0be3f38ba4be79/main.Layer<P, X, Y, S> -> @+path:String -> @+base:U32 -> @+copies:U32 -> @+tail:U32 -> @tr:0x126b6a38011ee25cff0be3f38ba4be79/main.Train<P> -> IO(0x126b6a38011ee25cff0be3f38ba4be79/main.Train<P>)

The checkpoint layout is determined by the already initialized optimizer state, without a header: SGD: parameters; momentum: parameters, buffer; Adam/AdamW: parameters, m, v, 1 - b1^t, 1 - b2^t; AdamW with packed BF16 moments: parameters, the moments' tree (main.bend's pt_store), 1 - b1^t, 1 - b2^t, the whole file read.

template train source · line 213 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:0x126b6a38011ee25cff0be3f38ba4be79/main.Layer<P, X, Y, S> -> @+path:String -> @tr:0x126b6a38011ee25cff0be3f38ba4be79/main.Train<P> -> IO(0x126b6a38011ee25cff0be3f38ba4be79/main.Train<P>)

template piece source · line 594 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:0x126b6a38011ee25cff0be3f38ba4be79/main.Layer<P, X, Y, S> -> @+path:String -> @h:Header -> @+layout:List<&2, Entry> -> @p:P -> IO(P)

One layer's parameters from the file whose header is h: layout names its tensors in Layer.dump order, and Layer.load fills them; the array they were read into is gone once this answers

template safetensors source · line 611 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:0x126b6a38011ee25cff0be3f38ba4be79/main.Layer<P, X, Y, S> -> @+path:String -> @+layout:List<&2, Entry> -> @p:P -> IO(P)

All parameters from a safetensors file: layout names the model's tensors in Layer.dump order (a tied output head is not in it, as SmolLM2's is not in its file), and Layer.load fills the parameters from them as weights does