gizai:tensor/core
the Tensor and its storage, f32 vector math in lanes (Cephes polynomials), reductions with an order policy, layout, matmul on the CPU's FMA tiles, sums to a shape, views and files.
import * as Core from "gizai:tensor/core";Types
Named
type Named = { name: string; tensor: Tensor; }A named tensor, as a safetensors file holds them.
name:stringtensor:_gizlang_lib_ai__tensor__storage.Tensor
Functions
matmulT
function matmulT(x: Tensor, transposeA: bool, y: Tensor, transposeB: bool): TensorA times B with either read transposed in place, no copy made: A is (..., m, k) or, with transposeA, a 2-d (k, m); B is (k, n) or, with transposeB, (n, k). Each sum adds a[i][p] b[p][j] by fma for p in order from 0, so the result is the same bits however the work is split. B is packed once in panels of 32 columns; rows go in blocks across the cores once there are 2^21 or more multiply-adds, as many blocks as cores (in multiples of 6 rows, at most 96), and within a block 6x32 FMA tiles stay in registers. The result is of productType.
Effects: fail, read, write, console, io.
productEval
function productEval(x: Lazy, p: Product, dtype: i64, into: Option<Buffer>): TensorA product chain's tensor: on the GPU of any operand or input in one kernel, else the product in host memory and then the chain over it.
Effects: fail, read, write, console, io.
longs
function longs(xs: Vec<i64>): bytesXS as 64-bit little-endian words, for kernel arguments.
Effects: fail.
concat
function concat(xs: Vec<Tensor>, axis: i64, dtype: i64 = F32): TensorThe tensors joined along axis (equal elsewhere), in order, stored as dtype on a GPU.
Effects: fail, read, write, console, io.
batchMatmul
function batchMatmul(x: Tensor, transposeA: bool, y: Tensor, transposeB: bool): TensorBatched matmul: x (b, m, k) (or (b, k, m) with transposeA) times y (b, k, n) (or (b, n, k) with transposeB), or one y (k, n) for every batch.
Effects: fail, read, write, console, io.
within
function within(buffer: Buffer, gpu: Gpu, shape: Vec<i64>, dtype: i64): TensorA tensor of shape and dtype on the first bytes of buffer (work memory a caller reuses).
Effects: fail.
toSafetensors
function toSafetensors(tensors: Vec<Named>): bytesTENSORS as a safetensors file of F32 data, in their order.
Effects: fail, read, write, console, io.
fromSafetensors
function fromSafetensors(data: bytes): Vec<Named> throws stringThe tensors of a safetensors file, in the header's order, as f32 (gizlang:safetensors decoded).
Effects: fail.
placedOn
function placedOn(t: Tensor, gpu: Option<Gpu>): TensorTensor t on gpu when there is one, else as it is: where a model runs.
Effects: fail, read, write, console, io.
storedOn
function storedOn(sys: System, s: Stored, gpu: Option<Gpu>, linear: bool): Tensor throws stringA stored tensor where it runs: a linear as BF16 on a GPU, anything else as f32 (BF16, F16, F32 and E4M3 bytes go to the GPU as they are stored and are cast there).
Effects: fail, read, write, console, io.
textOf
function textOf(t: Tensor, axis: i64, at: i64): stringThe values of tensor t along axis from flat offset at, nested as bracketed lists.
Effects: fail.
onDevice
function onDevice(shape: Vec<i64>, gpu: Gpu, buffer: Buffer, dtype: i64): TensorThe tensor of shape whose dtype elements (row-major) are device memory buffer of gpu: a kernel's result,
or weights uploaded as their bytes.
Methods
Tensor.text
function Tensor.text(self: Tensor): stringThe values nested by shape, as numpy prints them: [[1, 2], [3, 4]]. A GPU tensor is copied to the host first.
Effects: fail, read, write, console, io.
Tensor.size
function Tensor.size(self: Tensor): i64The number of values: the product of the shape.
Effects: fail.
Tensor.gpu
function Tensor.gpu(self: Tensor): Option<Gpu>Where the tensor is: none for host memory.
Tensor.isHost
function Tensor.isHost(self: Tensor): boolWhether the tensor is in host memory.
Tensor.dtype
function Tensor.dtype(self: Tensor): i64The element type: F32 for a host tensor.
Tensor.on
function Tensor.on(self: Tensor, gpu: Gpu): TensorA copy on gpu (the same tensor when it is there).
Effects: fail, read, write, console, io.
Tensor.host
function Tensor.host(self: Tensor): TensorA copy in host memory (the same tensor when it is there), as f32.
Effects: fail, read, write, console, io.
Tensor.cast
function Tensor.cast(self: Tensor, dtype: i64): TensorThe same values stored as dtype (F32, F16, BF16 or FP8; rounded to nearest even, FP8 saturated at ±448):
on a GPU only, as host tensors are f32.
Effects: fail, read, write, console, io.
Tensor.assign
function Tensor.assign(self: Tensor, x: Tensor): voidWrites x's values into this GPU tensor's memory (x of the same shape, on the same GPU or the host), so that the kernels of a recording (Gpu.record) that read this tensor read them.
Effects: fail, read, write, console, io.
Tensor.buffer
function Tensor.buffer(self: Tensor): BufferThe buffer of a GPU tensor.
Effects: fail.
Tensor.onGpu
function Tensor.onGpu(self: Tensor): OnGpuWhere a GPU tensor is: its device, buffer and element type.
Effects: fail.
Tensor.placement
function Tensor.placement(self: Tensor): Option<OnGpu>Where the tensor is: on a GPU (its device, buffer and element type), or none for host memory.
Tensor.deviceBuffer
function Tensor.deviceBuffer(self: Tensor): BufferA GPU tensor's device memory, for kernels made elsewhere (gizai:tile): its dtype() elements in row-major order.
Effects: fail.
Tensor.rank
function Tensor.rank(self: Tensor): i64The number of axes.
Tensor.get
function Tensor.get(self: Tensor, index: Vec<i64>): f64The element at a full index (R001 outside).
Effects: fail, read, write, console, io.
Tensor.values
function Tensor.values(self: Tensor): Vec<f64>The values in row-major order, as f64, copied to the host when the tensor is on a GPU.
Effects: fail, read, write, console, io.
Tensor.reshape
function Tensor.reshape(self: Tensor, shape: Vec<i64>): TensorThe same elements in another shape of the same size; one -1 is inferred.
Effects: fail.
Tensor.glu
function Tensor.glu(self: Tensor): TensorSwiGLU of the rows [m, n] of a host tensor as pairs, [m, n / 2]: column c is silu(v[2c]) times v[2c + 1], rounded to f32 (Lazy.gluIn on the host).
Effects: fail.
Tensor.add
function Tensor.add(self: Tensor, y: Tensor): TensorSELF + Y, element by element, with broadcasting.
Effects: fail, read, write, console, io.
Tensor.sub
function Tensor.sub(self: Tensor, y: Tensor): TensorSELF - Y, element by element, with broadcasting.
Effects: fail, read, write, console, io.
Tensor.mul
function Tensor.mul(self: Tensor, y: Tensor): TensorSELF * Y, element by element, with broadcasting.
Effects: fail, read, write, console, io.
Tensor.div
function Tensor.div(self: Tensor, y: Tensor): TensorSELF / Y, element by element, with broadcasting.
Effects: fail, read, write, console, io.
Tensor.maximum
function Tensor.maximum(self: Tensor, y: Tensor): TensorThe larger of SELF and Y, element by element, with broadcasting.
Effects: fail, read, write, console, io.
Tensor.minimum
function Tensor.minimum(self: Tensor, y: Tensor): TensorThe smaller of SELF and Y, element by element, with broadcasting.
Effects: fail, read, write, console, io.
Tensor.neg
function Tensor.neg(self: Tensor): Tensor-SELF, element by element.
Effects: fail, read, write, console, io.
Tensor.abs
function Tensor.abs(self: Tensor): TensorThe absolute value of SELF, element by element.
Effects: fail, read, write, console, io.
Tensor.sqrt
function Tensor.sqrt(self: Tensor): TensorThe square root of SELF, element by element.
Effects: fail, read, write, console, io.
Tensor.exp
function Tensor.exp(self: Tensor): Tensore to the power SELF, element by element.
Effects: fail, read, write, console, io.
Tensor.log
function Tensor.log(self: Tensor): TensorThe natural logarithm of SELF, element by element.
Effects: fail, read, write, console, io.
Tensor.tanh
function Tensor.tanh(self: Tensor): TensorThe hyperbolic tangent of SELF, element by element.
Effects: fail, read, write, console, io.
Tensor.sigmoid
function Tensor.sigmoid(self: Tensor): TensorThe sigmoid 1 / (1 + e^-x) of SELF, element by element.
Effects: fail, read, write, console, io.
Tensor.relu
function Tensor.relu(self: Tensor): Tensormax(SELF, 0), element by element.
Effects: fail, read, write, console, io.
Tensor.square
function Tensor.square(self: Tensor): TensorSELF squared, element by element.
Effects: fail, read, write, console, io.
Tensor.gelu
function Tensor.gelu(self: Tensor): TensorGELU of SELF (the tanh approximation), element by element.
Effects: fail, read, write, console, io.
Tensor.geluErf
function Tensor.geluErf(self: Tensor): TensorGELU of SELF (the exact erf form), element by element.
Effects: fail, read, write, console, io.
Tensor.expand
function Tensor.expand(self: Tensor, shape: Vec<i64>): TensorThe tensor broadcast to shape, where it is.
Effects: fail, read, write, console, io.
Tensor.filled
function Tensor.filled(self: Tensor, shape: Vec<i64>, value: f64): Tensorvalue in every element of shape, where this tensor is (host memory or its GPU).
Effects: fail, read, write, console, io.
Tensor.item
function Tensor.item(self: Tensor): f64The value of a one-element tensor.
Effects: fail, read, write, console, io.
Tensor.sum
function Tensor.sum(self: Tensor, policy: Reduction): f64The sum of all values, in the order POLICY sets (strict: the same on any core count and device).
Effects: fail, read, write, console, io.
Tensor.mean
function Tensor.mean(self: Tensor, policy: Reduction): f64The mean of all values, summed in the order POLICY sets.
Effects: fail, read, write, console, io.
Tensor.max
function Tensor.max(self: Tensor): f64The largest value.
Effects: fail, read, write, console, io.
Tensor.transpose
function Tensor.transpose(self: Tensor): TensorThe 2-d transpose, in bands of 32 output rows on every core (Task.fill); columns past the last whole band one by one.
Effects: fail, read, write, console, io.
Tensor.softmax
function Tensor.softmax(self: Tensor): Tensorsoftmax over the last axis: max-subtracted so no exp overflows.
Effects: fail, read, write, console, io.
Tensor.packed
function Tensor.packed(self: Tensor, transposed: bool): TensorThis 2-d host tensor as the B of products (read transposed when transposed, as matmulT's transposeB), its panels
packed once and kept: a product with it on the CPU packs nothing. A weight is packed once, at load; a reshape drops
the panels.
Effects: fail.
Tensor.matmul
function Tensor.matmul(self: Tensor, y: Tensor): Tensorself (..., m, k) times y (k, n): leading axes are rows too.
Effects: fail, read, write, console, io.
Tensor.total
function Tensor.total(self: Tensor, policy: Reduction): TensorThe sum (Tensor.sum) as a tensor of shape [] where this one is: on a GPU it stays there, with no wait.
Effects: fail, read, write, console, io.
Tensor.average
function Tensor.average(self: Tensor, policy: Reduction): TensorThe mean (Tensor.mean) as a tensor of shape [] where this one is, as Tensor.total.
Effects: fail, read, write, console, io.
Tensor.sumTo
function Tensor.sumTo(self: Tensor, shape: Vec<i64>): TensorSummed down to shape over the axes broadcasting stretched (the adjoint of broadcasting; a reduction over
any axes that keeps them as 1). Over the last axis only, each row adds as sum(Reduction.strict()) adds
a part (f64 lanes); otherwise each element adds its sources in index order from 0 in f32.
Effects: fail, read, write, console, io.
Tensor.maxTo
function Tensor.maxTo(self: Tensor, shape: Vec<i64>): TensorThe largest element over the axes that shape keeps as 1 (NaN is ignored, as by max; -Infinity
where every element is NaN or none is left).
Effects: fail, read, write, console, io.
Tensor.take
function Tensor.take(self: Tensor, ids: Vec<i64>): TensorRows ids of the tensor (its first axis): an embedding lookup.
Effects: fail, read, write, console, io.
Tensor.addRows
function Tensor.addRows(self: Tensor, ids: Vec<i64>, rows: i64): TensorThe adjoint of take: a (rows, ...) tensor whose row r adds the rows i of this one with ids[i] = r, in increasing i (on a GPU each output element adds its own list, so no two threads write one place and the order holds).
Effects: fail, read, write, console, io.
Tensor.slice
function Tensor.slice(self: Tensor, axis: i64, start: i64, end: i64): TensorElements start..end of axis.
Effects: fail, read, write, console, io.
Tensor.permute
function Tensor.permute(self: Tensor, axes: Vec<i64>): TensorThe axes reordered: output axis i is input axis axes[i].
Effects: fail, read, write, console, io.
Tensor.rows
function Tensor.rows(self: Tensor, r0: i64, r1: i64): TensorRows [r0, r1) of the tensor ([rows, ...] on a GPU) on the same memory, with no copy (Tensor.slice copies).
Effects: fail.
Tensor.address
function Tensor.address(self: Tensor): u64The device address of the tensor's first element (a kernel's buffer argument).
Effects: fail.
Stored.upload
function Stored.upload(self: Stored, sys: System, gpu: Gpu): Buffer throws stringThe stored tensor's raw bytes in a new buffer on gpu, read from its file straight to the device (Gpu.uploadFile).
Effects: fail, read, write, io.