PIXELBANKv8.2.1
Menu

Analyze Transformer Self-Attention Complexity

The self-attention mechanism is a critical component of the Transformer architecture. It processes a sequence of NN input vectors (tokens or patches), each with embedding dimension DD.

The computation requires calculating pairwise affinities between every element (query qq) and every other element (key kk) in the sequence, typically followed by scaling and multiplying by the value matrix VV.

Given:

  • NN: Sequence length
  • DD: Embedding dimension
  • KK: Convolution kernel size (for comparison)

Task: Determine the asymptotic time complexity for:

  1. A single layer of self-attention: O(N2D)O(N^2 \cdot D)
  2. A standard convolution layer: O(ND2K2)O(N \cdot D^2 \cdot K^2)

Return a tuple of strings representing both complexities.

Example:

Input:
N=100, D=512, K=3
Output:
("O(N^2*D)", "O(N*D^2*K^2)")
Reasoning:

Self-attention computes QK^T V where complexity is dominated by N×N attention matrix computation: O(N²D). Convolution operates on N spatial locations with D² channel interactions and K² kernel elements.

Constraints:

  • NN (Sequence Length): 1N1051 \leq N \leq 10^5
  • DD (Embedding Dimension): 1D10241 \leq D \leq 1024
  • KK (Kernel Size): 1K101 \leq K \leq 10
Editor

Test Results

0/0
Run code to see test results.