Install ring-attention-pytorch
mainInstall the package via pip to use Ring Attention, Striped Attention, and Tree Attention Decoding implementations.
$ pip install ring-attention-pytorchrepository·main·Indexed 19 days ago
https://github.com/lucidrains/ring-attention-pytorchA PyTorch implementation of Ring Attention designed for extremely long sequences by splitting data across the sequence dimension. It includes the RingAttention class for ring reduce attention matrix tiles, Striped Attention for workload balancing, and the tree_attn_decode function for Tree Attention Decoding.
Install the package via pip to use Ring Attention, Striped Attention, and Tree Attention Decoding implementations.
$ pip install ring-attention-pytorchTo test the implementation, first install the requirements, then run the assertion scripts for specific attention types.
Testing autoregressive striped ring attention on CUDA:
$ python assert.py --use-cuda --causal --striped-ring-attnTesting tree attention:
$ python assert_tree_attn.py --use-cuda --seq-len 8192$ pip install -r requirements.txt
$ python assert.py --use-cuda --causal --striped-ring-attn
$ python assert_tree_attn.py --use-cuda --seq-len 8192The RingAttention class implements Ring Attention by splitting data across the sequence dimension and applying ring reduce to attention matrix tiles. It supports features like causal masking, automatic sequence sharding, and grouped query attention.
Key arguments for RingAttention:
dim: Dimension of the input tokens.dim_head: Dimension of each attention head.heads: Number of attention heads.causal: Boolean, whether to use causal masking.auto_shard_seq: Boolean, whether to automatically shard the sequence.ring_attn: Boolean, enables ring attention logic.ring_seq_size: The size of the sequence chunks used in the ring reduction.import torch
from ring_attention_pytorch import RingAttention
attn = RingAttention(
dim = 512,
dim_head = 64,
heads = 8,
causal = True,
auto_shard_seq = True,
ring_attn = True,
ring_seq_size = 512
)
tokens = torch.randn(1, 1024, 512)
attended = attn(tokens)
assert attended.shape == tokens.shapeThe tree_attn_decode function implements Tree Attention Decoding. This function expects the query (q), key (k), and value (v) tensors to already exist across all machines in a distributed environment.
from ring_attention_pytorch import tree_attn_decode
out = tree_attn_decode(q, k, v) # where q, k, v exists across all machines