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.

anf_converter.cc 4.0 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright 2020 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 "utils/load_onnx/anf_converter.h"
  17. #include <fcntl.h>
  18. #include <fstream>
  19. #include <memory>
  20. #include <vector>
  21. #include <string>
  22. #include "pybind11/pybind11.h"
  23. #include "utils/load_onnx/anf_model_parser.h"
  24. #include "google/protobuf/io/zero_copy_stream_impl.h"
  25. #include "proto/onnx.pb.h"
  26. #include "utils/log_adapter.h"
  27. namespace mindspore {
  28. namespace lite {
  29. const char WHITESPACE[] = "\t\n\v\f\r ";
  30. const int FLAG_PREFIX_LEN = 2;
  31. void AnfConverter::Trim(std::string *input) {
  32. if (input == nullptr) {
  33. return;
  34. }
  35. if (input->empty()) {
  36. return;
  37. }
  38. input->erase(0, input->find_first_not_of(WHITESPACE));
  39. input->erase(input->find_last_not_of(WHITESPACE) + 1);
  40. }
  41. int AnfConverter::ValidateFileStr(const std::string &modelFile, std::string fileType) {
  42. if (modelFile.size() > fileType.size()) {
  43. if (modelFile.substr(modelFile.size() - fileType.size()) == fileType) {
  44. return 0;
  45. } else {
  46. return 1;
  47. }
  48. } else {
  49. return 1;
  50. }
  51. }
  52. bool AnfConverter::ReadOnnxFromBinary(const std::string &modelFile, google::protobuf::Message *onnx_model) {
  53. std::unique_ptr<char> onnx_file(new (std::nothrow) char[PATH_MAX]{0});
  54. if (modelFile.size() > PATH_MAX) {
  55. MS_LOG(DEBUG) << "file path " << modelFile << " is too long.";
  56. return false;
  57. }
  58. char real_path[PATH_MAX + 1] = {0};
  59. #if defined(_WIN32) || defined(_WIN64)
  60. if (nullptr == _fullpath(real_path, modelFile.c_str(), PATH_MAX)) {
  61. MS_LOG(DEBUG) << modelFile << " does not exit.";
  62. return false;
  63. }
  64. #else
  65. if (nullptr == realpath(modelFile.c_str(), real_path)) {
  66. MS_LOG(DEBUG) << modelFile << " does not exit.";
  67. return false;
  68. }
  69. #endif
  70. int fd = open(real_path, O_RDONLY);
  71. if (fd < 0) {
  72. MS_LOG(EXCEPTION) << "failed to open file";
  73. }
  74. google::protobuf::io::FileInputStream input(fd);
  75. google::protobuf::io::CodedInputStream code_input(&input);
  76. code_input.SetTotalBytesLimit(INT_MAX, 536870912);
  77. bool ret = onnx_model->ParseFromCodedStream(&code_input);
  78. if (!ret) {
  79. MS_LOG(ERROR) << "load onnx file failed";
  80. return false;
  81. }
  82. (void)close(fd);
  83. MS_LOG(INFO) << "enter ReadProtoFromBinary success!" << std::endl;
  84. return true;
  85. }
  86. std::shared_ptr<FuncGraph> AnfConverter::RunAnfConverter(const std::string &file_path) {
  87. std::string modelFile;
  88. std::string tmp = file_path;
  89. Trim(&tmp);
  90. const std::string flagItem(tmp);
  91. size_t pos = flagItem.find_first_of("=");
  92. if (pos == std::string::npos) {
  93. MS_LOG(ERROR) << "Trans data not support input format!";
  94. } else {
  95. modelFile = flagItem.substr(pos + 1);
  96. std::cout << "input protobuf file path is: " << modelFile << std::endl;
  97. }
  98. if (ValidateFileStr(modelFile, ".pb") != 0) {
  99. MS_LOG(EXCEPTION) << "INPUT ILLEGAL: modelFile must be *.pb";
  100. }
  101. onnx::ModelProto model_;
  102. ReadOnnxFromBinary(modelFile, &model_);
  103. MSANFModelParser model_parser;
  104. FuncGraphPtr dstgraph_ptr = model_parser.Parse(model_);
  105. return dstgraph_ptr;
  106. }
  107. std::shared_ptr<FuncGraph> AnfConverter::RunAnfConverter(const char *buf, const size_t buf_size) {
  108. Py_Initialize();
  109. MS_EXCEPTION_IF_NULL(buf);
  110. std::string str((const char *)buf, buf_size);
  111. onnx::ModelProto model_;
  112. if (!model_.ParseFromString(str)) {
  113. MS_LOG(EXCEPTION) << "Parse model from buffer fail!";
  114. }
  115. MSANFModelParser model_parser;
  116. FuncGraphPtr dstgraph_ptr = model_parser.Parse(model_);
  117. return dstgraph_ptr;
  118. }
  119. } // namespace lite
  120. } // namespace mindspore