跳转至

RoPE Operators

本页暂无中文版。以下为英文原文。

Every op on this page is used the same way: construct it once, then call it. The constructor takes what the kernel is compiled with; the call takes the tensors. Both are documented under each op — __init__ and forward, where forward is what runs when you call op(...).

Base frequencies

One op serves both rotation conventions: pass is_neox_style=True to rotate the two halves of a head against each other, or is_neox_style=False to rotate each adjacent pair.

tileops.rope.RoPEFwdOp

RoPE with standard frequencies, either rotation convention, in place when inplace (vLLM RotaryEmbedding).

is_neox_style rotates the two halves of each head's rotary columns against each other; otherwise each adjacent pair rotates (GPT-J). inplace rewrites the rotary columns of query and key and leaves the rest as they were, as RotaryEmbedding.forward_cuda does. query and key may then be views of any strides: rows of contiguous columns any stride apart are rewritten where they lie, and any other layout is rotated on a contiguous copy that is copied back.

Positions in the cache are the caller's obligation, checked only with validate_inputs=True; unchecked, a position outside the cache reads its nearest row, row 0 or the last.

__init__

__init__(
    head_size,
    rotary_dim,
    max_position_embeddings,
    base,
    is_neox_style,
    dtype,
    init_cache=True,
    *,
    inplace=False,
    validate_inputs=False,
    target=None
)

Build the op and its cos/sin cache settings.

Parameters:

  • head_size (int) –

    Columns per head.

  • rotary_dim (int) –

    Leading columns of each head that rotate; even, at most head_size.

  • max_position_embeddings (int) –

    Rows of the cos/sin cache.

  • base (float) –

    Frequency base.

  • is_neox_style (bool) –

    Rotate halves (NeoX) rather than adjacent pairs (GPT-J).

  • dtype (dtype) –

    The cos/sin cache's dtype, before it takes the query's.

  • init_cache (bool, default: True ) –

    Must be True: the op computes its own cache.

  • inplace (bool, default: False ) –

    Rewrite the rotary columns of query and key and return them.

  • validate_inputs (bool, default: False ) –

    Check on the CPU that every position lies in the cache before the kernels run; this synchronizes the device and cannot run inside CUDA Graph capture.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    positions,
    query,
    key=None,
)

Rotate query and key at positions.

Parameters:

  • positions (Tensor) –

    Each token's position, [*S], int32 or int64, in [0, cache rows).

  • query (Tensor) –

    [*S, head_size * Hq], each token's heads flattened.

  • key (Tensor | None, default: None ) –

    [*S, head_size * Hk], or None.

Returns:

  • Tensor –

    (query_out, key_out), shaped as query and key; key_out is None

  • Tensor | None –

    where key is. In place they are query and key.

Scaled frequencies

tileops.rope.RoPELlama31FwdOp

Llama 3.1 RoPE with piecewise-scaled frequencies (vLLM Llama3RotaryEmbedding).

A frequency whose wavelength is below orig_max_position / high_freq_factor is kept, one above orig_max_position / low_freq_factor is divided by scaling_factor, and one between blends the two.

Positions in the cache are the caller's obligation, checked only with validate_inputs=True; unchecked, a position outside the cache reads its nearest row, row 0 or the last.

__init__

__init__(
    head_size,
    rotary_dim,
    max_position_embeddings,
    base,
    is_neox_style,
    dtype,
    scaling_factor,
    low_freq_factor,
    high_freq_factor,
    orig_max_position,
    *,
    validate_inputs=False,
    target=None
)

Build the op and its cos/sin cache settings.

Parameters:

  • head_size (int) –

    Columns per head.

  • rotary_dim (int) –

    Leading columns of each head that rotate; even, at most head_size.

  • max_position_embeddings (int) –

    Rows of the cos/sin cache.

  • base (float) –

    Frequency base.

  • is_neox_style (bool) –

    Rotate halves (NeoX) rather than adjacent pairs (GPT-J).

  • dtype (dtype) –

    The cos/sin cache's dtype, before it takes the query's.

  • scaling_factor (float) –

    Divisor of the low frequencies.

  • low_freq_factor (float) –

    Bounds the low-frequency band.

  • high_freq_factor (float) –

    Bounds the high-frequency band.

  • orig_max_position (int) –

    The context length the bands are measured against.

  • validate_inputs (bool, default: False ) –

    Check on the CPU that every position lies in the cache before the kernels run; this synchronizes the device and cannot run inside CUDA Graph capture.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    positions,
    query,
    key=None,
)

Rotate query and key at positions.

Parameters:

  • positions (Tensor) –

    Each token's position, [*S], int32 or int64, in [0, cache rows).

  • query (Tensor) –

    [*S, head_size * Hq], each token's heads flattened.

  • key (Tensor | None, default: None ) –

    [*S, head_size * Hk], or None.

Returns:

  • Tensor –

    (query_out, key_out), shaped as query and key; key_out is None

  • Tensor | None –

    where key is. In place they are query and key.

tileops.rope.YaRNFwdOp

YaRN RoPE (vLLM YaRNScalingRotaryEmbedding).

Frequencies blend interpolation by scaling_factor into extrapolation between the correction dimensions beta_fast and beta_slow name; cos and sin are scaled by the YaRN magnitude. The cache holds max_position_embeddings * scaling_factor rows, rounded up.

Positions in the cache are the caller's obligation, checked only with validate_inputs=True; unchecked, a position outside the cache reads its nearest row, row 0 or the last.

__init__

__init__(
    head_size,
    rotary_dim,
    max_position_embeddings,
    base,
    is_neox_style,
    scaling_factor,
    dtype,
    *,
    extrapolation_factor=1,
    attn_factor=1,
    beta_fast=32,
    beta_slow=1,
    apply_yarn_scaling=True,
    truncate=True,
    validate_inputs=False,
    target=None
)

Build the op and its cos/sin cache settings.

Parameters:

  • head_size (int) –

    Columns per head.

  • rotary_dim (int) –

    Leading columns of each head that rotate; even, at most head_size.

  • max_position_embeddings (int) –

    The original context length.

  • base (float) –

    Frequency base.

  • is_neox_style (bool) –

    Rotate halves (NeoX) rather than adjacent pairs (GPT-J).

  • scaling_factor (float) –

    Context extension factor.

  • dtype (dtype) –

    The cos/sin cache's dtype, before it takes the query's.

  • extrapolation_factor (float, default: 1 ) –

    Weight of the extrapolated frequencies.

  • attn_factor (float, default: 1 ) –

    Magnitude scale of cos and sin.

  • beta_fast (int, default: 32 ) –

    Rotations bounding the extrapolated band.

  • beta_slow (int, default: 1 ) –

    Rotations bounding the interpolated band.

  • apply_yarn_scaling (bool, default: True ) –

    Scale the magnitude by YaRN's 0.1 * ln(scale) + 1.

  • truncate (bool, default: True ) –

    Round the correction dimensions outward to integers.

  • validate_inputs (bool, default: False ) –

    Check on the CPU that every position lies in the cache before the kernels run; this synchronizes the device and cannot run inside CUDA Graph capture.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    positions,
    query,
    key=None,
)

Rotate query and key at positions.

Parameters:

  • positions (Tensor) –

    Each token's position, [*S], int32 or int64, in [0, cache rows).

  • query (Tensor) –

    [*S, head_size * Hq], each token's heads flattened.

  • key (Tensor | None, default: None ) –

    [*S, head_size * Hk], or None.

Returns:

  • Tensor –

    (query_out, key_out), shaped as query and key; key_out is None

  • Tensor | None –

    where key is. In place they are query and key.

tileops.rope.LongRoPEFwdOp

LongRoPE: vLLM Phi3LongRoPEScaledRotaryEmbedding once one set of factors is chosen.

Phi-3 chooses at construction its long factors and long magnitude when the serving context exceeds original_max_position_embeddings, else its short ones; the caller passes the chosen factors as rescale_factors (None for every factor 1) and the chosen magnitude as mscale (None for sqrt(1 + ln(scale) / ln(original)) with scale = max_position_embeddings / original_max_position_embeddings, 1 where scale <= 1). Token n reads cache row positions[n] + offsets[n], which lies in [0, max_position_embeddings), and in [0, original_max_position_embeddings) with the short factors. Both bounds are the caller's obligation; validate_inputs=True checks positions alone against the cache, not the sum. Unchecked, a row outside the cache reads its nearest row, row 0 or the last. The rotation is NeoX, and the cache dtype is the query's.

__init__

__init__(
    head_size,
    rotary_dim,
    max_position_embeddings,
    original_max_position_embeddings,
    base,
    is_neox_style,
    dtype,
    rescale_factors,
    mscale=None,
    *,
    validate_inputs=False,
    target=None
)

Build the op and its cos/sin cache settings.

Parameters:

  • head_size (int) –

    Columns per head.

  • rotary_dim (int) –

    Leading columns of each head that rotate; even, at most head_size.

  • max_position_embeddings (int) –

    Rows of the cos/sin cache.

  • original_max_position_embeddings (int) –

    The original context length.

  • base (float) –

    Frequency base.

  • is_neox_style (bool) –

    Must be True.

  • dtype (dtype) –

    The cos/sin cache's dtype, the query's.

  • rescale_factors (Sequence[float] | None) –

    rotary_dim // 2 divisors of the frequencies, or None for 1.

  • mscale (float | None, default: None ) –

    Magnitude scale of cos and sin, or None for the default.

  • validate_inputs (bool, default: False ) –

    Check on the CPU that every position lies in the cache before the kernels run; this synchronizes the device and cannot run inside CUDA Graph capture.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A rescale factor is zero or NaN; the frequency divides by it.

forward

forward(
    positions,
    query,
    key=None,
    offsets=None,
)

Rotate query and key at positions + offsets.

Parameters:

  • positions (Tensor) –

    Each token's position, [N], int32 or int64.

  • query (Tensor) –

    [N, head_size * Hq], each token's heads flattened.

  • key (Tensor | None, default: None ) –

    [N, head_size * Hk]; required, as Phi-3 requires it.

  • offsets (Tensor | None, default: None ) –

    [N] added to positions, or None.

Returns:

  • tuple[Tensor, Tensor | None] –

    (query_out, key_out), shaped as query and key.

Caller-given cos and sin

The op reads the rotation from cos and sin rows the caller computes, so any frequency scheme the caller builds applies.

tileops.rope.RoPECosSinFwdOp

Rotary embedding by caller-given cos and sin rows, in place when inplace.

Follows flash_attn.layers.rotary.apply_rotary_emb. Token s of sequence b reads row s + seqlen_offsets of cos and sin [P, Rh] (one offset for every sequence, or one per sequence) and rotates the first 2 * Rh columns of each head; the rest are copied, or left as they are in place. interleaved pairs (x[2i], x[2i + 1]), otherwise (x[i], x[i + Rh]) (NeoX). The rotation is computed in float32 and rounded once to x's dtype; cos and sin may each have their own dtype.

x is dense [B, S, H, D], or packed [Tt, H, D] with cu_seqlens, s counting from each sequence's start. x may be a strided view, such as the trailing columns of a wider head, which inplace rotates without a copy. Passing -sin applies the inverse rotation.

A row inside the tables is the caller's obligation, checked for tensor offsets only with validate_inputs=True. Unchecked, a row outside the tables reads its nearest row, row 0 or the last. max_seqlen bounds the packed sequences for the caller; the rotation does not read it.

__init__

__init__(
    interleaved=False,
    inplace=False,
    max_seqlen=None,
    *,
    validate_inputs=False,
    target=None
)

Build the op.

Parameters:

  • interleaved (bool, default: False ) –

    Rotate adjacent pairs rather than the two halves (NeoX).

  • inplace (bool, default: False ) –

    Write the rotation into x and return x.

  • max_seqlen (int | None, default: None ) –

    The longest packed sequence; required with cu_seqlens.

  • validate_inputs (bool, default: False ) –

    Check on the CPU that cu_seqlens starts at 0, never decreases, ends at the token count and holds no sequence longer than max_seqlen, and that each tensor of seqlen_offsets lies in [0, P - L], before the kernel runs; this synchronizes the device and cannot run inside CUDA Graph capture.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    cos,
    sin,
    cu_seqlens=None,
    seqlen_offsets=0,
)

Rotate the leading 2 * Rh columns of each head of x.

Parameters:

  • x (Tensor) –

    [B, S, H, D], or [Tt, H, D] with cu_seqlens; any strides.

  • cos (Tensor) –

    [P, Rh].

  • sin (Tensor) –

    [P, Rh].

  • cu_seqlens (Tensor | None, default: None ) –

    [B + 1] sequence starts of packed x, int32 or int64, or None for dense x.

  • seqlen_offsets (int | Tensor, default: 0 ) –

    The table row of each sequence's first token: an int, or [B] int32 or int64.

Returns:

  • Tensor –

    x when inplace, else a new tensor of its shape and dtype.