2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "CircleExpContract.h"
19 #include <foder/FileLoader.h>
21 #include <luci/Importer.h>
22 #include <luci/CircleOptimizer.h>
23 #include <luci/Service/Validate.h>
24 #include <luci/CircleExporter.h>
26 #include <oops/InternalExn.h>
27 #include <arser/arser.h>
34 using OptionHook = std::function<int(const char **)>;
36 using Algorithms = luci::CircleOptimizer::Options::Algorithm;
37 using AlgorithmParameters = luci::CircleOptimizer::Options::AlgorithmParameters;
39 int entry(int argc, char **argv)
41 // Simple argument parser (based on map)
42 std::map<std::string, OptionHook> argparse;
43 luci::CircleOptimizer optimizer;
45 auto options = optimizer.options();
47 const std::string qdqw = "--quantize_dequantize_weights";
48 const std::string qwmm = "--quantize_with_minmax";
50 arser::Arser arser("circle-quantizer provides circle model quantization");
52 arser.add_argument(qdqw)
54 .type(arser::DataType::STR_VEC)
56 .help("Quantize-dequantize weight values required action before quantization. "
57 "Three arguments required: input_dtype(float32) "
58 "output_dtype(uint8) granularity(layer)");
60 arser.add_argument(qwmm)
62 .type(arser::DataType::STR_VEC)
64 .help("Quantize with min/max values. "
65 "Three arguments required: input_dtype(float32) "
66 "output_dtype(uint8) granularity(layer)");
68 arser.add_argument("input").nargs(1).type(arser::DataType::STR).help("Input circle model");
69 arser.add_argument("output").nargs(1).type(arser::DataType::STR).help("Output circle model");
73 arser.parse(argc, argv);
75 catch (const std::runtime_error &err)
77 std::cout << err.what() << std::endl;
84 auto values = arser.get<std::vector<std::string>>(qdqw);
85 if (values.size() != 3)
90 options->enable(Algorithms::QuantizeDequantizeWeights);
92 options->param(AlgorithmParameters::Quantize_input_dtype, values.at(0));
93 options->param(AlgorithmParameters::Quantize_output_dtype, values.at(1));
94 options->param(AlgorithmParameters::Quantize_granularity, values.at(2));
99 auto values = arser.get<std::vector<std::string>>(qwmm);
100 if (values.size() != 3)
105 options->enable(Algorithms::QuantizeWithMinMax);
107 options->param(AlgorithmParameters::Quantize_input_dtype, values.at(0));
108 options->param(AlgorithmParameters::Quantize_output_dtype, values.at(1));
109 options->param(AlgorithmParameters::Quantize_granularity, values.at(2));
112 std::string input_path = arser.get<std::string>("input");
113 std::string output_path = arser.get<std::string>("output");
115 // Load model from the file
116 foder::FileLoader file_loader{input_path};
117 std::vector<char> model_data = file_loader.load();
118 const circle::Model *circle_model = circle::GetModel(model_data.data());
119 if (circle_model == nullptr)
121 std::cerr << "ERROR: Failed to load circle '" << input_path << "'" << std::endl;
125 // Import from input Circle file
126 luci::Importer importer;
127 auto module = importer.importModule(circle_model);
129 for (size_t idx = 0; idx < module->size(); ++idx)
131 auto graph = module->graph(idx);
133 // quantize the graph
134 optimizer.quantize(graph);
136 if (!luci::validate(graph))
138 std::cerr << "ERROR: Quantized graph is invalid" << std::endl;
143 // Export to output Circle file
144 luci::CircleExporter exporter;
146 CircleExpContract contract(module.get(), output_path);
148 if (!exporter.invoke(&contract))
150 std::cerr << "ERROR: Failed to export '" << output_path << "'" << std::endl;