ariG23498 HF Staff commited on
Commit
51adf3e
·
verified ·
1 Parent(s): f01572e

Update broadcast.py

Browse files
Files changed (1) hide show
  1. broadcast.py +20 -0
broadcast.py CHANGED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_broadcast():
9
+ if dist.get_rank() == 0:
10
+ tensor = torch.tensor([1, 2, 3, 4], dtype=torch.float32).cuda()
11
+ else:
12
+ tensor = torch.zeros(4, dtype=torch.float32).cuda()
13
+
14
+ print(f"Before broadcast on rank {dist.get_rank()}: {tensor}")
15
+ dist.broadcast(tensor, src=0)
16
+ print(f"After broadcast on rank {dist.get_rank()}: {tensor}")
17
+
18
+ init_process()
19
+ example_broadcast()
20
+ dist.destroy_process_group()