Md. Asif Uddin
    I.5.X08

    A dense layer is memory-bound until the batch is large

    complexity▲▲△

    For a single dense layer of shape 1024×10241024 \times 1024 in fp32, derive the arithmetic intensity — FLOPs performed per byte of memory moved — as a function of the batch size BB. Tabulate it for BB from 1 to 1024, and say what the numbers imply about running an MLP one example at a time.

    Hint

    Count the traffic honestly: the weights are read once per batch, the inputs and outputs scale with BB. Then take the limit in BB.

    Solution

    The two counts. A batch of BB rows through W∈Rd×d\mat{W} \in \R^{d \times d} performs Bd2B d^2 multiply–adds, or 2Bd22Bd^2 FLOPs. The bytes moved, at 4 bytes each, are the weights once plus the input and output rows:

    bytes=4(d2+Bd+Bd),intensity(B)=2Bd24(d2+2Bd)\text{bytes} = 4\left(d^2 + Bd + Bd\right), \qquad \text{intensity}(B) = \frac{2Bd^2}{4\left(d^2 + 2Bd\right)}

    The table, at d=1024d = 1024:

    BBFLOP/byteweights as a share of traffic
    10.4990.49999.8%99.8\%
    20.9960.996
    83.9383.938
    3215.05915.05994.1%94.1\%
    12851.20051.200
    25685.33385.33366.7%66.7\%
    1024170.667170.667

    The limit. Divide through by BB:

    lim⁡B→∞intensity(B)=2d28d=d4=256\lim_{B \to \infty} \text{intensity}(B) = \frac{2d^2}{8d} = \frac{d}{4} = 256

    so the intensity rises towards d/4d/4 and never passes it. Small BB is the regime where the denominator is dominated by d2d^2, and the intensity is approximately B/2B/2.

    What it implies. At B=1B = 1 the layer performs one multiply–add per weight and does half a FLOP per byte moved. Accelerators reach their peak arithmetic rate only above roughly 100100 FLOP/byte, so a batch of one leaves the machine idle by more than two orders of magnitude: the time is spent reading weights, not multiplying. The layer is memory-bound, and making the matrix bigger does not help, since the weights grow as fast as the arithmetic.

    Three consequences.

    Batching is not only a statistical decision. I.4 discusses the batch as a variance choice; this is the other axis, and at B=1B = 1 the hardware cost is paid whatever the statistics say.

    FLOPs are a poor proxy for time. Two models with the same FLOP count differ by a factor of hundreds if one runs at B=1B = 1 and the other at B=256B = 256. I.5.B04’s caution has its arithmetic here.

    Single-example inference is the hard case. Serving one request at a time is exactly the memory-bound regime, which is why inference systems batch requests across users, and why the same problem returns for the KV cache in Book IV.

    Draws on