---
title: "ClikaRT::nn::RotaryEmbedding"
sidebar_label: "RotaryEmbedding"
description: "Rotary position embedding module: the angle tables built once at make, the rotation applied per call. The module form of ops::rotary_embedding."
---

<!-- Generated by tools/doxygen2mdx from Doxygen XML. Do not edit. -->

<span class="api-kind api-kind--class">class</span>

**Header:** `ClikaRT/nn/rotary_embedding.h`  
**Inherits:** [`ClikaRT::nn::Module`](./Module.md)

Rotary position embedding module: the angle tables built once at [`make`](./RotaryEmbedding.md#make), the rotation applied per call. The module form of [`ops::rotary_embedding`](../ops/rotary_embedding.md#rotary_embedding).

`forward(input, position_ids)` rotates the leading [`dim`](./RotaryEmbedding.md#dim) entries of every head vector of `input` (`[.., S, D]` with `D >= `[`dim`](./RotaryEmbedding.md#dim)) by the angle of the token's position: row `position_ids[.., s]` of the tables, or position `s` when `position_ids` is absent. The output keeps the input's shape and dtype. [`forward_qk`](./RotaryEmbedding.md#forward_qk) rotates a query / key pair with one angle gather.

```cpp
auto rope = ClikaRT::nn::RotaryEmbedding::make(/*dim=*/128, /*max_positions=*/4096);
auto [q_rot, k_rot] = rope->forward_qk(q, k, position_ids);
```

## Static member functions

### make() {#make}

<div class="api-signature">

`static` `std::shared_ptr<RotaryEmbedding>` **`make`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::int64_t` `dim`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::int64_t` `max_positions`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`RotaryEmbeddingOptions`](./RotaryEmbeddingOptions.md) `options` `=` `{}`<br />
)

</div>

Defaults: every option at its default ([`RotaryEmbeddingOptions`](./RotaryEmbeddingOptions.md)).

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 138

</div>

## Member functions

### \~RotaryEmbedding() {#RotaryEmbedding}

<div class="api-signature">

**`~RotaryEmbedding`**() `override`

</div>

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 143

</div>

### to\_impl(StreamOrDevice) {#to_impl}

<div class="api-signature">

`virtual` [`Result`](../Result.md)`<void>` **`to_impl`**([`StreamOrDevice`](../StreamOrDevice.md) `where`) `override`

</div>

Move the tables to a placement ([`Device`](../Device.md) / [`Stream`](../Stream.md)); the rotation is rebuilt there on the next [`forward`](./RotaryEmbedding.md#forward).

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 147

</div>

### to\_impl(DataType) {#to_impl-2}

<div class="api-signature">

`virtual` [`Result`](../Result.md)`<void>` **`to_impl`**([`DataType`](../../index.md#DataType) `dtype`) `override`

</div>

A dtype move leaves the tables at Float32 (the rotation's output follows its input's dtype), so a model-wide cast walks through this module without effect and returns success.

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 151

</div>

### forward() {#forward}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`forward`**([`Tensor`](../Tensor.md) `input`, [`OptionalTensor`](../../index.md#OptionalTensor) `position_ids` `=` `{}`) `const`

</div>

[`forward_impl`](./RotaryEmbedding.md#forward), unwrapped: raises [`ClikaRT::Error`](../Error.md) on failure.

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 161

</div>

### operator()() {#operator-call}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`operator()`**([`Tensor`](../Tensor.md) `input`, [`OptionalTensor`](../../index.md#OptionalTensor) `position_ids` `=` `{}`) `const`

</div>

Same as [`forward`](./RotaryEmbedding.md#forward).

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 165

</div>

### forward\_qk() {#forward_qk}

<div class="api-signature">

`std::array<`[`Tensor`](../Tensor.md) `2>` **`forward_qk`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `query`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `key`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`OptionalTensor`](../../index.md#OptionalTensor) `position_ids` `=` `{}`<br />
) `const`

</div>

[`forward_qk_impl`](./RotaryEmbedding.md#forward_qk), unwrapped: raises [`ClikaRT::Error`](../Error.md) on failure.

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 175

</div>

### cos() {#cos}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`cos`**() `const`

</div>

The cosine table, `[`[`max_positions`](./RotaryEmbedding.md#max_positions)`, `[`dim`](./RotaryEmbedding.md#dim)` / 2]` Float32 (the [`cos`](./RotaryEmbedding.md#cos) buffer).

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 181

</div>

### sin() {#sin}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`sin`**() `const`

</div>

The sine table, `[`[`max_positions`](./RotaryEmbedding.md#max_positions)`, `[`dim`](./RotaryEmbedding.md#dim)` / 2]` Float32 (the [`sin`](./RotaryEmbedding.md#sin) buffer).

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 183

</div>

### dim() {#dim}

<div class="api-signature">

`std::int64_t` **`dim`**() `const noexcept`

</div>

The rotated head prefix.

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 185

</div>

### max\_positions() {#max_positions}

<div class="api-signature">

`std::int64_t` **`max_positions`**() `const noexcept`

</div>

The number of positions the tables cover.

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 187

</div>

### options() {#options}

<div class="api-signature">

`const` [`RotaryEmbeddingOptions`](./RotaryEmbeddingOptions.md)`&` **`options`**() `const noexcept`

</div>

The settings this module was made with.

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 189

</div>

## Protected member functions

### initialize\_impl() {#initialize_impl}

<div class="api-signature">

`virtual` [`Result`](../Result.md)`<void>` **`initialize_impl`**() `override`

</div>

The pack hook: prepares the rotation (idempotent, thread-safe).

<div class="api-declared">

Declared in `ClikaRT/nn/rotary_embedding.h`, line 198

</div>
