beetlebug▾
/

ztcl::tensor

Makes and computes typed tensors of f32, f64, or i32 with bit-exact kernels.

ztcl::tensor SUBCOMMAND ?ARG ...?

Description

A tensor is a kind of value of ztcl: a typed buffer of numbers with a shape of at most 8 dimensions, in row-major order. Each command gets the buffer of a tensor argument with no conversion and gives a new tensor. No command changes a tensor that it gets.

The string form is the list tensor DTYPE SHAPE VALUES, with each element as the shortest decimal text that reads back to the same bits. A NaN that a command computes is the plain NaN, so its bits do not depend on the CPU. A string in that form is a tensor for each command.

Load the module with package require ztcl::tensor. It needs no capability. Each subcommand is also the command ztcl::tensor::NAME, such as ztcl::tensor::add.

Subcommands

SubcommandDescription
ztcl::tensor ofMakes a tensor of a list of numbers.
ztcl::tensor fullMakes a tensor with one value in each element.
ztcl::tensor zerosMakes a tensor of zeros.
ztcl::tensor onesMakes a tensor of ones.
ztcl::tensor scalarMakes a tensor of rank 0.
ztcl::tensor arangeMakes the tensor 0, 1, and up to N - 1.
ztcl::tensor randMakes a tensor of uniform random numbers from a seed.
ztcl::tensor randnMakes a tensor of normal random numbers from a seed.
ztcl::tensor permMakes a random order of the numbers 0 to N - 1.
ztcl::tensor frombytesMakes a tensor of unsigned bytes times a scale.
ztcl::tensor rawMakes a tensor of the little-endian bytes of its elements.
ztcl::tensor readReads the little-endian bytes of a tensor from a channel.
ztcl::tensor bytesGives the little-endian bytes of the elements.
ztcl::tensor shapeGives the shape of a tensor.
ztcl::tensor dtypeGives the dtype of a tensor.
ztcl::tensor rankGives the count of dimensions of a tensor.
ztcl::tensor sizeGives the count of elements of a tensor.
ztcl::tensor itemGives the number of a tensor with one element.
ztcl::tensor flatGives the elements as a flat list of numbers.
ztcl::tensor tolistGives the elements as a nested list.
ztcl::tensor equalTests that two tensors have the same dtype, shape, and bits.
ztcl::tensor checkChecks a tensor and gives it as a tensor value.
ztcl::tensor reshapeGives the elements of a tensor with another shape.
ztcl::tensor sliceCopies a box of a tensor.
ztcl::tensor rowsCopies a range of rows of the first axis.
ztcl::tensor gatherCopies the rows of a list of row numbers.
ztcl::tensor concatJoins tensors along the first axis.
ztcl::tensor reserveGives a copy of a tensor with room to append rows in place.
ztcl::tensor castConverts the elements to another dtype.
ztcl::tensor transposeGives the transpose of a matrix.
ztcl::tensor rowbytesGives the rows of the first axis as a list of tensors.
ztcl::tensor fromrowsJoins a list of rows into one tensor.
ztcl::tensor matmulGives the matrix product of two matrices.
ztcl::tensor linearGives the product of a matrix and the transpose of another.
ztcl::tensor attentionGives the attention of one query row of each head over a key-value cache.
ztcl::tensor rmsnormGives the root-mean-square norm of each row, times a weight row.
ztcl::tensor ropeTurns each pair of elements of the last axis by an angle.
ztcl::tensor argsortGives the indices that sort a tensor of rank 1.
ztcl::tensor cumsumGives the running sum along the last axis.
ztcl::tensor whereGives the indices of the elements that are not 0.
ztcl::tensor aboveGives the first index of an element above a number.
ztcl::tensor softmaxGives the softmax of each row of a matrix.
ztcl::tensor logsoftmaxGives the log of the softmax of each row of a matrix.
ztcl::tensor pickGives one element of each row of a matrix.
ztcl::tensor unpickMakes a zero matrix with one value of G in each row.
ztcl::tensor onehotMakes the one-hot rows of a list of classes.
ztcl::tensor sumtoSums a tensor down to a shape that broadcasts to it.
ztcl::tensor expandRepeats a tensor to a shape by the broadcast rule.
ztcl::tensor axpbyGives A * X + B * Y for two numbers and two tensors.
ztcl::tensor mapRuns one expression for each element of the broadcast of its arguments.
ztcl::tensor kernelMakes a command that runs one expression for each element of its tensor arguments.
ztcl::tensor addAdds two tensors, element by element.
ztcl::tensor subSubtracts two tensors, element by element.
ztcl::tensor mulMultiplies two tensors, element by element.
ztcl::tensor divDivides two tensors, element by element.
ztcl::tensor powRaises to a power two tensors, element by element.
ztcl::tensor maximumGives the larger of two tensors, element by element.
ztcl::tensor minimumGives the smaller of two tensors, element by element.
ztcl::tensor gtTests greater than for two tensors, element by element.
ztcl::tensor ltTests less than for two tensors, element by element.
ztcl::tensor geTests greater than or equal for two tensors, element by element.
ztcl::tensor leTests less than or equal for two tensors, element by element.
ztcl::tensor eqTests equality for two tensors, element by element.
ztcl::tensor negGives the negative of each element.
ztcl::tensor absGives the absolute value of each element.
ztcl::tensor expGives e to the power of each element.
ztcl::tensor logGives the natural logarithm of each element.
ztcl::tensor sqrtGives the square root of each element.
ztcl::tensor tanhGives the hyperbolic tangent of each element.
ztcl::tensor sigmoidGives 1 / (1 + exp(-x)) for each element.
ztcl::tensor reluGives the element, or 0 for an element below 0 for each element.
ztcl::tensor stepGives 1 for an element above 0, else 0 for each element.
ztcl::tensor squareGives the square of each element.
ztcl::tensor signGives 1, -1, or 0 by the sign for each element.
ztcl::tensor sinGives the sine of each element.
ztcl::tensor cosGives the cosine of each element.
ztcl::tensor recipGives 1 / x for each element.
ztcl::tensor sumGives the sum of a tensor or of one axis.
ztcl::tensor meanGives the mean of a tensor or of one axis.
ztcl::tensor maxGives the largest element of a tensor or of one axis.
ztcl::tensor minGives the smallest element of a tensor or of one axis.
ztcl::tensor argmaxGives the index of the largest element of a tensor or of one axis.
ztcl::tensor prodGives the product of a tensor or of one axis.

Returns

The result of the subcommand.

Examples

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

% ztcl::tensor of {{1 2} {3 4}}
tensor f32 {2 2} {1.0 2.0 3.0 4.0}

% ztcl::tensor add [ztcl::tensor of {1 2}] 0.5
tensor f32 2 {1.5 2.5}

Errors

% ztcl::tensor bogus
unknown or ambiguous subcommand "bogus": must be above, abs, add, arange, argmax, argsort, attention, axpby, bytes, cast, check, concat, cos, cumsum, div, dtype, eq, equal, exp, expand, flat, frombytes, fromrows, full, gather, ge, gt, item, kernel, le, linear, log, logsoftmax, lt, map, matmul, max, maximum, mean, min, minimum, mul, neg, of, onehot, ones, perm, pick, pow, prod, rand, randn, rank, raw, read, recip, relu, reserve, reshape, rmsnorm, rope, rowbytes, rows, scalar, shape, sigmoid, sign, sin, size, slice, softmax, sqrt, square, step, sub, sum, sumto, tanh, tolist, transpose, unpick, where, or zeros
-errorcode TCL LOOKUP SUBCOMMAND bogus

Facts

Capability
None.
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.

The full page: ztcl::tensor.