Implementation:Microsoft Onnxruntime CPU GRU IOUtils
| Knowledge Sources | |
|---|---|
| Domains | Training, CPU_Kernels |
| Last Updated | 2026-02-10 04:00 GMT |
Overview
Concrete tool for managing GRU input/output tensor construction, validation, and attribute parsing on CPU in the ONNX Runtime training framework.
Description
This file provides I/O utility structures and validation logic for the GRU training and gradient kernels. It implements GRUAttributes (parsing kernel attributes like direction, activations, clip, linear_before_reset, hidden_size), GRUInputs (loading and validating forward-pass inputs including splitting recurrence weights into zr and h portions), GRUOutputs (allocating output tensors for the forward pass), GRUGradInputs (loading gradient-pass inputs including all hidden states and zrh gates), and GRUGradOutputs (allocating gradient output tensors for dX, dW, dR, dB, dH0). Validation functions enforce shape constraints (X must be 3D, W must be [directions, 3*H, I], R must be [directions, 3*H, H]). Only forward direction is currently supported and only sigmoid/tanh activations are allowed.
Usage
These utilities are used by both the GRUTraining (forward) and GRUGrad (backward) operators to prepare input spans and allocate output tensors before invoking the actual compute kernels.
Code Reference
Source Location
- Repository: Microsoft_Onnxruntime
- File: orttraining/orttraining/training_ops/cpu/rnn/gru_io_utils.cc
- Lines: 1-297
Signature
struct GRUAttributes {
GRUAttributes(const OpKernelInfo& info);
};
template <typename T>
struct GRUInputs {
GRUInputs(OpKernelContext* context, const int directions, const int hidden_size);
};
template <typename T>
struct GRUOutputs {
GRUOutputs(OpKernelContext* context, const int directions, const int sequence_length,
const int batch_size, const int hidden_size);
};
template <typename T>
struct GRUGradInputs {
GRUGradInputs(OpKernelContext* context, const int directions, const int hidden_size);
};
template <typename T>
struct GRUGradOutputs {
GRUGradOutputs(OpKernelContext* context, const int directions, const int sequence_length,
const int batch_size, const int hidden_size, const int input_size);
};
Import
#include "orttraining/orttraining/training_ops/cpu/rnn/gru_io_utils.h"
I/O Contract
Inputs (GRUInputs - Forward)
| 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] |
| SL | Tensor(int) | No | Sequence lengths (not supported) |
| H0 | Tensor(float) | No | Initial hidden state [directions, batch, H] |
Outputs (GRUGradOutputs)
| Name | Type | Description |
|---|---|---|
| dX | Tensor(float) | Gradient w.r.t. input [seq_length, batch, input_size] |
| dW | Tensor(float) | Gradient w.r.t. weights [directions, 3*H, input_size] |
| dR | Tensor(float) | Gradient w.r.t. recurrence weights [directions, 3*H, H] |
| dB | Tensor(float) | Gradient w.r.t. bias [directions, 6*H] |
| dH0 | Tensor(float) | Gradient w.r.t. initial hidden state |
Usage Examples
// Using GRUInputs in the forward pass
const auto gru_inputs = gru::GRUInputs<float>(context, num_directions, hidden_size);
auto gru_outputs = gru::GRUOutputs<float>(context, num_directions,
gru_inputs.shape.sequence_length, gru_inputs.shape.batch_size, hidden_size);