Unit 8 of 11 · about 4 hours · derive, implement

Online softmax and its extreme-value policy

Softmax is the numerical heart of attention. The online form removes a pass over the logits while staying exact, and its running statistics become tiled-attention state later.

You'll be able to

  • Derive the running-maximum and normalizer recurrence and its single-pass invariant.
  • Implement naive, safe two-pass and online softmax and compare their per-element traffic.
  • Define and test the extreme-value and all-masked-row policy.

Covers

Numerically stable online normalizer and softmaxExtreme logits, all-masked rows and empty-row policy
1

Read

In this order, only the parts listed

0 of 2 done

  1. Online normalizer recurrenceOnline Normalizer Calculation for Softmax

    Sections 2 and 3 with Algorithms 1 to 3 and Theorem 1 (arXiv:1805.02867v2): naive 3-access softmax, safe 4-access softmax, and the online recurrence with running maximum and rescaled normalizer

    Work the induction behind Theorem 1 by hand. Save Section 4 (top-k fusion) for the attention track.

  2. Parallel form and benchmarksOnline Normalizer Calculation for Softmax

    Section 3.1 (associative pair operator for parallel reduction) and Sections 5 to 6 (1.15x to 1.3x on a V100, fused top-k down to one access per element)

    Read the benchmark setup for method. Treat the V100 ratios as historical.

2

Focus on

  • Subtracting the maximum first is what makes softmax safe: exp of a nonpositive finite value cannot overflow, and the online form keeps that guarantee in one pass.
  • The invariant is that the normalizer always matches the current running maximum, rescaled by exp of the old-minus-new maximum at each step. Theorem 1 proves it by induction.
  • Traffic per element drops from 4 accesses (safe three-pass) to 3 (online). The win is memory, not FLOPs.
  • Row [1, 2, 3, 1000] gives running maximum 1000 with normalizer 1.0 and output [0, 0, 0, 1] up to rounding, while naive exp(1000) overflows to inf.
Common mistakes · 3
  • Forgetting to rescale the normalizer when a new maximum arrives. The result looks plausible but is wrong; test with an increasing sequence where every element is a new maximum.
  • Assuming the paper covers fully masked rows. It assumes valid rows, so an all-negative-infinity row gives 0/0 (NaN) unless you declare a policy, for example all zeros, and test it.
  • Comparing against torch.softmax at 1e-7. Order and rounding differ across implementations, so use 1e-5 with the magnitude stated.
3

Try it yourself

Closed-book, before checking anything

Derive the recurrence from the safe form, hand-run it on [1, 2, 3, 1000], then implement all three versions so they agree within 1e-5 on 500000 uniform (-10, 10) logits, the Challenge 5 perf shape.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Why does the safe softmax take 3 passes and 4 accesses per element?Answer

    One read for the maximum, one read for the normalizer, one read plus one write for the output. The online form merges the first two passes into one.

  2. 2.What does the rescaling factor do when a new maximum arrives?Answer

    Old partial sums were normalized by the old maximum, so multiplying by exp of old-minus-new maximum (at most 1) renormalizes them to the new maximum.

  3. 3.The paper bounds the running normalizer between which values?Answer

    Between 1 and the element count. The running maximum also stays within the input range, so neither can overflow.

  4. 4.What does an all-negative-infinity row produce without an explicit policy, and what fixes it?Answer

    Maximum negative infinity, normalizer 0 and 0/0, which is NaN. Declare the empty-row policy (for example a zero vector) and test that row directly.

5

Practice

  • LeetGPU challenge 5: Softmax

    Challenge 5: solve(input, output, N) with max subtraction, with functional cases including the [1000, 1, 2, 3] overflow trap and a single element, and a performance case of

    Run it on LeetGPU or locally with the harness from the transpose unit. Pass the public tests, then add your own all-masked-row test with a documented policy. The public tests assume at least one finite logit.

You're done when

All three implementations agree within 1e-5 on the perf shape, your hand recurrence matches the code, and the all-masked policy is tested.

Only when you can do this without notes.

Leads to

Notes

Resources · 1 for this unit, 10 more in the section
  • Online Normalizer Calculation for SoftmaxOnline Softmax Authors · required

    Derive the running maximum and normalizer recurrence, test extreme and all-masked rows, and compare materialized, two-pass and online implementations before using the recurrence inside attention.

Also in this section

  • Programming Massively Parallel Processors, 4th edition - Chapters 6 and 9 to 12Wen-mei Hwu, David Kirk and Izzat El Hajj · primary

    Read thread coarsening in Chapter 6, then Chapters 9 (histogram), 10 (reduction), 11 (prefix sum) and 12 (merge, optional). Work the exercises that test work, depth, contention and boundary handling.

  • AI Systems Performance Engineering - Chapters 11 and 12Chris Fregly (O'Reilly) · required

    Read Chapter 11 on streams, events, inter-kernel pipelining and stream-ordered allocation, and Chapter 12 on CUDA graphs and dynamic scheduling. Skip device-initiated launch beyond the vocabulary.

  • Efficient Matrix Transpose in CUDA C/C++NVIDIA · required

    Rebuild copy, naive transpose, coalesced transpose and padded shared-memory variants. Update deprecated details for the installed toolkit and compare requested transactions, bank behavior and achieved bandwidth on odd as well as friendly shapes.

  • Optimizing Parallel Reduction in CUDANVIDIA · required

    Implement the optimization sequence from interleaved addressing through sequential addressing, first-add-during-load, unrolling and warp-level completion. Correct outdated assumptions and retain the stage where each optimization stops helping.

  • Single-pass Parallel Prefix Scan with Decoupled Look-backNVIDIA Research · depth

    Read after two-pass scans pass. Derive the state and progress argument, then implement or inspect a bounded version only if the available hardware and time support meaningful comparison.

  • LeetGPU - CUDA Primitive ChallengesAlphaGPU · required

    Solve challenge 3 Matrix Transpose, 4 Reduction, 5 Softmax, 13 Histogramming and 16 Prefix Sum in CUDA. Add 70 Segmented Prefix Sum or 72 Stream Compaction only after the required set. Keep local source, hidden-shape assumptions, hardware, raw timings and correctness evidence outside the leaderboard.

  • GPU MODE - Reductions and ScansGPU MODE · support

    Use Lectures 9, 20, 21 and 24 after the PMPP implementations. Reproduce one faster scan or reduction idea and show the exact shape and device regime where it changes the bottleneck.

  • MIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 6, 7 and 9 and Lab 7MIT · support

    Use Lecture 6 for host-device copies and streams, and Lectures 7 and 9 for map, stencil, reduce, compaction, scan, segmented scan, histogram and merge. Do Lab 7 (Run-Length Compression, with its scan and shuffle warm-ups) built for your own GPU.

  • GPU Microbenchmark and Kernel Suite - CUDA CoreRoadmap-Final · support

    Complete the CUDA vector map, reduction, transpose and stable softmax requirements plus seeded access, synchronization, mask, stream and numerical failures. Preserve library and framework baselines.

  • CUDA C++ Programming Guide (v13.4.2)NVIDIA · reference

    Use Section 2.3.4 for coalesced access, the transpose example and bank-conflict padding; Section 2.5 for streams, events, synchronization and the stream-capture intro; and Section 4.2 for graph creation, update and capture constraints. This is the API reference beside the book chapters.