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

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