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
| Subcommand | Description |
|---|---|
ztcl::tensor of | Makes a tensor of a list of numbers. |
ztcl::tensor full | Makes a tensor with one value in each element. |
ztcl::tensor zeros | Makes a tensor of zeros. |
ztcl::tensor ones | Makes a tensor of ones. |
ztcl::tensor scalar | Makes a tensor of rank 0. |
ztcl::tensor arange | Makes the tensor 0, 1, and up to N - 1. |
ztcl::tensor rand | Makes a tensor of uniform random numbers from a seed. |
ztcl::tensor randn | Makes a tensor of normal random numbers from a seed. |
ztcl::tensor perm | Makes a random order of the numbers 0 to N - 1. |
ztcl::tensor frombytes | Makes a tensor of unsigned bytes times a scale. |
ztcl::tensor raw | Makes a tensor of the little-endian bytes of its elements. |
ztcl::tensor read | Reads the little-endian bytes of a tensor from a channel. |
ztcl::tensor bytes | Gives the little-endian bytes of the elements. |
ztcl::tensor shape | Gives the shape of a tensor. |
ztcl::tensor dtype | Gives the dtype of a tensor. |
ztcl::tensor rank | Gives the count of dimensions of a tensor. |
ztcl::tensor size | Gives the count of elements of a tensor. |
ztcl::tensor item | Gives the number of a tensor with one element. |
ztcl::tensor flat | Gives the elements as a flat list of numbers. |
ztcl::tensor tolist | Gives the elements as a nested list. |
ztcl::tensor equal | Tests that two tensors have the same dtype, shape, and bits. |
ztcl::tensor check | Checks a tensor and gives it as a tensor value. |
ztcl::tensor reshape | Gives the elements of a tensor with another shape. |
ztcl::tensor slice | Copies a box of a tensor. |
ztcl::tensor rows | Copies a range of rows of the first axis. |
ztcl::tensor gather | Copies the rows of a list of row numbers. |
ztcl::tensor concat | Joins tensors along the first axis. |
ztcl::tensor reserve | Gives a copy of a tensor with room to append rows in place. |
ztcl::tensor cast | Converts the elements to another dtype. |
ztcl::tensor transpose | Gives the transpose of a matrix. |
ztcl::tensor rowbytes | Gives the rows of the first axis as a list of tensors. |
ztcl::tensor fromrows | Joins a list of rows into one tensor. |
ztcl::tensor matmul | Gives the matrix product of two matrices. |
ztcl::tensor linear | Gives the product of a matrix and the transpose of another. |
ztcl::tensor attention | Gives the attention of one query row of each head over a key-value cache. |
ztcl::tensor rmsnorm | Gives the root-mean-square norm of each row, times a weight row. |
ztcl::tensor rope | Turns each pair of elements of the last axis by an angle. |
ztcl::tensor argsort | Gives the indices that sort a tensor of rank 1. |
ztcl::tensor cumsum | Gives the running sum along the last axis. |
ztcl::tensor where | Gives the indices of the elements that are not 0. |
ztcl::tensor above | Gives the first index of an element above a number. |
ztcl::tensor softmax | Gives the softmax of each row of a matrix. |
ztcl::tensor logsoftmax | Gives the log of the softmax of each row of a matrix. |
ztcl::tensor pick | Gives one element of each row of a matrix. |
ztcl::tensor unpick | Makes a zero matrix with one value of G in each row. |
ztcl::tensor onehot | Makes the one-hot rows of a list of classes. |
ztcl::tensor sumto | Sums a tensor down to a shape that broadcasts to it. |
ztcl::tensor expand | Repeats a tensor to a shape by the broadcast rule. |
ztcl::tensor axpby | Gives A * X + B * Y for two numbers and two tensors. |
ztcl::tensor map | Runs one expression for each element of the broadcast of its arguments. |
ztcl::tensor kernel | Makes a command that runs one expression for each element of its tensor arguments. |
ztcl::tensor add | Adds two tensors, element by element. |
ztcl::tensor sub | Subtracts two tensors, element by element. |
ztcl::tensor mul | Multiplies two tensors, element by element. |
ztcl::tensor div | Divides two tensors, element by element. |
ztcl::tensor pow | Raises to a power two tensors, element by element. |
ztcl::tensor maximum | Gives the larger of two tensors, element by element. |
ztcl::tensor minimum | Gives the smaller of two tensors, element by element. |
ztcl::tensor gt | Tests greater than for two tensors, element by element. |
ztcl::tensor lt | Tests less than for two tensors, element by element. |
ztcl::tensor ge | Tests greater than or equal for two tensors, element by element. |
ztcl::tensor le | Tests less than or equal for two tensors, element by element. |
ztcl::tensor eq | Tests equality for two tensors, element by element. |
ztcl::tensor neg | Gives the negative of each element. |
ztcl::tensor abs | Gives the absolute value of each element. |
ztcl::tensor exp | Gives e to the power of each element. |
ztcl::tensor log | Gives the natural logarithm of each element. |
ztcl::tensor sqrt | Gives the square root of each element. |
ztcl::tensor tanh | Gives the hyperbolic tangent of each element. |
ztcl::tensor sigmoid | Gives 1 / (1 + exp(-x)) for each element. |
ztcl::tensor relu | Gives the element, or 0 for an element below 0 for each element. |
ztcl::tensor step | Gives 1 for an element above 0, else 0 for each element. |
ztcl::tensor square | Gives the square of each element. |
ztcl::tensor sign | Gives 1, -1, or 0 by the sign for each element. |
ztcl::tensor sin | Gives the sine of each element. |
ztcl::tensor cos | Gives the cosine of each element. |
ztcl::tensor recip | Gives 1 / x for each element. |
ztcl::tensor sum | Gives the sum of a tensor or of one axis. |
ztcl::tensor mean | Gives the mean of a tensor or of one axis. |
ztcl::tensor max | Gives the largest element of a tensor or of one axis. |
ztcl::tensor min | Gives the smallest element of a tensor or of one axis. |
ztcl::tensor argmax | Gives the index of the largest element of a tensor or of one axis. |
ztcl::tensor prod | Gives 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.