vllm_omni.diffusion.models.magi2.attention ¶
Native MAGI-2 packed attention with sinks and Ulysses exchange.
The sink and context-parallel math is adapted from SandAI's Apache-2.0 MAGI-2 preview implementation. This version uses vLLM's bundled FlashAttention extension and vLLM-Omni's existing Ulysses process group; the PyTorch path is an exact, portable oracle for small tests.
Magi2PackedAttentionKernel ¶
Bases: Module
Model kernel plugged into the shared diffusion Attention contract.
forward ¶
forward(
query: Tensor,
key: Tensor,
value: Tensor,
attn_metadata: AttentionMetadata | None = None,
) -> Tensor
VarlenHandler dataclass ¶
Packed-sequence metadata consumed by MAGI-2 attention.
apply_rotary_emb ¶
apply_rotary_emb(
x: Tensor,
cos: Tensor,
sin: Tensor,
*,
interleaved: bool = False,
) -> Tensor
Apply the released element-wise RoPE layout to [...,H,D].
correct_out_lse_with_sink ¶
correct_out_lse_with_sink(
out: Tensor, lse: Tensor, sink: Tensor | None
) -> tuple[Tensor, Tensor]
Add zero-valued attention sinks to an already-computed softmax.
FlashAttention returns out[T,H,D] and conventionally lse[H,T]. A MAGI sink contains additional logits [num_sink,H] whose values are zero vectors, so only the denominator and LSE change.
packed_attention_with_sink ¶
packed_attention_with_sink(
q: Tensor,
k: Tensor,
v: Tensor,
varlen: VarlenHandler,
*,
softcap: float = -1.0,
sink: Tensor | None = None,
) -> Tensor
Run packed attention on one rank after Ulysses head exchange.
torch_varlen_attention_with_sink ¶
torch_varlen_attention_with_sink(
q: Tensor,
k: Tensor,
v: Tensor,
*,
cu_seqlens_q: Tensor,
cu_seqlens_k: Tensor,
softcap: float = -1.0,
sink: Tensor | None = None,
) -> Tensor
Reference packed attention, including GQA and sink logits.
ulysses_packed_attention_with_sink ¶
ulysses_packed_attention_with_sink(
q: Tensor,
k: Tensor,
v: Tensor,
varlen: VarlenHandler,
split_sizes: list[int] | Tensor,
*,
softcap: float = -1.0,
sink: Tensor | None = None,
group: Magi2ParallelGroup | None = None,
) -> Tensor
MAGI-2 attention with overlapping Ulysses CP/head exchange.