ai.onnx.GRU
ai.onnx · standard ONNX operator · ONNX opset ≥ 22
Description
Computes a single-layer Gated Recurrent Unit (GRU) over an input sequence, applying update (z), reset (r), and hidden (h) gates at each time step. Supports forward, reverse, and bidirectional traversal; activation functions default to Sigmoid for f and Tanh for g.
See the ONNX GRU spec for the reference semantics.
Inputs
| Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
X |
x |
T |
3 |
— | Input sequences with shape [seq_length, batch_size, input_size] when layout=0, or [batch_size, seq_length, input_size] when layout=1. |
required |
W |
w |
T |
3 |
— | Weight tensor for the gates, shape [num_directions, 3*hidden_size, input_size], concatenating W[zrh] (and WB[zrh] if bidirectional). |
required |
R |
r |
T |
3 |
— | Recurrence weight tensor, shape [num_directions, 3*hidden_size, hidden_size], concatenating R[zrh] (and RB[zrh] if bidirectional). |
required |
B |
b |
T |
2 |
— | Bias tensor for the gates, shape [num_directions, 6*hidden_size], concatenating input and recurrence biases. ONNX defines an omitted bias as zero, but this kernel ABI requires an explicit tensor; callers representing omission must bind a zero-filled tensor. |
required |
sequence_lens |
sequence_lens |
int32 |
1 |
— | Per-batch sequence lengths of shape [batch_size]; assumed all equal to seq_length if absent. |
optional |
initial_h |
initial_h |
T |
3 |
— | Initial hidden state with shape [num_directions, batch_size, hidden_size] when layout=0, or [batch_size, num_directions, hidden_size] when layout=1; assumed zero if absent. |
optional |
Outputs
| Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
Y |
y |
T |
4 |
derived; see description | Intermediate hidden outputs for all time steps, with shape [seq_length, num_directions, batch_size, hidden_size] when layout=0, or [batch_size, seq_length, num_directions, hidden_size] when layout=1. |
required |
Y_h |
y_h |
T |
3 |
derived; see description | Final hidden state with shape [num_directions, batch_size, hidden_size] when layout=0, or [batch_size, num_directions, hidden_size] when layout=1. |
required |
Attributes
Attributes and default values (overridable per request):
| Attribute | Default | Description |
|---|---|---|
layout |
0 |
Controls the shape format of X, initial_h, Y, and Y_h: 0 uses [seq_length, batch_size, ...] ordering (default), 1 uses [batch_size, seq_length, ...]. |
direction |
"forward" |
Specifies traversal direction: "forward" (default), "reverse", or "bidirectional". |
linear_before_reset |
0 |
When non-zero, applies the recurrence linear transform to the hidden state before multiplying by the reset gate output. |
hidden_size |
— | Optional number of neurons in the hidden layer; when omitted, it is inferred from the W and R tensor shapes. |
clip |
— | Optional non-negative threshold applied to activation inputs as [-clip, +clip]; omission disables clipping, while an explicit 0 clamps them to zero. |
activation_alpha |
— | Optional alpha parameters for activation functions that use alpha, consumed in activation-list order; omitted entries use the ONNX defaults for their activation. |
activation_beta |
— | Optional beta parameters for activation functions that use beta, consumed in activation-list order; omitted entries use the ONNX defaults for their activation. |
activations |
— | Activation functions for the update/reset gates and candidate state. Defaults to ["Sigmoid", "Tanh"] per direction. |
Type constraints
| Variable | Allowed dtypes |
|---|---|
T |
float32 |
Files
metadata.json— kernel metadata (id, digests, provenance)manifest.json— the op contract (source of truth)test.json— correctness casesbench.json— benchmark + tuning casesdatamove-transpose-2d-tiled.wgsl.jinjadense-tiled-matmul.wgsl.jinjagru-general.wgsl.jinjagru-parallel-global.wgsl.jinjagru-recur-candidate-step.wgsl.jinjagru-recur-candidate-subgroup-step.wgsl.jinjagru-recur-gates-step.wgsl.jinjagru-recur-gates-subgroup-step.wgsl.jinjagru-recur-init-step.wgsl.jinjagru-timestep-body.wgsl.jinjarnn-input-proj.wgsl.jinjarnn-sequence-mask.wgsl.jinja
Use with @huggingface/kernels
The loader derives every required output's shape and logical dtype from the manifest contract and this call. It then allocates the result tensors automatically.
The version: 1 option selects the published kernel contract; it is independent of any operator opset, contrib since_version, or model version.
Replace each *Data placeholder with a typed array containing the corresponding input data.
import { getKernel } from "@huggingface/kernels";
const kernel = await getKernel("webgpu-kernels/ai.onnx.GRU", { version: 1 });
const { y, y_h } = await kernel({
x: { data: xData, shape: [1, 1, 1] },
w: { data: wData, shape: [1, 3, 1] },
r: { data: rData, shape: [1, 3, 1] },
b: { data: bData, shape: [1, 6] },
});
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.