Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / circle-tensordump / driver / Driver.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 "Dump.h"
18
19 #include <arser/arser.h>
20 #include <foder/FileLoader.h>
21
22 #include <functional>
23 #include <iostream>
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <vector>
28
29 int entry(int argc, char **argv)
30 {
31   arser::Arser arser{
32       "circle-tensordump allows users to retrieve tensor information from a Circle model file"};
33
34   arser.add_argument("circle").nargs(1).type(arser::DataType::STR).help("Circle file path to dump");
35   arser.add_argument("--tensors").nargs(0).help("Dump to console");
36   arser.add_argument("--tensors_to_hdf5")
37       .nargs(1)
38       .type(arser::DataType::STR)
39       .help("Dump to hdf5 file. Specify hdf5 file path to be dumped");
40
41   try
42   {
43     arser.parse(argc, argv);
44   }
45   catch (const std::runtime_error &err)
46   {
47     std::cout << err.what() << std::endl;
48     std::cout << arser;
49     return 255;
50   }
51
52   if (arser["--tensors_to_hdf5"] == arser["--tensors"])
53   {
54     std::cout << "[Error] You must specify one option for how to print." << std::endl;
55     std::cout << arser;
56     return 255;
57   }
58
59   std::unique_ptr<circletensordump::DumpInterface> dump;
60
61   std::string model_file = arser.get<std::string>("circle");
62   std::string output_path;
63   if (arser["--tensors_to_hdf5"])
64   {
65     dump = std::move(std::make_unique<circletensordump::DumpTensorsToHdf5>());
66     output_path = arser.get<std::string>("--tensors_to_hdf5");
67   }
68   if (arser["--tensors"])
69   {
70     dump = std::move(std::make_unique<circletensordump::DumpTensors>());
71   }
72
73   // Load Circle model from a circle file
74   foder::FileLoader fileLoader{model_file};
75   std::vector<char> modelData = fileLoader.load();
76   const circle::Model *circleModel = circle::GetModel(modelData.data());
77   if (circleModel == nullptr)
78   {
79     std::cerr << "ERROR: Failed to load circle '" << model_file << "'" << std::endl;
80     return EXIT_FAILURE;
81   }
82
83   dump->run(std::cout, circleModel, output_path);
84
85   return EXIT_SUCCESS;
86 }