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.

coder_utils.cc 4.6 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Copyright 2021 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 "coder/utils/coder_utils.h"
  17. #include <set>
  18. #include <queue>
  19. #include <string>
  20. #include <memory>
  21. #include <fstream>
  22. #include "coder/log.h"
  23. #include "coder/utils/type_cast.h"
  24. #include "coder/allocator/allocator.h"
  25. namespace mindspore::lite::micro {
  26. template <typename T>
  27. void TensorDataToFile(const lite::Tensor *tensor, std::ofstream &ofs) {
  28. const int NUM = 45;
  29. T *data = reinterpret_cast<T *>(tensor->data_c());
  30. if (data == nullptr) {
  31. MS_LOG(ERROR) << "data is nullptr";
  32. return;
  33. }
  34. ofs << "{\n";
  35. if (typeid(T) == typeid(float)) {
  36. ofs.precision(kWeightPrecision);
  37. }
  38. int len = tensor->ElementsNum();
  39. for (int i = 0; i < len; ++i) {
  40. ofs << std::to_string(data[i]) << ", ";
  41. if (i % NUM == NUM - 1) {
  42. ofs << "\n";
  43. }
  44. }
  45. ofs << "\n};\n\n";
  46. }
  47. void PrintTensorData(const lite::Tensor *tensor, std::ofstream &ofs) {
  48. TypeId type = tensor->data_type();
  49. switch (tensor->data_type()) {
  50. case kNumberTypeFloat:
  51. case kNumberTypeFloat32:
  52. TensorDataToFile<float>(tensor, ofs);
  53. break;
  54. case kNumberTypeInt8:
  55. TensorDataToFile<int8_t>(tensor, ofs);
  56. break;
  57. case kNumberTypeInt:
  58. case kNumberTypeInt32:
  59. TensorDataToFile<int32_t>(tensor, ofs);
  60. break;
  61. case kNumberTypeInt64:
  62. TensorDataToFile<int64_t>(tensor, ofs);
  63. break;
  64. case kNumberTypeUInt8:
  65. TensorDataToFile<uint8_t>(tensor, ofs);
  66. break;
  67. case kNumberTypeUInt32:
  68. TensorDataToFile<uint32_t>(tensor, ofs);
  69. break;
  70. default:
  71. MS_LOG(ERROR) << "unsupported data type: " << EnumNameDataType(type);
  72. break;
  73. }
  74. }
  75. std::string TensorsToString(const std::vector<Tensor *> &tensors, const std::string &is_input) {
  76. MemoryAllocator *allocator = MemoryAllocator::GetInstance();
  77. std::string info;
  78. for (const auto &tensor : tensors) {
  79. if (tensor->category() == Tensor::Category::CONST_TENSOR) {
  80. continue;
  81. }
  82. info += " {\n";
  83. info += " int dim[] = " + ArrayToString(tensor->shape()) + ";\n";
  84. info += " MicroTensor tensor = {";
  85. info += EnumMicroTensorDataType(tensor->data_type()) + ", ";
  86. info += EnumMicroTensorFormat(tensor->format()) + ", ";
  87. info += std::to_string(tensor->shape().size()) + ", dim, ";
  88. info += allocator->GetRuntimeAddr(tensor) + "};\n";
  89. info += " fprintf(output_file, \"" + is_input + " Tensor: " + allocator->GetRuntimeAddr(tensor) + "\\n\");\n";
  90. info += " PrintTensor(&tensor, output_file, \"" + is_input + "\");\n";
  91. info += " }\n";
  92. }
  93. return info;
  94. }
  95. std::vector<std::string> AddDumpDataInfo(const std::vector<std::string> &blocks,
  96. const std::vector<std::unique_ptr<OperatorCoder>> &opcoders) {
  97. std::vector<std::string> results;
  98. if (blocks.size() != opcoders.size()) {
  99. MS_LOG(ERROR) << "error, coder blocks size is not equal to opcoders size";
  100. return results;
  101. }
  102. size_t num = opcoders.size();
  103. for (size_t i = 0; i < num; ++i) {
  104. auto &opcoder = opcoders.at(i);
  105. std::string code = blocks.at(i);
  106. std::string name = opcoder->name();
  107. code += " {\n";
  108. code += " FILE *output_file = fopen(\"./" + name + ".ir\", \"w\");\n";
  109. code += " fprintf(output_file, \"Node:" + name + "\\n\");\n";
  110. code += TensorsToString(opcoder->input_tensors(), "input");
  111. code += TensorsToString(opcoder->output_tensors(), "output");
  112. code += " fclose(output_file);\n";
  113. code += " }\n";
  114. results.emplace_back(code);
  115. }
  116. return results;
  117. }
  118. std::vector<std::string> SplitString(std::string str, const std::string &pattern) {
  119. std::vector<std::string> results;
  120. if (str.empty()) {
  121. MS_LOG(ERROR) << "source string is empty";
  122. return results;
  123. }
  124. str += pattern;
  125. while (!str.empty()) {
  126. size_t size = str.size();
  127. size_t pos = str.find(pattern);
  128. std::string sub_string = str.substr(0, pos);
  129. results.push_back(sub_string);
  130. str = str.substr(pos + 1, size);
  131. }
  132. return results;
  133. }
  134. } // namespace mindspore::lite::micro