Download broadcast.py from ariG23498/distributed: direct link, hf CLI and curl.
- Browser
- Download file 583 Bytes
-
https://huggingface.co/datasets/ariG23498/distributed/resolve/main/broadcast.py
- Command line
-
hf download hf://datasets/ariG23498/distributed/broadcast.py
-
curl -L -o broadcast.py https://huggingface.co/datasets/ariG23498/distributed/resolve/main/broadcast.py
583 Bytes
| import torch | |
| import torch.distributed as dist | |
| def init_process(): | |
| dist.init_process_group(backend="nccl") | |
| torch.cuda.set_device(dist.get_rank()) | |
| def example_broadcast(): | |
| if dist.get_rank() == 0: | |
| tensor = torch.tensor([1, 2, 3, 4], dtype=torch.float32).cuda() | |
| else: | |
| tensor = torch.zeros(4, dtype=torch.float32).cuda() | |
| print(f"Before broadcast on rank {dist.get_rank()}: {tensor}") | |
| dist.broadcast(tensor, src=0) | |
| print(f"After broadcast on rank {dist.get_rank()}: {tensor}") | |
| init_process() | |
| example_broadcast() | |
| dist.destroy_process_group() |