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.

akg_kernel_build.h 3.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_AKG_AKGKERNELBUILD_H_
  17. #define MINDSPORE_CCSRC_KERNEL_AKG_AKGKERNELBUILD_H_
  18. #include <unordered_map>
  19. #include <string>
  20. #include <vector>
  21. #include <memory>
  22. #include <map>
  23. #include <utility>
  24. #include "kernel/kernel.h"
  25. #include "ir/dtype.h"
  26. #include <nlohmann/json.hpp>
  27. #include "kernel/common_utils.h"
  28. #include "kernel/oplib/oplib.h"
  29. namespace mindspore {
  30. namespace kernel {
  31. class AkgKernelBuild {
  32. public:
  33. AkgKernelBuild() {
  34. input_tensor_idx_ = {};
  35. output_tensor_idx_ = 0;
  36. }
  37. ~AkgKernelBuild() = default;
  38. KernelPackPtr BuildByJson(const AnfNodePtr &anf_node, std::vector<size_t> *const input_size,
  39. std::vector<size_t> *const output_size);
  40. static std::string GetProcessor(const AnfNodePtr &anf_node);
  41. static std::string PyObjectToStr(PyObject *const PyObj);
  42. protected:
  43. bool CreateInputDescJson(const AnfNodePtr &anf_node, nlohmann::json *const inputs_json);
  44. bool CreateOutputDescJson(const AnfNodePtr &anf_node, nlohmann::json *const outputs_json);
  45. bool CreateAttrDescJson(const AnfNodePtr &anf_node, const std::string &op_name,
  46. const std::shared_ptr<OpInfo> &op_info, nlohmann::json *const attrs_json);
  47. KernelPackPtr OpBuild(const std::string &node_json, const AnfNodePtr &anf_node);
  48. int GetOpCntInc();
  49. size_t GetInputTensorIdxInc(const AnfNodePtr &anf_node, size_t input_idx);
  50. size_t GetOutputTensorIdxInc();
  51. bool GenerateSingleKernelJson(const AnfNodePtr &anf_node, const std::string &op_name,
  52. nlohmann::json *const node_json);
  53. static int op_cnt_;
  54. // lock for variable fusionOpCnt in singleton mode
  55. static std::mutex op_cnt_mtx_;
  56. std::string json_name_;
  57. std::string json_info_;
  58. std::unordered_map<AnfNodePtr, size_t> input_tensor_idx_;
  59. size_t output_tensor_idx_;
  60. };
  61. bool GetIOSize(const nlohmann::json &node_json, std::vector<size_t> *const input_size,
  62. std::vector<size_t> *const output_size);
  63. void SetTensorName(const std::string &tag, const std::string &new_name, const std::pair<size_t, size_t> &position,
  64. nlohmann::json *const node_json);
  65. std::string GetTensorName(const nlohmann::json &node_json, const std::string &tag,
  66. const std::pair<size_t, size_t> &position);
  67. } // namespace kernel
  68. } // namespace mindspore
  69. #endif // MINDSPORE_CCSRC_KERNEL_AKG_AKGKERNELBUILD_H_