Ring attention implementation with flash attention.
Low maintenance activity. Last release 15 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install ring-flash-attnRing Flash Attention
This repo implements RingAttention using FlashAttention. The current implementation supports:
- varlen (packing samples) api, corresponding to
flash_attn_varlen_func:ring_flash_attn_varlen_func: A basic implementation of ring attention.zigzag_ring_flash_attn_varlen_func: an more compute-balanced version of ring attention. More details in issue#2.llama3_flash_attn_varlen_func: The context parallelism used in llama3 tech report with extra design for varlen and low memory overhead. Although technically not ring attention, this is recommended for most varlen use cases, as it offers a less intrusive alternative for training frameworks with fewer data manipulations and better arithmetic precision.
- batch api, corresponding to
flash_attn_func:ring_flash_attn_func: basic ring attention.zigzag_ring_flash_attn_func: An more compute balanced version of ring attention, see issue#2.stripe_flash_attn_func: Stripe attention version ofring_flash_attn_func, the block size is set to 1 to use flash_attn api, see: https://arxiv.org/abs/2311.09431
- huggingface model adapter. Here is an example to use the adapter:
# torchrun --nproc_per_node=2 this_script.py
import torch
from ring_flash_attn import substitute_hf_flash_attn, update_ring_flash_attn_params
from torch import distributed as dist
from transformers import AutoModelForCausalLM, AutoTokenizer
def main():
# Initialize distributed training
dist.init_process_group(backend="nccl")
# Get rank and world size
rank = dist.get_rank()
world_size = dist.get_world_size()
# Set device
torch.cuda.set_device(rank)
device = torch.device(f"cuda:{rank}")
# Load model and tokenizer
model = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen3-0.6B", attn_implementation="flash_attention_2", torch_dtype=torch.bfloat16, device_map=device
)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
# Create group and substitute flash attention
group = dist.new_group(ranks=range(world_size), backend="nccl")
substitute_hf_flash_attn(group, heads_k_stride=1)
# Get the ring attention rank
ring_attn_rank = dist.get_rank(group=group) # only one group for ring attention here: this should be the same as rank
# Tokenize input and prepare position IDs
input_ids = tokenizer(["Lorem ipsum dolor sit", "amet, consectetur adipiscing", "elit, sed do"]).input_ids
lengths = [len(seq) for seq in input_ids]
input_ids = torch.cat([torch.tensor(seq, device=device) for seq in input_ids]).unsqueeze(0)
position_ids = torch.cat([torch.arange(length, device=device) for length in lengths]).unsqueeze(0)
# Compute cu_seqlens and update parameters
cu_seqlens = torch.cat([torch.tensor([0], device=device), torch.cumsum(torch.tensor(lengths, device=device), dim=0)]).to(torch.int32)
update_ring_flash_attn_params(cu_seqlens, group)
# Chunk input_ids and position_ids
input_ids = torch.chunk(input_ids, world_size, dim=1)[ring_attn_rank]
position_ids = torch.chunk(position_ids, world_size, dim=1)[ring_attn_rank]
output = model(input_ids=input_ids, position_ids=position_ids)
# Clean up
dist.destroy_process_group()
if __name__ == "__main__":
main()