main.bend

bend-ml-binary-read@0.1.0.0 · 22 lines · 0 definitions

1# bend-ml-binary-read: little-endian F32 numbers from a file, read straight into an Array<F32>.
2#
3#   import bend-ml-binary-read@0.1.0.0/main.bend as R
4#
5# File.read_at answers a List<U32> with one cell per byte, and building and walking that list is
6# what loading model weights costs (NOTES.md, exp. 13; bendlang/bend#1573). This effect reads the
7# bytes into the array's own cells instead, with no list in between. The idea and its shape come
8# from bendlang/bend#1430 by @totaland, which proposed it for Base; Base keeps thin syscall
9# wrappers, so it lives here as a library effect.
10#
11# It is foreign code: --verdict fails for any file that imports this one, used or not. Keep it out
12# of files that hold laws, and import it only where weights are loaded.
13import Base
14
15# Reads n numbers (4 * n bytes) at byte `offset` of `file` into cells 0 .. n-1 of `a`, stopping at
16# a's capacity and at the end of the file. Answers the file, the array and the number of bytes
17# read, or the error; the file's position does not move. A short read leaves the cells past it as
18# they were (a last partial number writes only its bytes).
19def read_f32(file: File, offset: U32, n: U32, a: Array<F32>) ->
20  IO(File & (Array<F32> & Result<&1, &1, U32 & String, U32>)):
21  import "./read_f32.c"
22  import "./read_f32.js"