~/bend-docscommunity

tensor_file.bend checks

raw source on the hub · import stelliferous@0.0.3.0/tensor_file.bend as Tensor_file

Raw little-endian FP32 files and NumPy .npy files through the official File effects, and the reader that other formats share: a byte range of an open file, decoded element by element, written straight into a tensor's storage.

7 imports
import Base
import ./storage_bytes.bend as Bytes
import ./storage_element_format.bend as Formats
import ./math_fp32.bend as Fp32
import ./storage_buffer.bend as Buffers
import ./tensor.bend as Tensors
import ./tensor_layout.bend as Layout

Types

type Partial source · line 37 · raw

Data

An element in progress: its first four bytes, the bytes above them, the weight of the next byte within its word, and how many bytes arrived.

type Words source · line 40 · raw

Data

type Target source · line 44 · raw

Type

The buffer of the tensor, the values found so far, and an element in progress.

type Reading source · line 47 · raw

Type

type Outcome source · line 51 · raw

Type

type Writing source · line 372 · raw

Type

Definitions

def Tensor source · line 12 · raw

Type

def Read source · line 15 · raw

Type

def Written source · line 18 · raw

Type

def Sized source · line 21 · raw

Type

def chunk source · line 25 · raw

U32

FP32 values per File effect; a read takes four times as many bytes.

def chunk_bytes source · line 28 · raw

U32

def fresh_partial source · line 54 · raw

Partial

def gathered source · line 57 · raw

@lower:Bool -> @done:Bool -> @wrap:Bool -> @+byte:U32 -> @format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @values:List<&2, F32> -> @+count:U32 -> @+low:U32 -> @+high:U32 -> @+weight:U32 -> @+filled:U32 -> Words

def push source · line 65 · raw

@+byte:U32 -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @words:Words -> Words

def trickle source · line 70 · raw

@bytes:List<&2, U32> -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @words:Words -> Words

def pairs source · line 76 · raw

@bytes:List<&2, U32> -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @values:List<&2, F32> -> @+count:U32 -> Words

Whole elements at a time while the bytes allow; the values are newest first.

def quads source · line 82 · raw

@bytes:List<&2, U32> -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @values:List<&2, F32> -> @+count:U32 -> Words

def octets source · line 88 · raw

@bytes:List<&2, U32> -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @values:List<&2, F32> -> @+count:U32 -> Words

def by_width source · line 95 · raw

@+width:U32 -> @bytes:List<&2, U32> -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> Words

def words source · line 102 · raw

@whole:Bool -> @bytes:List<&2, U32> -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @partial:Partial -> Words

def placed source · line 108 · raw

@read:Words -> @buffer:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> @+found:U32 -> @+count:U32 -> Target

Values beyond the count of the shape are counted and not written.

def fill source · line 113 · raw

@bytes:List<&2, U32> -> @target:Target -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @+count:U32 -> Target

def ranged source · line 118 · raw

@exhausted:Bool -> @file:File -> @+position:U32 -> @+remaining:U32 -> @target:Target -> Reading

def absorbed source · line 124 · raw

@bytes:List<&2, U32> -> @file:File -> @+position:U32 -> @+remaining:U32 -> @target:Target -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @+count:U32 -> Reading

A read that ends early leaves the count short, which the count check reports.

def received source · line 131 · raw

@result:Result<&1, &1, Pair(U32, String), List<&2, U32>> -> @file:File -> @+position:U32 -> @+remaining:U32 -> @target:Target -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @+count:U32 -> Reading

def taken source · line 136 · raw

@read:Read -> @+position:U32 -> @+remaining:U32 -> @target:Target -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @+count:U32 -> Reading

def read_all source · line 140 · raw

@fuel:Nat -> @reading:Reading -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @+count:U32 -> IO(Outcome)

def fresh source · line 150 · raw

@+count:U32 -> Target

def fuel source · line 153 · raw

@+length:U32 -> Nat

def read_range source · line 158 · raw

@file:File -> @+position:U32 -> @+length:U32 -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @+count:U32 -> IO(Outcome)

The elements stored from the position on, as many bytes as the count of the format takes; the file stays open.

def fault source · line 161 · raw

@operation:String -> @path:String -> @detail:String -> @shape:List<&2, U32> -> Tensor

def sized source · line 164 · raw

@matches:Bool -> @operation:String -> @path:String -> @+shape:List<&2, U32> -> @buffer:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> Tensor

def whole source · line 169 · raw

@aligned:Bool -> @operation:String -> @path:String -> @+shape:List<&2, U32> -> @buffer:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> @+found:U32 -> @+count:U32 -> Tensor

def aligned source · line 174 · raw

@operation:String -> @path:String -> @+shape:List<&2, U32> -> @target:Target -> @+count:U32 -> Tensor

def checked source · line 179 · raw

@complete:Bool -> @operation:String -> @path:String -> @+shape:List<&2, U32> -> @target:Target -> @+count:U32 -> Tensor

def decoded source · line 184 · raw

@operation:String -> @path:String -> @+shape:List<&2, U32> -> @outcome:Outcome -> @+count:U32 -> Pair(File, Tensor)

def ranged_count source · line 188 · raw

@fits:Bool -> @file:File -> @operation:String -> @path:String -> @+shape:List<&2, U32> -> @+position:U32 -> @+length:U32 -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> @+count:U32 -> IO(Pair(File, Tensor))

def ranged_shape source · line 196 · raw

@count:Maybe<&2, U32> -> @file:File -> @operation:String -> @path:String -> @+shape:List<&2, U32> -> @+position:U32 -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> IO(Pair(File, Tensor))

def load_range source · line 205 · raw

@file:File -> @operation:String -> @+path:String -> @+shape:List<&2, U32> -> @+position:U32 -> @+format:0xd6aaab858a4fbd1461bd0fee27b97184/storage_element_format.Format -> IO(Pair(File, Tensor))

The tensor of the shape whose elements of the format start at the position of an open file, which stays open.

def closed source · line 208 · raw

@pair:Pair(File, Tensor) -> IO(Tensor)

def refused source · line 214 · raw

@file:File -> @tensor:Tensor -> IO(Tensor)

def measured source · line 221 · raw

@aligned:Bool -> @matches:Bool -> @file:File -> @operation:String -> @+path:String -> @+shape:List<&2, U32> -> @+position:U32 -> @+count:U32 -> IO(Tensor)

A raw file holds exactly the bytes of its shape: a size that is not a whole number of values, or another number of them, is refused before reading.

def with_size source · line 230 · raw

@result:Result<&1, &1, Pair(U32, String), U32> -> @file:File -> @operation:String -> @+path:String -> @+shape:List<&2, U32> -> @+position:U32 -> @+count:U32 -> IO(Tensor)

def sized_file source · line 236 · raw

@sized:Sized -> @operation:String -> @+path:String -> @+shape:List<&2, U32> -> @+position:U32 -> @+count:U32 -> IO(Tensor)

def fp32_values source · line 240 · raw

@file:File -> @operation:String -> @+path:String -> @+shape:List<&2, U32> -> @+position:U32 -> @+count:U32 -> IO(Tensor)

def opened source · line 245 · raw

@result:Result<&1, &1, Pair(U32, String), File> -> @+path:String -> @+shape:List<&2, U32> -> @+count:U32 -> IO(Tensor)

def counted source · line 250 · raw

@count:Maybe<&2, U32> -> @+path:String -> @+shape:List<&2, U32> -> IO(Tensor)

def load source · line 259 · raw

@+path:String -> @+shape:List<&2, U32> -> IO(Tensor)

The file holds the values of the shape in row-major order and nothing else.

def magic source · line 269 · raw

List<&2, U32>

def contains source · line 272 · raw

@text:List<&2, U32> -> @+pattern:List<&2, U32> -> Bool

def after source · line 278 · raw

@text:List<&2, U32> -> @+pattern:List<&2, U32> -> Maybe<&2, List<&2, U32>>

The text after the first occurrence of the pattern.

def pushed source · line 284 · raw

@digits:Bool -> @current:U32 -> @shape:List<&2, U32> -> List<&2, U32>

def closed_shape source · line 289 · raw

@valid:Bool -> @shape:List<&2, U32> -> Maybe<&2, List<&2, U32>>

def extents source · line 295 · raw

@text:List<&2, U32> -> @+current:U32 -> @digits:Bool -> @valid:Bool -> @shape:List<&2, U32> -> Maybe<&2, List<&2, U32>>

Extents up to the closing parenthesis: digits, commas and spaces.

def shaped source · line 305 · raw

@shape:Maybe<&2, List<&2, U32>> -> @+data:U32 -> Maybe<&2, Header>

def typed source · line 310 · raw

@supported:Bool -> @text:Maybe<&2, List<&2, U32>> -> @+data:U32 -> Maybe<&2, Header>

def described source · line 316 · raw

@+text:List<&2, U32> -> @+length:Nat -> @+data:U32 -> Maybe<&2, Header>

def region source · line 320 · raw

@+bytes:List<&2, U32> -> @+prefix:U32 -> @+length:U32 -> Maybe<&2, Header>

def versioned source · line 324 · raw

@major:U32 -> @+bytes:List<&2, U32> -> Maybe<&2, Header>

Version 1 has a two-byte header length; later versions have four bytes.

def marked source · line 329 · raw

@valid:Bool -> @+bytes:List<&2, U32> -> Maybe<&2, Header>

def npy_counted source · line 337 · raw

@count:Maybe<&2, U32> -> @+shape:List<&2, U32> -> @+data:U32 -> @file:File -> @+path:String -> IO(Tensor)

def npy_parsed source · line 342 · raw

@parsed:Maybe<&2, Header> -> @file:File -> @+path:String -> IO(Tensor)

def npy_received source · line 347 · raw

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

def npy_headed source · line 352 · raw

@read:Read -> @+path:String -> IO(Tensor)

def npy_opened source · line 356 · raw

@result:Result<&1, &1, Pair(U32, String), File> -> @+path:String -> IO(Tensor)

def load_npy source · line 365 · raw

@+path:String -> IO(Tensor)

An FP32 array saved by NumPy, with its shape.

def bytes source · line 376 · raw

@+word:U32 -> @rest:List<&2, U32> -> List<&2, U32>

def encode source · line 379 · raw

@count:Nat -> @values:List<&2, F32> -> List<&2, U32>

def read_chunk source · line 386 · raw

@count:Nat -> @+offset:U32 -> @storage:Array<F32> -> @values:List<&2, F32> -> Pair(Array<F32>, List<&2, F32>)

The cursor retains the array; only the current chunk becomes a list.

def pending source · line 393 · raw

@remaining:Nat -> @file:File -> @storage:Array<F32> -> @offset:U32 -> Writing

def continued source · line 398 · raw

@result:Result<&1, &1, Pair(U32, String), Unit> -> @file:File -> @storage:Array<F32> -> @remaining:Nat -> @offset:U32 -> Writing

def after_write source · line 403 · raw

@written:Written -> @storage:Array<F32> -> @remaining:Nat -> @offset:U32 -> Writing

def write_chunk source · line 407 · raw

@file:File -> @read:Pair(Array<F32>, List<&2, F32>) -> @+count:Nat -> @remaining:Nat -> @offset:U32 -> IO(Writing)

def finished source · line 413 · raw

@file:File -> @complete:Bool -> IO(Bool)

def write_all source · line 418 · raw

@fuel:Nat -> @writing:Writing -> IO(Bool)

def unsaved source · line 429 · raw

@pair:Pair(Tensor, List<&2, U32>) -> @operation:String -> @path:String -> @detail:String -> Tensor

def written source · line 433 · raw

@complete:Bool -> @operation:String -> @path:String -> @tensor:Tensor -> Tensor

def created source · line 439 · raw

@result:Result<&1, &1, Pair(U32, String), File> -> @operation:String -> @+path:String -> @tensor:Tensor -> @prefix:List<&2, U32> -> @storage:Array<F32> -> @+length:Nat -> IO(Tensor)

The prefix is written first; a raw file has none.

def saving source · line 448 · raw

@operation:String -> @+path:String -> @tensor:Tensor -> @prefix:List<&2, U32> -> @buffer:Result<&2, &1, 0xd6aaab858a4fbd1461bd0fee27b97184/tensor.Problem, 0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32>> -> IO(Tensor)

def save source · line 458 · raw

@+path:String -> @tensor:Tensor -> IO(Tensor)

Writes the values in row-major order and returns the tensor. A failed tensor writes nothing and keeps its problem.

def joined source · line 461 · raw

@shape:List<&2, U32> -> String

def tuple source · line 468 · raw

@shape:List<&2, U32> -> String

NumPy writes (), (3,) and (2, 3).

def spaces source · line 474 · raw

@count:Nat -> List<&2, U32>

def framed source · line 480 · raw

@+text:List<&2, U32> -> @+length:U32 -> List<&2, U32>

Version 1: the header ends in a newline and the data starts at a multiple of 64 bytes.

def npy_header source · line 485 · raw

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

def npy_saving source · line 489 · raw

@+path:String -> @pair:Pair(Tensor, Tensor) -> @+shape:List<&2, U32> -> IO(Tensor)

def save_npy source · line 494 · raw

@+path:String -> @tensor:Tensor -> IO(Tensor)

Writes a .npy file that NumPy loads with the tensor's shape.