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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 = "dynamicFormat";
  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. {kBroadcast, kBroadcastPattern},
  100. {kReduce, kReducePattern},
  101. {kDynamicFormat, kDynamicFormatPattern}};
  102. MS_EXCEPTION_IF_NULL(op_info);
  103. op_info->set_async_flag(obj.at(kAsyncFlag));
  104. op_info->set_binfile_name(obj.at(kBinfileName));
  105. op_info->set_compute_cost(obj.at(kComputeCost));
  106. op_info->set_kernel_name(obj.at(kKernelName));
  107. op_info->set_partial_flag(obj.at(kPartialFlag));
  108. if (obj.find(kOpPattern) != obj.end()) {
  109. std::string op_pattern = obj.at(kOpPattern);
  110. auto find_iter = kOpPatternMap.find(op_pattern);
  111. if (find_iter == kOpPatternMap.end()) {
  112. if (!op_pattern.empty()) {
  113. MS_LOG(WARNING) << "Op pattern set value error: " << op_pattern;
  114. }
  115. op_info->set_op_pattern(kCommonPattern);
  116. } else {
  117. op_info->set_op_pattern(find_iter->second);
  118. }
  119. }
  120. }
  121. bool OpLib::DecodeOpInfo(const nlohmann::json &obj, const mindspore::kernel::OpImplyType imply_type,
  122. const std::string &impl_path) {
  123. std::shared_ptr<OpInfo> op_info = std::make_shared<OpInfo>();
  124. MS_EXCEPTION_IF_NULL(op_info);
  125. op_info->set_op_name(obj.at(kOpName));
  126. op_info->set_impl_path(impl_path);
  127. op_info->set_imply_type(imply_type);
  128. op_info->set_fusion_type(obj.at(kFusionType));
  129. if (imply_type == kTBE) {
  130. DecodeTBESpecificInfo(obj, op_info);
  131. }
  132. auto attrs = obj.at(kAttr);
  133. for (const auto &attr : attrs) {
  134. if (!DecodeAttr(attr, imply_type, op_info)) {
  135. MS_LOG(ERROR) << "DecodeAttr Failed";
  136. return false;
  137. }
  138. }
  139. nlohmann::json dtype_format;
  140. if (obj.find(kDtypeFormat) != obj.end()) {
  141. dtype_format = obj.at(kDtypeFormat);
  142. }
  143. auto inputs = obj.at(kIputs);
  144. for (const auto &input : inputs) {
  145. if (!DecodeInputOutput(input, imply_type, kInput, op_info, dtype_format)) {
  146. MS_LOG(ERROR) << "DecodeInputOutput Failed";
  147. return false;
  148. }
  149. }
  150. auto outputs = obj.at(kOutputs);
  151. for (const auto &output : outputs) {
  152. if (!DecodeInputOutput(output, imply_type, kOutput, op_info, dtype_format)) {
  153. MS_LOG(ERROR) << "DecodeInputOutput Failed";
  154. return false;
  155. }
  156. }
  157. if (!GetRefInfo(op_info)) {
  158. MS_LOG(ERROR) << "GetRefInfo Failed";
  159. return false;
  160. }
  161. if (!CheckRepetition(op_info)) {
  162. MS_LOG(ERROR) << "CheckRepetition Failed";
  163. return false;
  164. }
  165. op_info_.push_back(op_info);
  166. return true;
  167. }
  168. bool OpLib::DecodeAttr(const nlohmann::json &obj, const OpImplyType imply_type,
  169. const std::shared_ptr<OpInfo> &op_info) {
  170. MS_EXCEPTION_IF_NULL(op_info);
  171. bool ret = true;
  172. try {
  173. std::shared_ptr<OpAttr> op_attr = std::make_shared<OpAttr>();
  174. MS_EXCEPTION_IF_NULL(op_attr);
  175. op_attr->set_name(obj.at(kName));
  176. if (imply_type != kAICPU) {
  177. op_attr->set_param_type(obj.at(kParamType));
  178. }
  179. op_attr->set_type(obj.at(kType));
  180. if (imply_type == kTBE) {
  181. op_attr->set_value(obj.at(kValue));
  182. }
  183. if (obj.find(kDefaultValue) != obj.end()) {
  184. op_attr->set_default_value(obj.at(kDefaultValue));
  185. }
  186. op_info->add_attrs_ptr(op_attr);
  187. } catch (const std::exception &e) {
  188. MS_LOG(ERROR) << "DecodeAttr failed:" << e.what();
  189. ret = false;
  190. }
  191. return ret;
  192. }
  193. bool OpLib::DecodeDtypeFormat(const nlohmann::json &dtype_format, const std::shared_ptr<OpIOInfo> &op_io,
  194. size_t index) {
  195. MS_EXCEPTION_IF_NULL(op_io);
  196. bool ret = true;
  197. try {
  198. std::vector<std::string> dtype;
  199. std::vector<std::string> format;
  200. for (const auto &it : dtype_format) {
  201. dtype.emplace_back(it[index][0]);
  202. format.emplace_back(it[index][1]);
  203. }
  204. op_io->set_dtypes(dtype);
  205. op_io->set_formats(format);
  206. } catch (const std::exception &e) {
  207. MS_LOG(ERROR) << "DecodeDtypeFormat falied" << e.what();
  208. ret = false;
  209. }
  210. return ret;
  211. }
  212. bool OpLib::DecodeInputOutput(const nlohmann::json &obj, const OpImplyType imply_type, const OpIOType io_type,
  213. const std::shared_ptr<OpInfo> &op_info, const nlohmann::json &dtype_format) {
  214. MS_EXCEPTION_IF_NULL(op_info);
  215. bool ret = true;
  216. try {
  217. std::shared_ptr<OpIOInfo> op_io = std::make_shared<OpIOInfo>();
  218. MS_EXCEPTION_IF_NULL(op_io);
  219. op_io->set_index(obj.at(kIndex));
  220. op_io->set_name(obj.at(kName));
  221. if (!dtype_format.empty()) {
  222. if (!DecodeDtypeFormat(dtype_format, op_io, op_info->inputs_ptr().size() + op_info->outputs_ptr().size())) {
  223. MS_LOG(ERROR) << "Decode dtype format failed";
  224. return false;
  225. }
  226. } else {
  227. op_io->set_dtypes(obj.at(kDtype));
  228. op_io->set_formats(obj.at(kFormat));
  229. }
  230. if (op_io->dtypes().size() != op_io->formats().size()) {
  231. MS_LOG(ERROR) << "op " << op_io->name() << " dtype size: " << op_io->dtypes()
  232. << " is not equal to format size: " << op_io->formats();
  233. return false;
  234. }
  235. if (obj.find(kParamType) != obj.end()) {
  236. op_io->set_param_type(obj.at(kParamType));
  237. }
  238. if (imply_type == kTBE) {
  239. if (obj.find(kNeedCompile) != obj.end()) {
  240. op_io->set_need_compile(obj.at(kNeedCompile));
  241. }
  242. if (obj.find(kShape) != obj.end()) {
  243. op_io->set_shape(obj.at(kShape));
  244. }
  245. if (obj.find(kReshapeType) != obj.end()) {
  246. op_io->set_reshape_type(obj.at(kReshapeType));
  247. }
  248. }
  249. if (io_type == kInput) {
  250. op_info->add_inputs_ptr(op_io);
  251. } else if (io_type == kOutput) {
  252. op_info->add_outputs_ptr(op_io);
  253. }
  254. } catch (const std::exception &e) {
  255. MS_LOG(ERROR) << "DecodeInputOutput failed" << e.what();
  256. ret = false;
  257. }
  258. return ret;
  259. }
  260. std::shared_ptr<OpInfo> OpLib::FindOp(const std::string &op_name, OpImplyType imply_type) {
  261. auto context = MsContext::GetInstance();
  262. MS_EXCEPTION_IF_NULL(context);
  263. bool is_gpu = (context->device_target() == kGPUDevice);
  264. if (is_gpu && (imply_type == kTBE || imply_type == kAICPU)) {
  265. MS_LOG(ERROR) << "FindOp failed: opname: " << op_name << ", imply_type: " << ImplTypeToStr(imply_type)
  266. << ", current op num: " << op_info_.size();
  267. return nullptr;
  268. }
  269. for (const auto &op_info : op_info_) {
  270. MS_EXCEPTION_IF_NULL(op_info);
  271. if (op_info->op_name() == op_name && op_info->imply_type() == imply_type) {
  272. return op_info;
  273. }
  274. }
  275. MS_LOG(DEBUG) << "FindOp failed: opname: " << op_name << ", imply_type: " << ImplTypeToStr(imply_type)
  276. << ", current op num: " << op_info_.size();
  277. return nullptr;
  278. }
  279. bool OpLib::GetRefInfo(const std::shared_ptr<OpInfo> &op_info) {
  280. MS_EXCEPTION_IF_NULL(op_info);
  281. const auto &output_infos = op_info->outputs_ptr();
  282. const auto &input_infos = op_info->inputs_ptr();
  283. for (size_t out_index = 0; out_index < output_infos.size(); out_index++) {
  284. const auto &out_name = output_infos[out_index]->name();
  285. for (size_t in_index = 0; in_index < input_infos.size(); in_index++) {
  286. const auto &in_name = input_infos[in_index]->name();
  287. if (out_name == in_name) {
  288. if (op_info->has_ref_index(out_index)) {
  289. MS_LOG(ERROR) << "The out_index " << out_index << " is already in ref_info";
  290. return false;
  291. }
  292. op_info->add_ref_pair(out_index, in_index);
  293. MS_LOG(INFO) << "add ref info, op name is " << op_info->op_name() << ", outindex is " << out_index
  294. << ", in_index is " << in_index;
  295. }
  296. }
  297. }
  298. return true;
  299. }
  300. bool OpLib::CheckRepetition(const std::shared_ptr<OpInfo> &op_info) {
  301. MS_EXCEPTION_IF_NULL(op_info);
  302. for (const auto &exist_op_info : op_info_) {
  303. MS_EXCEPTION_IF_NULL(exist_op_info);
  304. if (exist_op_info->op_name() == op_info->op_name() && exist_op_info->imply_type() == op_info->imply_type() &&
  305. exist_op_info->impl_path() != op_info->impl_path()) {
  306. MS_LOG(ERROR) << "Op has already exist, please use other name, op name: " << op_info->op_name()
  307. << " op type: " << ImplTypeToStr(op_info->imply_type());
  308. return false;
  309. }
  310. }
  311. return true;
  312. }
  313. } // namespace kernel
  314. } // namespace mindspore