BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / GPU kernels (simulated)19 of 24 in topic

690. Online softmax, the trick inside FlashAttention

HardGPU kernels (simulated) Pro
Asked atNVIDIAOpenAI

This is a NumPy simulation of FlashAttention's math, not a GPU kernel.

FlashAttention never stores the full row of attention scores. It streams over blocks of keys, keeping a running max m, a running normaliser l and a running output o, and rescales them whenever a new block raises the max.

Write online_softmax_weighted_sum(scores, values, block) for scores (n,) and values (n, d). Process them in blocks of block. For each block with scores s_b and values v_b:
m_new = max(m, max(s_b))
l = l * exp(m - m_new) + sum(exp(s_b - m_new))
o = o * exp(m - m_new) + exp(s_b - m_new) @ v_b
Start with m = -inf, l = 0, o = 0. Return o / l, which must equal softmax(scores) @ values.

Functiondef online_softmax_weighted_sum(scores, values, block)
Play with it

The softmax that online softmax must reproduce

Online softmax gives exactly these numbers in one pass by rescaling a running sum when the max changes. Push one logit high to see why subtracting the max is needed.

Sweep the logit of
0%25%50%75%100%-4-2024logit of x0probability0%25%50%75%100%probabilityx084.4%x111.4%x24.2%
entropy (nats)
0.524
top class
x0

Example 1

Input: online_softmax_weighted_sum([2.3, 3.1, -2.9, 2.0, 4.9, 0.2, 5.4, 4.6, -1.0, 2.7], [[0.6, 1.4, 0.3], [-1.8, 0.4, 0.7], [0.2, 0.6, -1.1], [1.3, 0.2, -1.2], [-0.5, 0.0, 0.6], [-0.6, 2.0, -0.5], [-0.5, -0.9, -0.1], [1.3, -0.6, -0.3], [0.5, -1.4, 0.7], [0.6, 0.9, 1.0]], 3)

Output: [-0.1261, -0.4289, 0.1031]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Keep a running max, normaliser and output, and rescale the old totals whenever a new block raises the max.

Hint 2

Loop over blocks of size block: compute the new max, the rescale factor exp(m - m_new), update l and o with the block's exp(s_b - m_new) terms, then store the new max; return o / l.

Hint 3

On the first block m is -inf, so make the rescale factor 0 there (for example with np.isfinite(m)) to avoid -inf - -inf; the output update is o * c + e @ v_b.

Common mistakes
  • Normalising each block by its own max without rescaling (the buggy version) weights blocks wrongly.
Understand the idea

Online softmax computes softmax(scores) @ values while streaming over blocks, never holding the full row of scores. It keeps a running max m, a running sum of exponentials l and a running weighted output o. When a new block has a bigger max, everything collected so far was computed relative to the old max, so it is multiplied by exp(old m - new m) to rescale.

Worked example

scores [1, 3], values [[10], [20]], block 1. Start m = -inf, l = 0, o = 0. Block 1: m = 1, l = exp(0) = 1, o = 1 x 10 = 10. Block 2: new m = 3, factor exp(1 - 3) = 0.135. l = 1 x 0.135 + exp(0) = 1.135. o = 10 x 0.135 + 1 x 20 = 21.35. Answer o / l = 18.81. Direct softmax of [1, 3] is [0.119, 0.881], giving 1.19 + 17.62 = 18.81, the same.

m_new = max(m, max s_b); l = l e^(m - m_new) + sum e^(s_b - m_new); o = o e^(m - m_new) + e^(s_b - m_new) v_b

FlashAttention is built on this trick.

Topics
GPU kernels (simulated)
Companies2
  • NVIDIA the topic appears in interview reports NVIDIA Interview Prep: Questions, Process & Study Plan
  • OpenAI a candidate reported this question OpenAI Interview Process & Questions (2026) (TrueInterview)

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Count shared-memory bank conflict passes per warpHard
  • Simulate a work-efficient parallel prefix sumHard
  • Simulate a multi-stage load and compute pipelineHard
  • Compute dK and dV tile by tile from the saved log-sum-expHard
All GPU kernels (simulated) problems
Report
690 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def online_softmax_weighted_sum(scores, values, block):
# your code here
pass
​
online_softmax_weighted_sum(_s, _v, 3)

This case uses generated data, so it runs as written. Press + to add your own case.

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS