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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. rank: Optional[int] = None,
  19. root: Optional[int] = 0,
  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 rank: rank of the current process, use util.get_rank() as default
  30. :param root: rank of root node, use 0 as default
  31. :param dtype: output data type, use dtype of inp as default
  32. :param device: output comp node, use comp node of inp as default
  33. :param comp_graph: output comp graph, use comp graph of inp as default
  34. """
  35. return mgb.opr.collective_comm(
  36. inp,
  37. key=str(key),
  38. nr_devices=nr_ranks if nr_ranks is not None else get_world_size(),
  39. rank=rank if rank is not None else get_rank(),
  40. root=root,
  41. server_addr=get_master_ip(),
  42. port=get_master_port(),
  43. param=CollParam(mode=op),
  44. dtype=dtype,
  45. backend=get_backend(),
  46. comp_node=device,
  47. comp_graph=comp_graph,
  48. )

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