Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[LPT] Skip PadTransformation for non-representable CONSTANT pad values (#37411)

Pramit Biswasaa06f7465c28
diff --git a/src/common/low_precision_transformations/include/low_precision/network_helper.hpp b/src/common/low_precision_transformations/include/low_precision/network_helper.hpp
index 8386c7212c5..c2f31f9dfa2 100644
--- a/src/common/low_precision_transformations/include/low_precision/network_helper.hpp
+++ b/src/common/low_precision_transformations/include/low_precision/network_helper.hpp
@@ -247,6 +247,10 @@ public:
static bool checkConstantNotInf(const std::shared_ptr<Node> constant_node);
+ // Returns the inclusive [minimum, maximum] range for supported integral precisions.
+ // Returns std::nullopt when no range is defined for the given precision.
+ static std::optional<std::pair<double, double>> getPrecisionLimits(const element::Type_t precision);
+
private:
static std::shared_ptr<Node> foldFakeQuantize(
const std::shared_ptr<ov::opset1::FakeQuantize>& fq,
diff --git a/src/common/low_precision_transformations/src/network_helper.cpp b/src/common/low_precision_transformations/src/network_helper.cpp
index 472d0f140fc..dd271573779 100644
--- a/src/common/low_precision_transformations/src/network_helper.cpp
+++ b/src/common/low_precision_transformations/src/network_helper.cpp
@@ -8,6 +8,7 @@
#include <map>
#include <memory>
#include <numeric>
+#include <optional>
#include <queue>
#include <string>
#include <unordered_set>
@@ -21,6 +22,7 @@
#include "low_precision/rt_info/precision_preserved_attribute.hpp"
#include "low_precision/rt_info/quantization_alignment_attribute.hpp"
#include "openvino/core/rt_info.hpp"
+#include "openvino/core/type/element_type_traits.hpp"
#include "openvino/core/validation_util.hpp"
#include "openvino/opsets/opset3_decl.hpp"
#include "openvino/opsets/opset6_decl.hpp"
@@ -1876,6 +1878,31 @@ bool NetworkHelper::checkConstantNotInf(const std::shared_ptr<Node> constant_nod
const auto values = constant->cast_vector<float>();
return std::all_of(values.begin(), values.end(), [](const float x) { return !std::isinf(x); });
}
+
+namespace {
+template <ov::element::Type_t precision>
+std::pair<double, double> makeIntegralPrecisionLimits() {
+ using FundamentalType = ov::fundamental_type_for<precision>;
+ return std::make_pair(static_cast<double>(std::numeric_limits<FundamentalType>::lowest()),
+ static_cast<double>(std::numeric_limits<FundamentalType>::max()));
+}
+} // namespace
+
+std::optional<std::pair<double, double>> NetworkHelper::getPrecisionLimits(const element::Type_t precision) {
+ static const std::map<ov::element::Type_t, std::pair<double, double>> limits{
+ // Sub-byte types use byte-sized storage types, so their narrower ranges are specified explicitly.
+ {ov::element::i4, {-8.0, 7.0}},
+ {ov::element::u4, {0.0, 15.0}},
+ {ov::element::i8, makeIntegralPrecisionLimits<ov::element::i8>()},
+ {ov::element::u8, makeIntegralPrecisionLimits<ov::element::u8>()},
+ {ov::element::i16, makeIntegralPrecisionLimits<ov::element::i16>()},
+ {ov::element::u16, makeIntegralPrecisionLimits<ov::element::u16>()},
+ {ov::element::i32, makeIntegralPrecisionLimits<ov::element::i32>()},
+ {ov::element::u32, makeIntegralPrecisionLimits<ov::element::u32>()}};
+
+ const auto it = limits.find(precision);
+ return it == limits.end() ? std::nullopt : std::optional<std::pair<double, double>>(it->second);
+}
} // namespace low_precision
} // namespace pass
} // namespace ov
diff --git a/src/common/low_precision_transformations/src/pad.cpp b/src/common/low_precision_transformations/src/pad.cpp
index 93278c9ea12..04da519affa 100644
--- a/src/common/low_precision_transformations/src/pad.cpp
+++ b/src/common/low_precision_transformations/src/pad.cpp
@@ -4,6 +4,7 @@
#include "low_precision/pad.hpp"
+#include <cmath>
#include <memory>
#include "itt.hpp"
@@ -42,6 +43,9 @@ PadTransformation::PadTransformation(const Params& params) : LayerTransformation
}
namespace {
+ // Input port of PadBase carrying the pad value (arg_pad_value).
+ constexpr size_t padValueInputIndex = 3;
+
bool hasPositiveIndexes(const std::shared_ptr<ov::op::util::PadBase>& pad) {
const auto padsBegin = pad->get_pads_begin();
const auto padsEnd = pad->get_pads_end();
@@ -51,6 +55,27 @@ namespace {
return std::any_of(padsBegin.begin(), padsBegin.end(), pred) ||
std::any_of(padsEnd.begin(), padsEnd.end(), pred);
}
+
+ // Returns true when every constant Pad value is finite and fits the requested precision.
+ // Types without configured limits are checked only for finite values.
+ bool padValueIsRepresentable(const std::shared_ptr<ov::op::util::PadBase>& pad, const element::Type& precision) {
+ const auto padValue = ov::as_type_ptr<ov::opset1::Constant>(pad->get_input_node_shared_ptr(padValueInputIndex));
+ if (padValue == nullptr) {
+ return false;
+ }
+
+ const auto limits = NetworkHelper::getPrecisionLimits(precision);
+
+ const auto values = padValue->cast_vector<float>();
+
+ return std::all_of(values.begin(), values.end(), [&limits](const float value) {
+ const bool isFinite = std::isfinite(value);
+ const bool isWithinPrecisionLimits = !limits.has_value() ||
+ (static_cast<double>(value) >= limits->first &&
+ static_cast<double>(value) <= limits->second);
+ return isFinite && isWithinPrecisionLimits;
+ });
+ }
} // namespace
bool PadTransformation::transform(ov::pass::pattern::Matcher& m) {
@@ -59,7 +84,7 @@ bool PadTransformation::transform(ov::pass::pattern::Matcher& m) {
}
const auto pad = ov::as_type_ptr<ov::op::util::PadBase>(NetworkHelper::separateInStandaloneBranch(m.get_match_root(), defaultPrecisions));
- const auto padConstant = ov::as_type_ptr<ov::opset1::Constant>(pad->get_input_node_shared_ptr(3));
+ const auto padConstant = ov::as_type_ptr<ov::opset1::Constant>(pad->get_input_node_shared_ptr(padValueInputIndex));
const auto padConstantValue = padConstant->cast_vector<float>()[0];
const auto padsBegin = pad->get_pads_begin();
@@ -165,7 +190,7 @@ bool PadTransformation::transform(ov::pass::pattern::Matcher& m) {
// we must convert pad value in low precision
const auto convertedZero = ov::opset1::Constant::create(dequantization.data.get_element_type(), Shape{}, { padConstantValue });
- pad->set_argument(3, convertedZero);
+ pad->set_argument(padValueInputIndex, convertedZero);
const auto newOperation = moveDequantizationAfter(pad, dequantization);
@@ -196,6 +221,11 @@ bool PadTransformation::canBeTransformed(const std::shared_ptr<Node>& op) const
return false;
}
+ if (mode == op::PadMode::CONSTANT &&
+ !padValueIsRepresentable(pad, dequantization.data.get_element_type())) {
+ return false;
+ }
+
if (!hasPositiveIndexes(pad))
return true;
@@ -266,7 +296,7 @@ bool PadTransformation::canBeTransformed(const std::shared_ptr<Node>& op) const
return false;
}
- const auto constant = ov::as_type_ptr<ov::opset1::Constant>(pad->get_input_node_shared_ptr(3));
+ const auto constant = ov::as_type_ptr<ov::opset1::Constant>(pad->get_input_node_shared_ptr(padValueInputIndex));
const auto constantValue = constant->cast_vector<float>()[0];
if (constantValue != 0.f && !padAndDqByTheSameDimension(dequantization.multiplyConstant)) {
return false;
diff --git a/src/common/low_precision_transformations/tests/pad_transformation.cpp b/src/common/low_precision_transformations/tests/pad_transformation.cpp
index dd9cc831fec..2282e03560b 100644
--- a/src/common/low_precision_transformations/tests/pad_transformation.cpp
+++ b/src/common/low_precision_transformations/tests/pad_transformation.cpp
@@ -4,6 +4,7 @@
#include "layer_transformation.hpp"
+#include <limits>
#include <string>
#include <sstream>
#include <gtest/gtest.h>
@@ -14,6 +15,7 @@
#include "common_test_utils/ov_test_utils.hpp"
#include "ov_lpt_models/common/dequantization_operations.hpp"
#include "ov_lpt_models/pad.hpp"
+#include "openvino/op/pad.hpp"
#include "simple_low_precision_transformer.hpp"
namespace {
@@ -1086,4 +1088,137 @@ INSTANTIATE_TEST_SUITE_P(
::testing::ValuesIn(testValuesForDynamicRank)),
PadTransformation::getTestCaseName);
} // namespace testCasesWithDynamicRank
+
+// CONSTANT-mode Pad transforms representable values and skips non-representable values.
+namespace testCasesForPadValueRepresentability {
+const std::pair<std::vector<int64_t>, std::vector<int64_t>> padsByUniqueDimension = {
+ {0, 0, 2, 0}, {0, 0, 1, 0}
+};
+
+struct PadValuePrecisionLimits {
+ ov::element::Type precision;
+ float minimum;
+ float maximum;
+ float outsideBelow;
+ float outsideAbove;
+};
+
+const std::vector<PadValuePrecisionLimits> supportedIntegralPrecisions = {
+ { ov::element::i4, -8.f, 7.f, -9.f, 8.f },
+ { ov::element::u4, 0.f, 15.f, -1.f, 16.f },
+ { ov::element::i8, -128.f, 127.f, -129.f, 128.f },
+ { ov::element::u8, 0.f, 255.f, -1.f, 256.f },
+ { ov::element::i16, -32768.f, 32767.f, -32769.f, 32768.f },
+ { ov::element::u16, 0.f, 65535.f, -1.f, 65536.f },
+ { ov::element::i32, -2147483648.f, 2147483520.f, -2147483904.f, 2147483648.f },
+ { ov::element::u32, 0.f, 4294967040.f, -1.f, 4294967296.f }
+};
+
+PadTransformationTestValues makeTestValues(const ov::element::Type& precision,
+ const float padValue,
+ const bool shouldTransform) {
+ const TestTransformationParams params(
+ true, { precision }, { precision }, true, ov::element::f32, false, { precision });
+ const ov::builder::subgraph::DequantizationOperations dequantization{ {ov::element::f32}, {}, {3.f} };
+
+ // Keep the original graph when the Pad transformation is skipped.
+ if (!shouldTransform) {
+ return { params, { precision, dequantization }, { precision, dequantization, ov::element::f32, {} } };
+ }
+
+ // a non zero pad value makes the scalar multiply constant broadcasted and padded by the padded dimension
+ const ov::builder::subgraph::DequantizationOperations dequantizationAfter = padValue == 0.f
+ ? dequantization
+ : ov::builder::subgraph::DequantizationOperations{
+ {ov::element::f32},
+ {},
+ {{1.f, 1.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 1.f}, ov::element::f32, {1, 1, 9, 1}}};
+
+ return { params, { precision, dequantization }, { precision, {}, precision, dequantizationAfter } };
+}
+
+std::vector<PadTransformationParams> generateTestParams() {
+ std::vector<PadTransformationParams> result;
+ for (const auto& inputShape : inputShapes) {
+ for (const auto& limits : supportedIntegralPrecisions) {
+ const std::vector<std::pair<float, bool>> padValues = {
+ { limits.minimum, true },
+ { limits.maximum, true },
+ { limits.outsideBelow, false },
+ { limits.outsideAbove, false },
+ { -std::numeric_limits<float>::infinity(), false },
+ { std::numeric_limits<float>::infinity(), false },
+ { std::numeric_limits<float>::quiet_NaN(), false }
+ };
+
+ for (const auto& padValue : padValues) {
+ result.emplace_back(inputShape,
+ padsByUniqueDimension,
+ ov::op::PadMode::CONSTANT,
+ padValue.first,
+ makeTestValues(limits.precision, padValue.first, padValue.second));
+ }
+ }
+ }
+ return result;
+}
+
+INSTANTIATE_TEST_SUITE_P(
+ smoke_LPT,
+ PadTransformation,
+ ::testing::ValuesIn(generateTestParams()),
+ PadTransformation::getTestCaseName);
+} // namespace testCasesForPadValueRepresentability
+
+
+// These cases build the Pad graph with an f64 constant because PadTransformationParams stores
+// padValue as float; passing a value such as 1e300 through that fixture would overflow to infinity
+// when converted to float before the transformation can validate the value.
+namespace testCasesForF64PadValue {
+std::shared_ptr<ov::Model> buildModel(const double padValue) {
+ const auto input = std::make_shared<ov::opset1::Parameter>(ov::element::u8, ov::PartialShape{ 1, 3, 6, 6 });
+ const auto convert = std::make_shared<ov::opset1::Convert>(input, ov::element::f64);
+ const auto multiply = std::make_shared<ov::opset1::Multiply>(
+ convert, ov::opset1::Constant::create(ov::element::f64, ov::Shape{}, std::vector<double>{ 3.0 }));
+ const auto pad = std::make_shared<ov::op::v12::Pad>(
+ multiply,
+ ov::opset1::Constant::create(ov::element::i64, ov::Shape{ 4 }, std::vector<int64_t>{ 0, 0, 2, 0 }),
+ ov::opset1::Constant::create(ov::element::i64, ov::Shape{ 4 }, std::vector<int64_t>{ 0, 0, 1, 0 }),
+ ov::opset1::Constant::create(ov::element::f64, ov::Shape{}, std::vector<double>{ padValue }),
+ ov::op::PadMode::CONSTANT);
+ pad->set_friendly_name("Pad");
+ return std::make_shared<ov::Model>(ov::ResultVector{ std::make_shared<ov::opset1::Result>(pad) },
+ ov::ParameterVector{ input });
+}
+
+void checkTransformationIsSkipped(const double padValue) {
+ auto actualFunction = buildModel(padValue);
+ const auto referenceFunction = buildModel(padValue);
+
+ SimpleLowPrecisionTransformer transformer;
+ transformer.add<ov::pass::low_precision::PadTransformation, ov::op::v1::Pad>(
+ TestTransformationParams(true,
+ { ov::element::u8 },
+ { ov::element::u8 },
+ true,
+ ov::element::f64,
+ false,
+ { ov::element::u8 }));
+ transformer.transform(actualFunction);
+ actualFunction->validate_nodes_and_infer_types();
+
+ const auto res = compare_functions(actualFunction, referenceFunction, true, true);
+ ASSERT_TRUE(res.first) << res.second;
+}
+
+TEST(PadValueRepresentability, RejectsPadValueOverflowingFloat) {
+ checkTransformationIsSkipped(1e300);
+}
+
+// counterpart of the case above: 1e30 survives the narrowing,
+// so the u8 range check does the rejection
+TEST(PadValueRepresentability, RejectsFinitePadValueOutOfDataPrecisionRange) {
+ checkTransformationIsSkipped(1e30);
+}
+} // namespace testCasesForF64PadValue
} // namespace