ai.onnx.RotaryEmbedding

ai.onnx · standard ONNX operator · ONNX opset ≥ 23

Description

ONNX opset-23 rotary positional embeddings for float16 and float32. Rotates each head using cos_cache and sin_cache; rotary_embedding_dim can restrict rotation to a prefix. int64 position_ids are stored as uint32 after a range check, which is exact for every non-negative cache position. Positions outside the cache copy the token unchanged; bounds are checked before any addition or cache lookup. Other floating-point input types are unsupported.

See the ONNX RotaryEmbedding spec for the reference semantics.

Inputs

Name Upstream name Logical dtype WebGPU storage Rank Shape Description Presence
x X T same as logical dtype — — Input token embeddings. Shape is (batch_size, sequence_length, hidden_size) for rank 3 or (batch_size, num_heads, sequence_length, head_size) for rank 4. head_size must be even, and the num_heads attribute is required for rank-3 input. required
cos_cache — T same as logical dtype — — Precomputed cosine values. Without position_ids, shape is (batch_size, sequence_length, rotary_dim/2); with position_ids, shape is (max_sequence_length, rotary_dim/2). required
sin_cache — T same as logical dtype — — Precomputed sine values with the same shape and type as cos_cache. required
position_ids — M uint32 2 — Optional logical int64 per-token position indices of shape (batch_size, sequence_length). Valid positions are non-negative cache-row indices and use uint32 WebGPU storage. When supplied, the 2D cache tables are gathered at these positions. Out-of-cache positions copy the token unchanged. optional

Outputs

Name Upstream name Logical dtype Rank Shape Description Presence
y Y T same as x same as x Rotary-position-encoded tensor with the same shape and type as X. required

Attributes

Attributes and default values (overridable per request):

Attribute Default Description
interleaved 0 Set to 1 to rotate using an interleaved pattern (even/odd elements), or 0 to split the head dimension into two contiguous halves. Default is 0.
num_heads — Optional number of attention heads. ONNX requires this attribute when X is rank 3; it is unnecessary for rank-4 input because the head count is explicit in the shape.
rotary_embedding_dim 0 Number of head-dimension elements to rotate; 0 means rotate the full head dimension. When set, only the leading rotary_embedding_dim elements are rotated and the rest are passed through unchanged.

Type constraints

Variable Allowed dtypes
T float32, float16
M int64

Implementation variants

One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.

  • quad — Processes four pairs with vector loads and stores when head and cache alignment keep each group within a head and rotation region. Rank-3 and rank-4 inputs with 2D (gathered through position_ids) or 3D cache tables share it.
  • pair — Processes one rotated pair per invocation with scalar storage, or copies an unchanged pair from the tail.
  • output_vec4 — Writes four contiguous outputs per invocation with gathered pair values. Non-rotated components retain their original storage bits.

Files

Use with @huggingface/kernels

npm install --save-exact @huggingface/kernels@0.0.1-preview.4

Paste the example into a browser app that resolves npm imports. Serve the app over HTTPS or localhost with WebGPU available.

Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.

The version: 1 option selects the published kernel contract; it is independent of any operator opset, contrib since_version, or model version. It follows the v1 branch as fixes land. To pin exact artifact bytes, pass a 40-character commit revision instead of version.

import { getKernel } from "@huggingface/kernels";

const kernel = await getKernel("webgpu-kernels/ai.onnx.RotaryEmbedding", { version: 1 });

const xData = Float32Array.of(
  0.31634176, 0.27833304, 0.22933294, 0.19270967, 0.18712601, 0.22278252, 0.29923823,
  0.40519565,
);
const cos_cacheData = Float32Array.of(
  1, 0.994, 0.9553, 0.9171, 0.8253, 0.7584, 0.6216, 0.5319, 0.3624, 0.2579, 0.0707,
  -0.0392, -0.2272, -0.3327, -0.5048, -0.5966, -0.7374, -0.8071, -0.9041, -0.9455, -0.99,
  -0.9995, -0.9875, -0.9642, -0.8968, -0.8428, -0.7259, -0.646, -0.4903, -0.3916,
  -0.2108, -0.1022,
);
const sin_cacheData = Float32Array.of(
  0, 0.1692, 0.2955, 0.4529, 0.5646, 0.6961, 0.7833, 0.8772, 0.932, 0.9799, 0.9975,
  0.9951, 0.9738, 0.9214, 0.8632, 0.7654, 0.6755, 0.541, 0.4274, 0.2683, 0.1411, -0.0284,
  -0.1577, -0.3225, -0.4425, -0.5879, -0.6878, -0.8007, -0.8716, -0.942, -0.9775,
  -0.9991,
);
const position_idsData = Uint32Array.of(9);

const { y } = await kernel({
  x: { data: xData, shape: [1, 2, 1, 4] },
  cos_cache: { data: cos_cacheData, shape: [16, 2] },
  sin_cache: { data: sin_cacheData, shape: [16, 2] },
  position_ids: { data: position_idsData, shape: [1, 1] },
});
console.log({ y });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.