> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/automodel/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/automodel/_mcp/server.

# nemo_automodel.components.models.gpt_oss.rope_utils

## Module Contents

### Classes

| Name                                                                                      | Description |
| ----------------------------------------------------------------------------------------- | ----------- |
| [`RotaryEmbedding`](#nemo_automodel-components-models-gpt_oss-rope_utils-RotaryEmbedding) | -           |

### Functions

| Name                                                                                                          | Description                                       |
| ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- |
| [`apply_rotary_emb`](#nemo_automodel-components-models-gpt_oss-rope_utils-apply_rotary_emb)                   | Apply rotary embeddings to input tensor.          |
| [`apply_rotary_emb_qk`](#nemo_automodel-components-models-gpt_oss-rope_utils-apply_rotary_emb_qk)             | Apply rotary embeddings to query and key tensors. |
| [`position_ids_to_freqs_cis`](#nemo_automodel-components-models-gpt_oss-rope_utils-position_ids_to_freqs_cis) | -                                                 |

### API

```python
class nemo_automodel.components.models.gpt_oss.rope_utils.RotaryEmbedding(
    head_dim: int,
    base: int,
    dtype: torch.dtype,
    initial_context_length: int = 4096,
    scaling_factor: float = 1.0,
    ntk_alpha: float = 1.0,
    ntk_beta: float = 32.0,
    partial_rotary_factor: float = 1.0,
    device: torch.device | None = None
)
```

**Bases:** `Module`

```python
nemo_automodel.components.models.gpt_oss.rope_utils.RotaryEmbedding._compute_concentration_and_inv_freq() -> torch.Tensor
```

See YaRN paper: [https://arxiv.org/abs/2309.00071](https://arxiv.org/abs/2309.00071)

Uses rotary\_dim instead of head\_dim to support partial rotary embeddings.

```python
nemo_automodel.components.models.gpt_oss.rope_utils.RotaryEmbedding._compute_cos_sin(
    num_tokens: int
)
```

```python
nemo_automodel.components.models.gpt_oss.rope_utils.RotaryEmbedding.forward(
    query: torch.Tensor,
    key: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]
```

```python
nemo_automodel.components.models.gpt_oss.rope_utils.apply_rotary_emb(
    x: torch.Tensor,
    cos: torch.Tensor,
    sin: torch.Tensor
) -> torch.Tensor
```

Apply rotary embeddings to input tensor.

If cos/sin have fewer dimensions than x (due to partial\_rotary\_factor \< 1.0),
only the first rotary\_dim dimensions of x are rotated, and the rest are passed through.

**Parameters:**

Input tensor (..., head\_dim)

Cosine tensor (..., rotary\_dim // 2)

Sine tensor (..., rotary\_dim // 2)

```python
nemo_automodel.components.models.gpt_oss.rope_utils.apply_rotary_emb_qk(
    q: torch.Tensor,
    k: torch.Tensor,
    freqs_cis: torch.Tensor,
    format: str = 'bshd',
    rope_fusion: bool = True,
    cu_seqlens: torch.Tensor | None = None,
    concentration: float | None = None,
    cp_size: int = 1,
    cp_rank: int = 0
) -> tuple[torch.Tensor, torch.Tensor]
```

Apply rotary embeddings to query and key tensors.

**Parameters:**

Query tensor.

Key tensor.

Frequency tensor. Format depends on rope\_fusion:

* If rope\_fusion=True: \[angles, angles] for TE fused rope
* If rope\_fusion=False: \[cos, sin] with concentration applied

QKV format ("bshd" or "thd").

If True, use TE fused rope. If False, use non-fused rope.

Cumulative sequence lengths for variable-length sequences.

Context parallelism size.

Context parallelism rank.

**Returns:** `tuple[torch.Tensor, torch.Tensor]`

Tuple of (q, k) with rotary embeddings applied.

```python
nemo_automodel.components.models.gpt_oss.rope_utils.position_ids_to_freqs_cis(
    rotary_emb: nemo_automodel.components.models.gpt_oss.rope_utils.RotaryEmbedding,
    position_ids: torch.Tensor,
    qkv_format: str = 'bshd',
    for_fused_rope: bool = True,
    cp_size: int = 1
) -> torch.Tensor
```