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.

oplib.cc 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include "kernel/oplib/oplib.h"
  17. #include <pybind11/pybind11.h>
  18. #include <unordered_map>
  19. #include <memory>
  20. #include <map>
  21. #include "utils/log_adapter.h"
  22. #include "utils/overload.h"
  23. #include "utils/context/ms_context.h"
  24. namespace mindspore {
  25. namespace kernel {
  26. constexpr auto kImplyType = "imply_type";
  27. constexpr auto kOpName = "op_name";
  28. constexpr auto kFusionType = "fusion_type";
  29. constexpr auto kAsyncFlag = "async_flag";
  30. constexpr auto kBinfileName = "binfile_name";
  31. constexpr auto kComputeCost = "compute_cost";
  32. constexpr auto kKernelName = "kernel_name";
  33. constexpr auto kPartialFlag = "partial_flag";
  34. constexpr auto kReshapeType = "reshape_type";
  35. constexpr auto kOpPattern = "op_pattern";
  36. constexpr auto kDynamicFormat = "dynamic_format";
  37. constexpr auto kFormatAgnostic = "formatAgnostic";
  38. constexpr auto kBroadcast = "broadcast";
  39. constexpr auto kReduce = "reduce";
  40. constexpr auto kDtypeFormat = "dtype_format";
  41. constexpr auto kAttr = "attr";
  42. constexpr auto kIputs = "inputs";
  43. constexpr auto kOutputs = "outputs";
  44. constexpr auto kAiCPU = "AiCPU";
  45. constexpr auto kTbe = "TBE";
  46. constexpr auto kAkg = "akg";
  47. constexpr auto kAutodiff = "AutoDiff";
  48. constexpr auto kName = "name";
  49. constexpr auto kParamType = "param_type";
  50. constexpr auto kDtype = "dtype";
  51. constexpr auto kType = "type";
  52. constexpr auto kValue = "value";
  53. constexpr auto kDefaultValue = "default_value";
  54. constexpr auto kIndex = "index";
  55. constexpr auto kFormat = "format";
  56. constexpr auto kNeedCompile = "need_compile";
  57. constexpr auto kShape = "shape";
  58. std::vector<std::shared_ptr<OpInfo>> OpLib::op_info_;
  59. std::string ImplTypeToStr(OpImplyType impl_type) {
  60. switch (impl_type) {
  61. case kTBE:
  62. return kTbe;
  63. case kAKG:
  64. return kAkg;
  65. case kAICPU:
  66. return kAiCPU;
  67. default:
  68. return "unknow";
  69. }
  70. }
  71. bool OpLib::RegOp(const std::string &json_string, const std::string &impl_path) {
  72. bool ret = false;
  73. try {
  74. auto op_json = nlohmann::json::parse(json_string);
  75. std::string imply_type_string = op_json.at(kImplyType);
  76. std::string op_name = op_json.at(kOpName);
  77. if (imply_type_string == kTbe) {
  78. OpImplyType imply_type = kTBE;
  79. ret = DecodeOpInfo(op_json, imply_type, impl_path);
  80. } else if (imply_type_string == kAutodiff) {
  81. OpImplyType imply_type = kAKG;
  82. ret = DecodeOpInfo(op_json, imply_type, impl_path);
  83. } else if (imply_type_string == kAiCPU) {
  84. OpImplyType imply_type = kAICPU;
  85. ret = DecodeOpInfo(op_json, imply_type, impl_path);
  86. } else {
  87. MS_LOG(ERROR) << "Not support imply_type";
  88. }
  89. if (!ret) {
  90. MS_LOG(ERROR) << "RegOp failed: op_name: " << op_name << " imply_type " << imply_type_string;
  91. }
  92. } catch (const std::exception &e) {
  93. MS_LOG(ERROR) << "get op json elements failed: " << e.what();
  94. }
  95. return ret;
  96. }
  97. void OpLib::DecodeTBESpecificInfo(const nlohmann::json &obj, const std::shared_ptr<OpInfo> &op_info) {
  98. const std::map<std::string, kernel::OpPattern> kOpPatternMap = {{kFormatAgnostic, kFormatAgnosticPattern},
  99. {kFormatAgnostic, kBroadcastPattern},
  100. {kReduce, kReducePattern},
  101. {kDynamicFormat, kDynamicFormatPattern}};
  102. op_info->set_async_flag(obj.at(kAsyncFlag));
  103. op_info->set_binfile_name(obj.at(kBinfileName));
  104. op_info->set_compute_cost(obj.at(kComputeCost));
  105. op_info->set_kernel_name(obj.at(kKernelName));
  106. op_info->set_partial_flag(obj.at(kPartialFlag));
  107. if (obj.find(kOpPattern) != obj.end()) {
  108. if (kOpPatternMap.find(obj.at(kOpPattern)) != kOpPatternMap.end()) {
  109. op_info->set_op_pattern(obj.at(kOpPattern));
  110. }
  111. }
  112. if (obj.find(kDynamicFormat) != obj.end()) {
  113. op_info->set_dynamic_format(obj.at(kDynamicFormat));
  114. }
  115. }
  116. bool OpLib::DecodeOpInfo(const nlohmann::json &obj, const mindspore::kernel::OpImplyType imply_type,
  117. const std::string &impl_path) {
  118. std::shared_ptr<OpInfo> op_info = std::make_shared<OpInfo>();
  119. MS_EXCEPTION_IF_NULL(op_info);
  120. op_info->set_op_name(obj.at(kOpName));
  121. op_info->set_impl_path(impl_path);
  122. op_info->set_imply_type(imply_type);
  123. op_info->set_fusion_type(obj.at(kFusionType));
  124. if (imply_type == kTBE) {
  125. DecodeTBESpecificInfo(obj, op_info);
  126. }
  127. auto attrs = obj.at(kAttr);
  128. for (const auto &attr : attrs) {
  129. if (!DecodeAttr(attr, imply_type, op_info)) {
  130. MS_LOG(ERROR) << "DecodeAttr Failed";
  131. return false;
  132. }
  133. }
  134. nlohmann::json dtype_format;
  135. if (obj.find(kDtypeFormat) != obj.end()) {
  136. dtype_format = obj.at(kDtypeFormat);
  137. }
  138. auto inputs = obj.at(kIputs);
  139. for (const auto &input : inputs) {
  140. if (!DecodeInputOutput(input, imply_type, kInput, op_info, dtype_format)) {
  141. MS_LOG(ERROR) << "DecodeInputOutput Failed";
  142. return false;
  143. }
  144. }
  145. auto outputs = obj.at(kOutputs);
  146. for (const auto &output : outputs) {
  147. if (!DecodeInputOutput(output, imply_type, kOutput, op_info, dtype_format)) {
  148. MS_LOG(ERROR) << "DecodeInputOutput Failed";
  149. return false;
  150. }
  151. }
  152. if (!GetRefInfo(op_info)) {
  153. MS_LOG(ERROR) << "GetRefInfo Failed";
  154. return false;
  155. }
  156. if (!CheckRepetition(op_info)) {
  157. MS_LOG(ERROR) << "CheckRepetition Failed";
  158. return false;
  159. }
  160. op_info_.push_back(op_info);
  161. return true;
  162. }
  163. bool OpLib::DecodeAttr(const nlohmann::json &obj, const OpImplyType imply_type,
  164. const std::shared_ptr<OpInfo> &op_info) {
  165. MS_EXCEPTION_IF_NULL(op_info);
  166. bool ret = true;
  167. try {
  168. std::shared_ptr<OpAttr> op_attr = std::make_shared<OpAttr>();
  169. MS_EXCEPTION_IF_NULL(op_attr);
  170. op_attr->set_name(obj.at(kName));
  171. if (imply_type != kAICPU) {
  172. op_attr->set_param_type(obj.at(kParamType));
  173. }
  174. op_attr->set_type(obj.at(kType));
  175. if (imply_type == kTBE) {
  176. op_attr->set_value(obj.at(kValue));
  177. }
  178. if (obj.find(kDefaultValue) != obj.end()) {
  179. op_attr->set_default_value(obj.at(kDefaultValue));
  180. }
  181. op_info->add_attrs_ptr(op_attr);
  182. } catch (const std::exception &e) {
  183. MS_LOG(ERROR) << "DecodeAttr failed:" << e.what();
  184. ret = false;
  185. }
  186. return ret;
  187. }
  188. bool OpLib::DecodeDtypeFormat(const nlohmann::json &dtype_format, const std::shared_ptr<OpIOInfo> &op_io,
  189. size_t index) {
  190. bool ret = true;
  191. try {
  192. std::vector<std::string> dtype;
  193. std::vector<std::string> format;
  194. for (const auto &it : dtype_format) {
  195. dtype.emplace_back(it[index][0]);
  196. format.emplace_back(it[index][1]);
  197. }
  198. op_io->set_dtypes(dtype);
  199. op_io->set_formats(format);
  200. } catch (const std::exception &e) {
  201. MS_LOG(ERROR) << "DecodeDtypeFormat falied" << e.what();
  202. ret = false;
  203. }
  204. return ret;
  205. }
  206. bool OpLib::DecodeInputOutput(const nlohmann::json &obj, const OpImplyType imply_type, const OpIOType io_type,
  207. const std::shared_ptr<OpInfo> &op_info, const nlohmann::json &dtype_format) {
  208. bool ret = true;
  209. try {
  210. std::shared_ptr<OpIOInfo> op_io = std::make_shared<OpIOInfo>();
  211. MS_EXCEPTION_IF_NULL(op_io);
  212. op_io->set_index(obj.at(kIndex));
  213. op_io->set_name(obj.at(kName));
  214. if (!dtype_format.empty()) {
  215. if (!DecodeDtypeFormat(dtype_format, op_io, op_info->inputs_ptr().size() + op_info->outputs_ptr().size())) {
  216. MS_LOG(ERROR) << "Decode dtype format failed";
  217. return false;
  218. }
  219. } else {
  220. op_io->set_dtypes(obj.at(kDtype));
  221. op_io->set_formats(obj.at(kFormat));
  222. }
  223. if (op_io->dtypes().size() != op_io->formats().size()) {
  224. MS_LOG(ERROR) << "op " << op_io->name() << " dtype size: " << op_io->dtypes()
  225. << " is not equal to format size: " << op_io->formats();
  226. return false;
  227. }
  228. if (obj.find(kParamType) != obj.end()) {
  229. op_io->set_param_type(obj.at(kParamType));
  230. }
  231. if (imply_type == kTBE) {
  232. if (obj.find(kNeedCompile) != obj.end()) {
  233. op_io->set_need_compile(obj.at(kNeedCompile));
  234. }
  235. if (obj.find(kShape) != obj.end()) {
  236. op_io->set_shape(obj.at(kShape));
  237. }
  238. if (obj.find(kReshapeType) != obj.end()) {
  239. op_io->set_reshape_type(obj.at(kReshapeType));
  240. }
  241. }
  242. if (io_type == kInput) {
  243. op_info->add_inputs_ptr(op_io);
  244. } else if (io_type == kOutput) {
  245. op_info->add_outputs_ptr(op_io);
  246. }
  247. } catch (const std::exception &e) {
  248. MS_LOG(ERROR) << "DecodeInputOutput failed" << e.what();
  249. ret = false;
  250. }
  251. return ret;
  252. }
  253. std::shared_ptr<OpInfo> OpLib::FindOp(const std::string &op_name, OpImplyType imply_type) {
  254. auto context = MsContext::GetInstance();
  255. MS_EXCEPTION_IF_NULL(context);
  256. bool is_gpu = (context->device_target() == kGPUDevice);
  257. if ((is_gpu && (imply_type == kTBE || imply_type == kAICPU)) ||
  258. (!is_gpu && (imply_type != kTBE && imply_type != kAICPU))) {
  259. MS_LOG(ERROR) << "FindOp failed: opname: " << op_name << ", imply_type: " << ImplTypeToStr(imply_type)
  260. << ", current op num: " << op_info_.size();
  261. return nullptr;
  262. }
  263. for (const auto &op_info : op_info_) {
  264. MS_EXCEPTION_IF_NULL(op_info);
  265. if (op_info->op_name() == op_name && op_info->imply_type() == imply_type) {
  266. return op_info;
  267. }
  268. }
  269. MS_LOG(DEBUG) << "FindOp failed: opname: " << op_name << ", imply_type: " << ImplTypeToStr(imply_type)
  270. << ", current op num: " << op_info_.size();
  271. return nullptr;
  272. }
  273. bool OpLib::GetRefInfo(const std::shared_ptr<OpInfo> &op_info) {
  274. MS_EXCEPTION_IF_NULL(op_info);
  275. const auto &output_infos = op_info->outputs_ptr();
  276. const auto &input_infos = op_info->inputs_ptr();
  277. for (size_t out_index = 0; out_index < output_infos.size(); out_index++) {
  278. const auto &out_name = output_infos[out_index]->name();
  279. for (size_t in_index = 0; in_index < input_infos.size(); in_index++) {
  280. const auto &in_name = input_infos[in_index]->name();
  281. if (out_name == in_name) {
  282. if (op_info->has_ref_index(out_index)) {
  283. MS_LOG(ERROR) << "The out_index " << out_index << " is already in ref_info";
  284. return false;
  285. }
  286. op_info->add_ref_pair(out_index, in_index);
  287. MS_LOG(INFO) << "add ref info, op name is " << op_info->op_name() << ", outindex is " << out_index
  288. << ", in_index is " << in_index;
  289. }
  290. }
  291. }
  292. return true;
  293. }
  294. bool OpLib::CheckRepetition(const std::shared_ptr<OpInfo> &op_info) {
  295. MS_EXCEPTION_IF_NULL(op_info);
  296. for (const auto &exist_op_info : op_info_) {
  297. MS_EXCEPTION_IF_NULL(exist_op_info);
  298. if (exist_op_info->op_name() == op_info->op_name() && exist_op_info->imply_type() == op_info->imply_type() &&
  299. exist_op_info->impl_path() != op_info->impl_path()) {
  300. MS_LOG(ERROR) << "Op has already exist, please use other name, op name: " << op_info->op_name()
  301. << " op type: " << ImplTypeToStr(op_info->imply_type());
  302. return false;
  303. }
  304. }
  305. return true;
  306. }
  307. } // namespace kernel
  308. } // namespace mindspore