Md. Asif Uddin
    Problem I.10.B05

    What four gates cost

    shape▲▲△

    Exact values where possible; six decimal places for non-exact numerical results.

    STATEMENT

    Audit the shapes, parameter counts, arithmetic and training storage of a tanh RNN, a GRU and an LSTM, then find a fair width for comparing two of them.

    GIVEN

    Input width D=3D=3, hidden width H=5H=5. Count one bias per gate as in (I.10.8), then PyTorch’s two bias vectors per block. For storage, take N=4N=4 sequences of length T=6T=6 in float32, as in I.9.B04. Assume the plain cell stores ht\vec h_t for the backward pass and the LSTM stores it,ft,ot,c~t,ct,ht\vec i_t,\vec f_t,\vec o_t,\tilde{\vec c}_t,\vec c_t,\vec h_t.

    FIND

    (a) PyTorch’s LSTM weight and bias shapes, and the rows of the forget gate. (b) Parameter counts for all three cells under both bias conventions, with the GRU’s reset-after form. (c) Multiply–accumulates per step for one sequence. (d) Stored activation bytes. (e) The GRU width whose one-bias count equals the LSTM’s at H=5H=5.

    STRATEGY

    Each gated block is a plain recurrent layer’s affine map with its own weights. Count one block, then multiply.

    SOLUTION

    (a) PyTorch stacks the four LSTM blocks in the order input, forget, candidate, output. The input weights are 4H×D=20×34H\times D=20\times3, the recurrent weights 4H×H=20×54H\times H=20\times5, and each bias vector has 4H=204H=20 entries. Counting from zero, the forget gate occupies rows 55 to 99. Rows 00 to 44 are the input gate. For a GRU the order is reset, update, new, so the blocks are 3H3H rows tall.

    (b) One block has H(D+H+1)=5⋅9=45H(D+H+1)=5\cdot9=45 parameters with one bias. So

    RNN 45,GRU 3⋅45=135,LSTM 4⋅45=180.\text{RNN }45,\qquad\text{GRU }3\cdot45=135,\qquad\text{LSTM }4\cdot45=180.

    The reset-after GRU keeps bhn\vec b_{hn} inside the reset, which cannot be merged with bn\vec b_n: 135+5=140135+5=140. PyTorch keeps two bias vectors per block, H(D+H)+2HH(D+H)+2H each: 40+10=5040+10=50, then 150150 and 200200.

    (c) One affine map from (xt,ht−1)(\vec x_t,\vec h_{t-1}) costs H(D+H)=40H(D+H)=40 multiply–accumulates. The LSTM computes four, 160160 per step; the GRU three, 120120; the plain cell one, 4040. The elementwise gate products add O(H)O(H) per step and do not change the leading count.

    (d) The plain cell stores NTH=120NTH=120 numbers, 480480 bytes. The LSTM, under the stated scheme, stores six vectors per step: 6⋅120=7206\cdot120=720 numbers, 28802880 bytes. Other schemes store tanh⁡ct\tanh\vec c_t or recompute gates in the backward pass. Name the scheme before quoting a ratio.

    (e) A GRU of width H′H' at D=3D=3 has 3H′(H′+4)3H'(H'+4) parameters. Setting this to 180180 gives H′2+4H′−60=0H'^2+4H'-60=0, whose positive root is H′=6H'=6: 3⋅6⋅10=1803\cdot6\cdot10=180 exactly. Its arithmetic is 3⋅6⋅9=1623\cdot6\cdot9=162 multiply–accumulates per step, against the LSTM’s 160160. In PyTorch’s convention the two counts are 198198 and 200200.

    A plain RNN cannot match exactly: H′(H′+4)=180H'(H'+4)=180 has no integer root, and widths 1111 and 1212 give 165165 and 192192.

    Answer

    (a) 20×320\times3, 20×520\times5, two biases of 2020; forget rows 55–99. (b) One bias: 4545, 135135 (140140 reset-after), 180180. PyTorch: 5050, 150150, 200200. (c) 4040, 120120, 160160 per step. (d) 480480 and 28802880 bytes. (e) H′=6H'=6, 180180 parameters, 162162 multiply–accumulates.

    Check — sanity

    Setting D=0D=0 removes every input weight: the one-bias LSTM count becomes 4H(H+1)=1204H(H+1)=120, four copies of a plain recurrent map with bias. Counts should always factor this way.

    Where this breaks

    Multiply–accumulate counts are not runtimes. Fused kernels compute all four LSTM blocks as one 4H4H-row product, and memory traffic often decides speed. Projection layers, peepholes and bidirectional stacks change every count above.

    Variation

    At D=H=512D=H=512, a common width, the one-bias LSTM block count is 4⋅512⋅1025=2,099,2004\cdot512\cdot1025=2{,}099{,}200 parameters, and the GRU’s is 1,574,4001{,}574{,}400.

    Draws on