Where the quadratic term comes from
Attention computes, for each query, a weighted average of value vectors, with weights given by the softmax of query-key similarities. Written as matrices, the output is the softmax of Q times K transposed, all multiplied by V.
Q, K and V each have n rows and d columns, where n is the sequence length and d the head dimension. In language modelling n is thousands or hundreds of thousands and d is typically 64 or 128, so n is enormously larger than d.
Now look at where the cost sits. Q times K transposed produces an n-by-n matrix. That is the quadratic term, in both compute and memory.
The question worth asking is whether that matrix has to exist. Matrix multiplication is associative, so the product of three matrices can be bracketed either way. Multiplying K transposed by V first gives a d-by-d matrix, which is tiny and independent of sequence length, and the total work becomes linear in n.
So the quadratic cost is not inherent in attention's shape. It is imposed by one thing sitting between the two multiplications.

