From 20676cc2f719b9d13d4c816c6b403ab8ea176acc Mon Sep 17 00:00:00 2001 From: fuqiang6 Date: Wed, 12 Sep 2018 18:30:02 +0800 Subject: [PATCH] generate activation histogram and support entropy calibration --- mace/core/net.cc | 44 ++++++++++++++----- mace/core/net.h | 5 +++ .../tools/converter_tool/transformer.py | 5 +-- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/mace/core/net.cc b/mace/core/net.cc index faa4e1d3..e4bd4c37 100644 --- a/mace/core/net.cc +++ b/mace/core/net.cc @@ -134,18 +134,40 @@ MaceStatus SerialNet::Run(RunMetadata *run_metadata) { if (EnvEnabled("MACE_LOG_TENSOR_RANGE") && device_type == CPU) { for (int i = 0; i < op->OutputSize(); ++i) { - int data_type = op->GetOptionalArg("T", static_cast(DT_FLOAT)); - if (data_type == static_cast(DT_FLOAT)) { - float max_v = std::numeric_limits::lowest(); - float min_v = std::numeric_limits::max(); - Tensor::MappingGuard guard(op->Output(i)); - const float *output_data = op->Output(i)->data(); - for (index_t j = 0; j < op->Output(i)->size(); ++j) { - max_v = std::max(max_v, output_data[j]); - min_v = std::min(min_v, output_data[j]); + if (op->debug_def().quantize_info_size() == 0) { + int data_type = op->GetOptionalArg("T", static_cast(DT_FLOAT)); + if (data_type == static_cast(DT_FLOAT)) { + float max_v = std::numeric_limits::lowest(); + float min_v = std::numeric_limits::max(); + Tensor::MappingGuard guard(op->Output(i)); + const float *output_data = op->Output(i)->data(); + for (index_t j = 0; j < op->Output(i)->size(); ++j) { + max_v = std::max(max_v, output_data[j]); + min_v = std::min(min_v, output_data[j]); + } + LOG(INFO) << "Tensor range @@" << op->debug_def().output(i) + << "@@" << min_v << "," << max_v; + } + } else { + for (int ind = 0; ind < op->debug_def().quantize_info_size(); ++ind) { + float min_v = op->debug_def().quantize_info(ind).minval(); + float max_v = op->debug_def().quantize_info(ind).maxval(); + std::vector bin_distribution(kBinSize, 0); + float bin_v = (max_v - min_v) / kBinSize; + Tensor::MappingGuard guard(op->Output(i)); + const float *output_data = op->Output(i)->data(); + for (index_t j = 0; j < op->Output(i)->size(); ++j) { + int ind = static_cast((output_data[j] - min_v) / bin_v); + if (ind < 0) + ind = 0; + else if (ind > kBinSize-1) + ind = kBinSize-1; + bin_distribution[ind]++; + } + LOG(INFO) << "Tensor range @@" << op->debug_def().output(i) + << "@@" << min_v << "," << max_v<< "@@" + << MakeString(bin_distribution); } - LOG(INFO) << "Tensor range @@" << op->debug_def().output(i) - << "@@" << min_v << "," << max_v; } } } diff --git a/mace/core/net.h b/mace/core/net.h index a63ded66..140eb8c4 100644 --- a/mace/core/net.h +++ b/mace/core/net.h @@ -18,8 +18,13 @@ #include #include #include +#include +#include #include "mace/core/operator.h" +#include "mace/utils/string_util.h" + +#define kBinSize 2048 namespace mace { diff --git a/mace/python/tools/converter_tool/transformer.py b/mace/python/tools/converter_tool/transformer.py index b591ab2b..787697cb 100644 --- a/mace/python/tools/converter_tool/transformer.py +++ b/mace/python/tools/converter_tool/transformer.py @@ -1797,9 +1797,6 @@ class Transformer(base_converter.ConverterInterface): return False def add_quantize_tensor_range(self): - if not self._option.quantize: - return False - # Quantize info from range statistics print("Add quantize tensor range") range_file = self._option.quantize_range_file @@ -1829,6 +1826,8 @@ class Transformer(base_converter.ConverterInterface): self._quantize_activation_info[output] for output in op.output]) + if not self._option.quantize: + return False print ("Add default quantize info for ops like Pooling, Softmax") for op in self._model.op: if op.type in [MaceOp.Pooling.name, -- GitLab