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.

helper.py 2.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. from typing import Optional, Union
  10. import megengine._internal as mgb
  11. from megengine._internal.opr_param_defs import CollectiveComm as CollParam
  12. from .util import get_backend, get_master_ip, get_master_port, get_rank, get_world_size
  13. def collective_comm_symvar(
  14. inp: Union[mgb.SymbolVar, mgb.CompGraph],
  15. key: str,
  16. op: CollParam.Mode,
  17. nr_ranks: Optional[int] = None,
  18. is_root: Optional[bool] = None,
  19. rank: Optional[int] = None,
  20. dtype: Optional[type] = None,
  21. device: Optional[mgb.CompNode] = None,
  22. comp_graph: Optional[mgb.CompGraph] = None,
  23. ) -> mgb.SymbolVar:
  24. """Helper function for creating collective_comm operators
  25. :param inp: tensor or comp_graph
  26. :param key: unique identifier for collective communication
  27. :param op: mode of collective communication
  28. :param nr_ranks: number of ranks, use util.get_world_size() as default
  29. :param is_root: whether this node is root node
  30. :param dtype: output data type, use dtype of inp as default
  31. :param device: output comp node, use comp node of inp as default
  32. :param comp_graph: output comp graph, use comp graph of inp as default
  33. """
  34. return mgb.opr.collective_comm(
  35. inp,
  36. key=str(key),
  37. nr_devices=nr_ranks if nr_ranks is not None else get_world_size(),
  38. is_root=is_root if is_root is not None else (get_rank() == 0),
  39. rank=rank if rank is not None else -1,
  40. server_addr=get_master_ip(),
  41. port=get_master_port(),
  42. param=CollParam(mode=op),
  43. dtype=dtype,
  44. backend=get_backend(),
  45. comp_node=device,
  46. comp_graph=comp_graph,
  47. )

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