Imported Upstream version 1.21.0
[platform/core/ml/nnfw.git] / compiler / circle-quantizer / src / CircleQuantizer.cpp
index e0c85cb..f1e31ed 100644 (file)
@@ -14,9 +14,7 @@
  * limitations under the License.
  */
 
-#include <foder/FileLoader.h>
-
-#include <luci/Importer.h>
+#include <luci/ImporterEx.h>
 #include <luci/CircleQuantizer.h>
 #include <luci/Service/Validate.h>
 #include <luci/CircleExporter.h>
@@ -59,13 +57,31 @@ std::vector<std::shared_ptr<LayerParam>> read_layer_params(std::string &filename
   std::vector<std::shared_ptr<LayerParam>> p;
   for (auto layer : layers)
   {
-    auto l = std::make_shared<LayerParam>();
+    if (layer.isMember("name"))
     {
-      l->name = layer["name"].asString();
-      l->dtype = layer["dtype"].asString();
-      l->granularity = layer["granularity"].asString();
+      auto l = std::make_shared<LayerParam>();
+      {
+        l->name = layer["name"].asString();
+        l->dtype = layer["dtype"].asString();
+        l->granularity = layer["granularity"].asString();
+      }
+      p.emplace_back(l);
+    }
+
+    // Multiple names with the same dtype & granularity
+    if (layer.isMember("names"))
+    {
+      for (auto name : layer["names"])
+      {
+        auto l = std::make_shared<LayerParam>();
+        {
+          l->name = name.asString();
+          l->dtype = layer["dtype"].asString();
+          l->granularity = layer["granularity"].asString();
+        }
+        p.emplace_back(l);
+      }
     }
-    p.emplace_back(l);
   }
 
   return p;
@@ -109,23 +125,12 @@ int entry(int argc, char **argv)
 
   arser::Arser arser("circle-quantizer provides circle model quantization");
 
-  arser.add_argument("--version")
-    .nargs(0)
-    .required(false)
-    .default_value(false)
-    .help("Show version information and exit")
-    .exit_with(print_version);
-
-  arser.add_argument("-V", "--verbose")
-    .nargs(0)
-    .required(false)
-    .default_value(false)
-    .help("output additional information to stdout or stderr");
+  arser::Helper::add_version(arser, print_version);
+  arser::Helper::add_verbose(arser);
 
   arser.add_argument(qdqw)
     .nargs(3)
     .type(arser::DataType::STR_VEC)
-    .required(false)
     .help("Quantize-dequantize weight values required action before quantization. "
           "Three arguments required: input_model_dtype(float32) "
           "output_model_dtype(uint8) granularity(layer, channel)");
@@ -133,28 +138,24 @@ int entry(int argc, char **argv)
   arser.add_argument(qwmm)
     .nargs(3)
     .type(arser::DataType::STR_VEC)
-    .required(false)
     .help("Quantize with min/max values. "
           "Three arguments required: input_model_dtype(float32) "
           "output_model_dtype(uint8) granularity(layer, channel)");
 
   arser.add_argument(tf_maxpool)
     .nargs(0)
-    .required(false)
     .default_value(false)
     .help("Force MaxPool Op to have the same input/output quantparams. NOTE: This feature can "
           "degrade accuracy of some models");
 
   arser.add_argument(fake_quant)
     .nargs(0)
-    .required(false)
     .help("Convert a quantized model to a fake-quantized model. NOTE: This feature will "
           "generate an fp32 model.");
 
   arser.add_argument(rq)
     .nargs(2)
     .type(arser::DataType::STR_VEC)
-    .required(false)
     .help("Requantize a quantized model. "
           "Two arguments required: input_model_dtype(int8) "
           "output_model_dtype(uint8)");
@@ -162,7 +163,6 @@ int entry(int argc, char **argv)
   arser.add_argument(fq)
     .nargs(3)
     .type(arser::DataType::STR_VEC)
-    .required(false)
     .accumulated(true)
     .help("Write quantization parameters to the specified tensor. "
           "Three arguments required: tensor_name(string), "
@@ -171,32 +171,21 @@ int entry(int argc, char **argv)
   arser.add_argument(cq)
     .nargs(2)
     .type(arser::DataType::STR_VEC)
-    .required(false)
     .accumulated(true)
     .help("Copy quantization parameter from a tensor to another tensor."
           "Two arguments required: source_tensor_name(string), "
           "destination_tensor_name(string)");
 
   arser.add_argument("--input_type")
-    .nargs(1)
-    .type(arser::DataType::STR)
-    .required(false)
-    .help("Input type of quantized model (uint8 or int16)");
+    .help("Input type of quantized model (uint8, int16, or float32)");
 
   arser.add_argument("--output_type")
-    .nargs(1)
-    .type(arser::DataType::STR)
-    .required(false)
-    .help("Output type of quantized model (uint8 or int16)");
+    .help("Output type of quantized model (uint8, int16, or float32)");
 
-  arser.add_argument(cfg)
-    .nargs(1)
-    .type(arser::DataType::STR)
-    .required(false)
-    .help("Path to the quantization configuration file");
+  arser.add_argument(cfg).help("Path to the quantization configuration file");
 
-  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");
+  arser.add_argument("input").help("Input circle model");
+  arser.add_argument("output").help("Output circle model");
 
   arser.add_argument(gpd).nargs(0).required(false).default_value(false).help(
     "This will turn on profiling data generation.");
@@ -384,27 +373,10 @@ int entry(int argc, char **argv)
     settings->set(luci::UserSettings::Key::ProfilingDataGen, true);
 
   // 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)
-  {
-    std::cerr << "ERROR: Failed to load circle '" << input_path << "'" << std::endl;
+  luci::ImporterEx importerex;
+  auto module = importerex.importVerifyModule(input_path);
+  if (module.get() == nullptr)
     return EXIT_FAILURE;
-  }
-
-  // Import from input Circle file
-  luci::Importer importer;
-  auto module = importer.importModule(circle_model);
 
   for (size_t idx = 0; idx < module->size(); ++idx)
   {