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.

opinfo.h 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_BACKEND_KERNEL_COMPILER_OPLIB_OPINFO_H_
  17. #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_OPLIB_OPINFO_H_
  18. #include <vector>
  19. #include <string>
  20. #include <memory>
  21. #include <unordered_map>
  22. #include "ir/dtype.h"
  23. #include "backend/kernel_compiler/kernel.h"
  24. namespace mindspore {
  25. namespace kernel {
  26. enum OpImplyType { kAKG = 0, kTBE = 1, kAICPU };
  27. enum OpIOType { kInput = 0, kOutput };
  28. class OpAttr {
  29. public:
  30. OpAttr() = default;
  31. ~OpAttr() = default;
  32. std::string name() const { return name_; }
  33. std::string param_type() const { return param_type_; }
  34. std::string type() const { return type_; }
  35. std::string value() const { return value_; }
  36. std::string default_value() const { return default_value_; }
  37. void set_name(const std::string &name) { name_ = name; }
  38. void set_param_type(const std::string &param_type) { param_type_ = param_type; }
  39. void set_type(const std::string &type) { type_ = type; }
  40. void set_value(const std::string &value) { value_ = value; }
  41. void set_default_value(const std::string &default_value) { default_value_ = default_value; }
  42. private:
  43. std::string name_;
  44. std::string param_type_;
  45. std::string type_;
  46. std::string value_;
  47. std::string default_value_;
  48. };
  49. class OpIOInfo {
  50. public:
  51. OpIOInfo() = default;
  52. ~OpIOInfo() = default;
  53. int index() const { return index_; }
  54. const std::string &name() const { return name_; }
  55. bool need_compile() const { return need_compile_; }
  56. const std::string &param_type() const { return param_type_; }
  57. const std::string &reshape_type() const { return reshape_type_; }
  58. const std::string &shape() const { return shape_; }
  59. const std::vector<std::string> &dtypes() const { return dtypes_; }
  60. const std::vector<std::string> &formats() const { return formats_; }
  61. void set_index(const int index) { index_ = index; }
  62. void set_name(const std::string &name) { name_ = name; }
  63. void set_need_compile(const bool need_compile) { need_compile_ = need_compile; }
  64. void set_param_type(const std::string &param_type) { param_type_ = param_type; }
  65. void set_reshape_type(const std::string &reshape_type) { reshape_type_ = reshape_type; }
  66. void set_shape(const std::string &shape) { shape_ = shape; }
  67. void set_dtypes(const std::vector<std::string> &dtype) { dtypes_ = dtype; }
  68. void set_formats(const std::vector<std::string> &formats) { formats_ = formats; }
  69. private:
  70. int index_ = 0;
  71. std::string name_;
  72. bool need_compile_ = false;
  73. std::string param_type_;
  74. std::string reshape_type_;
  75. std::string shape_;
  76. std::vector<std::string> dtypes_;
  77. std::vector<std::string> formats_;
  78. };
  79. class OpInfo {
  80. public:
  81. OpInfo() = default;
  82. OpInfo(const OpInfo &opinfo) {
  83. op_name_ = opinfo.op_name();
  84. imply_type_ = opinfo.imply_type();
  85. impl_path_ = opinfo.impl_path();
  86. fusion_type_ = opinfo.fusion_type();
  87. async_flag_ = opinfo.async_flag_;
  88. binfile_name_ = opinfo.binfile_name_;
  89. compute_cost_ = opinfo.compute_cost_;
  90. kernel_name_ = opinfo.kernel_name();
  91. partial_flag_ = opinfo.partial_flag_;
  92. dynamic_format_ = opinfo.dynamic_format_;
  93. dynamic_shape_ = opinfo.dynamic_shape_;
  94. op_pattern_ = opinfo.op_pattern();
  95. processor_ = opinfo.processor_;
  96. need_check_supported_ = opinfo.need_check_supported();
  97. for (const auto &attr : opinfo.attrs_ptr()) {
  98. attrs_ptr_.push_back(std::make_shared<OpAttr>(*attr));
  99. }
  100. for (const auto &input : opinfo.inputs_ptr()) {
  101. inputs_ptr_.push_back(std::make_shared<OpIOInfo>(*input));
  102. }
  103. for (const auto &output : opinfo.outputs_ptr()) {
  104. outputs_ptr_.push_back(std::make_shared<OpIOInfo>(*output));
  105. }
  106. ref_infos_ = opinfo.ref_infos();
  107. }
  108. ~OpInfo() = default;
  109. std::string op_name() const { return op_name_; }
  110. OpImplyType imply_type() const { return imply_type_; }
  111. std::string impl_path() const { return impl_path_; }
  112. std::string fusion_type() const { return fusion_type_; }
  113. std::string kernel_name() const { return kernel_name_; }
  114. OpPattern op_pattern() const { return op_pattern_; }
  115. bool dynamic_shape() const { return dynamic_shape_; }
  116. std::string processor() const { return processor_; }
  117. bool need_check_supported() const { return need_check_supported_; }
  118. std::vector<std::shared_ptr<OpAttr>> attrs_ptr() const { return attrs_ptr_; }
  119. std::vector<std::shared_ptr<OpIOInfo>> inputs_ptr() const { return inputs_ptr_; }
  120. std::vector<std::shared_ptr<OpIOInfo>> outputs_ptr() const { return outputs_ptr_; }
  121. const std::unordered_map<size_t, size_t> &ref_infos() const { return ref_infos_; }
  122. void set_dynamic_shape(bool dynamic_shape) { dynamic_shape_ = dynamic_shape; }
  123. void set_op_name(const std::string &op_name) { op_name_ = op_name; }
  124. void set_imply_type(const OpImplyType imply_type) { imply_type_ = imply_type; }
  125. void set_impl_path(const std::string &impl_path) { impl_path_ = impl_path; }
  126. void set_fusion_type(const std::string &fusion_type) { fusion_type_ = fusion_type; }
  127. void set_async_flag(const bool async_flag) { async_flag_ = async_flag; }
  128. void set_binfile_name(const std::string &binfile_name) { binfile_name_ = binfile_name; }
  129. void set_compute_cost(const int compute_cost) { compute_cost_ = compute_cost; }
  130. void set_kernel_name(const std::string &kernel_name) { kernel_name_ = kernel_name; }
  131. void set_partial_flag(const bool partial_flag) { partial_flag_ = partial_flag; }
  132. void set_op_pattern(const OpPattern op_pattern) { op_pattern_ = op_pattern; }
  133. void set_processor(const std::string &processor) { processor_ = processor; }
  134. void set_need_check_supported(const bool need_check_supported) { need_check_supported_ = need_check_supported; }
  135. void add_attrs_ptr(const std::shared_ptr<OpAttr> &attr) { attrs_ptr_.push_back(attr); }
  136. void add_inputs_ptr(const std::shared_ptr<OpIOInfo> &input) { inputs_ptr_.push_back(input); }
  137. void add_outputs_ptr(const std::shared_ptr<OpIOInfo> &output) { outputs_ptr_.push_back(output); }
  138. bool is_ref() const { return !ref_infos_.empty(); }
  139. bool has_ref_index(size_t out_index) const { return ref_infos_.find(out_index) != ref_infos_.end(); }
  140. void add_ref_pair(size_t out_index, size_t in_index) { (void)ref_infos_.emplace(out_index, in_index); }
  141. void ClearInputs() { (void)inputs_ptr_.clear(); }
  142. void ClearOutputs() { (void)outputs_ptr_.clear(); }
  143. bool equals_to(const std::shared_ptr<OpInfo> &other_info) const {
  144. return this->op_name_ == other_info->op_name_ && this->imply_type_ == other_info->imply_type_ &&
  145. this->processor_ == other_info->processor_ && this->op_pattern_ == other_info->op_pattern_ &&
  146. this->dynamic_shape_ == other_info->dynamic_shape_;
  147. }
  148. private:
  149. std::string op_name_;
  150. OpImplyType imply_type_ = kTBE;
  151. std::string impl_path_;
  152. std::string fusion_type_;
  153. bool async_flag_ = false;
  154. std::string binfile_name_;
  155. int compute_cost_ = 0;
  156. std::string kernel_name_;
  157. bool partial_flag_ = false;
  158. bool dynamic_format_ = false;
  159. bool dynamic_shape_ = false;
  160. bool need_check_supported_ = false;
  161. OpPattern op_pattern_ = kCommonPattern;
  162. std::string processor_;
  163. std::vector<std::shared_ptr<OpAttr>> attrs_ptr_;
  164. std::vector<std::shared_ptr<OpIOInfo>> inputs_ptr_;
  165. std::vector<std::shared_ptr<OpIOInfo>> outputs_ptr_;
  166. std::unordered_map<size_t, size_t> ref_infos_;
  167. };
  168. using OpAttrPtr = std::shared_ptr<OpAttr>;
  169. using OpIOInfoPtr = std::shared_ptr<OpIOInfo>;
  170. using OpInfoPtr = std::shared_ptr<OpInfo>;
  171. } // namespace kernel
  172. } // namespace mindspore
  173. #endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_OPLIB_OPINFO_H_