Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / circlechef / tools / reverse / 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 <circlechef/RecipeChef.h>
18
19 #include <arser/arser.h>
20 #include <foder/FileLoader.h>
21
22 #include <memory>
23 #include <iostream>
24
25 int entry(int argc, char **argv)
26 {
27   arser::Arser arser;
28   arser.add_argument("circle")
29       .type(arser::DataType::STR)
30       .help("Source circle file path to convert");
31   arser.add_argument("recipe").type(arser::DataType::STR).help("Target recipe file path");
32
33   try
34   {
35     arser.parse(argc, argv);
36   }
37   catch (const std::runtime_error &err)
38   {
39     std::cout << err.what() << std::endl;
40     std::cout << arser;
41     return 255;
42   }
43
44   std::string circle_path = arser.get<std::string>("circle");
45   // Load TF lite model from a circle file
46   const foder::FileLoader fileLoader{circle_path};
47   std::vector<char> modelData = fileLoader.load();
48   const circle::Model *circlemodel = circle::GetModel(modelData.data());
49   if (circlemodel == nullptr)
50   {
51     std::cerr << "ERROR: Failed to load circle '" << circle_path << "'" << std::endl;
52     return 255;
53   }
54
55   // Generate ModelRecipe recipe
56   std::unique_ptr<circlechef::ModelRecipe> recipe = circlechef::generate_recipe(circlemodel);
57   if (recipe.get() == nullptr)
58   {
59     std::cerr << "ERROR: Failed to generate recipe" << std::endl;
60     return 255;
61   }
62
63   std::string recipe_path = arser.get<std::string>("recipe");
64   // Save to a file
65   bool result = circlechef::write_recipe(recipe_path, recipe);
66   if (!result)
67   {
68     std::cerr << "ERROR: Failed to write to recipe '" << recipe_path << "'" << std::endl;
69     return 255;
70   }
71   return 0;
72 }