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.

parser_st_utils.cc 4.5 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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 "st/parser_st_utils.h"
  17. #include "framework/common/debug/ge_log.h"
  18. #include <limits.h>
  19. #include <google/protobuf/io/coded_stream.h>
  20. #include <google/protobuf/io/zero_copy_stream_impl.h>
  21. #include <google/protobuf/text_format.h>
  22. #include <fstream>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. namespace ge {
  27. void ParerSTestsUtils::ClearParserInnerCtx() {
  28. ge::GetParserContext().input_nodes_format_map.clear();
  29. ge::GetParserContext().output_formats.clear();
  30. ge::GetParserContext().user_input_dims.clear();
  31. ge::GetParserContext().input_dims.clear();
  32. ge::GetParserContext().op_conf_map.clear();
  33. ge::GetParserContext().user_out_nodes.clear();
  34. ge::GetParserContext().default_out_nodes.clear();
  35. ge::GetParserContext().out_nodes_map.clear();
  36. ge::GetParserContext().user_out_tensors.clear();
  37. ge::GetParserContext().net_out_nodes.clear();
  38. ge::GetParserContext().out_tensor_names.clear();
  39. ge::GetParserContext().data_tensor_names.clear();
  40. ge::GetParserContext().is_dynamic_input = false;
  41. ge::GetParserContext().train_flag = false;
  42. ge::GetParserContext().format = domi::DOMI_TENSOR_ND;
  43. ge::GetParserContext().type = domi::FRAMEWORK_RESERVED;
  44. ge::GetParserContext().run_mode = GEN_OM_MODEL;
  45. ge::GetParserContext().custom_proto_path = "";
  46. ge::GetParserContext().caffe_proto_path = "";
  47. ge::GetParserContext().enable_scope_fusion_passes = "";
  48. GELOGI("Clear parser inner context successfully.");
  49. }
  50. MemBuffer* ParerSTestsUtils::MemBufferFromFile(const char *path) {
  51. char path_temp[PATH_MAX + 1] = {0x00};
  52. if(strlen(path) > PATH_MAX || nullptr == realpath(path, path_temp)) {
  53. return nullptr;
  54. }
  55. FILE *fp = fopen(path_temp, "r+");
  56. if (fp == nullptr) {
  57. return nullptr;
  58. }
  59. // get model file length
  60. if (0 != fseek(fp, 0, SEEK_END)) {
  61. fclose(fp);
  62. return nullptr;
  63. }
  64. long file_length = ftell(fp);
  65. if (fseek(fp, 0, SEEK_SET)) {
  66. fclose(fp);
  67. return nullptr;
  68. }
  69. if (file_length <= 0) {
  70. fclose(fp);
  71. return nullptr;
  72. }
  73. // alloc model buffer
  74. void *data = malloc((unsigned int)file_length);
  75. if (!data) {
  76. fclose(fp);
  77. return nullptr;
  78. }
  79. // read file into memory
  80. uint32_t read_size = (uint32_t)fread(data, 1, (unsigned int)file_length, fp);
  81. // check if read success
  82. if ((long)read_size != file_length) {
  83. free(data);
  84. data = nullptr;
  85. fclose(fp);
  86. return nullptr;
  87. }
  88. // close model file
  89. fclose(fp);
  90. // create an MemBuffer
  91. MemBuffer* membuf = new MemBuffer();
  92. if (!membuf) {
  93. free(data);
  94. data = nullptr;
  95. return nullptr;
  96. }
  97. membuf->data = malloc((unsigned int)read_size);
  98. // set size && data
  99. membuf->size = (uint32_t)read_size;
  100. memcpy((char*)membuf->data, (char*)data, read_size);
  101. free(data);
  102. return membuf;
  103. }
  104. bool ParerSTestsUtils::ReadProtoFromText(const char *file, google::protobuf::Message *message) {
  105. std::ifstream fs(file);
  106. if (!fs.is_open()) {
  107. return false;
  108. }
  109. google::protobuf::io::IstreamInputStream input(&fs);
  110. bool ret = google::protobuf::TextFormat::Parse(&input, message);
  111. fs.close();
  112. return ret;
  113. }
  114. void ParerSTestsUtils::WriteProtoToBinaryFile(const google::protobuf::Message &proto, const char *filename) {
  115. size_t size = proto.ByteSizeLong();
  116. char *buf = new char[size];
  117. proto.SerializeToArray(buf, size);
  118. std::ofstream out(filename);
  119. out.write(buf, size);
  120. out.close();
  121. delete[] buf;
  122. }
  123. void ParerSTestsUtils::WriteProtoToTextFile(const google::protobuf::Message &proto, const char *filename) {
  124. const int32_t fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 384U);
  125. if (fd >= 0) {
  126. google::protobuf::io::FileOutputStream output(fd);
  127. google::protobuf::TextFormat::Print(proto, &output);
  128. output.Close();
  129. close(fd);
  130. }
  131. }
  132. } // namespace ge