nn.GRUCell is a PyTorch recurrent-cell module for advancing an RNN one time step at a time. Each call receives the current input and a previous hidden state, then returns the updated hidden state. Unlike nn.GRU, it does not consume an entire sequence in one operation, so the surrounding code must manage the time-step loop and hidden states. section 7.5.1 section 11.4.1
Use the cell when explicit sequence control matters:
- Process inputs incrementally when the complete sequence is unavailable or has unknown length; accept slower execution than the normal nn.GRU path. section 7.5.1
- Maintain one hidden-state tensor per layer and update it after every call. A typical stacked design uses nn.ModuleList, with the first cell mapping embedding dimensions to hidden dimensions and later cells mapping hidden dimensions to hidden dimensions. section 7.5.1
- Feed each layer's output into the next layer, optionally apply LayerNorm, and pass the final hidden state to a prediction network. section 7.5.1
Manual time-step loop
A typical step function embeds token IDs when needed, initializes hidden states when absent, then updates every layer: h = self.norms[l](self.layers[l](x_in, h_prevs[l])); h_prevs[l] = h; x_in = h. The forward method can call this function for each t and stack the returned predictions across time. section 7.5.1
Autoregressive sampling
For generation, run the seed through the cell once, retain h_prevs, sample the next token, and call the cell again with the updated token and hidden states. This changes generation from repeatedly reprocessing the full prefix to one update per generated token. section 7.5.4
Variable-length sequence-to-sequence decoding
Use nn.GRU for an encoder when the input is available at once, but use stacked nn.GRUCell layers for a decoder that produces tokens until an end condition or max_decode_length. The decoder manually tracks hidden states and selects the next token at each step. section 11.4.1
