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.

hccl_kernel.h 3.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_KERNEL_HCCL_HCCL_KERNEL_H_
  17. #define MINDSPORE_CCSRC_KERNEL_HCCL_HCCL_KERNEL_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include <algorithm>
  23. #include <utility>
  24. #include "kernel/ascend_kernel_mod.h"
  25. #include "kernel/hccl/hcom_util.h"
  26. #include "hccl/hcom.h"
  27. #include "common/utils.h"
  28. namespace mindspore {
  29. namespace kernel {
  30. class HcclKernel : public AscendKernelMod {
  31. public:
  32. HcclKernel();
  33. ~HcclKernel() override;
  34. virtual bool Init(const AnfNodePtr &anf_node);
  35. const std::vector<size_t> &GetInputSizeList() const override;
  36. const std::vector<size_t> &GetOutputSizeList() const override;
  37. const std::vector<size_t> &GetWorkspaceSizeList() const override;
  38. std::vector<TaskInfoPtr> GenTask(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
  39. const std::vector<AddressPtr> &outputs, uint32_t stream_id) override;
  40. protected:
  41. std::vector<std::vector<size_t>> hccl_kernel_input_shape_list_;
  42. std::vector<std::vector<size_t>> hccl_kernel_output_shape_list_;
  43. std::vector<hcclDataType_t> hccl_data_type_list_;
  44. std::vector<std::string> hccl_format_list_;
  45. uint64_t hccl_count_;
  46. hcclRedOp_t op_type_;
  47. uint32_t root_id_;
  48. mutable std::vector<size_t> input_size_list_;
  49. mutable std::vector<size_t> output_size_list_;
  50. mutable std::vector<size_t> workspace_size_list_;
  51. AnfNodePtr anf_node_;
  52. std::string op_name_;
  53. std::string group_;
  54. };
  55. using HcclKernelCreater = std::function<std::shared_ptr<HcclKernel>()>;
  56. class HcclKernelFactory {
  57. HcclKernelFactory() = default;
  58. ~HcclKernelFactory() = default;
  59. public:
  60. static HcclKernelFactory &Get();
  61. void Registe(const string &name, HcclKernelCreater &&fun);
  62. static std::shared_ptr<HcclKernel> Get(const string &name);
  63. private:
  64. std::map<string, HcclKernelCreater> hcclKernelMap_;
  65. };
  66. class _HcclKernelRegister {
  67. public:
  68. _HcclKernelRegister(const string &name, HcclKernelCreater &&fun) {
  69. HcclKernelFactory::Get().Registe(name, std::move(fun));
  70. }
  71. ~_HcclKernelRegister() = default;
  72. };
  73. #define _MS_HCCL_REG_KERNEL_REG(KNAME, clazz) \
  74. static_assert(std::is_base_of<HcclKernel, clazz>::value, " must be base of HcclKernel"); \
  75. static const _HcclKernelRegister g_##KNAME##_##_kernel_reg(#KNAME, []() { \
  76. std::shared_ptr<clazz> ptr = nullptr; \
  77. ptr = std::make_shared<clazz>(); \
  78. MS_EXCEPTION_IF_NULL(ptr); \
  79. return ptr; \
  80. });
  81. #define MS_HCCL_REG_KERNEL(KNAME, clazz) _MS_HCCL_REG_KERNEL_REG(KNAME, clazz)
  82. } // namespace kernel
  83. } // namespace mindspore
  84. #endif