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

util.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import functools
  10. from typing import Callable, Optional
  11. import megengine._internal as mgb
  12. from ..core import set_default_device
  13. _master_ip = None
  14. _master_port = 0
  15. _world_size = 0
  16. _rank = 0
  17. _backend = None
  18. def init_process_group(
  19. master_ip: str,
  20. master_port: int,
  21. world_size: int,
  22. rank: int,
  23. dev: int,
  24. backend: Optional[str] = "nccl",
  25. ) -> None:
  26. """Initialize the distributed process group, and also specify the device used in the current process.
  27. :param master_ip: IP address of the master node.
  28. :param master_port: Port available for all processes to communicate.
  29. :param world_size: Total number of processes participating in the job.
  30. :param rank: Rank of the current process.
  31. :param dev: The GPU device id to bind this process to.
  32. :param backend: Communicator backend, currently support 'nccl' and 'ucx'
  33. """
  34. global _master_ip # pylint: disable=global-statement
  35. global _master_port # pylint: disable=global-statement
  36. global _world_size # pylint: disable=global-statement
  37. global _rank # pylint: disable=global-statement
  38. global _backend # pylint: disable=global-statement
  39. if not isinstance(master_ip, str):
  40. raise TypeError("Expect type str but got {}".format(type(master_ip)))
  41. if not isinstance(master_port, int):
  42. raise TypeError("Expect type int but got {}".format(type(master_port)))
  43. if not isinstance(world_size, int):
  44. raise TypeError("Expect type int but got {}".format(type(world_size)))
  45. if not isinstance(rank, int):
  46. raise TypeError("Expect type int but got {}".format(type(rank)))
  47. if not isinstance(backend, str):
  48. raise TypeError("Expect type str but got {}".format(type(backend)))
  49. _master_ip = master_ip
  50. _master_port = master_port
  51. _world_size = world_size
  52. _rank = rank
  53. _backend = backend
  54. set_default_device(mgb.comp_node("gpu" + str(dev)))
  55. if rank == 0:
  56. _master_port = mgb.config.create_mm_server("0.0.0.0", master_port)
  57. if _master_port == -1:
  58. raise Exception("Failed to start server on port {}".format(master_port))
  59. else:
  60. assert master_port > 0, "master_port must be specified for non-zero rank"
  61. def is_distributed() -> bool:
  62. """Return True if the distributed process group has been initialized"""
  63. return _world_size is not None and _world_size > 1
  64. def get_master_ip() -> str:
  65. """Get the IP address of the master node"""
  66. return str(_master_ip)
  67. def get_master_port() -> int:
  68. """Get the port of the rpc server on the master node"""
  69. return _master_port
  70. def get_world_size() -> int:
  71. """Get the total number of processes participating in the job"""
  72. return _world_size
  73. def get_rank() -> int:
  74. """Get the rank of the current process"""
  75. return _rank
  76. def get_backend() -> str:
  77. """Get the backend str"""
  78. return str(_backend)
  79. def group_barrier() -> None:
  80. """Block until all ranks in the group reach this barrier"""
  81. mgb.config.group_barrier(_master_ip, _master_port, _world_size, _rank)
  82. def synchronized(func: Callable):
  83. """Decorator. Decorated function will synchronize when finished.
  84. Specifically, we use this to prevent data race during hub.load"""
  85. @functools.wraps(func)
  86. def _(*args, **kwargs):
  87. if not is_distributed():
  88. return func(*args, **kwargs)
  89. ret = func(*args, **kwargs)
  90. group_barrier()
  91. return ret
  92. return _

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台