ztcl::tensor reserve
Gives a copy of a tensor with room to append rows in place.
ztcl::tensor reserve TENSOR ROWS
Description
Gives a tensor equal to TENSOR in a block with room for ROWS rows of its first axis. A concat of that tensor and one more tensor then writes the new rows after the old rows, with no copy of the old rows, and the result has the rest of the room. So a cache that grows by one row for each step, such as the key-value cache of a transformer, costs the copy of one row for each step.
No tensor changes: each tensor of the block has its own count of rows, and an append writes only after the last row of each. A concat to a tensor that another concat already extended copies. A concat past the room gives a tensor with room for twice its rows. The cost of concat is the copy of each element of the result in both cases, so the compute units depend on the shapes only. The memory limit counts the whole block.
Returns
A tensor.
Examples
Each example of this page runs after package require ztcl::tensor.
% set c [ztcl::tensor reserve [ztcl::tensor zeros {0 2}] 8]
> set c [ztcl::tensor concat $c [ztcl::tensor of {{1 2}}]]
> ztcl::tensor concat $c [ztcl::tensor of {{3 4}}]
tensor f32 {2 2} {1.0 2.0 3.0 4.0}
Errors
% ztcl::tensor reserve [ztcl::tensor zeros {2 2}] 1
tensor reserve: the count of rows 1 is outside 2..4294967295
-errorcode ZTCL TENSOR
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.