diff options
Diffstat (limited to 'compiler/circle-quantizer')
-rw-r--r-- | compiler/circle-quantizer/CMakeLists.txt | 4 | ||||
-rw-r--r-- | compiler/circle-quantizer/include/CircleExpContract.h | 49 | ||||
-rw-r--r-- | compiler/circle-quantizer/src/CircleExpContract.cpp | 33 | ||||
-rw-r--r-- | compiler/circle-quantizer/src/CircleQuantizer.cpp | 60 |
4 files changed, 58 insertions, 88 deletions
diff --git a/compiler/circle-quantizer/CMakeLists.txt b/compiler/circle-quantizer/CMakeLists.txt index 009bfabea..5075b13d5 100644 --- a/compiler/circle-quantizer/CMakeLists.txt +++ b/compiler/circle-quantizer/CMakeLists.txt @@ -1,8 +1,6 @@ -file(GLOB_RECURSE SOURCES "src/*.cpp") +set (SOURCES src/CircleQuantizer.cpp) add_executable(circle-quantizer "${SOURCES}") -target_include_directories(circle-quantizer PRIVATE include) -target_include_directories(circle-quantizer PRIVATE src) target_link_libraries(circle-quantizer foder) target_link_libraries(circle-quantizer safemain) target_link_libraries(circle-quantizer oops) diff --git a/compiler/circle-quantizer/include/CircleExpContract.h b/compiler/circle-quantizer/include/CircleExpContract.h deleted file mode 100644 index e888e4a12..000000000 --- a/compiler/circle-quantizer/include/CircleExpContract.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CIRCLEQUANTIZER_CIRCLEXPCONTRACT_H__ -#define __CIRCLEQUANTIZER_CIRCLEXPCONTRACT_H__ - -#include <loco.h> -#include <luci/CircleExporter.h> -#include <luci/IR/Module.h> - -#include <memory> -#include <string> - -struct CircleExpContract : public luci::CircleExporter::Contract -{ -public: - CircleExpContract(luci::Module *module, const std::string &filename) - : _module(module), _filepath(filename) - { - // NOTHING TO DO - } - virtual ~CircleExpContract() = default; - -public: - loco::Graph *graph(void) const final { return nullptr; } - luci::Module *module(void) const final { return _module; }; - -public: - bool store(const char *ptr, const size_t size) const final; - -private: - luci::Module *_module; - const std::string _filepath; -}; - -#endif // __CIRCLEQUANTIZER_CIRCLEXPCONTRACT_H__ diff --git a/compiler/circle-quantizer/src/CircleExpContract.cpp b/compiler/circle-quantizer/src/CircleExpContract.cpp deleted file mode 100644 index b56b7eedc..000000000 --- a/compiler/circle-quantizer/src/CircleExpContract.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "CircleExpContract.h" - -#include <oops/InternalExn.h> - -#include <fstream> -#include <iostream> - -bool CircleExpContract::store(const char *ptr, const size_t size) const -{ - if (!ptr) - INTERNAL_EXN("Graph was not serialized by FlatBuffer for some reason"); - - std::ofstream fs(_filepath.c_str(), std::ofstream::binary); - fs.write(ptr, size); - - return fs.good(); -} diff --git a/compiler/circle-quantizer/src/CircleQuantizer.cpp b/compiler/circle-quantizer/src/CircleQuantizer.cpp index 8d3a80c91..54b38a170 100644 --- a/compiler/circle-quantizer/src/CircleQuantizer.cpp +++ b/compiler/circle-quantizer/src/CircleQuantizer.cpp @@ -14,14 +14,13 @@ * limitations under the License. */ -#include "CircleExpContract.h" - #include <foder/FileLoader.h> #include <luci/Importer.h> #include <luci/CircleOptimizer.h> #include <luci/Service/Validate.h> #include <luci/CircleExporter.h> +#include <luci/CircleFileExpContract.h> #include <oops/InternalExn.h> #include <arser/arser.h> @@ -37,6 +36,14 @@ using OptionHook = std::function<int(const char **)>; using Algorithms = luci::CircleOptimizer::Options::Algorithm; using AlgorithmParameters = luci::CircleOptimizer::Options::AlgorithmParameters; +void print_exclusive_options(void) +{ + std::cout << "Use only one of the 3 options below." << std::endl; + std::cout << " --quantize_dequantize_weights" << std::endl; + std::cout << " --quantize_with_minmax" << std::endl; + std::cout << " --requantize" << std::endl; +} + void print_version(void) { std::cout << "circle-quantizer version " << vconone::get_string() << std::endl; @@ -53,6 +60,7 @@ int entry(int argc, char **argv) const std::string qdqw = "--quantize_dequantize_weights"; const std::string qwmm = "--quantize_with_minmax"; + const std::string rq = "--requantize"; arser::Arser arser("circle-quantizer provides circle model quantization"); @@ -79,6 +87,14 @@ int entry(int argc, char **argv) "Three arguments required: input_dtype(float32) " "output_dtype(uint8) granularity(layer, channel)"); + arser.add_argument(rq) + .nargs(2) + .type(arser::DataType::STR_VEC) + .required(false) + .help("Requantize a quantized model. " + "Two arguments required: input_dtype(int8) " + "output_dtype(uint8)"); + arser.add_argument("input").nargs(1).type(arser::DataType::STR).help("Input circle model"); arser.add_argument("output").nargs(1).type(arser::DataType::STR).help("Output circle model"); @@ -95,6 +111,11 @@ int entry(int argc, char **argv) if (arser[qdqw]) { + if (arser[qwmm] || arser[rq]) + { + print_exclusive_options(); + return 255; + } auto values = arser.get<std::vector<std::string>>(qdqw); if (values.size() != 3) { @@ -110,6 +131,11 @@ int entry(int argc, char **argv) if (arser[qwmm]) { + if (arser[qdqw] || arser[rq]) + { + print_exclusive_options(); + return 255; + } auto values = arser.get<std::vector<std::string>>(qwmm); if (values.size() != 3) { @@ -123,12 +149,40 @@ int entry(int argc, char **argv) options->param(AlgorithmParameters::Quantize_granularity, values.at(2)); } + if (arser[rq]) + { + if (arser[qwmm] || arser[qdqw]) + { + print_exclusive_options(); + return 255; + } + auto values = arser.get<std::vector<std::string>>(rq); + if (values.size() != 2) + { + std::cerr << arser; + return 255; + } + options->enable(Algorithms::Requantize); + + options->param(AlgorithmParameters::Quantize_input_dtype, values.at(0)); + options->param(AlgorithmParameters::Quantize_output_dtype, values.at(1)); + } + std::string input_path = arser.get<std::string>("input"); std::string output_path = arser.get<std::string>("output"); // Load model from the file foder::FileLoader file_loader{input_path}; std::vector<char> model_data = file_loader.load(); + + // Verify flatbuffers + flatbuffers::Verifier verifier{reinterpret_cast<uint8_t *>(model_data.data()), model_data.size()}; + if (!circle::VerifyModelBuffer(verifier)) + { + std::cerr << "ERROR: Invalid input file '" << input_path << "'" << std::endl; + return EXIT_FAILURE; + } + const circle::Model *circle_model = circle::GetModel(model_data.data()); if (circle_model == nullptr) { @@ -157,7 +211,7 @@ int entry(int argc, char **argv) // Export to output Circle file luci::CircleExporter exporter; - CircleExpContract contract(module.get(), output_path); + luci::CircleFileExpContract contract(module.get(), output_path); if (!exporter.invoke(&contract)) { |