AI Hardware & Software Stack

How does the computation of Transformer self-attention grow with input sequence length n?

  1. AIn proportion to n
  2. BIn proportion to n squared
  3. CIt is constant regardless of n
Show answer and explanation

Answer: B. In proportion to n squared

In self-attention, each token's query is dotted with the keys of all tokens, producing an n × n table of weights. Computation, and the memory for that intermediate table if built naively, therefore scale with the square of sequence length. Doubling the length quadruples the attention part, so attention efficiency increasingly dominates performance for long contexts.

  • A. Parts such as fully connected layers scale with n, but attention scales with n squared.
  • B. Correct. Every pair of tokens is compared.
  • C. It grows with length.
Practice this topic Back to topic

More AI Hardware & Software Stack questions

  1. What is the main idea behind fast attention implementations such as FlashAttention?
  2. What is the main purpose of operator fusion in AI computation?
  3. A matrix product followed by a bias add and a ReLU is fused from three separate kernels into one. What mainly decreases?
  4. What do AI compilers such as XLA, TVM or PyTorch's compile feature mainly do?
  5. Which describes the computational graph used by AI frameworks and compilers?