com.microsoft.RotaryEmbedding

com.microsoft · ONNX Runtime contrib operator · contrib since_version 1

Description

Rotary positional embeddings for rank-3 BSH or rank-4 BNSH input. position_ids supplies a base offset or per-token rows. rotary_embedding_dim rotates a prefix and copies the tail. Arithmetic is float32 with one narrowing store. Positions outside the cache copy the token unchanged; bounds are checked before adding positions or indexing the cache. The int64 positions are stored as uint32 after a range check. Bfloat16 and non-default scale are unsupported.

See the ONNX Runtime RotaryEmbedding contrib-operator spec for the reference semantics.

Differences from the ONNX Runtime spec

  • Attribute is_packed_batching: Accepted and ignored: this package does not impose the sequence_length <= max_sequence_length bound the flag lifts, so neither setting changes the result.

Inputs

Name Upstream name Logical dtype WebGPU storage Rank Shape Description Presence
x input T same as logical dtype — — Input token embeddings, shaped (batch_size, sequence_length, hidden_size) at rank 3 or (batch_size, num_heads, sequence_length, head_size) at rank 4. At rank 3 the head size comes from num_heads when that attribute is positive and from 2 * cos_cache.shape[1] otherwise; at rank 4 both the head count and head size are read from the shape. required
positionIds position_ids M uint32 — — Logical int64 cache-row selector in either upstream format: a scalar or one-element vector holding a base offset, so token s reads row position_ids[0] + s; or a (batch_size, sequence_length) table read per token. Valid positions are non-negative rows of the caches and use uint32 WebGPU storage, so a negative value is rejected at the host boundary. Out-of-cache positions copy the token unchanged. required
cos cos_cache T same as logical dtype 2 — Precomputed cosine values of shape (max_sequence_length, rotary_dim / 2), where rotary_dim is rotary_embedding_dim when that is positive and the head size otherwise. required
sin sin_cache T same as logical dtype 2 — Precomputed sine values with the same shape and type as cos_cache. required

Outputs

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

Attributes

Default values (overridable per request):

Attribute Default Description
interleaved 0 Set to 1 to pair adjacent even/odd elements, or 0 to pair each element of the first half of the rotary window with the matching element of the second half. Default is 0.
is_packed_batching 0 Ragged (packed) batch inputs. Its only upstream effect is to lift the sequence_length <= max_sequence_length bound, and this implementation never imposes that bound: every gathered row is required to index the caches whatever the sequence length. Default is 0.
num_heads 0 Number of attention heads. Default is 0, which asks the rank-3 path to take the head size from the cache width instead; a positive value is required whenever rotary_embedding_dim is nonzero. At rank 4 the head count comes from the input shape and this attribute is not consulted.
rotary_embedding_dim 0 Positive even count of leading head-dimension elements to rotate; the remaining tail is copied unchanged. Default is 0, meaning the whole head dimension, which must then be even. An odd value is rejected.
scale 1 Declared scale for the gathered rotation. No ONNX Runtime provider applies it, so the default and only accepted value is 1.0 and a caller's other value is rejected rather than silently discarded.

Type constraints

Variable Allowed dtypes
T float32, float16
M int64

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/com.microsoft.RotaryEmbedding", { version: 1 });

const xData = Float32Array.of(
  -1.0408, 0.9166, -1.3042, -1.1097, -1.2188, 1.1676, 1.0076, -0.7529, -0.225, -0.4327,
  -1.5071, -0.4586, -0.8663, -0.2656, 0.1665, 0.7911, -0.932, -0.8579, -1.0574, -0.1188,
  -0.9078, 0.3452, -0.5713, -0.2351, -0.848, 0.5266, -1.2944, -0.0243, -0.2354, -0.7087,
  -0.9647, -0.0991, -0.2994, -0.065, -1.572, -1.3211,
);
const positionIdsData = Uint32Array.of(0, 1);
const cosData = Float32Array.of(1, 1, 1, 0.5403, 0.9989, 1, -0.4161, 0.9957, 1, -0.99, 0.9903, 1);
const sinData = Float32Array.of(
  0, 0, 0, 0.8415, 0.0464, 0.0022, 0.9093, 0.0927, 0.0043, 0.1411, 0.1388, 0.0065,
);

const { y } = await kernel({
  x: { data: xData, shape: [1, 2, 18] },
  positionIds: { data: positionIdsData, shape: [1, 2] },
  cos: { data: cosData, shape: [4, 3] },
  sin: { data: sinData, shape: [4, 3] },
});
console.log({ y });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.