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 3.3 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. namespace ge {
  20. void ParerSTestsUtils::ClearParserInnerCtx() {
  21. ge::GetParserContext().input_nodes_format_map.clear();
  22. ge::GetParserContext().output_formats.clear();
  23. ge::GetParserContext().user_input_dims.clear();
  24. ge::GetParserContext().input_dims.clear();
  25. ge::GetParserContext().op_conf_map.clear();
  26. ge::GetParserContext().user_out_nodes.clear();
  27. ge::GetParserContext().default_out_nodes.clear();
  28. ge::GetParserContext().out_nodes_map.clear();
  29. ge::GetParserContext().user_out_tensors.clear();
  30. ge::GetParserContext().net_out_nodes.clear();
  31. ge::GetParserContext().out_tensor_names.clear();
  32. ge::GetParserContext().data_tensor_names.clear();
  33. ge::GetParserContext().is_dynamic_input = false;
  34. ge::GetParserContext().train_flag = false;
  35. ge::GetParserContext().format = domi::DOMI_TENSOR_ND;
  36. ge::GetParserContext().type = domi::FRAMEWORK_RESERVED;
  37. ge::GetParserContext().run_mode = GEN_OM_MODEL;
  38. ge::GetParserContext().custom_proto_path = "";
  39. ge::GetParserContext().caffe_proto_path = "";
  40. ge::GetParserContext().enable_scope_fusion_passes = "";
  41. GELOGI("Clear parser inner context successfully.");
  42. }
  43. MemBuffer* ParerSTestsUtils::MemBufferFromFile(const char *path) {
  44. char path_temp[PATH_MAX + 1] = {0x00};
  45. if(strlen(path) > PATH_MAX || nullptr == realpath(path, path_temp)) {
  46. return nullptr;
  47. }
  48. FILE *fp = fopen(path_temp, "r+");
  49. if (fp == nullptr) {
  50. return nullptr;
  51. }
  52. // get model file length
  53. if (0 != fseek(fp, 0, SEEK_END)) {
  54. fclose(fp);
  55. return nullptr;
  56. }
  57. long file_length = ftell(fp);
  58. if (fseek(fp, 0, SEEK_SET)) {
  59. fclose(fp);
  60. return nullptr;
  61. }
  62. if (file_length <= 0) {
  63. fclose(fp);
  64. return nullptr;
  65. }
  66. // alloc model buffer
  67. void *data = malloc((unsigned int)file_length);
  68. if (!data) {
  69. fclose(fp);
  70. return nullptr;
  71. }
  72. // read file into memory
  73. uint32_t read_size = (uint32_t)fread(data, 1, (unsigned int)file_length, fp);
  74. // check if read success
  75. if ((long)read_size != file_length) {
  76. free(data);
  77. data = nullptr;
  78. fclose(fp);
  79. return nullptr;
  80. }
  81. // close model file
  82. fclose(fp);
  83. // create an MemBuffer
  84. MemBuffer* membuf = new MemBuffer();
  85. if (!membuf) {
  86. free(data);
  87. data = nullptr;
  88. return nullptr;
  89. }
  90. membuf->data = malloc((unsigned int)read_size);
  91. // set size && data
  92. membuf->size = (uint32_t)read_size;
  93. memcpy((char*)membuf->data, (char*)data, read_size);
  94. free(data);
  95. return membuf;
  96. }
  97. } // namespace ge