Autograd node for Rotary Position Embedding (RoPE, Su et al. 2021).
Rotates pairs of features in the last dimension of input by
position-dependent angles, encoding absolute position information
in a way that lets dot-product attention recover relative position.
During the forward, the backend returns a triple
{rotated_input, cos, sin}; the cos/sin tables are saved into
saved_cos_ and saved_sin_ so the backward can
apply the conjugate rotation without recomputing the trigonometry.
interleaved_ selects the pair layout:
true— adjacent pairs(d=0, d=1), (d=2, d=3), …false— split-half(d=0, d=D/2), (d=1, d=D/2+1), …
The chosen layout must match how the model was trained or attention scores will be silently corrupted.
Attributes
schema_ v1OpSchema"rotary_pos_embedding", version 1, AmpPolicy::ForceFP32). Forced to F32 because the rotation math is precision-sensitive at long sequence lengths.interleaved_boolorig_ shape_Shapesaved_ cos_Storagesaved_ sin_StorageStatic methods
1forward
→TensorImplPtrint forward(const int & input, const int & position_ids_or_null, bool interleaved)Apply RoPE to input.
Parameters
inputTensorImplPtrD even. L is the sequence axis and D is rotated in pairs.position_ ids_ or_ nullTensorImplPtr(L,). When null, the kernel uses [0, 1, …, L-1] implicitly.interleavedboolReturns
TensorImplPtrRotated output with the same shape as input.
Raises
ShapeMismatchinput is fewer than 2-D or its last dim is odd.DeviceMismatchposition_ids_or_null lives on a different device than input.Methods
3Apply the conjugate rotation to grad_out using the saved cos/sin tables.
Parameters
grad_ outStorageorig_shape_.Returns
std::vector<Storage>Single-element {grad_in} of the same shape.
The same rotation by from tracked ops, for create_graph=True; the saved tables are constants.