nemo_automodel.components.speculative.eagle.zigzag_ring_attention
nemo_automodel.components.speculative.eagle.zigzag_ring_attention
Load-balanced (zig-zag) ring FlashAttention over a context-parallel group.
The sequence is chunked into 2 * cp pieces; rank r owns chunk r (early)
and chunk 2*cp-1-r (late). Pairing an early + late chunk balances the causal
triangle so every rank does equal work — unlike a contiguous shard where the last
rank does ~2x. Varlen (cu_seqlens) throughout, matching the FlashAttention
varlen kernels; causal=True only (zig-zag is meaningless otherwise).
Module Contents
Classes
Functions
API
P2P ring: send to rank+1, receive from rank-1 (cp group order).
Bases: Function
Autograd wrapper around the zig-zag ring varlen forward/backward.
One flash varlen forward block; front/back half uses the halved cu_seqlens.
Index selecting the front (early) or back (late) half of each varlen document.
Front/back half of a [num_heads, seqlen] lse, per varlen document.
Drop a leading batch dim of size 1 from each tensor (varlen tensors are unbatched).
Online-softmax merge; also returns sigmoid(block_lse - lse) for the backward.
Backward via the merged-lse trick (zhuzilin): each block’s flash backward is fed
the FINAL merged out/lse, so the joint-softmax gradient (both the output and the
normalization/lse paths) is captured without a separate merge backward. ms-swift’s
lse_grad variant drops the lse path, which skews dq/dk.
Zig-zag ring forward: rotate K/V, per-block flash, online-softmax merge -> (out, lse).
Load-balanced causal ring FlashAttention. q/k/v are [1, S_local, H, D] (varlen packed).