roce_tp4_adapt.py 1.9 KB

123456789101112131415161718192021
  1. """Adapt the SG17 RoCEnante overlay (TP8-only) to TP4 after `patch -p1` applied it.
  2. DSV41_ROCE_RING=1 loads b12x.comm.roce_ring (per-peer paths for a switchless ring, see
  3. docs/switchless-ring.md) instead of b12x.comm.roce; unset, the overlay is unchanged."""
  4. p = "/sgl-workspace/sglang/python/sglang/srt/distributed/device_communicators/pynccl.py"
  5. s = open(p).read()
  6. def rep(a, b):
  7. global s
  8. assert s.count(a) == 1, a
  9. s = s.replace(a, b)
  10. rep('if roce_enabled and os.environ.get("SGLANG8_ROCE_ALLREDUCE", "0") == "1":\n if self.world_size != 8 or isinstance(group, StatelessProcessGroup):\n raise RuntimeError("SG17 RoCEnante requires the TP8 Gloo exchange group")',
  11. 'if roce_enabled and (os.environ.get("SGLANG_ROCE_ALLREDUCE", "0") == "1"\n or os.environ.get("SGLANG8_ROCE_ALLREDUCE", "0") == "1"):\n if self.world_size not in (4, 8) or isinstance(group, StatelessProcessGroup):\n raise RuntimeError("RoCEnante route needs the TP4/TP8 Gloo exchange group")')
  12. rep('if len(self.roce.hca_names) != 2:\n raise RuntimeError("SG17 RoCEnante requires two active RoCE interfaces")',
  13. 'if len(self.roce.hca_names) < 1:\n raise RuntimeError("RoCEnante found no active RoCE interface")\n if len(self.roce.hca_names) == 1:\n logger.warning("ROCE single-rail: %s", self.roce.hca_names)')
  14. rep(' from b12x.comm import roce\n',
  15. ' import importlib\n'
  16. ' roce = importlib.import_module(\n'
  17. ' "b12x.comm.roce_ring" if os.environ.get("DSV41_ROCE_RING", "0") == "1" else "b12x.comm.roce")\n')
  18. rep('max_size=512 * 1024, max_gather_bytes=0,', 'max_size=int(os.environ.get("SGLANG_ROCE_MAX_SIZE", str(512 * 1024))), max_gather_bytes=0,')
  19. open(p, "w").write(s)
  20. import ast; ast.parse(s); print("pynccl.py adapted to TP4")