[LPT] Quantization to point support (#24872)

### Details:
 - *Quaintization to point support*
 - Tests: wip

### Tickets:
 - *CVS-139557*
This commit is contained in:
Edward Shogulin 2024-06-11 10:11:04 +01:00 committed by GitHub
parent 395d6b7e09
commit deb5e63ff2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 27 deletions

View File

@ -691,6 +691,15 @@ size_t NetworkHelper::calculateLevels(
float& dequantizationSub,
float& updatedOutputLowValue,
float& updatedOutputHighValue) {
if (combinedIntervalHigh == combinedIntervalLow) {
// degenerate case: quantization to a point
dequantizationMul = 1.f;
dequantizationSub = 0.f;
updatedOutputLowValue = combinedIntervalLow;
updatedOutputHighValue = combinedIntervalHigh;
return 1ull;
}
const float maxOutputInterval = combinedIntervalHigh - combinedIntervalLow;
// FQ -> SUB_quantization -> MUL_quantization -[INT8]-> SUB_dequantization -> MUL_dequantization ->
const float quantizationMul = (dataPrecisionMax - dataPrecisionMin) / maxOutputInterval;
@ -882,38 +891,27 @@ std::tuple<std::shared_ptr<Node>, std::shared_ptr<Node>> NetworkHelper::decompos
std::vector<float> shifts(outputSize, 0.f);
std::vector<float> scales(outputSize);
// compute dequantizations (in double for INT32)
if (precision == element::i32 || precision == element::u32) {
for (size_t i = 0; i < outputSize; ++i) {
if (outputHighValues[i] != outputLowValues[i]) {
for (size_t i = 0; i < outputSize; ++i) {
if (outputHighValues[i] != outputLowValues[i]) {
// compute dequantizations (in double for INT32)
if (precision == element::i32 || precision == element::u32) {
shifts[i] = static_cast<float>(
(static_cast<double>(min) * outputHighValues[i] - static_cast<double>(max) * outputLowValues[i]) /
(static_cast<double>(outputHighValues[i]) - outputLowValues[i]));
scales[i] = static_cast<float>(
(static_cast<double>(outputHighValues[i]) - outputLowValues[i]) / (static_cast<double>(max) - min));
if (shifts[i] == -0.f) {
shifts[i] = 0.f;
}
} else {
scales[i] = outputHighValues[i];
minValues[i] = 1.f;
maxValues[i] = 1.f;
}
}
} else {
for (size_t i = 0; i < outputSize; ++i) {
if (outputHighValues[i] != outputLowValues[i]) {
shifts[i] = (min * outputHighValues[i] - max * outputLowValues[i]) /
(outputHighValues[i] - outputLowValues[i]);
scales[i] = (outputHighValues[i] - outputLowValues[i]) / (max - min);
if (shifts[i] == -0.f) {
shifts[i] = 0.f;
}
} else {
scales[i] = outputHighValues[i];
minValues[i] = 1.f;
maxValues[i] = 1.f;
}
if (shifts[i] == -0.f) {
shifts[i] = 0.f;
}
} else {
scales[i] = 1.f;
minValues[i] = outputHighValues[i];
maxValues[i] = outputHighValues[i];
}
}
@ -981,15 +979,12 @@ std::tuple<std::shared_ptr<Node>, std::shared_ptr<Node>> NetworkHelper::decompos
std::shared_ptr<ov::Node> convert2;
if (updatePrecision) {
std::shared_ptr<Node> convert;
std::shared_ptr<ov::opset1::Constant> newFqConstant = ov::as_type_ptr<ov::opset1::Constant>(newFQ);
if (ov::is_type<ov::opset1::Constant>(newFQ)) {
convert = foldConvert(newFQ->output(0), precision);
} else if (ov::is_type<ov::opset1::FakeQuantize>(newFQ)) {
if (ov::is_type<ov::opset1::FakeQuantize>(newFQ)) {
newFQ = setOutDataPrecision(ov::as_type_ptr<ov::opset1::FakeQuantize>(newFQ), precision);
convert = newFQ;
} else {
THROW_IE_LPT_EXCEPTION(*newFQ) << "unexpected operation type";
convert = foldConvert(newFQ->output(0), precision);
}
convert2 = std::make_shared<ov::opset1::Convert>(convert, element::f32);

View File

@ -141,6 +141,14 @@ const std::vector<LayerTestsDefinitions::ConvolutionTransformationParam> params
false,
"Convolution",
"f32"
},
{
{ 256ul, ov::Shape { 1, 1, 1, 1 }, { 0.f }, { 0.f }, { 0.f }, { 0.f } },
false,
{ 255ul, ov::Shape { 1, 1, 1, 1 }, { 0.f }, { 254.f }, { -12.7f }, { 12.7f } },
false,
"Convolution",
"u8"
}
};

View File

@ -110,6 +110,14 @@ const std::vector<LayerTestsDefinitions::ConvolutionTransformationParam> params
false,
"Convolution",
"FP32"
},
{
{ 256ul, ov::Shape { 1, 1, 1, 1 }, { 0.f }, { 0.f }, { 0.f }, { 0.f } },
false,
{ 255ul, ov::Shape { 1, 1, 1, 1 }, { 0.f }, { 254.f }, { -12.7f }, { 12.7f } },
false,
"Convolution",
"u8"
}
};