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