How does torch.nn.functional.scaled_dot_product_attention relate to FlashAttention?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
SDPA is PyTorch's unified attention API. It dispatches at runtime to FlashAttention-2, mem-efficient attention, or a naive math kernel based on shape, dtype, and GPU support.
Picture asking a phone assistant to send a message. You do not pick whether it goes by SMS, iMessage, or email; the assistant looks at who you are messaging and picks the best channel automatically. SDPA is that assistant for one of the slowest steps in a language model. You write one line in your model code and PyTorch quietly checks your GPU, the size of the numbers you are using, and the length of the input, then routes the call to whichever underlying engine will run it fastest. You never have to install a separate library or rewrite your model when a faster engine arrives, because the wrapper picks it up for free.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
60s: name SDPA as the unified PyTorch attention API, list the three backends (FlashAttention-2, mem-efficient, math) and selection priority, state output is identical across backends, give the sdp_kernel context manager for backend pinning, mention the typical fallback triggers (unsupported mask, unusual head dim, fp32).
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Believing SDPA is a slower wrapper around FlashAttention. It actually IS FlashAttention-2 when shapes and hardware support it, with mem-efficient and math kernels as automatic fallbacks.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.