main.bend relies on unsafe/foreign
raw source on the hub · import bendtorch-load@1.2.0.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.0, with 64-bit offsets, BF16 files and reads into a cell offset
3 imports
import Base import bend-ml-binary-read@0.2.0.0/main.bend as R import bendtorch@1.2.0.0/main.bend as T
Types
type W64 source · line 18 · raw
Data
W64@hi:U32 -> @lo:U32 -> W64
type Entry source · line 233 · raw
Data
one tensor the model expects, in Layer.dump order: its name in the file and its shape (rows, columns, ...)
Entry@name:String -> @shape:List<&2, U32> -> Entry
type Tok source · line 240 · raw
Data
TStr@s:String -> Tok
TNum@v:W64 -> Tok
TPun@c:U32 -> Tok
TBad@c:U32 -> Tok
type Lex source · line 248 · 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
LOutLex
LStr@acc:String -> Lex
LEsc@acc:String -> Lex
LNum@v:W64 -> Lex
type LexR source · line 255 · raw
Data
a state and the tokens one byte completed, in order
LexR@st:Lex -> @toks:List<&2, Tok> -> LexR
type Rec source · line 323 · raw
Data
one top-level entry of the header: a tensor (or __metadata__, which no layout names)
Rec@name:String -> @dtype:String -> @shape:List<&2, W64> -> @offs:List<&2, W64> -> Rec
type Ps source · line 329 · 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
Ps@depth:U32 -> @colon:Bool -> @pend:String -> @name:String -> @field:String -> @dtype:String -> @shape:List<&2, W64> -> @offs:List<&2, W64> -> @nums:List<&2, W64> -> @recs:List<&2, Rec> -> @bad:U32 -> Ps
type Plan source · line 446 · raw
Data
where a tensor is and how to read it, or why it cannot be read
PlanOk@name:String -> @bf16:Bool -> @off:W64 -> @n:U32 -> Plan
PlanBad@msg:String -> Plan
type Header source · line 529 · raw
Data
A parsed safetensors header: where the data starts and each tensor's record. Reading it once lets a model be filled
one piece at a time (piece), so that no array of the whole model exists. One array for all of SmolLM2-1.7B's
1.71e9 parameters takes a 2^31-cell block (8 GiB) whose memory the training step's smaller blocks never reuse.
Header@start:W64 -> @recs:List<&2, Rec> -> Header
Definitions
def require source · line 8 · raw
@ok:Bool -> @+message:String -> IO(Unit)
def W64.of source · line 21 · raw
@x:U32 -> W64
def W64.of_pair source · line 24 · raw
@p:Pair(U32, U32) -> W64
def W64.bit source · line 28 · raw
@b:Bool -> U32
def W64.eq source · line 33 · raw
@a:W64 -> @b:W64 -> Bool
def W64.add_go source · line 37 · raw
@+ah:U32 -> @+bh:U32 -> @+s:U32 -> @+al:U32 -> W64
def W64.add source · line 41 · 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 45 · raw
@+h:U32 -> @+k:U32 -> @+p0:U32 -> @+p1:U32 -> W64
def W64.mul_small source · line 50 · 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 54 · raw
@+qh:U32 -> @+r1:U32 -> @+q1:U32 -> @+l:U32 -> Pair(W64, U32)
def W64.div10 source · line 59 · 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 65 · raw
@a:W64 -> Bool
def W64.show_go source · line 69 · raw
@f:Nat -> @z:Bool -> @qr:Pair(W64, U32) -> @acc:String -> String
def W64.show_if source · line 75 · raw
@z:Bool -> @+a:W64 -> String
def W64.show source · line 81 · raw
@+a:W64 -> String
the decimal digits, as U32.show writes them
def W64.digits source · line 84 · raw
@s:String -> @f:Nat -> Bool
def W64.read_go source · line 90 · raw
@s:String -> @+acc:W64 -> W64
def W64.read_if source · line 95 · raw
@ok:Bool -> @s:String -> Maybe<&2, W64>
def W64.read source · line 101 · 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 110 · 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 119 · raw
@+path:String -> @+n:U32 -> @f:File -> @+size:W64 -> IO(Array<F32>)
def read_sized source · line 125 · raw
@+path:String -> @+n:U32 -> @r:Pair(File, Result<&1, &1, Pair(U32, String), Pair(U32, U32)>) -> IO(Array<F32>)
def read source · line 133 · 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 140 · 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 149 · 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 157 · raw
@r:Pair(File, Result<&1, &1, Pair(U32, String), Pair(U32, U32)>) -> IO(W64)
def size source · line 166 · raw
@+path:String -> IO(W64)
A file's size in bytes, at any size
def weights_done source · line 172 · raw
@-P:Type -> @+n:U32 -> @r:0xea477ac4399d6f3f46b9d25d99c1900c/main.PA<P> -> IO(P)
def cells_done source · line 196 · raw
@r:Pair(File, Result<&1, &1, Pair(U32, String), U32>) -> IO(U32)
def cells_of source · line 205 · 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 236 · raw
@e:Entry -> String
def St.digit source · line 258 · raw
@+b:U32 -> Bool
def St.space source · line 261 · raw
@+b:U32 -> Bool
def St.pun source · line 265 · raw
@+b:U32 -> Bool
{ } [ ] : ,
def St.out_go source · line 268 · raw
@quote:Bool -> @digit:Bool -> @space:Bool -> @pun:Bool -> @+b:U32 -> LexR
def St.out source · line 276 · raw
@+b:U32 -> LexR
def St.str_go source · line 279 · raw
@quote:Bool -> @slash:Bool -> @+b:U32 -> @acc:String -> LexR
def St.num_end source · line 286 · 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 290 · raw
@digit:Bool -> @+b:U32 -> @+v:W64 -> LexR
def St.step source · line 295 · raw
@st:Lex -> @+b:U32 -> LexR
def St.push source · line 303 · 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 309 · 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 317 · 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 333 · 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 340 · 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 347 · raw
@fresh:Bool -> @ps:Ps -> Ps
{ opens a level; at depth 2 an entry starts empty
def Sp.close source · line 353 · raw
@emit:Bool -> @ps:Ps -> Ps
} closes a level; closing depth 2 ends an entry
def Sp.lopen source · line 358 · raw
@ps:Ps -> Ps
def Sp.lclose source · line 363 · 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 369 · raw
@ps:Ps -> Ps
def Sp.num source · line 374 · 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 379 · raw
@colon:Bool -> @ob:Bool -> @cb:Bool -> @ol:Bool -> @cl:Bool -> @+d:U32 -> @+f:String -> @ps:Ps -> Ps
def Sp.step source · line 388 · raw
@t:Tok -> @ps:Ps -> Ps
def Sp.parse source · line 399 · raw
@ts:List<&2, Tok> -> @ps:Ps -> Ps
def St.rec_name source · line 404 · raw
@r:Rec -> String
def St.find_put source · line 408 · raw
@r:Rec -> @rest:Maybe<&2, Rec> -> @hit:Bool -> Maybe<&2, Rec>
def St.find source · line 414 · raw
@rs:List<&2, Rec> -> @+name:String -> Maybe<&2, Rec>
the entry named name
def St.prod source · line 419 · raw
@xs:List<&2, U32> -> U32
def St.total source · line 424 · raw
@es:List<&2, Entry> -> U32
def St.shape_eq source · line 429 · raw
@a:List<&2, W64> -> @b:List<&2, U32> -> Bool
def St.show_u source · line 435 · raw
@xs:List<&2, U32> -> @sep:String -> String
def St.show_w source · line 440 · raw
@xs:List<&2, W64> -> @sep:String -> String
def St.plan_span source · line 450 · raw
@ok:Bool -> @+name:String -> @bf16:Bool -> @off:W64 -> @+n:U32 -> Plan
def St.plan_of source · line 456 · 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 463 · 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 469 · 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 475 · raw
@+start:W64 -> @e:Entry -> @mr:Maybe<&2, Rec> -> Plan
def St.read source · line 480 · 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 485 · 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 494 · 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 501 · 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 510 · raw
@+a:U32 -> @+b:U32 -> @+c:U32 -> @+d:U32 -> U32
def St.le64 source · line 514 · 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 521 · raw
@bs:List<&2, U32> -> @+acc:U32 -> U32
def St.parsed source · line 532 · raw
@+path:String -> @+start:W64 -> @ps:Ps -> IO(Header)
def St.header source · line 539 · raw
@+path:String -> @+len:U32 -> @r:Pair(File, Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> IO(Header)
def St.length source · line 549 · raw
@f:File -> @+path:String -> @h:Maybe<&2, W64> -> IO(Header)
the format caps the header at 100 MB
def St.first8 source · line 558 · raw
@+path:String -> @r:Pair(File, Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> IO(Header)
def header source · line 566 · raw
@+path:String -> IO(Header)
A safetensors file's header, parsed; the file is closed after it
def tensors_from source · line 573 · 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 584 · 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 599 · raw
@-P1:Type -> @-P2:Type -> @p:0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<P1, P2> -> @k1:(@_:P1 -> IO(P1)) -> @k2:(@_:P2 -> IO(P2)) -> IO(0xea477ac4399d6f3f46b9d25d99c1900c/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 180 · raw
@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:0xea477ac4399d6f3f46b9d25d99c1900c/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 189 · raw
@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer<P, X, Y, S> -> @+path:String -> @+base:U32 -> @+copies:U32 -> @+tail:U32 -> @tr:0xea477ac4399d6f3f46b9d25d99c1900c/main.Train<P> -> IO(0xea477ac4399d6f3f46b9d25d99c1900c/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 211 · raw
@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer<P, X, Y, S> -> @+path:String -> @tr:0xea477ac4399d6f3f46b9d25d99c1900c/main.Train<P> -> IO(0xea477ac4399d6f3f46b9d25d99c1900c/main.Train<P>)
template piece source · line 592 · raw
@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:0xea477ac4399d6f3f46b9d25d99c1900c/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 609 · raw
@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:0xea477ac4399d6f3f46b9d25d99c1900c/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