Online Softmax Stepper
Steps through the online (streaming) softmax — the recurrence Flash Attention uses to compute attention tile by tile without ever materialising the full score matrix. Standard softmax needs two passes (find the max, then normalise). Online softmax processes one logit at a time, keeping a running maximum m and a running normaliser l, and rescaling the accumulator by exp(oldM - newM) whenever a fresh maximum arrives.
Online softmax stepper. 5 values ready. Step 0 of 5.
Online softmax · step 0 / 5idle
Customize
Input
bump
Playback
step 0
Display
Installation
npx shadcn@latest add https://craftbits.dev/r/online-softmax-stepper.jsonUsage
import { OnlineSoftmaxStepper } from "@craft-bits/core";
<OnlineSoftmaxStepper values={[2, 1, 3, 0, 1]} />Drive playback from outside (parent scrubber, animation, narration sync):
const [step, setStep] = useState(0);
<OnlineSoftmaxStepper
values={[2, 1, 3, 0, 1]}
currentStep={step}
onCurrentStepChange={setStep}
playing
playSpeed={600}
/>Understanding the component
- Precomputed snapshots. When
valueschanges, the entire walk is recomputed insideuseMemo. Each snapshot carriesm,l, the per-index probability vector so far, plus arescaledflag and the rescale factorexp(oldM - newM). Scrubbing or stepping is O(1). - The update rule. On each new value
vat indexk:m_new = max(m_old, v),r = exp(m_old - m_new)(= 1 when no new max),l_new = l_old * r + exp(v - m_new). Recovered probabilities arep_j = exp(x_j - m_new) / l_newforj ≤ k. - Why rescaling matters. When a fresh maximum arrives, every previously accumulated term silently picks up the new
m. The single multiplicative factorexp(oldM - newM)corrects for that without touching individual terms — the trick that turns softmax into a streamable, tile-friendly operation in Flash Attention. - Numerical stability. Every
exp(...)sees a non-positive argument (x - m_new ≤ 0), so values never overflow no matter how large the input logits get. The standard "subtract the max" softmax trick, maintained incrementally. - Spring transitions on bar heights. Bar
yandheightanimate withSPRINGS.smooth; the state readout fades withSPRINGS.snap.prefers-reduced-motion: reducecollapses everything to instant, suppresses autoplay, and parks the timeline at the final snapshot on mount. - Controlled + uncontrolled. Both
currentStepandplayingaccept controlled props paired withonCurrentStepChange/onPlayingChange, or the matchingdefaultCurrentStep/defaultPlayinguncontrolled variants.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
values | readonly number[] | — | Input logits processed one at a time. |
labels | readonly string[] | indices | Bar labels — falls back to the index when missing. |
currentStep | number | — | Controlled active step (0..values.length). |
defaultCurrentStep | number | 0 | Uncontrolled initial step. |
onCurrentStepChange | (step: number) => void | — | Fires whenever the active step changes. |
playing | boolean | — | Controlled play state. |
defaultPlaying | boolean | false | Uncontrolled initial play state. |
onPlayingChange | (playing: boolean) => void | — | Fires when play / pause flips. |
playSpeed | number | 600 | Milliseconds between auto-played steps. |
showState | boolean | true | Render the running max / sum / rescale readout panel. |
transition | Transition | SPRINGS.smooth | Override the spring used for bar transitions. |
className | string | — | Merged onto the root <div> via cn(). |
Accessibility
- The root is
role="figure"with anaria-labelledbyheading and a visually hiddenaria-live="polite"summary — screen readers hear the step count, last value, running max, running sum, and rescale factor on every change. - The visualisation is read-only — no draggable bars or pointer-driven controls — so there is no keyboard interaction to enumerate.
- The fresh-max highlight is reinforced by the state readout text, so users not relying on colour still know when a rescale has just happened.
- Animation respects
prefers-reduced-motion: reduce: springs collapse to instant, autoplay is suppressed, and the component parks at the final snapshot on mount.
Credits
- Extracted from:
craftingattention(app/src/lessons/primitives/nn/OnlineSoftmaxStepper.tsx). The source paired the visualisation with a four-phase narration system, a Process-Block / Reset button strip, block grouping (4 blocks of 2 scores),ChallengeBtn/SvgLabelchrome, and inlinevar(--color-accent-400)references. The library extract is the pure stepthrough primitive — a precomputed online-softmax timeline parameterised byvalues. Per-element processing replaces block grouping; chrome and narration belong in the consuming lesson.