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

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