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 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_OPLIB_OPINFO_H_
  17. #define MINDSPORE_CCSRC_KERNEL_OPLIB_OPINFO_H_
  18. #include <vector>
  19. #include <string>
  20. #include <memory>
  21. #include <unordered_map>
  22. #include "ir/dtype.h"
  23. #include "kernel/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. std::string name() const { return name_; }
  55. bool need_compile() const { return need_compile_; }
  56. std::string param_type() const { return param_type_; }
  57. std::string reshape_type() const { return reshape_type_; }
  58. std::string shape() const { return shape_; }
  59. std::vector<std::string> dtypes() const { return dtypes_; }
  60. 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. op_pattern_ = opinfo.op_pattern();
  94. for (auto attr : opinfo.attrs_ptr()) {
  95. attrs_ptr_.push_back(std::make_shared<OpAttr>(*attr));
  96. }
  97. for (auto input : opinfo.inputs_ptr()) {
  98. inputs_ptr_.push_back(std::make_shared<OpIOInfo>(*input));
  99. }
  100. for (auto output : opinfo.outputs_ptr()) {
  101. outputs_ptr_.push_back(std::make_shared<OpIOInfo>(*output));
  102. }
  103. ref_infos_ = opinfo.ref_infos();
  104. }
  105. ~OpInfo() = default;
  106. std::string op_name() const { return op_name_; }
  107. OpImplyType imply_type() const { return imply_type_; }
  108. std::string impl_path() const { return impl_path_; }
  109. std::string fusion_type() const { return fusion_type_; }
  110. std::string kernel_name() const { return kernel_name_; }
  111. OpPattern op_pattern() const { return op_pattern_; }
  112. std::vector<std::shared_ptr<OpAttr>> attrs_ptr() const { return attrs_ptr_; }
  113. std::vector<std::shared_ptr<OpIOInfo>> inputs_ptr() const { return inputs_ptr_; }
  114. std::vector<std::shared_ptr<OpIOInfo>> outputs_ptr() const { return outputs_ptr_; }
  115. const std::unordered_map<size_t, size_t> &ref_infos() const { return ref_infos_; }
  116. void set_op_name(const std::string &op_name) { op_name_ = op_name; }
  117. void set_imply_type(const OpImplyType imply_type) { imply_type_ = imply_type; }
  118. void set_impl_path(const std::string &impl_path) { impl_path_ = impl_path; }
  119. void set_fusion_type(const std::string &fusion_type) { fusion_type_ = fusion_type; }
  120. void set_async_flag(const bool async_flag) { async_flag_ = async_flag; }
  121. void set_binfile_name(const std::string &binfile_name) { binfile_name_ = binfile_name; }
  122. void set_compute_cost(const int compute_cost) { compute_cost_ = compute_cost; }
  123. void set_kernel_name(const std::string &kernel_name) { kernel_name_ = kernel_name; }
  124. void set_partial_flag(const bool partial_flag) { partial_flag_ = partial_flag; }
  125. void set_op_pattern(const OpPattern op_pattern) { op_pattern_ = op_pattern; }
  126. void add_attrs_ptr(const std::shared_ptr<OpAttr> &attr) { attrs_ptr_.push_back(attr); }
  127. void add_inputs_ptr(const std::shared_ptr<OpIOInfo> &input) { inputs_ptr_.push_back(input); }
  128. void add_outputs_ptr(const std::shared_ptr<OpIOInfo> &output) { outputs_ptr_.push_back(output); }
  129. bool is_ref() const { return !ref_infos_.empty(); }
  130. bool has_ref_index(size_t out_index) const { return ref_infos_.find(out_index) != ref_infos_.end(); }
  131. void add_ref_pair(size_t out_index, size_t in_index) { (void)ref_infos_.emplace(out_index, in_index); }
  132. void ClearInputs() { (void)inputs_ptr_.clear(); }
  133. void ClearOutputs() { (void)outputs_ptr_.clear(); }
  134. private:
  135. std::string op_name_;
  136. OpImplyType imply_type_ = kTBE;
  137. std::string impl_path_;
  138. std::string fusion_type_;
  139. bool async_flag_ = false;
  140. std::string binfile_name_;
  141. int compute_cost_ = 0;
  142. std::string kernel_name_;
  143. bool partial_flag_ = false;
  144. bool dynamic_format_ = false;
  145. OpPattern op_pattern_ = kCommonPattern;
  146. std::vector<std::shared_ptr<OpAttr>> attrs_ptr_;
  147. std::vector<std::shared_ptr<OpIOInfo>> inputs_ptr_;
  148. std::vector<std::shared_ptr<OpIOInfo>> outputs_ptr_;
  149. std::unordered_map<size_t, size_t> ref_infos_;
  150. };
  151. } // namespace kernel
  152. } // namespace mindspore
  153. #endif // MINDSPORE_CCSRC_KERNEL_OPLIB_OPINFO_H_