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