~/bend-docscommunity

tensor_f32.bend checks

raw source on the hub · import stelliferous@0.0.2.0/tensor_f32.bend as Tensor_f32

The everyday F32 tensor module: import this one module and call F.add, F.concat, F.check, ... without element-type arguments. Every function is a direct specialization of the generic tensor modules; Base determines operand order, rounding, NaN and signed-zero behavior.

14 imports
import Base
import ./storage_buffer.bend as Buffers
import ./math_activation.bend as Activations
import ./math_fp32.bend as Arithmetic
import ./math_compare.bend as Compare
import ./tensor.bend as Tensors
import ./tensor_operations.bend as Operations
import ./tensor_lanes.bend as Lanes
import ./tensor_select.bend as Select
import ./tensor_layout.bend as Layout
import ./tensor_window.bend as Window
import ./tensor_convolution.bend as TensorConvolution
import ./tensor_file.bend as Files
import ./tensor_model.bend as Models

Definitions

def Tensor source · line 20 · raw

Type

def Problem source · line 23 · raw

Data

def Storage source · line 27 · raw

Type

Storage exchanged with foreign I/O; see from_storage and into_storage.

def full source · line 32 · raw

@+shape:List<&2, U32> -> @+value:F32 -> Tensor

def zeros source · line 35 · raw

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

def ones source · line 38 · raw

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

def from_list source · line 41 · raw

@+shape:List<&2, U32> -> @+values:List<&2, F32> -> Tensor

def from_storage source · line 44 · raw

@+shape:List<&2, U32> -> @storage:Storage -> Tensor

def load_raw source · line 49 · raw

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

Raw little-endian FP32 files: the values of the shape in row-major order. A file that cannot be read, or does not match the shape, gives a failed tensor.

def save_raw source · line 52 · raw

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

def load_npy source · line 56 · raw

@+path:String -> IO(Tensor)

NumPy .npy files of FP32 values; the file carries the shape.

def save_npy source · line 59 · raw

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

def Weights source · line 64 · raw

Type

Models: tensors by name, loaded once. In do Model<T>: a step takes a tensor from the weights with take, or puts a copy there with keep for a later take.

def Model source · line 67 · raw

@A:Type -> Type

def Model.pure source · line 70 · raw

@-A:Type -> @value:A -> Model(A)

def Model.bind source · line 73 · raw

@-A:Type -> @-B:Type -> @step:Model(A) -> @next:(@_:A -> Model(B)) -> Model(B)

def take source · line 76 · raw

@+name:String -> Model(Tensor)

def keep source · line 79 · raw

@+name:String -> @tensor:Tensor -> Model(Tensor)

def load_weights source · line 83 · raw

@+folder:String -> IO(Weights)

folder/names.txt lists one name per line; folder/<name>.npy holds its tensor.

def copy_weights source · line 86 · raw

@weights:Weights -> Pair(Weights, Weights)

def run source · line 89 · raw

@-A:Type -> @model:Model(A) -> @weights:Weights -> A

def run_keeping source · line 93 · raw

@-A:Type -> @model:Model(A) -> @weights:Weights -> Pair(A, Weights)

The result with what remains of the weights, kept tensors included.

def save_weights source · line 96 · raw

@+folder:String -> @weights:Weights -> IO(Weights)

def with_workers source · line 101 · raw

@tensor:Tensor -> @+count:U32 -> Tensor

Execution context: operations may use count workers, like a tensor's device. Read the runtime's pool once with IO.thread_count(); results keep the count.

def workers source · line 104 · raw

@tensor:Tensor -> Pair(Tensor, U32)

def shape source · line 109 · raw

@tensor:Tensor -> Pair(Tensor, List<&2, U32>)

def check source · line 112 · raw

@tensor:Tensor -> Result<&1, &1, Pair(Tensor, Problem), Tensor>

def describe source · line 115 · raw

@problem:Problem -> String

def expect source · line 118 · raw

@tensor:Tensor -> IO(Tensor)

def to_list source · line 121 · raw

@tensor:Tensor -> Result<&2, &2, Problem, List<&2, F32>>

def item source · line 124 · raw

@tensor:Tensor -> Result<&2, &2, Problem, F32>

def to_list_or source · line 129 · raw

@tensor:Tensor -> @fallback:List<&2, F32> -> List<&2, F32>

Reads with a fallback for a failed tensor, for callers that do not inspect the problem.

def item_or source · line 132 · raw

@tensor:Tensor -> @fallback:F32 -> F32

def into_storage source · line 135 · raw

@tensor:Tensor -> Result<&2, &1, Problem, Storage>

def expect_storage source · line 138 · raw

@tensor:Tensor -> IO(Storage)

def then source · line 144 · raw

@pair:Pair(Tensor, Tensor) -> @body:(@_:Tensor -> @_:Tensor -> Tensor) -> Tensor

def then_shape source · line 147 · raw

@pair:Pair(Tensor, List<&2, U32>) -> @body:(@_:Tensor -> @_:List<&2, U32> -> Tensor) -> Tensor

def copy source · line 150 · raw

@tensor:Tensor -> Pair(Tensor, Tensor)

def add source · line 155 · raw

@left:Tensor -> @right:Tensor -> Tensor

Binary elementwise operations reuse the left tensor's storage. The number after the name is the operation's scalar work for the band schedule.

def sub source · line 158 · raw

@left:Tensor -> @right:Tensor -> Tensor

def mul source · line 161 · raw

@left:Tensor -> @right:Tensor -> Tensor

def div source · line 165 · raw

@left:Tensor -> @right:Tensor -> Tensor

Base division: a zero divisor gives an infinity or NaN, never a failure.

def maximum source · line 168 · raw

@left:Tensor -> @right:Tensor -> Tensor

def minimum source · line 171 · raw

@left:Tensor -> @right:Tensor -> Tensor

def add_scalar source · line 174 · raw

@tensor:Tensor -> @+value:F32 -> Tensor

def sub_scalar source · line 177 · raw

@tensor:Tensor -> @+value:F32 -> Tensor

def mul_scalar source · line 180 · raw

@tensor:Tensor -> @+value:F32 -> Tensor

def div_scalar source · line 183 · raw

@tensor:Tensor -> @+value:F32 -> Tensor

def less source · line 186 · raw

@left:Tensor -> @right:Tensor -> Tensor

def less_equal source · line 189 · raw

@left:Tensor -> @right:Tensor -> Tensor

def greater source · line 192 · raw

@left:Tensor -> @right:Tensor -> Tensor

def greater_equal source · line 195 · raw

@left:Tensor -> @right:Tensor -> Tensor

def equal source · line 198 · raw

@left:Tensor -> @right:Tensor -> Tensor

def not_equal source · line 201 · raw

@left:Tensor -> @right:Tensor -> Tensor

def less_scalar source · line 207 · raw

@tensor:Tensor -> @+value:F32 -> Tensor

Scalar comparisons share the eight-lane map and preserve the tensor layout. Costs count AVX2 arithmetic, comparison, selection and value-bit instructions in one eight-element round, divided by eight; moves and addressing are excluded.

def less_equal_scalar source · line 210 · raw

@tensor:Tensor -> @+value:F32 -> Tensor

def greater_scalar source · line 213 · raw

@tensor:Tensor -> @+value:F32 -> Tensor

def greater_equal_scalar source · line 216 · raw

@tensor:Tensor -> @+value:F32 -> Tensor

def equal_scalar source · line 219 · raw

@tensor:Tensor -> @+value:F32 -> Tensor

def not_equal_scalar source · line 222 · raw

@tensor:Tensor -> @+value:F32 -> Tensor

def select source · line 225 · raw

@mask:Tensor -> @yes:Tensor -> @no:Tensor -> Tensor

def clamp source · line 228 · raw

@tensor:Tensor -> @+low:F32 -> @+high:F32 -> Tensor

def activate source · line 235 · raw

@tensor:Tensor -> @+activation:0x9be3b13bf249759bc81e1958bcd1a4c0/math_activation.Activation -> Tensor

Activations: Act.silu() and the other values of lib/math_activation.bend carry their own planner cost. activate applies one to a tensor; relu, silu, ... below are its shorthands. Custom carries only a cost; its function reaches conv2d_custom or apply, and activate fails on it.

def relu source · line 249 · raw

@tensor:Tensor -> Tensor

def relu6 source · line 252 · raw

@tensor:Tensor -> Tensor

def leaky_relu source · line 255 · raw

@tensor:Tensor -> @slope:F32 -> Tensor

def sigmoid source · line 258 · raw

@tensor:Tensor -> Tensor

def silu source · line 261 · raw

@tensor:Tensor -> Tensor

def tanh source · line 264 · raw

@tensor:Tensor -> Tensor

def hard_sigmoid source · line 267 · raw

@tensor:Tensor -> Tensor

def hard_swish source · line 270 · raw

@tensor:Tensor -> Tensor

def gelu_tanh source · line 274 · raw

@tensor:Tensor -> Tensor

PyTorch's gelu(approximate='tanh').

def conv2d source · line 279 · raw

@input:Tensor -> @weights:Tensor -> @bias:Tensor -> @stride:U32 -> @padding:U32 -> @activation:0x9be3b13bf249759bc81e1958bcd1a4c0/math_activation.Activation -> Tensor

Ordered convolution over CHW or NCHW; the activation runs on each finished leaf while it is in cache.

def arange source · line 296 · raw

@+start:F32 -> @+count:U32 -> Tensor

start, start + 1, ... as a rank-1 tensor of count elements.

def sum source · line 301 · raw

@tensor:Tensor -> Tensor

def max source · line 304 · raw

@tensor:Tensor -> Tensor

def min source · line 307 · raw

@tensor:Tensor -> Tensor

def mean source · line 311 · raw

@tensor:Tensor -> Tensor

sum divided by the number of elements; an empty tensor gives NaN.

def reshape source · line 318 · raw

@tensor:Tensor -> @+shape:List<&2, U32> -> Tensor

def transpose source · line 322 · raw

@tensor:Tensor -> @order:List<&2, U32> -> Tensor

The result's axis i is the argument's axis order[i].

def slice source · line 326 · raw

@tensor:Tensor -> @axis:U32 -> @start:U32 -> @count:U32 -> @step:U32 -> Tensor

count elements from start along axis, every step-th one.

def set_slice source · line 330 · raw

@target:Tensor -> @axis:U32 -> @start:U32 -> @part:Tensor -> Tensor

target with part written at start along axis.

def Parts source · line 333 · raw

Type

def single source · line 337 · raw

@tensor:Tensor -> Parts

Build the parts of concat: F.cons(a, F.cons(b, F.single(c))).

def cons source · line 340 · raw

@tensor:Tensor -> @rest:Parts -> Parts

def concat source · line 343 · raw

@parts:Parts -> @+axis:U32 -> Tensor

def upsample_nearest source · line 347 · raw

@tensor:Tensor -> @+scale:U32 -> Tensor

Repeats each element of the last two axes scale times in each direction.

def max_pool2d_with source · line 351 · raw

@tensor:Tensor -> @+kernel:U32 -> @+stride:U32 -> @+padding:U32 -> Tensor

Over the last two axes; positions outside the input count as -inf.

def max_pool2d source · line 355 · raw

@tensor:Tensor -> @+kernel:U32 -> Tensor

Windows of kernel by kernel that do not overlap: stride kernel, padding 0.

def broadcast_to source · line 362 · raw

@tensor:Tensor -> @+shape:List<&2, U32> -> Tensor

The tensor repeated to shape under NumPy broadcasting; add, sub, mul, max and min broadcast their operands in the same way.

def sum_axis source · line 367 · raw

@tensor:Tensor -> @+axis:U32 -> Tensor

Reductions along one axis remove that axis. Each result element takes the elements of the axis in increasing index, starting from 0, -inf or +inf.

def max_axis source · line 370 · raw

@tensor:Tensor -> @+axis:U32 -> Tensor

def min_axis source · line 373 · raw

@tensor:Tensor -> @+axis:U32 -> Tensor

def exp source · line 377 · raw

@tensor:Tensor -> Tensor

Elementwise functions share the eight-value traversal used by activations.

def log source · line 380 · raw

@tensor:Tensor -> Tensor

def softmax source · line 385 · raw

@tensor:Tensor -> @+axis:U32 -> Tensor

Keep a singleton reduction axis for broadcasting. Both reductions use the existing ordered fold; reduction gathers row bands to a dense owner.

def mean_axis source · line 393 · raw

@tensor:Tensor -> @+axis:U32 -> Tensor

sum_axis divided by the extent of the axis.

def pad source · line 400 · raw

@tensor:Tensor -> @+before:List<&2, U32> -> @+after:List<&2, U32> -> @+value:F32 -> Tensor

before[i] new elements in front of axis i and after[i] behind it, all equal to value.

def pad_edge source · line 404 · raw

@tensor:Tensor -> @+before:List<&2, U32> -> @+after:List<&2, U32> -> Tensor

The same, each new element repeating the nearest element of the tensor.

Templates

template conv2d_custom source · line 283 · raw

@-activation:(@_:F32 -> F32) -> @operations:F32 -> @input:Tensor -> @weights:Tensor -> @bias:Tensor -> @stride:U32 -> @padding:U32 -> Tensor

conv2d with a custom activation of the given scalar operations per element.

template apply source · line 288 · raw

@-operation:(@_:F32 -> F32) -> @operations:F32 -> @tensor:Tensor -> Tensor

Custom elementwise functions are passed as direct templates, never records, with their scalar operations per element.

template apply_indexed source · line 292 · raw

@-operation:(@_:U32 -> @_:F32 -> F32) -> @operations:F32 -> @tensor:Tensor -> Tensor

The function also receives the element's row-major index.