Implementation:Microsoft Onnxruntime CPU GRU Forward
| Knowledge Sources | |
|---|---|
| Domains | Training, CPU_Kernels |
| Last Updated | 2026-02-10 04:00 GMT |
Overview
Concrete tool for running the GRU (Gated Recurrent Unit) forward pass during training on CPU in the ONNX Runtime training framework.
Description
This file implements the GRUTraining kernel, which wraps the existing UniDirectionalGru forward implementation with training mode enabled. It parses GRU inputs and attributes via the gru::GRUInputs and gru::GRUOutputs utilities, then invokes the deep CPU GRU computation. The kernel outputs all hidden states, the final hidden state, and the z/r/h gate activations needed for gradient computation in the backward pass. It is registered under the kMSDomain with opset version 1.
Usage
This kernel is invoked during the forward pass of GRU training. The computed gate activations (zrh) and all hidden states are stored as outputs and later consumed by the GRUGrad kernel during backpropagation.
Code Reference
Source Location
- Repository: Microsoft_Onnxruntime
- File: orttraining/orttraining/training_ops/cpu/rnn/gru.cc
- Lines: 1-58
Signature
template <typename T>
Status GRUTraining<T>::Compute(OpKernelContext* context) const;
Import
#include "orttraining/orttraining/training_ops/cpu/rnn/gru.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| X | Tensor(float) | Yes | Input sequence [seq_length, batch_size, input_size] |
| W | Tensor(float) | Yes | Weights [directions, 3*H, input_size] |
| R | Tensor(float) | Yes | Recurrence weights [directions, 3*H, H] |
| B | Tensor(float) | No | Bias [directions, 6*H] |
| sequence_lengths | Tensor(int) | No | Per-sequence lengths (not supported) |
| initial_h | Tensor(float) | No | Initial hidden state [directions, batch, H] |
Outputs
| Name | Type | Description |
|---|---|---|
| HAll | Tensor(float) | All hidden states [seq_length, directions, batch, H] |
| Ht | Tensor(float) | Final hidden state [directions, batch, H] |
| ZRH | Tensor(float) | Gate activations [seq_length, directions, batch, 3*H] |
Usage Examples
// Kernel registration for GRUTraining
ONNX_OPERATOR_TYPED_KERNEL_EX(
GRUTraining, kMSDomain, 1, float, kCpuExecutionProvider,
(*KernelDefBuilder::Create())
.TypeConstraint("T", DataTypeImpl::GetTensorType<float>()),
GRUTraining<float>);