Meta Byte Track
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup_env.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  4. import cv2
  5. import os
  6. import subprocess
  7. __all__ = ["configure_nccl", "configure_module"]
  8. def configure_nccl():
  9. """Configure multi-machine environment variables of NCCL."""
  10. os.environ["NCCL_LAUNCH_MODE"] = "PARALLEL"
  11. os.environ["NCCL_IB_HCA"] = subprocess.getoutput(
  12. "pushd /sys/class/infiniband/ > /dev/null; for i in mlx5_*; "
  13. "do cat $i/ports/1/gid_attrs/types/* 2>/dev/null "
  14. "| grep v >/dev/null && echo $i ; done; popd > /dev/null"
  15. )
  16. os.environ["NCCL_IB_GID_INDEX"] = "3"
  17. os.environ["NCCL_IB_TC"] = "106"
  18. def configure_module(ulimit_value=8192):
  19. """
  20. Configure pytorch module environment. setting of ulimit and cv2 will be set.
  21. Args:
  22. ulimit_value(int): default open file number on linux. Default value: 8192.
  23. """
  24. # system setting
  25. try:
  26. import resource
  27. rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
  28. resource.setrlimit(resource.RLIMIT_NOFILE, (ulimit_value, rlimit[1]))
  29. except Exception:
  30. # Exception might be raised in Windows OS or rlimit reaches max limit number.
  31. # However, set rlimit value might not be necessary.
  32. pass
  33. # cv2
  34. # multiprocess might be harmful on performance of torch dataloader
  35. os.environ["OPENCV_OPENCL_RUNTIME"] = "disabled"
  36. try:
  37. cv2.setNumThreads(0)
  38. cv2.ocl.setUseOpenCL(False)
  39. except Exception:
  40. # cv2 version mismatch might rasie exceptions.
  41. pass