beetlebug▾
/

ztcl::tensor readchannels

Reads the little-endian bytes of a tensor from a channel.

ztcl::tensor read CHANNEL DTYPE SHAPE

Description

Reads the bytes of a tensor of SHAPE and DTYPE from CHANNEL: the elements in row-major order, little-endian, the layout of raw. The bytes go from the channel into the tensor with no string between, so a large weight file needs one copy of its bytes in memory. A file reads straight into the tensor.

CHANNEL needs -translation binary, as a file of open PATH rb has. The tensor comes from the heap before the read, so the memory limit counts it. When the channel ends before the count of SHAPE, the command gives an error, and the bytes that it read are gone from the channel.

Returns

A tensor.

Examples

Each example of this page runs after package require ztcl::tensor.

% set f [open w.bin wb]; puts -nonewline $f [binary format f2i {1.5 -2} 7]; close $f
> set f [open w.bin rb]
> set t [list [ztcl::tensor read $f f32 2] [ztcl::tensor read $f i32 {}]]
> close $f
> set t
{tensor f32 2 {1.5 -2.0}} {tensor i32 {} 7}

Errors

% set f [open w.bin wb]; puts -nonewline $f abc; close $f
> set f [open w.bin rb]
> try {ztcl::tensor read $f f32 1} finally {close $f}
tensor read: the shape {1} of f32 needs 4 bytes, and the channel gave 3
-errorcode ZTCL TENSOR

Facts

Capability
channels. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.4
Compute cost
A cost for each element operation by its kind (a copy, an add, a division, a math function, a multiply-add of a matmul), set so that one compute unit takes about the time of one unit of plain Tcl. docs/modules/tensor.md has the table.