LLM Construction
Ring vs. Tree Attention
Exact attention distributed across many devices needs a way to combine local softmax normalizers into one global result. Ring Attention rotates full K/V shards through a sequential ring; Tree Attention reduces small partial states through a balanced binary tree. Both use the same exact real-arithmetic reduction, while their communication costs differ.
Prerequisites
Learning position
Read this page in the graph.
llm-construction | layer 5 | tier 2. This page has 3 direct prerequisites and 0 published dependents.
What next
Sparse Attention and Long ContextThis is the first curated or graph-derived continuation from the current page.
Evidence badge
Source-grounded pageThis page has no public Lean mapping yet. Use the evidence page to inspect how claim status labels work.
Why This Matters
Ring and Tree Attention normalizer explorer
Step through the merges. This explorer verifies only the online-softmax normalizer state (m, l), not the attention output accumulator.
Flash Attention shows that one device can compute exact attention without ever materializing the full score matrix, by tiling into blocks and maintaining a running online-softmax state. The natural next question, once a context is too long or a batch too large for one device: what happens when the blocks themselves live on different devices?
The local attention calculation does not change. Ring Attention (Liu, Zaharia, Abbeel, ICLR 2024) and Tree Attention (Shyam et al., 2024) use different schedules to combine device-local partial results under the same exact real-arithmetic reduction.
The shared building block: the online-softmax merge
Each device holds a shard of and computes a local, unnormalized partial state from its own shard: a running max score and a running sum-of-exponentials (plus the corresponding unnormalized output accumulator, omitted here for clarity). Two partial states and combine via
This normalizer merge is associative and commutative. It is the log-sum-exp combine rule for a running maximum and normalizer. That fact lets devices fold partial states in any order or tree shape while preserving the same in exact arithmetic. Full attention also merges a separately scaled output accumulator; this page uses the normalizer pair because it makes the topology visible without introducing vector-valued state.
Two topologies, same answer, different communication cost
Ring Attention rotates shards around a ring of devices. Each device sends its local shard to a neighbor, receives another device's shard, and updates its running online-softmax state. After hops, every device has processed every shard. This is sequential point-to-point exchanges, each moving a full shard. Liu, Zaharia, and Abbeel overlap each exchange with local computation, but the round count and payload per round still grow linearly with device count.
Tree Attention has each device compute a local state against the shared query, then combines those small states rather than moving the underlying data. Pairs merge in round 1, survivors pair in round 2, and the schedule finishes in rounds, with up to merges per round. Shyam et al. prove the underlying per-query calculation admits an parallel decomposition (Theorem 1, arXiv:2408.04093). Their §6.1 experiment on 16 nodes and 128 H100 GPUs at a 5.12M-token sequence length reports close to lower decoding latency than their Ring Attention implementation. Their §6.2 memory formulas give versus for batch size , local chunk length , model dimension , and head count (arXiv:2408.04093, §6.2, Eqs. 8-9). Tree Attention's dominant term is half of Ring Attention's. Measured on two RTX 4090s, doubling hidden size from 2048 to 4096 doubled the absolute memory gap between the two methods, from 524MB to 1040MB (§6.2).
Neither topology introduces an attention approximation. In exact arithmetic, both evaluate the same attention reduction as a single-device Flash Attention calculation. In floating point, different reduction order can produce machine-epsilon differences. The interactive diagram checks the normalizer state against a direct calculation; it does not model the output accumulator.
Main Theorem
The Online-Softmax Normalizer Merge Is a Commutative Semigroup
Statement
For two normalizer states and , define where and . This operation is associative and commutative. Folding any partition of a score sequence into partial states, then merging those states in any order or binary-tree shape, produces the same as computing the normalizer over the whole sequence at once.
Intuition
is the log-sum-exp combine rule carried alongside a running maximum for numerical stability. Addition and max are associative and commutative; the exponential rescaling puts both local sums on the same maximum scale. The result depends on the included scores, not their grouping.
Proof Sketch
Commutativity follows because swapping the inputs leaves both and unchanged. For associativity, expand and . Both reduce to and . Induction extends the identity to any finite fold order or tree shape. Flash Attention uses the same identity across sequential blocks on one device.
Why It Matters
This is the entire reason Ring Attention and Tree Attention can both be exact. Neither paper is proposing a new numerical method; both propose a different schedule for combining the same associative operation, so the choice of topology is a communication-engineering decision, not a sacrifice of exactness.
Failure Mode
The result requires disjoint score subsets from the same query row. Merging states from different queries, or double-counting a score across shards, produces the wrong without an error signal. IEEE 754 arithmetic also changes the result: ring and tree parenthesize rescaling and addition in different orders, so each can differ from a direct calculation by machine epsilon. The full attention output needs its separately scaled output accumulator in addition to this normalizer pair.
Common Confusions
Ring and Tree Attention do not approximate attention
Both compute the exact same attention output as single-device Flash Attention, up to floating-point rounding. They are not sparse or linear attention methods (which do trade exactness for speed); they only change how many communication rounds combining distributed partial results takes.
This does not replace Flash Attention
Ring Attention and Tree Attention both use Flash Attention's tiled, online-softmax computation within each device; they extend it across devices, they do not compete with it. A cluster running Tree Attention still runs Flash Attention locally on each GPU's shard.
Exercises
Problem
8 devices hold local partial states with pairs in that order. Compute the final merged using the ring schedule (fold left to right), then using a balanced binary tree, and confirm they agree.
Problem
Using the peak-memory formulas and , find the condition on (number of attention heads) under which Tree Attention's peak memory is strictly lower than Ring Attention's, and check whether that condition holds for a typical configuration with , .
Mental Model
Think of the local partial states as numbers that need repeated
pairwise merge operations. A ring is a minimum-parallelism schedule: it
serializes merges, one per round. A balanced tree performs up to
merges per round and uses rounds. This is the
same reduction pattern used for a parallel sum or max; Tree Attention
applies it to the online-softmax state from Flash Attention.
Scope
This module is about the reduction step's communication structure, not about sparse or linear attention approximations (those trade exactness for speed; ring and tree do not) and not about single-device tiling mechanics (covered in Flash Attention). Three current papers ground it: FlashAttention (Dao et al., NeurIPS 2022) for the online-softmax merge itself, Ring Attention (Liu, Zaharia, Abbeel, ICLR 2024) for the sequential-ring schedule, and Tree Attention (Shyam et al., 2024, revised 2025) for the topology-aware tree schedule and its measured speedup over Ring Attention.
References
Exact attention and online softmax:
- Dao, T., Fu, D. Y., Ermon, S., Rudra, A., Re, C. "FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness." NeurIPS 2022, arXiv:2205.14135. §§3.1–3.3 for tiled attention and the online-softmax recurrence.
- Milakov, M., Gimelshein, N. "Online normalizer calculation for softmax." arXiv:1805.02867. Algorithm 3 for the stable online normalizer.
- Rabe, M. N., Staats, C. "Self-attention Does Not Need Memory." arXiv:2112.05682. §3 for exact memory-efficient attention.
- Dao, T. "FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning." ICLR 2024, arXiv:2307.08691. §§3–4 for parallel work partitioning within exact attention.
Distributed schedules:
- Liu, H., Zaharia, M., Abbeel, P. "Ring Attention with Blockwise Transformers for Near-Infinite Context." ICLR 2024, arXiv:2310.01889. Algorithm 1 for ring rotation and computation-communication overlap.
- Shyam, V., Pilault, J., Shepperd, E., Anthony, Q., Millidge, B. "Tree Attention: Topology-aware Decoding for Long-Context Attention on GPU Clusters." arXiv:2408.04093. Theorem 1; §§6.1–6.3; Eqs. 8–9.
Last reviewed: July 12, 2026
Canonical graph
Required before and derived from this topic
These links come from prerequisite edges in the curriculum graph. Editorial suggestions are shown here only when the target page also cites this page as a prerequisite.
Required prerequisites
3- Attention Mechanism Theorylayer 4 · tier 2
- Flash Attentionlayer 5 · tier 2
- KV Cachelayer 5 · tier 2
Derived topics
0No published topic currently declares this as a prerequisite.