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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 "backend/kernel_compiler/oplib/oplib.h"
  17. #include <memory>
  18. #include <map>
  19. #include <fstream>
  20. #include "utils/log_adapter.h"
  21. #include "utils/overload.h"
  22. #include "utils/ms_context.h"
  23. namespace mindspore {
  24. namespace kernel {
  25. constexpr auto kImplyType = "imply_type";
  26. constexpr auto kOpName = "op_name";
  27. constexpr auto kFusionType = "fusion_type";
  28. constexpr auto kAsyncFlag = "async_flag";
  29. constexpr auto kBinfileName = "binfile_name";
  30. constexpr auto kComputeCost = "compute_cost";
  31. constexpr auto kKernelName = "kernel_name";
  32. constexpr auto kPartialFlag = "partial_flag";
  33. constexpr auto kReshapeType = "reshape_type";
  34. constexpr auto kOpPattern = "op_pattern";
  35. constexpr auto kIsDynamicFormat = "is_dynamic_format";
  36. constexpr auto kDynamicFormat = "dynamicFormat";
  37. constexpr auto kFormatAgnostic = "formatAgnostic";
  38. constexpr auto kNeedCheckSupported = "need_check_supported";
  39. constexpr auto kBroadcast = "broadcast";
  40. constexpr auto kReduce = "reduce";
  41. constexpr auto kDynamicShape = "dynamic_shape";
  42. constexpr auto kDynamicCompileStatic = "dynamic_compile_static";
  43. constexpr auto kDtypeFormat = "dtype_format";
  44. constexpr auto kAttr = "attr";
  45. constexpr auto kIputs = "inputs";
  46. constexpr auto kOutputs = "outputs";
  47. constexpr auto kAiCPU = "AiCPU";
  48. constexpr auto kAiCore = "AiCore";
  49. constexpr auto kCUDA = "CUDA";
  50. constexpr auto kTbe = "TBE";
  51. constexpr auto kAkg = "AKG";
  52. constexpr auto kCpu = "CPU";
  53. constexpr auto kName = "name";
  54. constexpr auto kParamType = "param_type";
  55. constexpr auto kDtype = "dtype";
  56. constexpr auto kType = "type";
  57. constexpr auto kValue = "value";
  58. constexpr auto kDefaultValue = "default_value";
  59. constexpr auto kIndex = "index";
  60. constexpr auto kFormat = "format";
  61. constexpr auto kNeedCompile = "need_compile";
  62. constexpr auto kShape = "shape";
  63. constexpr auto kProcessor = "processor";
  64. std::multimap<std::string, std::shared_ptr<OpInfo>> OpLib::op_info_;
  65. static std::string ImplTypeToStr(OpImplyType impl_type) {
  66. switch (impl_type) {
  67. case kTBE:
  68. return kTbe;
  69. case kAKG:
  70. return kAkg;
  71. case kAICPU:
  72. return kAiCPU;
  73. default:
  74. return "unknown";
  75. }
  76. }
  77. bool OpLib::RegOp(const std::string &json_string, const std::string &impl_path) {
  78. bool ret = false;
  79. try {
  80. auto op_json = nlohmann::json::parse(json_string);
  81. std::string imply_type_string = op_json.at(kImplyType);
  82. std::string op_name = op_json.at(kOpName);
  83. if (imply_type_string == kTbe) {
  84. OpImplyType imply_type = kTBE;
  85. ret = DecodeOpInfo(op_json, imply_type, impl_path);
  86. } else if (imply_type_string == kAkg) {
  87. OpImplyType imply_type = kAKG;
  88. ret = DecodeOpInfo(op_json, imply_type, impl_path);
  89. } else if (imply_type_string == kAiCPU) {
  90. OpImplyType imply_type = kAICPU;
  91. ret = DecodeOpInfo(op_json, imply_type, impl_path);
  92. } else if (imply_type_string == kCpu) {
  93. OpImplyType imply_type = kCPU;
  94. ret = DecodeOpInfo(op_json, imply_type, impl_path);
  95. } else {
  96. MS_LOG(ERROR) << "Not support imply_type";
  97. }
  98. if (!ret) {
  99. MS_LOG(ERROR) << "RegOp failed: op_name: " << op_name << " imply_type " << imply_type_string;
  100. }
  101. } catch (const std::exception &e) {
  102. MS_LOG(ERROR) << "get op json elements failed: " << e.what();
  103. }
  104. return ret;
  105. }
  106. void OpLib::DecodeTBESpecificInfo(const nlohmann::json &obj, const std::shared_ptr<OpInfo> &op_info) {
  107. const std::map<std::string, kernel::OpPattern> kOpPatternMap = {
  108. {kFormatAgnostic, kFormatAgnosticPattern}, {kBroadcast, kBroadcastPattern}, {kReduce, kReducePattern}};
  109. MS_EXCEPTION_IF_NULL(op_info);
  110. op_info->set_async_flag(obj.at(kAsyncFlag));
  111. op_info->set_binfile_name(obj.at(kBinfileName));
  112. op_info->set_compute_cost(obj.at(kComputeCost));
  113. op_info->set_kernel_name(obj.at(kKernelName));
  114. op_info->set_partial_flag(obj.at(kPartialFlag));
  115. op_info->set_need_check_supported(obj.at(kNeedCheckSupported));
  116. if (obj.find(kDynamicShape) != obj.end()) {
  117. op_info->set_dynamic_shape(obj.at(kDynamicShape));
  118. }
  119. if (obj.find(kDynamicCompileStatic) != obj.end()) {
  120. op_info->set_dynamic_compile_static_(obj.at(kDynamicCompileStatic));
  121. }
  122. if (obj.find(kIsDynamicFormat) != obj.end()) {
  123. op_info->set_is_dynamic_format(obj.at(kIsDynamicFormat));
  124. }
  125. if (obj.find(kOpPattern) != obj.end()) {
  126. std::string op_pattern = obj.at(kOpPattern);
  127. auto find_iter = kOpPatternMap.find(op_pattern);
  128. if (find_iter == kOpPatternMap.end()) {
  129. if (!op_pattern.empty()) {
  130. MS_LOG(WARNING) << "Op pattern set value error: " << op_pattern;
  131. }
  132. op_info->set_op_pattern(kCommonPattern);
  133. } else {
  134. op_info->set_op_pattern(find_iter->second);
  135. }
  136. }
  137. }
  138. void OpLib::DecodeAKGSpecificInfo(const nlohmann::json &obj, const std::shared_ptr<OpInfo> &op_info) {
  139. MS_EXCEPTION_IF_NULL(op_info);
  140. op_info->set_processor(obj.at(kProcessor));
  141. }
  142. bool OpLib::RegOpFromLocalInfo() {
  143. static bool has_load = false;
  144. if (has_load) {
  145. return true;
  146. }
  147. MS_LOG(INFO) << "Start";
  148. has_load = true;
  149. std::string dir = common::GetEnv("MINDSPORE_OP_INFO_PATH");
  150. if (dir.empty()) {
  151. MS_LOG(INFO) << "MindSpore op info path does not been set. use op info from python pass.";
  152. return true;
  153. }
  154. char real_path[PATH_MAX] = {0};
  155. if (dir.size() >= PATH_MAX) {
  156. MS_LOG(ERROR) << "Op info path is invalid: " << dir;
  157. return false;
  158. }
  159. #if defined(_WIN32) || defined(_WIN64)
  160. if (_fullpath(real_path, common::SafeCStr(dir), PATH_MAX) == nullptr) {
  161. MS_LOG(ERROR) << "Op info path is invalid: " << dir;
  162. return false;
  163. }
  164. #else
  165. if (realpath(common::SafeCStr(dir), real_path) == nullptr) {
  166. MS_LOG(ERROR) << "Op info path is invalid: " << dir;
  167. return false;
  168. }
  169. if (strlen(real_path) >= PATH_MAX) {
  170. MS_LOG(ERROR) << "Op info path is invalid, the absolute path length is greater than PATH_MAX";
  171. return false;
  172. }
  173. #endif
  174. MS_LOG(INFO) << "Start to read op info from local file.";
  175. std::ifstream file(real_path);
  176. if (!file.is_open()) {
  177. MS_LOG(ERROR) << "Find op info file failed.";
  178. return false;
  179. }
  180. std::string line;
  181. while (getline(file, line)) {
  182. if (!line.empty()) {
  183. (void)OpLib::RegOp(line, "");
  184. }
  185. }
  186. file.close();
  187. MS_LOG(INFO) << "End";
  188. return true;
  189. }
  190. bool OpLib::DecodeOpInfo(const nlohmann::json &obj, const mindspore::kernel::OpImplyType imply_type,
  191. const std::string &impl_path) {
  192. std::shared_ptr<OpInfo> op_info = std::make_shared<OpInfo>();
  193. MS_EXCEPTION_IF_NULL(op_info);
  194. op_info->set_op_name(obj.at(kOpName));
  195. op_info->set_impl_path(impl_path);
  196. op_info->set_imply_type(imply_type);
  197. op_info->set_fusion_type(obj.at(kFusionType));
  198. if (imply_type == kTBE) {
  199. DecodeTBESpecificInfo(obj, op_info);
  200. } else if (imply_type == kAKG) {
  201. DecodeAKGSpecificInfo(obj, op_info);
  202. }
  203. auto attrs = obj.at(kAttr);
  204. for (const auto &attr : attrs) {
  205. if (!DecodeAttr(attr, imply_type, op_info)) {
  206. MS_LOG(ERROR) << "DecodeAttr Failed";
  207. return false;
  208. }
  209. }
  210. nlohmann::json dtype_format;
  211. if (obj.find(kDtypeFormat) != obj.end()) {
  212. dtype_format = obj.at(kDtypeFormat);
  213. }
  214. auto inputs = obj.at(kIputs);
  215. for (const auto &input : inputs) {
  216. if (!DecodeInputOutput(input, imply_type, kInput, op_info, dtype_format)) {
  217. MS_LOG(ERROR) << "DecodeInputOutput Failed";
  218. return false;
  219. }
  220. }
  221. auto outputs = obj.at(kOutputs);
  222. for (const auto &output : outputs) {
  223. if (!DecodeInputOutput(output, imply_type, kOutput, op_info, dtype_format)) {
  224. MS_LOG(ERROR) << "DecodeInputOutput Failed";
  225. return false;
  226. }
  227. }
  228. if (CheckRepetition(op_info)) {
  229. MS_LOG(WARNING) << "This op info has been already registered. op name: " << op_info->op_name()
  230. << ", impl type: " << ImplTypeToStr(op_info->imply_type())
  231. << ", impl path: " << op_info->impl_path();
  232. return true;
  233. }
  234. if (!GetRefInfo(op_info)) {
  235. MS_LOG(ERROR) << "GetRefInfo Failed";
  236. return false;
  237. }
  238. op_info_.emplace(op_info->op_name(), op_info);
  239. return true;
  240. }
  241. bool OpLib::DecodeAttr(const nlohmann::json &obj, const OpImplyType imply_type,
  242. const std::shared_ptr<OpInfo> &op_info) {
  243. MS_EXCEPTION_IF_NULL(op_info);
  244. bool ret = true;
  245. try {
  246. std::shared_ptr<OpAttr> op_attr = std::make_shared<OpAttr>();
  247. MS_EXCEPTION_IF_NULL(op_attr);
  248. op_attr->set_name(obj.at(kName));
  249. if (imply_type != kAICPU) {
  250. op_attr->set_param_type(obj.at(kParamType));
  251. }
  252. op_attr->set_type(obj.at(kType));
  253. if (imply_type == kTBE) {
  254. op_attr->set_value(obj.at(kValue));
  255. }
  256. if (obj.find(kDefaultValue) != obj.end()) {
  257. op_attr->set_default_value(obj.at(kDefaultValue));
  258. }
  259. op_info->add_attrs_ptr(op_attr);
  260. } catch (const std::exception &e) {
  261. MS_LOG(ERROR) << "DecodeAttr failed:" << e.what();
  262. ret = false;
  263. }
  264. return ret;
  265. }
  266. bool OpLib::DecodeDtypeFormat(const nlohmann::json &dtype_format, const std::shared_ptr<OpIOInfo> &op_io,
  267. size_t index) {
  268. MS_EXCEPTION_IF_NULL(op_io);
  269. bool ret = true;
  270. try {
  271. std::vector<std::string> dtype;
  272. std::vector<std::string> format;
  273. for (const auto &it : dtype_format) {
  274. dtype.emplace_back(it[index][0]);
  275. format.emplace_back(it[index][1]);
  276. }
  277. op_io->set_dtypes(dtype);
  278. op_io->set_formats(format);
  279. } catch (const std::exception &e) {
  280. MS_LOG(ERROR) << "DecodeDtypeFormat failed" << e.what();
  281. ret = false;
  282. }
  283. return ret;
  284. }
  285. bool OpLib::DecodeInputOutput(const nlohmann::json &obj, const OpImplyType imply_type, const OpIOType io_type,
  286. const std::shared_ptr<OpInfo> &op_info, const nlohmann::json &dtype_format) {
  287. MS_EXCEPTION_IF_NULL(op_info);
  288. bool ret = true;
  289. try {
  290. std::shared_ptr<OpIOInfo> op_io = std::make_shared<OpIOInfo>();
  291. MS_EXCEPTION_IF_NULL(op_io);
  292. op_io->set_index(obj.at(kIndex));
  293. op_io->set_name(obj.at(kName));
  294. if (!dtype_format.empty()) {
  295. if (!DecodeDtypeFormat(dtype_format, op_io, op_info->inputs_ptr().size() + op_info->outputs_ptr().size())) {
  296. MS_LOG(ERROR) << "Decode dtype format failed";
  297. return false;
  298. }
  299. } else {
  300. op_io->set_dtypes(obj.at(kDtype));
  301. op_io->set_formats(obj.at(kFormat));
  302. }
  303. if (op_io->dtypes().size() != op_io->formats().size()) {
  304. MS_LOG(ERROR) << "op " << op_io->name() << " dtype size: " << op_io->dtypes()
  305. << " is not equal to format size: " << op_io->formats();
  306. return false;
  307. }
  308. if (obj.find(kParamType) != obj.end()) {
  309. op_io->set_param_type(obj.at(kParamType));
  310. }
  311. if (imply_type == kTBE) {
  312. if (obj.find(kNeedCompile) != obj.end()) {
  313. op_io->set_need_compile(obj.at(kNeedCompile));
  314. }
  315. if (obj.find(kShape) != obj.end()) {
  316. op_io->set_shape(obj.at(kShape));
  317. }
  318. if (obj.find(kReshapeType) != obj.end()) {
  319. op_io->set_reshape_type(obj.at(kReshapeType));
  320. }
  321. }
  322. if (io_type == kInput) {
  323. op_info->add_inputs_ptr(op_io);
  324. } else if (io_type == kOutput) {
  325. op_info->add_outputs_ptr(op_io);
  326. }
  327. } catch (const std::exception &e) {
  328. MS_LOG(ERROR) << "DecodeInputOutput failed" << e.what();
  329. ret = false;
  330. }
  331. return ret;
  332. }
  333. std::shared_ptr<OpInfo> OpLib::FindOp(const std::string &op_name, OpImplyType imply_type, bool is_dynamic_shape) {
  334. if (!OpLib::RegOpFromLocalInfo()) {
  335. MS_LOG(INFO) << "Warning reg local op info failed.";
  336. }
  337. auto context = MsContext::GetInstance();
  338. MS_EXCEPTION_IF_NULL(context);
  339. bool is_gpu = (context->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice);
  340. if (is_gpu && (imply_type == kTBE || imply_type == kAICPU)) {
  341. MS_LOG(ERROR) << "FindOp failed: opname: " << op_name << ", imply_type: " << ImplTypeToStr(imply_type)
  342. << ", current op num: " << op_info_.size();
  343. return nullptr;
  344. }
  345. std::string target_processor = is_gpu ? kCUDA : kAiCore;
  346. for (auto [iter, end] = op_info_.equal_range(op_name); iter != end; ++iter) {
  347. auto &op_info = (*iter).second;
  348. MS_EXCEPTION_IF_NULL(op_info);
  349. if (op_info->imply_type() != imply_type) {
  350. continue;
  351. }
  352. if (imply_type == kAKG && op_info->processor() != target_processor) {
  353. continue;
  354. }
  355. if (is_dynamic_shape && !op_info->dynamic_shape()) {
  356. continue;
  357. }
  358. return op_info;
  359. }
  360. MS_LOG(INFO) << "FindOp failed: opname: " << op_name << ", imply_type: " << ImplTypeToStr(imply_type)
  361. << ", current op num: " << op_info_.size() << " is_dynamic_shape:" << is_dynamic_shape;
  362. return nullptr;
  363. }
  364. bool OpLib::GetRefInfo(const std::shared_ptr<OpInfo> &op_info) {
  365. MS_EXCEPTION_IF_NULL(op_info);
  366. const auto &output_infos = op_info->outputs_ptr();
  367. const auto &input_infos = op_info->inputs_ptr();
  368. for (size_t out_index = 0; out_index < output_infos.size(); out_index++) {
  369. MS_EXCEPTION_IF_NULL(output_infos[out_index]);
  370. const auto &out_name = output_infos[out_index]->name();
  371. for (size_t in_index = 0; in_index < input_infos.size(); in_index++) {
  372. MS_EXCEPTION_IF_NULL(input_infos[in_index]);
  373. const auto &in_name = input_infos[in_index]->name();
  374. if (out_name == in_name) {
  375. if (op_info->has_ref_index(out_index)) {
  376. MS_LOG(ERROR) << "The out_index " << out_index << " is already in ref_info";
  377. return false;
  378. }
  379. op_info->add_ref_pair(out_index, in_index);
  380. }
  381. }
  382. }
  383. return true;
  384. }
  385. bool OpLib::CheckRepetition(const std::shared_ptr<OpInfo> &op_info) {
  386. MS_EXCEPTION_IF_NULL(op_info);
  387. for (auto [iter, end] = op_info_.equal_range(op_info->op_name()); iter != end; ++iter) {
  388. auto &exist_op_info = (*iter).second;
  389. MS_EXCEPTION_IF_NULL(exist_op_info);
  390. if (exist_op_info->equals_to(op_info)) {
  391. return true;
  392. }
  393. }
  394. return false;
  395. }
  396. } // namespace kernel
  397. } // namespace mindspore