提交 20676cc2 编写于 作者: F fuqiang6

generate activation histogram and support entropy calibration

上级 416a7885
...@@ -134,6 +134,7 @@ MaceStatus SerialNet::Run(RunMetadata *run_metadata) { ...@@ -134,6 +134,7 @@ MaceStatus SerialNet::Run(RunMetadata *run_metadata) {
if (EnvEnabled("MACE_LOG_TENSOR_RANGE") && device_type == CPU) { if (EnvEnabled("MACE_LOG_TENSOR_RANGE") && device_type == CPU) {
for (int i = 0; i < op->OutputSize(); ++i) { for (int i = 0; i < op->OutputSize(); ++i) {
if (op->debug_def().quantize_info_size() == 0) {
int data_type = op->GetOptionalArg("T", static_cast<int>(DT_FLOAT)); int data_type = op->GetOptionalArg("T", static_cast<int>(DT_FLOAT));
if (data_type == static_cast<int>(DT_FLOAT)) { if (data_type == static_cast<int>(DT_FLOAT)) {
float max_v = std::numeric_limits<float>::lowest(); float max_v = std::numeric_limits<float>::lowest();
...@@ -147,6 +148,27 @@ MaceStatus SerialNet::Run(RunMetadata *run_metadata) { ...@@ -147,6 +148,27 @@ MaceStatus SerialNet::Run(RunMetadata *run_metadata) {
LOG(INFO) << "Tensor range @@" << op->debug_def().output(i) LOG(INFO) << "Tensor range @@" << op->debug_def().output(i)
<< "@@" << min_v << "," << max_v; << "@@" << 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<int> 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<float>();
for (index_t j = 0; j < op->Output(i)->size(); ++j) {
int ind = static_cast<int>((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);
}
}
} }
} }
} }
......
...@@ -18,8 +18,13 @@ ...@@ -18,8 +18,13 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map>
#include <sstream>
#include "mace/core/operator.h" #include "mace/core/operator.h"
#include "mace/utils/string_util.h"
#define kBinSize 2048
namespace mace { namespace mace {
......
...@@ -1797,9 +1797,6 @@ class Transformer(base_converter.ConverterInterface): ...@@ -1797,9 +1797,6 @@ class Transformer(base_converter.ConverterInterface):
return False return False
def add_quantize_tensor_range(self): def add_quantize_tensor_range(self):
if not self._option.quantize:
return False
# Quantize info from range statistics # Quantize info from range statistics
print("Add quantize tensor range") print("Add quantize tensor range")
range_file = self._option.quantize_range_file range_file = self._option.quantize_range_file
...@@ -1829,6 +1826,8 @@ class Transformer(base_converter.ConverterInterface): ...@@ -1829,6 +1826,8 @@ class Transformer(base_converter.ConverterInterface):
self._quantize_activation_info[output] self._quantize_activation_info[output]
for output in op.output]) for output in op.output])
if not self._option.quantize:
return False
print ("Add default quantize info for ops like Pooling, Softmax") print ("Add default quantize info for ops like Pooling, Softmax")
for op in self._model.op: for op in self._model.op:
if op.type in [MaceOp.Pooling.name, if op.type in [MaceOp.Pooling.name,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册