nccl_sweep.py 1.0 KB

1234567891011121314151617181920
  1. """8-rank bf16 all-reduce bandwidth sweep for choosing NCCL protocol/channels (prefill-sized messages)."""
  2. import datetime, json, os, sys, torch, torch.distributed as dist
  3. torch.cuda.set_device(0)
  4. dist.init_process_group("nccl", timeout=datetime.timedelta(minutes=5), device_id=torch.device("cuda:0"))
  5. rank, world = dist.get_rank(), dist.get_world_size()
  6. rows = []
  7. for mb in (1, 4, 16, 50, 100):
  8. x = torch.ones(mb * 1024 * 1024 // 2, device="cuda", dtype=torch.bfloat16)
  9. for _ in range(3): dist.all_reduce(x)
  10. torch.cuda.synchronize(); dist.barrier()
  11. s, e = torch.cuda.Event(True), torch.cuda.Event(True); n = 10
  12. s.record()
  13. for _ in range(n): dist.all_reduce(x)
  14. e.record(); e.synchronize()
  15. ms = s.elapsed_time(e) / n
  16. busbw = 2 * (world - 1) / world * mb * 1.048576e6 / (ms / 1e3) / 1e9
  17. rows.append({"MB": mb, "ms": round(ms, 3), "busbw_GBps": round(busbw, 1)})
  18. if rank == 0:
  19. print(json.dumps({"proto": os.environ.get("NCCL_PROTO"), "nch": os.environ.get("NCCL_MAX_NCHANNELS"), "rows": rows}))
  20. dist.destroy_process_group()