|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved. |
| 4 | + |
| 5 | +""" |
| 6 | +Minimal example demonstrating ring attention using the RingAttention layer. |
| 7 | +
|
| 8 | +The sequence is split evenly across GPUs along the sequence dimension. |
| 9 | +Each rank computes its share of the attention output. After the ring passes |
| 10 | +Q and V are combined via online-softmax, yielding the same result as a single |
| 11 | +device running full attention on the entire sequence. |
| 12 | +
|
| 13 | +Usage:: |
| 14 | +
|
| 15 | + # Run on 2 GPUs (default) |
| 16 | + python examples/32_ring_attention/example_run.py |
| 17 | +
|
| 18 | + # Run on 4 GPUs |
| 19 | + python examples/32_ring_attention/example_run.py --num_ranks 4 |
| 20 | +
|
| 21 | + # Non-causal (bidirectional) attention |
| 22 | + python examples/32_ring_attention/example_run.py --no_causal |
| 23 | +""" |
| 24 | + |
| 25 | +import argparse |
| 26 | + |
| 27 | +import torch |
| 28 | +import torch.distributed as dist |
| 29 | +import torch.multiprocessing as mp |
| 30 | + |
| 31 | +import iris |
| 32 | +from ring_attention_layer import RingAttention |
| 33 | + |
| 34 | + |
| 35 | +def parse_args(): |
| 36 | + parser = argparse.ArgumentParser(description="Ring Attention example") |
| 37 | + parser.add_argument("--total_seq_len", type=int, default=4096, help="Total sequence length (split across GPUs)") |
| 38 | + parser.add_argument("--num_heads", type=int, default=16, help="Number of attention heads") |
| 39 | + parser.add_argument("--head_dim", type=int, default=64, help="Head dimension") |
| 40 | + parser.add_argument("--num_ranks", type=int, default=2, help="Number of GPUs") |
| 41 | + parser.add_argument("--no_causal", action="store_true", help="Use bidirectional (non-causal) attention") |
| 42 | + parser.add_argument( |
| 43 | + "--dtype", |
| 44 | + type=str, |
| 45 | + default="float16", |
| 46 | + choices=["float16", "bfloat16"], |
| 47 | + help="Input tensor dtype", |
| 48 | + ) |
| 49 | + return parser.parse_args() |
| 50 | + |
| 51 | + |
| 52 | +def run(rank: int, world_size: int, init_url: str, args): |
| 53 | + backend = "nccl" if torch.cuda.is_available() else "gloo" |
| 54 | + dist.init_process_group( |
| 55 | + backend=backend, |
| 56 | + init_method=init_url, |
| 57 | + world_size=world_size, |
| 58 | + rank=rank, |
| 59 | + device_id=torch.device(f"cuda:{rank}"), |
| 60 | + ) |
| 61 | + |
| 62 | + shmem = iris.iris() |
| 63 | + torch.manual_seed(42) |
| 64 | + torch.set_default_device("cuda") |
| 65 | + |
| 66 | + dtype = getattr(torch, args.dtype) |
| 67 | + causal = not args.no_causal |
| 68 | + |
| 69 | + seq_local = args.total_seq_len // world_size |
| 70 | + num_heads = args.num_heads |
| 71 | + head_dim = args.head_dim |
| 72 | + |
| 73 | + if rank == 0: |
| 74 | + attn_type = "causal" if causal else "bidirectional" |
| 75 | + print(f"--- Ring Attention Example ({attn_type}) ---") |
| 76 | + print(f" GPUs : {world_size}") |
| 77 | + print(f" Total seq len : {args.total_seq_len}") |
| 78 | + print(f" Seq per GPU : {seq_local}") |
| 79 | + print(f" Heads × dim : {num_heads} × {head_dim}") |
| 80 | + print(f" dtype : {dtype}") |
| 81 | + |
| 82 | + # Each rank creates its local Q, K, V chunk |
| 83 | + q = torch.randn(seq_local, num_heads, head_dim, dtype=dtype) |
| 84 | + k = torch.randn(seq_local, num_heads, head_dim, dtype=dtype) |
| 85 | + v = torch.randn(seq_local, num_heads, head_dim, dtype=dtype) |
| 86 | + |
| 87 | + shmem.barrier() |
| 88 | + |
| 89 | + layer = RingAttention(shmem, num_heads=num_heads, head_dim=head_dim, causal=causal) |
| 90 | + |
| 91 | + # Warm-up pass |
| 92 | + _ = layer(q, k, v) |
| 93 | + torch.cuda.synchronize() |
| 94 | + shmem.barrier() |
| 95 | + |
| 96 | + # Timed pass |
| 97 | + start = torch.cuda.Event(enable_timing=True) |
| 98 | + end = torch.cuda.Event(enable_timing=True) |
| 99 | + |
| 100 | + start.record() |
| 101 | + output = layer(q, k, v) |
| 102 | + end.record() |
| 103 | + |
| 104 | + torch.cuda.synchronize() |
| 105 | + elapsed_ms = start.elapsed_time(end) |
| 106 | + |
| 107 | + if rank == 0: |
| 108 | + print(f"\nOutput shape : {output.shape}") |
| 109 | + print(f"Output dtype : {output.dtype}") |
| 110 | + print(f"Elapsed time : {elapsed_ms:.2f} ms") |
| 111 | + print(f"Output[0, 0, :4] = {output[0, 0, :4].float()}") |
| 112 | + |
| 113 | + shmem.barrier() |
| 114 | + dist.destroy_process_group() |
| 115 | + |
| 116 | + |
| 117 | +def main(): |
| 118 | + args = parse_args() |
| 119 | + init_url = "tcp://127.0.0.1:29500" |
| 120 | + mp.spawn( |
| 121 | + fn=run, |
| 122 | + args=(args.num_ranks, init_url, args), |
| 123 | + nprocs=args.num_ranks, |
| 124 | + join=True, |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments