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.

rt_kernel.h 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_RTS_RT_KERNEL_H
  17. #define MINDSPORE_CCSRC_KERNEL_RTS_RT_KERNEL_H
  18. #include <vector>
  19. #include <utility>
  20. #include <memory>
  21. #include <map>
  22. #include <string>
  23. #include "kernel/ascend_kernel_mod.h"
  24. #include "kernel/task_stream.h"
  25. namespace mindspore {
  26. namespace kernel {
  27. class RtKernel : public AscendKernelMod {
  28. public:
  29. RtKernel();
  30. ~RtKernel() override;
  31. virtual bool Init(const AnfNodePtr &anf_node);
  32. const std::vector<size_t> &GetInputSizeList() const override;
  33. const std::vector<size_t> &GetOutputSizeList() const override;
  34. const std::vector<size_t> &GetWorkspaceSizeList() const override;
  35. protected:
  36. mutable std::vector<size_t> input_size_list_;
  37. mutable std::vector<size_t> output_size_list_;
  38. mutable std::vector<size_t> workspace_size_list_;
  39. };
  40. using RTKernelPtr = std::shared_ptr<RtKernel>;
  41. using RtKernelCreater = std::function<std::shared_ptr<RtKernel>()>;
  42. class RtKernelFactory {
  43. RtKernelFactory() = default;
  44. ~RtKernelFactory() = default;
  45. public:
  46. static RtKernelFactory &Get();
  47. void Registe(const std::string &name, RtKernelCreater &&fun);
  48. static std::shared_ptr<RtKernel> Create(const std::string &name);
  49. private:
  50. std::map<string, RtKernelCreater> fmap_;
  51. };
  52. class _RtKernelRegister {
  53. public:
  54. _RtKernelRegister(const std::string &name, RtKernelCreater &&fun) {
  55. RtKernelFactory::Get().Registe(name, std::move(fun));
  56. }
  57. ~_RtKernelRegister() = default;
  58. };
  59. #define _MS_REG_RTKERNEL_REG(KNAME, clazz) \
  60. static_assert(std::is_base_of<RtKernel, clazz>::value, " must be base of RtKernel"); \
  61. static const _RtKernelRegister g_##KNAME##_##_RtKernel_reg(#KNAME, []() { return std::make_shared<clazz>(); });
  62. #define MS_REG_RTKERNEL(KNAME, clazz) _MS_REG_RTKERNEL_REG(KNAME, clazz)
  63. } // namespace kernel
  64. } // namespace mindspore
  65. #endif // MINDSPORE_CCSRC_KERNEL_RTS_RT_KERNEL_H