|
|
|
@ -33,7 +33,7 @@ STATUS QuantParamCalcer::ComputeConstQuantParam(const schema::TensorT &tensor, Q
|
|
|
|
|
return RET_OK;
|
|
|
|
|
}
|
|
|
|
|
if (tensor.dataType != TypeId::kNumberTypeFloat) {
|
|
|
|
|
MS_LOG(WARNING) << "Const Tensor without quantParam should has float dataType, in fact: " << tensor.dataType;
|
|
|
|
|
MS_LOG(ERROR) << "Const Tensor without quantParam should has float dataType, in fact: " << tensor.dataType;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
const auto *constData = reinterpret_cast<const float *>(tensor.data.data());
|
|
|
|
@ -83,7 +83,7 @@ int QuantParamCalcer::Calc(MetaGraphT *graph, const CNodeT &node) {
|
|
|
|
|
if (!tensor->data.empty() && !IsContain(graph->inputIndex, node.inputIndex.at(i))) {
|
|
|
|
|
auto status = ComputeConstQuantParam((*tensor), quantParam.get());
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "ComputeConstQuantParam failed: " << status;
|
|
|
|
|
MS_LOG(INFO) << "ComputeConstQuantParam failed: " << status;
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
tensor->quantParams.front() = std::move(quantParam);
|
|
|
|
@ -112,15 +112,15 @@ int QuantParamCalcer::Calc(MetaGraphT *graph, const CNodeT &node) {
|
|
|
|
|
int CommonCalcer::Calc(MetaGraphT *subGraph, const CNodeT &node) {
|
|
|
|
|
auto status = QuantParamCalcer::Calc(subGraph, node);
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "Call QuantParamCalcer::Calc failed: " << status;
|
|
|
|
|
MS_LOG(ERROR) << "Call QuantParamCalcer::Calc failed: " << status;
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
if (inputParamDone != node.inputIndex.size()) {
|
|
|
|
|
MS_LOG(WARNING) << "Can not determine inputTensor quantParam, node " << node.name;
|
|
|
|
|
MS_LOG(ERROR) << "Can not determine inputTensor quantParam, node " << node.name;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
if (outputParamDone != node.outputIndex.size()) {
|
|
|
|
|
MS_LOG(WARNING) << "Can not determine outputTensor quantParam, node " << node.name;
|
|
|
|
|
MS_LOG(ERROR) << "Can not determine outputTensor quantParam, node " << node.name;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
return RET_OK;
|
|
|
|
@ -129,7 +129,7 @@ int CommonCalcer::Calc(MetaGraphT *subGraph, const CNodeT &node) {
|
|
|
|
|
int LinearCalcer::Calc(MetaGraphT *graph, const CNodeT &node) {
|
|
|
|
|
auto status = QuantParamCalcer::Calc(graph, node);
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "Call QuantParamCalcer::Calc failed: " << status;
|
|
|
|
|
MS_LOG(ERROR) << "Call QuantParamCalcer::Calc failed: " << status;
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
if (inputParamDone != node.inputIndex.size()) {
|
|
|
|
@ -139,7 +139,7 @@ int LinearCalcer::Calc(MetaGraphT *graph, const CNodeT &node) {
|
|
|
|
|
auto outputQuantParam = GetTensorQuantParam(outTensor);
|
|
|
|
|
MS_ASSERT(outputQuantParam != nullptr);
|
|
|
|
|
if (outputQuantParam == nullptr || !outputQuantParam->inited) {
|
|
|
|
|
MS_LOG(WARNING) << "Can not determine inputTensor quantParam from outputTensor for node " << node.name;
|
|
|
|
|
MS_LOG(ERROR) << "Can not determine inputTensor quantParam from outputTensor for node " << node.name;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
for (unsigned int i : node.inputIndex) {
|
|
|
|
@ -159,7 +159,7 @@ int LinearCalcer::Calc(MetaGraphT *graph, const CNodeT &node) {
|
|
|
|
|
MS_ASSERT(inTensor != nullptr);
|
|
|
|
|
auto inQuantParam = GetTensorQuantParam(inTensor);
|
|
|
|
|
if (inQuantParam == nullptr || !inQuantParam->inited) {
|
|
|
|
|
MS_LOG(WARNING) << "Can not determine outputTensor quantParam from inputTensor for node %s" << node.name;
|
|
|
|
|
MS_LOG(ERROR) << "Can not determine outputTensor quantParam from inputTensor for node %s" << node.name;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
for (size_t i = 0; i < node.outputIndex.size(); i++) {
|
|
|
|
@ -188,12 +188,12 @@ class CalcConcat : public QuantParamCalcer {
|
|
|
|
|
MS_ASSERT(node.outputIndex.size() == 1);
|
|
|
|
|
auto status = QuantParamCalcer::Calc(graph, node);
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "Call QuantParamCalcer::Calc failed: " << status;
|
|
|
|
|
MS_LOG(ERROR) << "Call QuantParamCalcer::Calc failed: " << status;
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputParamDone != node.inputIndex.size()) {
|
|
|
|
|
MS_LOG(WARNING) << "Can not determine concat inputTensor quantParam, node " << node.name;
|
|
|
|
|
MS_LOG(ERROR) << "Can not determine concat inputTensor quantParam, node " << node.name;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -233,7 +233,7 @@ class CalcConcat : public QuantParamCalcer {
|
|
|
|
|
|
|
|
|
|
status = quant::CalQuantizationParams(outQuantParam.get(), minMin, maxMax, narrowRange, numBits);
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "in aware quantization run CalQuantizationParams failed!";
|
|
|
|
|
MS_LOG(ERROR) << "in aware quantization run CalQuantizationParams failed!";
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
outTensor->quantParams.emplace_back(std::move(outQuantParam));
|
|
|
|
@ -253,12 +253,12 @@ class CalcAdd : public QuantParamCalcer {
|
|
|
|
|
MS_ASSERT(node.outputIndex.size() == 1);
|
|
|
|
|
auto status = QuantParamCalcer::Calc(graph, node);
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "Call QuantParamCalcer::Calc failed: " << status;
|
|
|
|
|
MS_LOG(ERROR) << "Call QuantParamCalcer::Calc failed: " << status;
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputParamDone != 2) {
|
|
|
|
|
MS_LOG(WARNING) << "Can not determine add inputTensor quantParam, node " << node.name;
|
|
|
|
|
MS_LOG(ERROR) << "Can not determine add inputTensor quantParam, node " << node.name;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
if (outputParamDone != 1) {
|
|
|
|
@ -283,7 +283,7 @@ class CalcAdd : public QuantParamCalcer {
|
|
|
|
|
biasTensor = &tensor1;
|
|
|
|
|
paramTensor = &tensor0;
|
|
|
|
|
} else {
|
|
|
|
|
MS_LOG(WARNING) << "Can not determine add outputTensor quantParam, node " << node.name;
|
|
|
|
|
MS_LOG(ERROR) << "Can not determine add outputTensor quantParam, node " << node.name;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
auto quantParam = GetTensorQuantParam(*paramTensor);
|
|
|
|
@ -298,7 +298,7 @@ class CalcAdd : public QuantParamCalcer {
|
|
|
|
|
auto *bias = static_cast<float *>(oriTensorData);
|
|
|
|
|
status = quant::CalQuantizationParams(outQuantParam.get(), min + (*bias), max + (*bias));
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "in aware quantization run CalQuantizationParams failed!";
|
|
|
|
|
MS_LOG(ERROR) << "in aware quantization run CalQuantizationParams failed!";
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
} else if ((*biasTensor)->dataType == TypeId::kNumberTypeUInt8) {
|
|
|
|
@ -307,11 +307,11 @@ class CalcAdd : public QuantParamCalcer {
|
|
|
|
|
auto *bias = static_cast<uint8_t *>(oriTensorData);
|
|
|
|
|
status = quant::CalQuantizationParams(outQuantParam.get(), min + (*bias), max + (*bias));
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "in aware quantization run CalQuantizationParams failed!";
|
|
|
|
|
MS_LOG(ERROR) << "in aware quantization run CalQuantizationParams failed!";
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
MS_LOG(WARNING) << "Unsupported tensor dataType: " << (*biasTensor)->dataType;
|
|
|
|
|
MS_LOG(ERROR) << "Unsupported tensor dataType: " << (*biasTensor)->dataType;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -330,12 +330,12 @@ class CalcRealDiv : public QuantParamCalcer {
|
|
|
|
|
MS_ASSERT(node.outputIndex.size() == 1);
|
|
|
|
|
auto status = QuantParamCalcer::Calc(graph, node);
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "Call QuantParamCalcer::Calc failed: " << status;
|
|
|
|
|
MS_LOG(ERROR) << "Call QuantParamCalcer::Calc failed: " << status;
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputParamDone != 2) {
|
|
|
|
|
MS_LOG(WARNING) << "Can not determine realdiv inputTensor quantParam, node " << node.name;
|
|
|
|
|
MS_LOG(ERROR) << "Can not determine realdiv inputTensor quantParam, node " << node.name;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
if (outputParamDone != 1) {
|
|
|
|
@ -361,7 +361,7 @@ class CalcRealDiv : public QuantParamCalcer {
|
|
|
|
|
MS_ASSERT(*div != 0);
|
|
|
|
|
status = quant::CalQuantizationParams(outQuantParam.get(), min / (*div), max / (*div));
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "in aware quantization run CalQuantizationParams failed!";
|
|
|
|
|
MS_LOG(ERROR) << "in aware quantization run CalQuantizationParams failed!";
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
} else if (tensor1->dataType == TypeId::kNumberTypeUInt8) {
|
|
|
|
@ -370,17 +370,17 @@ class CalcRealDiv : public QuantParamCalcer {
|
|
|
|
|
auto *div = static_cast<uint8_t *>(oriTensorData);
|
|
|
|
|
status = quant::CalQuantizationParams(outQuantParam.get(), min / (*div), max + (*div));
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "in aware quantization run CalQuantizationParams failed!";
|
|
|
|
|
MS_LOG(ERROR) << "in aware quantization run CalQuantizationParams failed!";
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
MS_LOG(WARNING) << "Unsupported tensor dataType: " << tensor1->dataType;
|
|
|
|
|
MS_LOG(ERROR) << "Unsupported tensor dataType: " << tensor1->dataType;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
outTensor->quantParams.front() = std::move(outQuantParam);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
MS_LOG(WARNING) << "Can not determine realDiv outputTensor quantParam, node " << node.name;
|
|
|
|
|
MS_LOG(ERROR) << "Can not determine realDiv outputTensor quantParam, node " << node.name;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -397,19 +397,19 @@ class CalcToSet : public QuantParamCalcer {
|
|
|
|
|
MS_ASSERT(node.outputIndex.size() == 1);
|
|
|
|
|
auto status = QuantParamCalcer::Calc(graph, node);
|
|
|
|
|
if (status != RET_OK) {
|
|
|
|
|
MS_LOG(WARNING) << "Call QuantParamCalcer::Calc failed: %d" << status;
|
|
|
|
|
MS_LOG(ERROR) << "Call QuantParamCalcer::Calc failed: %d" << status;
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
// input
|
|
|
|
|
if (inputParamDone != node.inputIndex.size()) {
|
|
|
|
|
MS_LOG(WARNING) << "Can not determine inputTensor quantParam, node " << node.name;
|
|
|
|
|
MS_LOG(ERROR) << "Can not determine inputTensor quantParam, node " << node.name;
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
// output
|
|
|
|
|
if (outputParamDone != node.outputIndex.size()) {
|
|
|
|
|
std::unique_ptr<QuantParamT> quantParam = std::make_unique<QuantParamT>();
|
|
|
|
|
if (quantParam == nullptr) {
|
|
|
|
|
MS_LOG(WARNING) << "new QuantParamT failed";
|
|
|
|
|
MS_LOG(ERROR) << "new QuantParamT failed";
|
|
|
|
|
return RET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
quantParam->scale = (max - min) / 256;
|
|
|
|
|