Create all_reduce.py
Browse files- all_reduce.py +16 -0
all_reduce.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.distributed as dist
|
3 |
+
|
4 |
+
def init_process():
|
5 |
+
dist.init_process_group(backend="nccl")
|
6 |
+
torch.cuda.set_device(dist.get_rank())
|
7 |
+
|
8 |
+
def example_reduce():
|
9 |
+
tensor = torch.tensor([dist.get_rank()] * 4, dtype=torch.float32).cuda()
|
10 |
+
print(f"Before reduce on rank {dist.get_rank()}: {tensor}")
|
11 |
+
dist.all_reduce(tensor, op=dist.ReduceOp.SUM)
|
12 |
+
print(f"After reduce on rank {dist.get_rank()}: {tensor}")
|
13 |
+
|
14 |
+
init_process()
|
15 |
+
example_reduce()
|
16 |
+
dist.destroy_process_group()
|