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>
25 #include <luci/UserSettings.h>
27 #include <oops/InternalExn.h>
28 #include <arser/arser.h>
29 #include <vconone/vconone.h>
35 using Algorithms = luci::CircleOptimizer::Options::Algorithm;
36 using AlgorithmParameters = luci::CircleOptimizer::Options::AlgorithmParameters;
38 void print_version(void)
40 std::cout << "circle2circle version " << vconone::get_string() << std::endl;
41 std::cout << vconone::get_copyright() << std::endl;
44 int entry(int argc, char **argv)
46 // Simple argument parser (based on map)
47 luci::CircleOptimizer optimizer;
49 auto options = optimizer.options();
50 auto settings = luci::UserSettings::settings();
52 arser::Arser arser("circle2circle provides circle model optimization and transformations");
54 arser.add_argument("--version")
58 .help("Show version information and exit")
59 .exit_with(print_version);
61 arser.add_argument("--all").nargs(0).required(false).default_value(false).help(
62 "Enable all optimize options");
64 arser.add_argument("--fuse_bcq")
68 .help("This will fuse operators and apply Binary Coded Quantization");
70 arser.add_argument("--fuse_instnorm")
74 .help("This will fuse operators to InstanceNorm operator");
76 arser.add_argument("--resolve_customop_add")
80 .help("This will convert Custom(Add) to Add operator");
82 arser.add_argument("--resolve_customop_batchmatmul")
86 .help("This will convert Custom(BatchMatmul) to BatchMatmul operator");
88 arser.add_argument("--resolve_customop_matmul")
92 .help("This will convert Custom(Matmul) to Matmul operator");
94 arser.add_argument("--mute_warnings")
98 .help("This will turn off warning messages");
100 arser.add_argument("--disable_validation")
103 .default_value(false)
104 .help("This will turn off operator vaidations. May help input model investigation.");
106 arser.add_argument("input").nargs(1).type(arser::DataType::STR).help("Input circle model");
107 arser.add_argument("output").nargs(1).type(arser::DataType::STR).help("Output circle model");
111 arser.parse(argc, argv);
113 catch (const std::runtime_error &err)
115 std::cout << err.what() << std::endl;
120 if (arser.get<bool>("--all"))
122 options->enable(Algorithms::FuseBCQ);
123 options->enable(Algorithms::FuseInstanceNorm);
124 options->enable(Algorithms::ResolveCustomOpAdd);
125 options->enable(Algorithms::ResolveCustomOpBatchMatMul);
126 options->enable(Algorithms::ResolveCustomOpMatMul);
128 if (arser.get<bool>("--fuse_bcq"))
129 options->enable(Algorithms::FuseBCQ);
130 if (arser.get<bool>("--fuse_instnorm"))
131 options->enable(Algorithms::FuseInstanceNorm);
132 if (arser.get<bool>("--resolve_customop_add"))
133 options->enable(Algorithms::ResolveCustomOpAdd);
134 if (arser.get<bool>("--resolve_customop_batchmatmul"))
135 options->enable(Algorithms::ResolveCustomOpBatchMatMul);
136 if (arser.get<bool>("--resolve_customop_matmul"))
137 options->enable(Algorithms::ResolveCustomOpMatMul);
139 if (arser.get<bool>("--mute_warnings"))
140 settings->set(luci::UserSettings::Key::MuteWarnings, true);
141 if (arser.get<bool>("--disable_validation"))
142 settings->set(luci::UserSettings::Key::DisableValidation, true);
144 std::string input_path = arser.get<std::string>("input");
145 std::string output_path = arser.get<std::string>("output");
147 // Load model from the file
148 foder::FileLoader file_loader{input_path};
149 std::vector<char> model_data;
153 model_data = file_loader.load();
155 catch (const std::runtime_error &err)
157 std::cerr << err.what() << std::endl;
160 const circle::Model *circle_model = circle::GetModel(model_data.data());
161 if (circle_model == nullptr)
163 std::cerr << "ERROR: Failed to load circle '" << input_path << "'" << std::endl;
167 // Import from input Circle file
168 luci::Importer importer;
169 auto module = importer.importModule(circle_model);
171 for (size_t idx = 0; idx < module->size(); ++idx)
173 auto graph = module->graph(idx);
175 // call luci optimizations
176 optimizer.optimize(graph);
178 if (!luci::validate(graph))
180 std::cerr << "ERROR: Optimized graph is invalid" << std::endl;
185 // Export to output Circle file
186 luci::CircleExporter exporter;
188 CircleExpContract contract(module.get(), output_path);
190 if (!exporter.invoke(&contract))
192 std::cerr << "ERROR: Failed to export '" << output_path << "'" << std::endl;