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

generate activation histogram and support entropy calibration

上级 416a7885
......@@ -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<int>(DT_FLOAT));
if (data_type == static_cast<int>(DT_FLOAT)) {
float max_v = std::numeric_limits<float>::lowest();
float min_v = std::numeric_limits<float>::max();
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) {
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<int>(DT_FLOAT));
if (data_type == static_cast<int>(DT_FLOAT)) {
float max_v = std::numeric_limits<float>::lowest();
float min_v = std::numeric_limits<float>::max();
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) {
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<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);
}
LOG(INFO) << "Tensor range @@" << op->debug_def().output(i)
<< "@@" << min_v << "," << max_v;
}
}
}
......
......@@ -18,8 +18,13 @@
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>
#include <sstream>
#include "mace/core/operator.h"
#include "mace/utils/string_util.h"
#define kBinSize 2048
namespace mace {
......
......@@ -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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册