beetlebug▾
/

ztcl::tensor rope

Turns each pair of elements of the last axis by an angle.

ztcl::tensor rope X COS SIN

Description

Gives X * COS + S * SIN, where S is X with each pair of elements 2i and 2i + 1 of the last axis swapped. COS and SIN have the shape of X. With COS = {c c} and SIN = {-s s} for each pair, it turns the pair (a, b) to (a c - b s, b c + a s), the rotary position embedding of a transformer. The bits are those of add, and mul of each product, in that order.

Returns

A tensor of the shape of X.

Examples

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

% ztcl::tensor rope [ztcl::tensor of {{1 2}}] [ztcl::tensor of {{0 0}}] [ztcl::tensor of {{-1 1}}]
tensor f32 {1 2} {-2.0 1.0}

Errors

% ztcl::tensor rope [ztcl::tensor zeros {1 3}] [ztcl::tensor zeros {1 3}] [ztcl::tensor zeros {1 3}]
tensor rope: the shapes {1 3}, {1 3}, and {1 3} do not match: the three need one shape with an even last axis
-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.