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.

model_parser.h 2.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #ifndef MINDSPORE_LITE_TOOLS_CONVERTER_MODEL_PARSER_H
  17. #define MINDSPORE_LITE_TOOLS_CONVERTER_MODEL_PARSER_H
  18. #include <google/protobuf/message.h>
  19. #include <string>
  20. #include <memory>
  21. #include "schema/inner/model_generated.h"
  22. #include "tools/anf_importer/import_from_meta_graphT.h"
  23. #include "ir/anf.h"
  24. #include "tools/converter/converter_context.h"
  25. namespace mindspore::lite {
  26. using namespace schema;
  27. class ModelParser {
  28. public:
  29. ModelParser() {}
  30. virtual ~ModelParser() {}
  31. FuncGraphPtr Parse(const std::string &modelFile, const std::string &weightFile,
  32. const QuantType &quantType = QuantType_QUANT_NONE) {
  33. auto *meta_graph = ParseToFb(modelFile, weightFile, quantType);
  34. if (meta_graph == nullptr) {
  35. MS_LOG(ERROR) << "parse model to fb failed";
  36. return nullptr;
  37. }
  38. auto func_graph = this->Fb2Anf(meta_graph);
  39. delete (meta_graph);
  40. return func_graph;
  41. }
  42. virtual schema::MetaGraphT *ParseToFb(const std::string &modelFile, const std::string &weightFile,
  43. const QuantType &quantType = QuantType_QUANT_NONE) = 0;
  44. public:
  45. static FuncGraphPtr Fb2Anf(schema::MetaGraphT *meta_graph) {
  46. if (meta_graph == nullptr) {
  47. MS_LOG(ERROR) << "meta_graph is null";
  48. ReturnCode::GetSingleReturnCode()->UpdateReturnCode(RET_NULL_PTR);
  49. return nullptr;
  50. }
  51. auto func_graph = std::make_shared<FuncGraph>();
  52. AnfImporterFromMetaGraphT importer(meta_graph, func_graph);
  53. auto status = importer.Import();
  54. if (RET_OK != status) {
  55. MS_LOG(ERROR) << "Import anf_graph from meta_graphT failed, ret: " << status;
  56. ReturnCode::GetSingleReturnCode()->UpdateReturnCode(status);
  57. return nullptr;
  58. }
  59. return func_graph;
  60. }
  61. };
  62. } // namespace mindspore::lite
  63. #endif