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.

launcher.py 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 multiprocessing as mp
  10. from ..device import get_device_count
  11. from .group import init_process_group
  12. from .server import Server
  13. from .util import get_free_ports
  14. def _get_device_count():
  15. """use subprocess to avoid cuda environment initialization in the main process"""
  16. def run(q):
  17. count = get_device_count("gpu")
  18. q.put(count)
  19. q = mp.Queue()
  20. p = mp.Process(target=run, args=(q,))
  21. p.start()
  22. p.join()
  23. return q.get()
  24. def _run_wrapped(func, master_ip, port, world_size, rank, dev, args, kwargs):
  25. """init distributed process group and run wrapped function"""
  26. init_process_group(
  27. master_ip=master_ip, port=port, world_size=world_size, rank=rank, device=dev
  28. )
  29. func(*args, **kwargs)
  30. def launcher(n_gpus):
  31. """decorator for launching multiple processes in single-machine multi-gpu training"""
  32. count = _get_device_count()
  33. assert isinstance(n_gpus, int) and n_gpus > 1, "invalid n_gpus"
  34. assert n_gpus <= count, "{} gpus required, {} gpus provided".format(n_gpus, count)
  35. def decorator(func):
  36. def wrapper(*args, **kwargs):
  37. master_ip = "localhost"
  38. port = get_free_ports(1)[0]
  39. server = Server(port)
  40. procs = []
  41. for rank in range(n_gpus):
  42. p = mp.Process(
  43. target=_run_wrapped,
  44. args=(func, master_ip, port, n_gpus, rank, rank, args, kwargs),
  45. )
  46. p.start()
  47. procs.append(p)
  48. for rank in range(n_gpus):
  49. procs[rank].join()
  50. code = procs[rank].exitcode
  51. assert code == 0, "subprocess {} exit with code {}".format(rank, code)
  52. return wrapper
  53. return decorator

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