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.9 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. template <typename T>
  76. std::string ArrayToString(const std::vector<T> &array) {
  77. std::string result = "{";
  78. std::for_each(array.begin(), array.end(), [&result](const T &t) { result += std::to_string(t) + ", "; });
  79. return result + "}";
  80. }
  81. std::string TensorsToString(const std::vector<Tensor *> &tensors, const std::string &is_input) {
  82. MemoryAllocator *allocator = MemoryAllocator::GetInstance();
  83. std::string info;
  84. for (const auto &tensor : tensors) {
  85. if (tensor->category() == Tensor::Category::CONST_TENSOR) {
  86. continue;
  87. }
  88. info += " {\n";
  89. info += " int dim[] = " + ArrayToString(tensor->shape()) + ";\n";
  90. info += " MicroTensor tensor = {";
  91. info += EnumMicroTensorDataType(tensor->data_type()) + ", ";
  92. info += EnumMicroTensorFormat(tensor->format()) + ", ";
  93. info += std::to_string(tensor->shape().size()) + ", dim, ";
  94. info += allocator->GetRuntimeAddr(tensor) + "};\n";
  95. info += " fprintf(output_file, \"" + is_input + " Tensor: " + allocator->GetRuntimeAddr(tensor) + "\\n\");\n";
  96. info += " PrintTensor(&tensor, output_file, \"" + is_input + "\");\n";
  97. info += " }\n";
  98. }
  99. return info;
  100. }
  101. std::vector<std::string> AddDumpDataInfo(const std::vector<std::string> &blocks,
  102. const std::vector<std::unique_ptr<OperatorCoder>> &opcoders) {
  103. std::vector<std::string> results;
  104. if (blocks.size() != opcoders.size()) {
  105. MS_LOG(ERROR) << "error, coder blocks size is not equal to opcoders size";
  106. return results;
  107. }
  108. size_t num = opcoders.size();
  109. for (size_t i = 0; i < num; ++i) {
  110. auto &opcoder = opcoders.at(i);
  111. std::string code = blocks.at(i);
  112. std::string name = opcoder->name();
  113. code += " {\n";
  114. code += " FILE *output_file = fopen(\"./" + name + ".ir\", \"w\");\n";
  115. code += " fprintf(output_file, \"Node:" + name + "\\n\");\n";
  116. code += TensorsToString(opcoder->input_tensors(), "input");
  117. code += TensorsToString(opcoder->output_tensors(), "output");
  118. code += " fclose(output_file);\n";
  119. code += " }\n";
  120. results.emplace_back(code);
  121. }
  122. return results;
  123. }
  124. std::vector<std::string> SplitString(std::string str, const std::string &pattern) {
  125. std::vector<std::string> results;
  126. if (str.empty()) {
  127. MS_LOG(ERROR) << "source string is empty";
  128. return results;
  129. }
  130. str += pattern;
  131. while (!str.empty()) {
  132. size_t size = str.size();
  133. size_t pos = str.find(pattern);
  134. std::string sub_string = str.substr(0, pos);
  135. results.push_back(sub_string);
  136. str = str.substr(pos + 1, size);
  137. }
  138. return results;
  139. }
  140. } // namespace mindspore::lite::micro