9.3 GRU (torch gate order)
The LSTM solved vanishing gradients with two states and four gates. Cho et al. found in 2014 that one state and two gates do about as well: merge the cell and hidden state, and let a single update gate decide how much of the old state to keep, with the complement going to the new candidate. The result is the gated recurrent unit (GRU). The seq2seq models of Part 4, starting with Encoder-decoder with teacher forcing, use the GRU as their default encoder and decoder cell because it is a quarter smaller and a little faster. Two GRUs also make a bidirectional layer, and the RNN language model trains it as cell='gru', one row of the recurrent comparison.
Concepts from other sections (10)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- vanishing gradient: the shrinking of gradients through a long product of Jacobians, which prevents learning long-range dependencies. Read more: Chapter 3: Neural Architectures and Their History
- PCG32: an LCG whose output is a permutation (xorshift, then a state-chosen rotation) of the old state. Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python in Mathematics
- Dropout: randomly zeroing activations during training to approximate an ensemble of subnetworks. Read more: Chapter 2: Deep Learning
- sigmoid: , mapping the real line into , with . Read more: 7.3 Activation Functions and Their Derivatives in Mathematics
- Padding mask: a per-step boolean that freezes a finished sequence’s state and zeros its output. Read more: 9.2 LSTM (torch gate order)
- safetensors: a checkpoint format of an 8-byte header length, a JSON header, and raw little-endian row-major tensor bytes. Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- Gradcheck: a comparison of a hand-written gradient against central differences of . Read more: 3.3 Oracles, golden and differential tests, gradcheck as a test in Software Engineering
- central differences: the numerical derivative , used as an oracle for analytic gradients. Read more: 6.3 The op library and gradcheck_all
- registration order: the order layers are created in a
Module, which fixes both the state-dict keys and the order of random initialization draws. Read more: 8.2 Bengio’s neural probabilistic language model - Bidirectional layer: two recurrent modules, one reading left to right and one right to left, with outputs concatenated per position. Read more: 9.4 Bidirectional RNN with length-aware reversal
The GRU also brings the book’s most common weight-loading bug: the reset gate’s position. Papers write it one way, cuDNN and torch compute it another, and a checkpoint only works with the form it was trained in. In this section you build torch’s form and check it against torch.
Everything else reuses the LSTM’s machinery, so read that section first. The GRU uses the same Part 0 pieces (Tensor, the op library, and Module and Dropout from the module system) and the same initializers from Mathematics: xavier_uniform from Variance propagation and initialization, orthogonal_init from Orthogonality, Householder QR, and orthogonal initialization, and the PCG32 stream from Modular arithmetic, hashing, and PCG32. The solution is gru.py: gru_cell, GRUCell, and GRU (stacked layers, inter-layer dropout, lengths for padded batches), with _init and _mask. Its contract is gru.pyi and its model tests are test_gru.py.
Learning objectives
- Write the GRU cell in torch’s gate order , with the reset gate applied after the recurrent matmul and its bias.
- Tell torch’s (cuDNN’s) reset placement apart from Cho’s, and construct a test that distinguishes them.
- Explain why an update gate near 1 passes the state and its gradient through a step unchanged.
- Compare the GRU and the LSTM in parameters, compute, and behaviour.
The cell and its gates
Section titled “The cell and its gates”The notation:
| Symbol | Meaning | Type / shape |
|---|---|---|
| , | input and state | float32[B, D], float32[B, H] |
| , | input and recurrent weights, three gate blocks stacked | [3H, D], [3H, H] |
| , | the two biases | [3H] each |
| the input part of all three pre-activations | [B, 3H] | |
| the recurrent part, bias included | [B, 3H] | |
| reset gate, update gate, candidate | [B, H] each | |
| , , | sigmoid, tanh, elementwise product | |
| length of sequence in a padded batch | integer in |
Split and into blocks of columns, in torch’s order :
The reset gate decides how much of the old state the candidate may read: makes a function of the input alone, a reset. The update gate decides how much of the old state survives: is a convex combination of and , so it stays in without an extra tanh.
Where the reset gate goes
Section titled “Where the reset gate goes”Cho’s paper computes : reset first, then the matmul. cuDNN computes : matmul first, so the recurrent matmul for all three gates is one GEMM before any gate is known, then the reset, which also scales the recurrent bias . torch follows cuDNN. The two forms are different functions with the same parameter shapes, so nothing fails at load time; the outputs are just wrong. The worked example below shows the gap: 0.635 against 0.848.
Why the gradient does not vanish
Section titled “Why the gradient does not vanish”. With the first term is the identity and the others vanish ( and ), so the state and its gradient pass through a step unchanged. This is the GRU’s version of the LSTM’s forget gate. Saturating with a large bias gives to within rounding over 15 steps.
Layout, initialization, stacking, and padding
Section titled “Layout, initialization, stacking, and padding”Everything else follows the LSTM with three blocks instead of four. The parameters use torch’s names weight_ih_l{k}, weight_hh_l{k}, bias_ih_l{k}, bias_hh_l{k} in that order. Each layer draws three Xavier input blocks, then three orthogonal recurrent blocks, and both biases are 0: no gate here plays the forget gate’s role at initialization, since starts at . Dropout sits between layers in training mode only, and the lengths mask freezes a finished sequence’s state and zeroes its outputs.
Working it by hand
Section titled “Working it by hand”Take one unit, , . The weights are , , (only the candidate’s recurrent part reads ), .
- ; .
- ; .
- .
- .
- (the gates do not read here, so no other terms).
Cho’s form would compute ; swapping the interpolation would give .
The model tests in test_gru.py start from this example (gru_cell with , , , and must give ). The case that tells the two reset placements apart is the cell against a numpy formula with random weights and . The others check the state_dict keys, orthogonal blocks, a padded batch, a 2-layer GRU against stacked numpy cells from a nonzero , dropout ignored by a 1-layer GRU, and bad arguments.
Designing the interface
Section titled “Designing the interface”The interface mirrors the LSTM’s, with one state instead of two:
def gru_cell(x, h, w_ih, w_hh, b_ih, b_hh) -> Tensor # h'class GRUCell(Module): # weight_ih [3H, D], weight_hh [3H, H], bias_ih, bias_hh [3H] def __init__(self, d_in, d_h, rng=None); def forward(self, x, h=None)class GRU(Module): # weight_ih_l{k}, weight_hh_l{k}, bias_ih_l{k}, bias_hh_l{k} def __init__(self, d_in, d_h, num_layers=1, dropout=0.0, rng=None) def forward(self, x, state=None, lengths=None) -> tuple[Tensor, Tensor] # x [T, B, d_in] -> out [T, B, H], h_n [num_layers, B, H]Building it step by step
Section titled “Building it step by step”The design follows from five observations.
- One state, an interpolating update. Merge the LSTM’s cell and hidden state, and let one gate split the mass: . With the step is the identity, so the state and its gradient pass unchanged; this replaces the forget gate. Because is a convex combination of values in , no output tanh is needed.
- A reset gate for the candidate. The candidate should be able to ignore the old state (a reset). So scales the recurrent contribution inside ‘s tanh.
- Where multiplies is a choice of function. Placing after the recurrent matmul (and its bias) lets the recurrent GEMM for all three gates run before any gate is known, which is why cuDNN and torch use it. Cho’s original places before the matmul. Same shapes, different functions: the solution must use torch’s form to load torch weights.
- Everything else is the LSTM’s machinery. Three blocks in torch’s order with two biases, Xavier input and orthogonal recurrent blocks, layers registered with torch’s names, dropout between layers, and the same padding mask. Autograd derives the backward.
- No special bias at initialization. No gate plays the forget gate’s role at step 0: already keeps half the state, so both biases start at 0.
Parameters
Section titled “Parameters”Three Xavier input blocks, then three orthogonal recurrent blocks, from one generator; zero biases.
def _init(d_in, d_h, rng): r = rng if rng is not None else PCG32(0).substream("init") w_ih = np.concatenate([xavier_uniform((d_h, d_in), 1.0, r) for _ in range(GATES)]) # [3H, D] w_hh = np.concatenate([orthogonal_init((d_h, d_h), 1.0, r) for _ in range(GATES)]) # [3H, H] return w_ih, w_hh, np.zeros(GATES * d_h), np.zeros(GATES * d_h)The cell
Section titled “The cell”The input part a and the recurrent part b are each computed once for all three gates, biases included; the gates add the matching slices, and the candidate scales the recurrent slice by after its bias.
def gru_cell(x, h, w_ih, w_hh, b_ih, b_hh): a = F.matmul(x, F.transpose(w_ih, 0, 1)) + b_ih # [B, 3H]: x W_ih^T + b_ih b = F.matmul(h, F.transpose(w_hh, 0, 1)) + b_hh # [B, 3H]: h W_hh^T + b_hh, one GEMM H = a.shape[-1] // GATES r = F.sigmoid(a[..., 0:H] + b[..., 0:H]) # reset gate z = F.sigmoid(a[..., H : 2 * H] + b[..., H : 2 * H]) # update gate # torch's (and cuDNN's) form: r scales h @ w_hn^T + b_hn, bias included. n = F.tanh(a[..., 2 * H : 3 * H] + r * b[..., 2 * H : 3 * H]) return (1.0 - z) * n + z * h # z -> 1 keeps hOn the hand example, a is and b is , so , , , and . In Cho’s form the candidate would read , because the bias would not be scaled by .
The last line uses h itself, not h.data: the term is the state’s own path back through time, and cutting it from the graph would leave a correct forward with a weaker gradient.
The modules
Section titled “The modules”GRUCell registers weight_ih, weight_hh, bias_ih, bias_hh; with no h it starts from zeros. GRU registers weight_ih_l{k}, weight_hh_l{k}, bias_ih_l{k}, bias_hh_l{k} per layer, drawing every layer from one generator, exactly as the LSTM does. Its forward has one state instead of two:
h0 = _as_tensor(np.zeros((L, B, H), dtype=x.dtype) if state is None else state, x.dtype)mask = _mask(lengths, T, B) # bool [T, B, 1], true at real stepsseq = xh_n = []for k in range(L): w = [getattr(self, f"{n}_l{k}") for n in ("weight_ih", "weight_hh", "bias_ih", "bias_hh")] h = h0[k] # this layer's own initial state outs = [] for t in range(T): h_new = gru_cell(seq[t], h, *w) if mask is None: h = h_new outs.append(h_new) else: m = mask[t] h = F.where(m, h_new, h) # past its length: keep the state outs.append(F.where(m, h_new, 0.0)) # and output 0 seq = F.stack(outs, axis=0) if k < L - 1: seq = self.drop(seq) # between layers only h_n.append(h)return seq, F.stack(h_n, axis=0) # h_n [num_layers, B, H]The full solution is gru.py.
Why this design
Section titled “Why this design”Per step and layer the GRU does two matmuls of cost : three quarters of an LSTM of the same . Parameters per layer are ; memory under autograd is .
torch’s reset placement is the form torch and cuDNN compute, so it is the only form in which torch checkpoints are correct. It is also the faster form: the recurrent GEMM b does not depend on , so it is computed once for all three gates. That is also why a and b stay separate instead of being summed into one pre-activation as in the LSTM. The LSTM adds input and recurrent parts for all four gates, so one sum suffices; the GRU’s candidate needs the recurrent part on its own (to scale it by ), so the two halves must stay separate until the gates are formed.
There is no forget-style bias because the interpolation already keeps half of at ; there is no separate memory that would be wiped at the start. Zero biases match the contract and torch-style training.
GRU or LSTM? They give similar quality on most sequence tasks (Chung et al. 2014); the GRU has fewer parameters and one state to carry, while the LSTM’s separate cell can help on very long dependencies. The seq2seq models take the GRU for its size and speed. The tradeoffs accepted are the LSTM’s: Python loops, masking rather than packing, no fused kernel.
What can go wrong
Section titled “What can go wrong”The checks a correct solution passes:
| Case | Kind of check | Checks | Why it matters downstream |
|---|---|---|---|
| The hand example cell | unit | , | the gates, their order, and the reset placement are right |
| Matches torch’s cell | golden | torch.nn.GRUCell outputs and every gradient | torch weights load and run |
| Matches torch’s sequence | golden | one layer with a state, two layers, a packed padded batch | the seq2seq models load and train GRUs |
| State-dict names match torch | golden | torch’s names, order, and shapes | the safetensors key contract |
| Gradcheck of the cell | gradcheck | autograd through the cell against central differences | the backward is right without torch |
| The update gate near one keeps the state | property | saturated : and | the reason the GRU remembers |
| Orthogonal recurrence and zero bias | unit | zero biases, orthogonal blocks, Xavier bound, seeding | trainable from step 0 |
| Lengths keep padding out | boundary | padded outputs 0; final state equals running alone; no gradient into padding | batches of sentences |
| Dropout between layers only | unit | dropout between layers, in training mode only | evaluation is deterministic |
| Default state and bad arguments | boundary | no state means zeros; bad shapes, lengths, sizes raise | errors at the call site |
The pitfalls, several of which load a checkpoint without error and quietly produce wrong numbers:
Warning: Gates in the order train fine from scratch, but torch weights give garbage. The solution slices
aandbas .
Warning: The reset gate in Cho’s position, or applied to but not to , comes close to torch but is never equal; a loaded checkpoint loses accuracy for no visible reason. The solution writes
r * b[..., 2H:3H], wherebalready includesb_hh.
Warning: Swapping the interpolation to inverts the update gate’s meaning: a torch checkpoint forgets what it should keep. The solution returns
(1.0 - z) * n + z * h.
Warning: Padding leaking into the state or the outputs makes a short sentence’s encoding depend on its batch-mates. The solution applies
F.whereto the state and the output with the_mask.
Warning: A sigmoid candidate cannot be negative, so the state drifts to positive values. The solution uses
n = F.tanh(...).
Warning: Stacking mistakes (dropout after the last layer, layer starting from layer ‘s state) make 1-layer train and eval outputs differ and 2-layer outputs differ from torch. The solution guards dropout with
if k < L - 1and starts withh = h0[k].
Warning: Computing part of the step on raw arrays (
h.data), so that carries no gradient, leaves the forward right and training quietly worse: the state’s own path back through time is cut. The whole cell isFops on thehTensor.
Where this leads
Section titled “Where this leads”The GRU leans on the same foundations as the LSTM: Tensor parameters with getitem for x[t] and h0[k]; F.matmul, F.transpose, F.sigmoid, F.tanh, F.where, and F.stack from the op library; Module registration order as key order and Dropout between layers; and xavier_uniform, orthogonal_init, and PCG32(0).substream("init") from Mathematics.
Going forward, the bidirectional RNN builds bidirectional(GRU, GRU, x, lengths), which is the encoder of Encoder-decoder with teacher forcing, where the GRU is also the default decoder cell. The RNN language model builds RNNLM(cell='gru') and compares it with the vanilla RNN and the LSTM on the same text.
Production counterparts:
| This piece | Production equivalent | What it adds | Where to look |
|---|---|---|---|
gru_cell | cuDNN’s GRU (CUDNN_GRU) | the “linear before reset” form the solution implements, chosen so the recurrent GEMM runs before the gates | NVIDIA cuDNN API reference, cudnnRNNMode_t |
GRU | torch.nn.GRU | packed sequences, bidirectional layers, projection sizes | torch/nn/modules/rnn.py |
| the original form | Keras GRU(reset_after=False) | Cho’s placement, kept for old checkpoints; reset_after=True is the cuDNN form | keras/layers/rnn/gru.py |
| gating | minGRU (2024) | gates that read only the input, so the recurrence becomes a parallel scan | Feng et al., “Were RNNs All We Needed?” |
Further reading
Section titled “Further reading”- Cho et al., Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation (2014).
- Chung et al., “Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling” (2014).
- The cuDNN developer guide, RNN formulas.
Key terms
Section titled “Key terms”- Gated recurrent unit (GRU): a recurrent cell with one state and two gates, .
- Update gate : the fraction of the old state kept; copies the state and its gradient.
- Reset gate : scales the recurrent contribution to the candidate; makes the candidate depend on the input alone.
- Reset-after (linear-before-reset) form: torch’s and cuDNN’s , as opposed to Cho’s .
- torch gate order: the block order in torch’s stacked weights.
Summary
Section titled “Summary”- A GRU has one state and two gates: the update gate interpolates, , so copies the state and its gradient unchanged.
- The reset gate multiplies the recurrent part after its bias, : torch’s and cuDNN’s form, not Cho’s original, and weights trained one way do not load the other.
- Gate blocks are stacked with torch’s names, so torch GRUs load by name and give torch’s outputs and gradients, padded batches included.
- Three gate blocks instead of four: three quarters of the LSTM’s parameters for a similar memory, which is why the seq2seq models default to it.
Review questions
Section titled “Review questions”- In the hand example, where does each of the two terms of come from?
Answer
$0.75 = z$ is the direct path through $z \odot h$. $0.0746 = (1 - z)(1 - n^2)\, r\, W_{hn} = 0.25 \times 0.596586 \times 0.5 \times 1$ is the path through the candidate. The gates do not read $h$ here ($W_{hh}$ is zero in the $r$ and $z$ blocks), so there are no further terms. - Why does Cho’s form give instead of on the hand example’s weights?
Answer
Cho's form computes $W_{hn}(r \odot h) + b_n = 1 \times 0.25 + 1 = 1.25$: the bias is outside the reset. torch's form computes $r \odot (W_{hn} h + b_{hn}) = 0.5 \times 1.5 = 0.75$: the reset scales the bias too. The only difference is whether $r$ multiplies $b_{hn}$ and where it meets the matmul. - What goes wrong with the interpolation swapped, ?
Answer
The meaning of $z$ inverts: a value near 1 now overwrites the state instead of keeping it. Trained from scratch this is just a relabelling, but a torch checkpoint loaded into it forgets what it should keep (the hand example would give 0.601362 instead of 0.533787). - Why is a test with the one that separates the two reset placements?
Answer
With $b_{hn} = 0$ and one unit the two forms are identical ($r w h$ either way), and with zero biases they differ only through how $r$ mixes across units, which a small test can miss. With $b_{hn} \ne 0$, torch's form scales the bias by $r$ and Cho's does not, so the candidates differ by $(1 - r) b_{hn}$ inside the tanh even for one unit.