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.

common_utils.cc 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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/common_utils.h"
  17. #include <unordered_map>
  18. #include <map>
  19. #include <iostream>
  20. #include <fstream>
  21. #include "nlohmann/json.hpp"
  22. #include "session/anf_runtime_algorithm.h"
  23. #include "common/utils.h"
  24. namespace mindspore {
  25. namespace kernel {
  26. const std::unordered_map<std::string, TypeId> type_id_maps = {
  27. {"float", TypeId::kNumberTypeFloat32}, {"float16", TypeId::kNumberTypeFloat16},
  28. {"float32", TypeId::kNumberTypeFloat32}, {"float64", TypeId::kNumberTypeFloat64},
  29. {"int", TypeId::kNumberTypeInt}, {"int8", TypeId::kNumberTypeInt8},
  30. {"int16", TypeId::kNumberTypeInt16}, {"int32", TypeId::kNumberTypeInt32},
  31. {"int64", TypeId::kNumberTypeInt64}, {"uint", TypeId::kNumberTypeUInt},
  32. {"uint8", TypeId::kNumberTypeUInt8}, {"uint16", TypeId::kNumberTypeUInt16},
  33. {"uint32", TypeId::kNumberTypeUInt32}, {"uint64", TypeId::kNumberTypeUInt64},
  34. {"bool", TypeId::kNumberTypeBool},
  35. };
  36. const std::map<TypeId, std::string> type_id_str_map = {
  37. {TypeId::kNumberTypeFloat32, "float32"}, {TypeId::kNumberTypeFloat16, "float16"},
  38. {TypeId::kNumberTypeFloat, "float"}, {TypeId::kNumberTypeFloat64, "float64"},
  39. {TypeId::kNumberTypeInt, "int"}, {TypeId::kNumberTypeInt8, "int8"},
  40. {TypeId::kNumberTypeInt16, "int16"}, {TypeId::kNumberTypeInt32, "int32"},
  41. {TypeId::kNumberTypeInt64, "int64"}, {TypeId::kNumberTypeUInt, "uint"},
  42. {TypeId::kNumberTypeUInt8, "uint8"}, {TypeId::kNumberTypeUInt16, "uint16"},
  43. {TypeId::kNumberTypeUInt32, "uint32"}, {TypeId::kNumberTypeUInt64, "uint64"},
  44. {TypeId::kNumberTypeBool, "bool"},
  45. };
  46. const std::map<std::string, std::string> DATATYPE_STRING_MAP{
  47. {"Float32", "float32"}, {"Float16", "float16"}, {"Int8", "int8"}, {"Int16", "int16"},
  48. {"UInt16", "uint16"}, {"UInt8", "uint8"}, {"Int32", "int32"}, {"UInt32", "uint32"},
  49. {"Int64", "int64"}, {"UInt64", "uint64"}, {"Bool_", "bool"}, {"Float64", "double"},
  50. };
  51. const std::unordered_map<std::string, std::string> dtype_shortdtype_map_ = {
  52. {"float16", "f16"}, {"float32", "f32"}, {"float64", "f64"}, {"int8", "i8"}, {"int16", "i16"}, {"int32", "i32"},
  53. {"int64", "i64"}, {"uint8", "u8"}, {"uint16", "u16"}, {"uint32", "u32"}, {"uint64", "u64"}, {"bool", "bool"},
  54. };
  55. const std::unordered_map<std::string, size_t> dtype_nbyte_map = {
  56. {"float16", sizeof(float) / 2}, {"float32", sizeof(float)}, {"float64", sizeof(float) * 2},
  57. {"int8", sizeof(int) / 4}, {"int16", sizeof(int) / 2}, {"int32", sizeof(int)},
  58. {"int64", sizeof(int) * 2}, {"uint8", sizeof(int) / 4}, {"uint16", sizeof(int) / 2},
  59. {"uint32", sizeof(int)}, {"uint64", sizeof(int) * 2}, {"bool", sizeof(char)},
  60. };
  61. const std::unordered_map<std::string, FusionType> fusion_type_maps = {
  62. {"CONVLUTION", FusionType::CONVLUTION}, {"ELEMWISE", FusionType::ELEMWISE}, {"COMMREDUCE", FusionType::COMMREDUCE},
  63. {"SEGMENT", FusionType::SEGMENT}, {"OPAQUE", FusionType::OPAQUE},
  64. };
  65. bool IsAtomicNode(const CNodePtr &kernel_node) {
  66. MS_EXCEPTION_IF_NULL(kernel_node);
  67. auto kernel_mod = AnfAlgo::GetKernelMod(kernel_node);
  68. MS_EXCEPTION_IF_NULL(kernel_mod);
  69. auto parameters_indexs = kernel_mod->GenParameters();
  70. if (parameters_indexs.empty()) {
  71. return false;
  72. }
  73. auto atomic_flag = false;
  74. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  75. size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
  76. auto workspace_size_list = kernel_mod->GetWorkspaceSizeList();
  77. size_t workspace_num = kernel_mod->GetWorkspaceSizeList().size();
  78. if (input_num + workspace_num + output_num > parameters_indexs.size()) {
  79. size_t lossNum = (input_num + workspace_num + output_num) - parameters_indexs.size();
  80. for (size_t i = 0; i < lossNum; i++) {
  81. parameters_indexs.push_back(0);
  82. }
  83. }
  84. std::vector<int> clean_output_indexs;
  85. // in parameters data sort as input->workspace->output
  86. size_t index = 0;
  87. while (index < output_num) {
  88. if (parameters_indexs[input_num + workspace_num + index] == 1) {
  89. atomic_flag = true;
  90. clean_output_indexs.push_back(SizeToInt(index));
  91. }
  92. index++;
  93. }
  94. if (atomic_flag) {
  95. AnfAlgo::SetNodeAttr(kAttrAutomicOutputIndexs, MakeValue(clean_output_indexs), kernel_node);
  96. }
  97. for (size_t i = 0; i < workspace_num; ++i) {
  98. if (parameters_indexs[input_num + i] == 1) {
  99. atomic_flag = true;
  100. AnfAlgo::SetNodeAttr(kAttrAutomicWorkspaceSize,
  101. MakeValue(std::accumulate(workspace_size_list.begin(), workspace_size_list.end(), 0)),
  102. kernel_node);
  103. break;
  104. }
  105. }
  106. return atomic_flag;
  107. }
  108. bool KernelMeta::ReadIndex(const std::string &bin_dir) {
  109. DIR *dir = opendir(bin_dir.c_str());
  110. if (dir == nullptr) {
  111. #if defined(_WIN32) || defined(_WIN64)
  112. auto ret = mkdir(bin_dir.c_str());
  113. #else
  114. auto ret = mkdir(bin_dir.c_str(), S_IRWXG | S_IRWXU);
  115. #endif
  116. if (ret != 0) {
  117. MS_LOG(INFO) << "kernel dir not exist[" << bin_dir << "].";
  118. return false;
  119. }
  120. dir = opendir(bin_dir.c_str());
  121. }
  122. struct dirent *entry;
  123. while ((entry = readdir(dir)) != nullptr) {
  124. string bin_dir_tmp = bin_dir;
  125. std::string cce_json = entry->d_name;
  126. if (cce_json.length() <= 5) {
  127. continue;
  128. }
  129. std::string suffix = cce_json.substr(cce_json.length() - 5);
  130. if (suffix != kJsonSuffix) {
  131. continue;
  132. }
  133. auto sp = cce_json.rfind('/');
  134. if (sp != std::string::npos) {
  135. continue;
  136. }
  137. sp = cce_json.rfind('.');
  138. if (sp == std::string::npos) {
  139. continue;
  140. }
  141. auto kernel_name = cce_json.substr(0, sp);
  142. (void)bin_dir_tmp.append("/");
  143. (void)bin_dir_tmp.append(cce_json);
  144. kernel_meta_map_[kernel_name] = bin_dir_tmp;
  145. }
  146. (void)closedir(dir);
  147. MS_LOG(INFO) << "Cache kernel initialized, kernel size[" << kernel_meta_map_.size() << "].";
  148. initialized_ = true;
  149. return true;
  150. }
  151. std::string KernelMeta::Search(const std::string &kernel_name) const {
  152. if (!initialized_) {
  153. return "";
  154. }
  155. auto iter = kernel_meta_map_.find(kernel_name);
  156. if (iter == kernel_meta_map_.end()) {
  157. return "";
  158. } else {
  159. return iter->second;
  160. }
  161. }
  162. bool KernelMeta::Insert(const std::string &kernel_name, const std::string &cce_json) {
  163. if (!initialized_) {
  164. return false;
  165. }
  166. kernel_meta_map_[kernel_name] = cce_json;
  167. return true;
  168. }
  169. bool CheckCache(const std::string &kernel_name) {
  170. // check cache.
  171. KernelMeta *bin_map = KernelMeta::GetInstance();
  172. if (bin_map == nullptr) {
  173. MS_LOG(DEBUG) << "kernel cache is invalid.";
  174. return false;
  175. }
  176. std::string cce_json = bin_map->Search(kernel_name);
  177. bool ret = (!cce_json.empty());
  178. if (ret) {
  179. MS_LOG(INFO) << "Kernel name:" << kernel_name << " has registed.";
  180. } else {
  181. MS_LOG(INFO) << "Kernel name:" << kernel_name << " will been registed.";
  182. }
  183. return ret;
  184. }
  185. KernelPackPtr SearchCache(const std::string &kernel_name, const std::string &processor) {
  186. // search cache.
  187. KernelMeta *bin_map = KernelMeta::GetInstance();
  188. if (bin_map == nullptr) {
  189. MS_LOG(DEBUG) << "kernel cache is invalid.";
  190. return nullptr;
  191. }
  192. std::string cce_json = bin_map->Search(kernel_name);
  193. if (!cce_json.empty()) {
  194. KernelPackPtr kernel_pack = std::make_shared<KernelPack>();
  195. // just a tmp solution.
  196. if (!kernel_pack->ReadFromJsonFile(cce_json, processor)) {
  197. MS_LOG(DEBUG) << "Read cache json and bin file failed[" << cce_json << "].";
  198. return nullptr;
  199. } else {
  200. return kernel_pack;
  201. }
  202. } else {
  203. MS_LOG(INFO) << "cache kernel not found[" << kernel_name << "].";
  204. return nullptr;
  205. }
  206. }
  207. KernelPackPtr InsertCache(const std::string &kernel_name, const std::string &processor) {
  208. MS_LOG(INFO) << "kernel name:" << kernel_name << ", processr:" << processor;
  209. std::string cce_json;
  210. if (processor == kProcessorAiCore || processor == kProcessorAiCpu) {
  211. cce_json = kCceKernelMeta;
  212. } else {
  213. cce_json = kGpuKernelMeta;
  214. }
  215. (void)cce_json.append(kernel_name).append(kJsonSuffix);
  216. KernelPackPtr kernel_pack = std::make_shared<KernelPack>();
  217. if (!kernel_pack->ReadFromJsonFile(cce_json, processor)) {
  218. MS_LOG(DEBUG) << "Read json and bin file failed[" << cce_json << "].";
  219. return nullptr;
  220. }
  221. KernelMeta *bin_map = KernelMeta::GetInstance();
  222. if (bin_map == nullptr) {
  223. MS_LOG(DEBUG) << "kernel cache is invalid.";
  224. return nullptr;
  225. }
  226. if (bin_map->Insert(kernel_name, cce_json)) {
  227. MS_LOG(INFO) << "Insert to cache success[" << cce_json << "], kernelname[" << kernel_name << "].";
  228. }
  229. return kernel_pack;
  230. }
  231. TypeId DtypeToTypeId(const std::string &dtypes) {
  232. auto iter = type_id_maps.find(dtypes);
  233. if (iter != type_id_maps.end()) {
  234. return iter->second;
  235. } else {
  236. MS_EXCEPTION(ArgumentError) << "Illegal input device dtype:" << dtypes;
  237. }
  238. }
  239. std::string Dtype2String(const std::string &dtypes) {
  240. auto iter = DATATYPE_STRING_MAP.find(dtypes);
  241. if (iter == DATATYPE_STRING_MAP.end()) {
  242. MS_EXCEPTION(ArgumentError) << "Illegal input dtype:" << dtypes;
  243. }
  244. return iter->second;
  245. }
  246. std::string TypeId2String(TypeId type_id) {
  247. auto iter = type_id_str_map.find(type_id);
  248. if (iter == type_id_str_map.end()) {
  249. MS_EXCEPTION(ArgumentError) << "Illegal input dtype." << TypeIdLabel(type_id);
  250. }
  251. return iter->second;
  252. }
  253. std::string Dtype2ShortType(const std::string &dtypes) {
  254. auto iter = dtype_shortdtype_map_.find(dtypes);
  255. if (iter != dtype_shortdtype_map_.end()) {
  256. return iter->second;
  257. } else {
  258. MS_EXCEPTION(ArgumentError) << "Illegal input dtype:" << dtypes;
  259. }
  260. }
  261. size_t GetDtypeNbyte(const std::string &dtypes) {
  262. auto iter = dtype_nbyte_map.find(dtypes);
  263. if (iter != dtype_nbyte_map.end()) {
  264. return iter->second;
  265. } else {
  266. MS_EXCEPTION(ArgumentError) << "Illegal input dtype:" << dtypes;
  267. }
  268. }
  269. bool SetInputKernelBuilderInfo(const std::vector<std::shared_ptr<OpIOInfo>> &inputs, size_t real_input_num,
  270. size_t builder_idex, const std::vector<int> &dyn_input_sizes,
  271. const std::shared_ptr<KernelBuildInfo::KernelBuildInfoBuilder> &builder) {
  272. MS_EXCEPTION_IF_NULL(builder);
  273. std::vector<TypeId> inputs_device_type;
  274. std::vector<std::string> inputs_format;
  275. size_t dyn_input_idx = 0;
  276. size_t kernel_info_index = 0;
  277. MS_EXCEPTION_IF_NULL(inputs[0]);
  278. size_t kernel_info_cnt = inputs[0]->dtypes().size();
  279. for (const auto &input : inputs) {
  280. MS_EXCEPTION_IF_NULL(input);
  281. std::string param_type = input->param_type();
  282. std::vector<std::string> dtypes = input->dtypes();
  283. std::vector<std::string> formats = input->formats();
  284. if (dtypes.size() != kernel_info_cnt || formats.size() != kernel_info_cnt) {
  285. MS_LOG(DEBUG) << "Set input kernel builder info, dtyps size != formats size.";
  286. return false;
  287. }
  288. if (param_type == "dynamic") {
  289. if (dyn_input_sizes.empty()) {
  290. MS_LOG(DEBUG) << "Set input kernel builder info, dyn_input_sizes's size is 0 when param_type is dynamic";
  291. return false;
  292. }
  293. for (int t = 0; t < dyn_input_sizes[dyn_input_idx]; t++) {
  294. kernel_info_index++;
  295. auto type_id = DtypeToTypeId(dtypes[builder_idex]);
  296. inputs_device_type.push_back(type_id);
  297. inputs_format.push_back(formats[builder_idex]);
  298. }
  299. dyn_input_idx++;
  300. } else if (param_type == "required") {
  301. kernel_info_index++;
  302. auto type_id = DtypeToTypeId(dtypes[builder_idex]);
  303. inputs_device_type.push_back(type_id);
  304. inputs_format.push_back(formats[builder_idex]);
  305. } else {
  306. if (kernel_info_index < real_input_num) {
  307. MS_LOG(INFO) << "Set input kernel builder info, input type is optional, input index is :" << kernel_info_index;
  308. kernel_info_index++;
  309. auto type_id = DtypeToTypeId(dtypes[builder_idex]);
  310. inputs_device_type.push_back(type_id);
  311. inputs_format.push_back(formats[builder_idex]);
  312. }
  313. }
  314. }
  315. builder->SetInputsDeviceType(inputs_device_type);
  316. builder->SetInputsFormat(inputs_format);
  317. return true;
  318. }
  319. bool SetOutputKernelBuilderInfo(const std::vector<std::shared_ptr<OpIOInfo>> &outputs, size_t builder_idex,
  320. const size_t &real_output_num,
  321. const std::shared_ptr<KernelBuildInfo::KernelBuildInfoBuilder> &builder) {
  322. // not now but in the next we need to support dynamic output case
  323. MS_EXCEPTION_IF_NULL(builder);
  324. size_t output_idx = 0;
  325. std::vector<TypeId> outputs_device_type;
  326. std::vector<std::string> outputs_format;
  327. MS_EXCEPTION_IF_NULL(outputs[0]);
  328. size_t kernel_info_cnt = outputs[0]->dtypes().size();
  329. for (const auto &output : outputs) {
  330. MS_EXCEPTION_IF_NULL(output);
  331. if (output_idx >= real_output_num) {
  332. MS_LOG(DEBUG) << "real_output_num:" << real_output_num << ", output_idx:" << output_idx << " is out of limit!";
  333. continue;
  334. }
  335. size_t output_num = 0;
  336. if (output->param_type() == "dynamic") {
  337. if (outputs.size() > 1) {
  338. MS_EXCEPTION(ArgumentError) << "Dynamic output is unsupported multi output!";
  339. }
  340. output_num = real_output_num;
  341. } else if (output->param_type() == "required") {
  342. output_num = 1;
  343. } else {
  344. if (output_idx < real_output_num) {
  345. MS_LOG(INFO) << "Set output kernel builder info, output type is optional, output index is :" << output_idx;
  346. output_num = 1;
  347. }
  348. }
  349. for (size_t i = 0; i < output_num; i++) {
  350. std::vector<std::string> dtypes = output->dtypes();
  351. std::vector<std::string> formats = output->formats();
  352. if (dtypes.size() != kernel_info_cnt || formats.size() != kernel_info_cnt) {
  353. MS_LOG(DEBUG) << "Set output kernel builder info, dtyps size != formats size.";
  354. return false;
  355. }
  356. auto type_id = DtypeToTypeId(dtypes[builder_idex]);
  357. outputs_device_type.push_back(type_id);
  358. outputs_format.push_back(formats[builder_idex]);
  359. output_idx++;
  360. }
  361. }
  362. builder->SetOutputsFormat(outputs_format);
  363. builder->SetOutputsDeviceType(outputs_device_type);
  364. return true;
  365. }
  366. void SetKernelBuildInfo(const std::shared_ptr<KernelBuildInfo::KernelBuildInfoBuilder> &builder, Processor processor,
  367. const std::shared_ptr<const OpInfo> &op_info_ptr) {
  368. MS_EXCEPTION_IF_NULL(builder);
  369. MS_EXCEPTION_IF_NULL(op_info_ptr);
  370. auto imply_type = op_info_ptr->imply_type();
  371. builder->SetProcessor(processor);
  372. std::string fusion_type = op_info_ptr->fusion_type();
  373. auto iter = fusion_type_maps.find(fusion_type);
  374. if (iter != fusion_type_maps.end()) {
  375. builder->SetFusionType(iter->second);
  376. } else {
  377. if (imply_type == kAKG) {
  378. MS_EXCEPTION(NotExistsError) << "Illegal fusion type from dsl register:" << fusion_type;
  379. }
  380. }
  381. if (imply_type == kAKG) {
  382. builder->SetKernelType(AUTO_DIFF_KERNEL);
  383. } else if (imply_type == kAICPU) {
  384. builder->SetKernelType(AICPU_KERNEL);
  385. } else {
  386. builder->SetKernelType(TBE_KERNEL);
  387. }
  388. }
  389. bool ParseMetadata(const CNodePtr &kernel_node, const std::shared_ptr<const OpInfo> &op_info_ptr, Processor processor,
  390. std::vector<std::shared_ptr<KernelBuildInfo>> *const kernel_info_list) {
  391. MS_EXCEPTION_IF_NULL(kernel_node);
  392. MS_EXCEPTION_IF_NULL(kernel_info_list);
  393. size_t real_input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  394. size_t real_output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
  395. std::vector<std::shared_ptr<OpIOInfo>> inputs = op_info_ptr->inputs_ptr();
  396. std::vector<std::shared_ptr<OpIOInfo>> outputs = op_info_ptr->outputs_ptr();
  397. std::vector<int> dyn_input_sizes;
  398. auto primitive = AnfAlgo::GetCNodePrimitive(kernel_node);
  399. MS_EXCEPTION_IF_NULL(primitive);
  400. if (primitive->GetAttr("dyn_input_sizes") != nullptr) {
  401. dyn_input_sizes = GetValue<std::vector<int>>(primitive->GetAttr("dyn_input_sizes"));
  402. }
  403. if (inputs.size() > 0) {
  404. MS_EXCEPTION_IF_NULL(inputs[0]);
  405. size_t kernel_info_cnt = inputs[0]->dtypes().size();
  406. for (size_t j = 0; j < kernel_info_cnt; j++) {
  407. auto builder = std::make_shared<KernelBuildInfo::KernelBuildInfoBuilder>();
  408. MS_EXCEPTION_IF_NULL(builder);
  409. SetKernelBuildInfo(builder, processor, op_info_ptr);
  410. if (!SetInputKernelBuilderInfo(inputs, real_input_num, j, dyn_input_sizes, builder)) {
  411. MS_LOG(DEBUG) << "Parse kernel metadata, set inputs kernel builder info failed.";
  412. return false;
  413. }
  414. if (outputs.size() > 0) {
  415. if (!SetOutputKernelBuilderInfo(outputs, j, real_output_num, builder)) {
  416. MS_LOG(DEBUG) << "Parse kernel metadata, set outputs kernel builder info failed.";
  417. return false;
  418. }
  419. }
  420. kernel_info_list->push_back(builder->Build());
  421. }
  422. } else if (outputs.size() > 0) {
  423. MS_EXCEPTION_IF_NULL(outputs[0]);
  424. size_t kernel_info_cnt = outputs[0]->dtypes().size();
  425. for (size_t j = 0; j < kernel_info_cnt; j++) {
  426. auto builder = std::make_shared<KernelBuildInfo::KernelBuildInfoBuilder>();
  427. MS_EXCEPTION_IF_NULL(builder);
  428. SetKernelBuildInfo(builder, processor, op_info_ptr);
  429. if (!SetOutputKernelBuilderInfo(outputs, j, real_output_num, builder)) {
  430. MS_LOG(DEBUG) << "Parse kernel metadata, set outputs kernel builder info failed.";
  431. return false;
  432. }
  433. kernel_info_list->push_back(builder->Build());
  434. }
  435. } else {
  436. if (processor == AICPU) {
  437. auto builder = std::make_shared<KernelBuildInfo::KernelBuildInfoBuilder>();
  438. MS_EXCEPTION_IF_NULL(builder);
  439. SetKernelBuildInfo(builder, processor, op_info_ptr);
  440. kernel_info_list->push_back(builder->Build());
  441. }
  442. }
  443. return true;
  444. }
  445. void SaveJsonInfo(const std::string &json_name, const std::string &info) {
  446. char real_path[PATH_MAX] = {0};
  447. std::string path = kCceKernelMeta + json_name + kInfoSuffix;
  448. if (path.size() > PATH_MAX) {
  449. MS_LOG(DEBUG) << "file path " << path << " is too long.";
  450. return;
  451. }
  452. std::ofstream filewrite;
  453. filewrite.open(path);
  454. if (!filewrite.is_open()) {
  455. return;
  456. }
  457. filewrite << info << std::endl;
  458. filewrite.close();
  459. #if defined(_WIN32) || defined(_WIN64)
  460. if (nullptr == _fullpath(real_path, path.c_str(), PATH_MAX)) {
  461. MS_LOG(DEBUG) << "dir " << path << " does not exit.";
  462. return;
  463. }
  464. #else
  465. if (nullptr == realpath(path.c_str(), real_path)) {
  466. MS_LOG(DEBUG) << "dir " << path << " does not exit.";
  467. return;
  468. }
  469. #endif
  470. MS_LOG(INFO) << "real path is :" << real_path;
  471. if (chmod(real_path, S_IRUSR) == -1) {
  472. MS_LOG(DEBUG) << "modify file:" << real_path << " to read only fail.";
  473. }
  474. }
  475. std::string GetProcessor(const AnfNodePtr &anf_node) {
  476. MS_EXCEPTION_IF_NULL(anf_node);
  477. std::string device;
  478. switch (AnfAlgo::GetProcessor(anf_node)) {
  479. case Processor::AICORE:
  480. device = kProcessorAiCore;
  481. break;
  482. case Processor::AICPU:
  483. device = kProcessorAiCpu;
  484. break;
  485. case Processor::CUDA:
  486. device = kProcessorCuda;
  487. break;
  488. default:
  489. MS_LOG(DEBUG) << "Unknown processor type.";
  490. break;
  491. }
  492. return device;
  493. }
  494. } // namespace kernel
  495. } // namespace mindspore