Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compiler / luci / tester / src / ReadTester.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
17 #include <foder/FileLoader.h>
18
19 #include <luci/Importer.h>
20 #include <luci/Service/Validate.h>
21 #include <luci/Pass/ShapeInferencePass.h>
22 #include <luci/Pass/TypeInferencePass.h>
23
24 // Following passes will be removed after refactoring is finished
25 #include <luci/Pass/MigrateLegacyShapeDtypePass.h>
26
27 #include <iostream>
28 #include <map>
29 #include <string>
30
31 namespace
32 {
33
34 void show_help_message(const char *progname, std::ostream &os)
35 {
36   os << "USAGE: " << progname << " circlefile" << std::endl << std::endl;
37 }
38
39 void show_error_message(const char *progname, std::ostream &os, const std::string &msg)
40 {
41   os << "ERROR: " << msg << std::endl;
42   os << std::endl;
43
44   show_help_message(progname, os);
45 }
46
47 } // namespace
48
49 /*
50  * @brief ReadTest main
51  *
52  *        Give one Circle file as an argument
53  *
54  *        This will use luci_import to read the file and get loco graph
55  *        In luci_import, LUCI_LOG environment will be checked and will
56  *        dump graph to console if set.
57  *        i.e. "LUCI_LOG=1 luci_readtester mymodel.circle"
58  */
59 int entry(int argc, char **argv)
60 {
61   if (argc != 2)
62   {
63     show_error_message(argv[0], std::cerr, "Circle file is not specified");
64     return 255;
65   }
66
67   std::string input_path = argv[1];
68
69   std::cout << "[INFO] Circle is '" << input_path << "'" << std::endl;
70
71   // Load model from the file
72   foder::FileLoader file_loader{input_path};
73   std::vector<char> model_data = file_loader.load();
74   const circle::Model *circle_model = circle::GetModel(model_data.data());
75   if (circle_model == nullptr)
76   {
77     std::cerr << "ERROR: Failed to load circle '" << input_path << "'" << std::endl;
78     return EXIT_FAILURE;
79   }
80
81   luci::Importer importer;
82   auto module = importer.importModule(circle_model);
83   assert(module->size() > 0);
84
85   for (size_t g = 0; g < module->size(); ++g)
86   {
87     auto graph = module->graph(g);
88     if (graph == nullptr)
89       return 255;
90
91     {
92       luci::ShapeInferencePass pass;
93       while (pass.run(graph) == true)
94         ;
95     }
96     {
97       luci::TypeInferencePass pass;
98       while (pass.run(graph) == true)
99         ;
100     }
101     {
102       // This pass will be removed after refactoring is finished
103       luci::MigrateLegacyShapeDtypePass pass;
104       while (pass.run(graph) == true)
105         ;
106     }
107
108     if (!luci::validate(graph))
109       return 255;
110   }
111   return 0;
112 }