ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Driver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 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 <luci/ImporterEx.h>
20#include <luci/CircleExporter.h>
22#include <luci/UserSettings.h>
23
24#include <arser/arser.h>
25
26#include "QImplant.h"
27
28#include <iostream>
29#include <string>
30
31using namespace q_implant;
32
33int entry(int argc, char **argv)
34{
35 arser::Arser arser("q-implant provides circle model quantization");
36
38
39 arser.add_argument("input").help("Input circle model");
40 arser.add_argument("qparam").help("Quantization parameter file (.json)");
41 arser.add_argument("output").help("Output circle model");
42
43 try
44 {
45 arser.parse(argc, argv);
46 }
47 catch (const std::runtime_error &err)
48 {
49 std::cerr << err.what() << std::endl;
50 std::cout << arser;
51 return EXIT_FAILURE;
52 }
53
54 if (arser.get<bool>("--verbose"))
55 {
56 // The third parameter of setenv means REPLACE.
57 // If REPLACE is zero, it does not overwrite an existing value.
58 setenv("LUCI_LOG", "100", 0);
59 }
60
61 const std::string input_path = arser.get<std::string>("input");
62 const std::string qparam_path = arser.get<std::string>("qparam");
63 const std::string output_path = arser.get<std::string>("output");
64
65 // Load model from the file
66 luci::ImporterEx importerex;
67 auto module = importerex.importVerifyModule(input_path);
68 if (module.get() == nullptr)
69 return EXIT_FAILURE;
70
71 QImplant writer(qparam_path);
72
73 if (module->size() != 1)
74 {
75 std::cerr << "ERROR: Only a single subgraph is supported" << std::endl;
76 return EXIT_FAILURE;
77 }
78
79 for (size_t idx = 0; idx < module->size(); ++idx)
80 {
81 auto graph = module->graph(idx);
82
83 writer.write(graph);
84
85 if (!luci::validate(graph))
86 {
87 std::cerr << "ERROR: Quantized graph is invalid" << std::endl;
88 return EXIT_FAILURE;
89 }
90 }
91
92 // Export to output Circle file
93 luci::CircleExporter exporter;
94
95 luci::CircleFileExpContract contract(module.get(), output_path);
96
97 if (!exporter.invoke(&contract))
98 {
99 std::cerr << "ERROR: Failed to export '" << output_path << "'" << std::endl;
100 return EXIT_FAILURE;
101 }
102
103 return 0;
104}
static void add_verbose(Arser &arser)
Definition arser.h:765
bool invoke(Contract *) const
void write(loco::Graph *g)
Definition QImplant.cpp:205
int entry(int argc, char **argv)
Dump IR for given arguments.
Definition Driver.cpp:33
Definition arser.h:39
bool validate(luci::PartitionTable &partition)