main.bend relies on unsafe/foreign
raw source on the hub · import bend-ml-binary-read@0.2.0.1/main.bend as Main
bend-ml-binary-read: little-endian F32 numbers from a file, read straight into an Array<F32>.
import bend-ml-binary-read@0.2.0.1/main.bend as R
Since 0.2.0.0 it also reads at 64-bit byte offsets, reads BF16 files (each number widened exactly to F32), reads into a cell offset of an existing array, and gives a file's size past 4 GiB. Bend has no 64-bit integer (Base's numbers are U32, F32 and Nat, and File.read_at takes a U32 offset), so a 64-bit offset or size crosses as two U32s, hi and lo, meaning hi * 2^32 + lo. The offsets are bytes, not cells: a tensor in a safetensors file starts at 8 + the header's length + its own offset, and the header has any length.
File.read_at answers a List<U32> with one cell per byte, and building and walking that list is what loading model weights costs (NOTES.md, exp. 13; bendlang/bend#1573). This effect reads the bytes into the array's own cells instead, with no list in between. The idea and its shape come from bendlang/bend#1430 by @totaland, which proposed it for Base; Base keeps thin syscall wrappers, so it lives here as a library effect.
It is foreign code: --verdict fails for any file that imports this one, used or not. Keep it out of files that hold laws, and import it only where weights are loaded.
1 import
import Base
Effects (foreign code)
effect read_f32 source · line 25 · raw
@file:File -> @offset:U32 -> @n:U32 -> @a:Array<F32> -> IO(Pair(File, Pair(Array<F32>, Result<&1, &1, Pair(U32, String), U32>)))
Reads n numbers (4 * n bytes) at byte offset of file into cells 0 .. n-1 of a, stopping at
a's capacity and at the end of the file. Answers the file, the array and the number of bytes
read, or the error; the file's position does not move. A short read leaves the cells past it as
they were (a last partial number writes only its bytes).
foreign: read_f32.c, read_f32.js
effect size64 source · line 31 · raw
@file:File -> IO(Pair(File, Result<&1, &1, Pair(U32, String), Pair(U32, U32)>))
The size of file in bytes as (hi, lo): hi * 2^32 + lo. Base's File.size fails past 4 GiB.
foreign: read_at.c, read_at.js
effect read_f32_at source · line 40 · raw
@file:File -> @hi:U32 -> @lo:U32 -> @n:U32 -> @a:Array<F32> -> IO(Pair(File, Pair(Array<F32>, Result<&1, &1, Pair(U32, String), U32>)))
As read_f32, at the byte offset hi * 2^32 + lo, and it answers the number of whole numbers read (not bytes), so a
read of up to 2^32 - 1 numbers (16 GiB) answers its count in a U32. Reads n numbers into cells 0 .. n-1 of a,
stopping at a's capacity and at the end of the file; the file's position does not move. A short read leaves the
cells past it as they were, except that a last partial number writes only its bytes, as in read_f32.
foreign: read_at.c, read_at.js
effect read_bf16_at source · line 49 · raw
@file:File -> @hi:U32 -> @lo:U32 -> @n:U32 -> @a:Array<F32> -> IO(Pair(File, Pair(Array<F32>, Result<&1, &1, Pair(U32, String), U32>)))
n little-endian BF16 numbers (2 * n bytes) at the byte offset hi * 2^32 + lo, each widened exactly into a cell of
a: the F32 whose top 16 bits are the BF16's and whose low 16 bits are zero (what bf16.to(torch.float32) gives).
Stops at a's capacity and at the end of the file, answers the number of whole numbers read, and leaves the cells
past them as they were (a last odd byte is dropped). The file's position does not move.
foreign: read_at.c, read_at.js
effect read_f32_into source · line 56 · raw
@file:File -> @hi:U32 -> @lo:U32 -> @n:U32 -> @a:Array<F32> -> @at:U32 -> IO(Pair(File, Pair(Array<F32>, Result<&1, &1, Pair(U32, String), U32>)))
As read_f32_at, writing cells at .. at + n - 1 of a (stopping at a's capacity): one tensor of a file into its place
in a larger array, with no copy.
foreign: read_at.c, read_at.js
effect read_bf16_into source · line 62 · raw
@file:File -> @hi:U32 -> @lo:U32 -> @n:U32 -> @a:Array<F32> -> @at:U32 -> IO(Pair(File, Pair(Array<F32>, Result<&1, &1, Pair(U32, String), U32>)))
As read_bf16_at, writing cells at .. at + n - 1 of a (stopping at a's capacity).
foreign: read_at.c, read_at.js