Skip to content

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

[TF FE] Validate num_partitions attribute in DynamicPartition converter (#34393)

Evgeny Kotov93c2a388e4f6
diff --git a/src/frontends/tensorflow/tests/convert_tricky_models.cpp b/src/frontends/tensorflow/tests/convert_tricky_models.cpp
index 1c98002af27..100165a4b07 100644
--- a/src/frontends/tensorflow/tests/convert_tricky_models.cpp
+++ b/src/frontends/tensorflow/tests/convert_tricky_models.cpp
@@ -842,3 +842,45 @@ TEST_F(FrontEndConversionWithReferenceTestsF, UnitializedVariableV2AsInput) {
model_ref = make_shared<Model>(OutputVector{mul}, ParameterVector{x, var});
}
}
+
+TEST(FrontEndConvertTrickyModels, dynpart_zero_num_partitions) {
+ shared_ptr<Model> model = nullptr;
+ try {
+ model = convert_model("dynpart_zero_partitions/dynpart_zero_partitions.pbtxt");
+ FAIL() << "DynamicPartition with num_partitions=0 must throw OpConversionFailure.";
+ } catch (const OpConversionFailure& error) {
+ std::string error_message = error.what();
+ ASSERT_TRUE(error_message.find("num_partitions") != std::string::npos);
+ ASSERT_EQ(model, nullptr);
+ } catch (...) {
+ FAIL() << "DynamicPartition with num_partitions=0 failed with unexpected exception type.";
+ }
+}
+
+TEST(FrontEndConvertTrickyModels, dynpart_negative_num_partitions) {
+ shared_ptr<Model> model = nullptr;
+ try {
+ model = convert_model("dynpart_negative_partitions/dynpart_negative_partitions.pbtxt");
+ FAIL() << "DynamicPartition with num_partitions=-1 must throw OpConversionFailure.";
+ } catch (const OpConversionFailure& error) {
+ std::string error_message = error.what();
+ ASSERT_TRUE(error_message.find("num_partitions") != std::string::npos);
+ ASSERT_EQ(model, nullptr);
+ } catch (...) {
+ FAIL() << "DynamicPartition with num_partitions=-1 failed with unexpected exception type.";
+ }
+}
+
+TEST(FrontEndConvertTrickyModels, dynpart_overflow_num_partitions) {
+ shared_ptr<Model> model = nullptr;
+ try {
+ model = convert_model("dynpart_overflow_partitions/dynpart_overflow_partitions.pbtxt");
+ FAIL() << "DynamicPartition with num_partitions exceeding int32 range must throw OpConversionFailure.";
+ } catch (const OpConversionFailure& error) {
+ std::string error_message = error.what();
+ ASSERT_TRUE(error_message.find("num_partitions") != std::string::npos);
+ ASSERT_EQ(model, nullptr);
+ } catch (...) {
+ FAIL() << "DynamicPartition with overflowing num_partitions failed with unexpected exception type.";
+ }
+}
diff --git a/src/frontends/tensorflow/tests/test_models/models_pbtxt/dynpart_negative_partitions.pbtxt b/src/frontends/tensorflow/tests/test_models/models_pbtxt/dynpart_negative_partitions.pbtxt
new file mode 100644
index 00000000000..09f6cc83fac
--- /dev/null
+++ b/src/frontends/tensorflow/tests/test_models/models_pbtxt/dynpart_negative_partitions.pbtxt
@@ -0,0 +1,61 @@
+node {
+ name: "data"
+ op: "Placeholder"
+ attr {
+ key: "dtype"
+ value {
+ type: DT_FLOAT
+ }
+ }
+ attr {
+ key: "shape"
+ value {
+ shape {
+ dim {
+ size: 10
+ }
+ }
+ }
+ }
+}
+node {
+ name: "partitions"
+ op: "Placeholder"
+ attr {
+ key: "dtype"
+ value {
+ type: DT_INT32
+ }
+ }
+ attr {
+ key: "shape"
+ value {
+ shape {
+ dim {
+ size: 10
+ }
+ }
+ }
+ }
+}
+node {
+ name: "dynamic_partition"
+ op: "DynamicPartition"
+ input: "data"
+ input: "partitions"
+ attr {
+ key: "T"
+ value {
+ type: DT_FLOAT
+ }
+ }
+ attr {
+ key: "num_partitions"
+ value {
+ i: -1
+ }
+ }
+}
+versions {
+ producer: 808
+}
diff --git a/src/frontends/tensorflow/tests/test_models/models_pbtxt/dynpart_overflow_partitions.pbtxt b/src/frontends/tensorflow/tests/test_models/models_pbtxt/dynpart_overflow_partitions.pbtxt
new file mode 100644
index 00000000000..0d7e7bf36d7
--- /dev/null
+++ b/src/frontends/tensorflow/tests/test_models/models_pbtxt/dynpart_overflow_partitions.pbtxt
@@ -0,0 +1,61 @@
+node {
+ name: "data"
+ op: "Placeholder"
+ attr {
+ key: "dtype"
+ value {
+ type: DT_FLOAT
+ }
+ }
+ attr {
+ key: "shape"
+ value {
+ shape {
+ dim {
+ size: 10
+ }
+ }
+ }
+ }
+}
+node {
+ name: "partitions"
+ op: "Placeholder"
+ attr {
+ key: "dtype"
+ value {
+ type: DT_INT32
+ }
+ }
+ attr {
+ key: "shape"
+ value {
+ shape {
+ dim {
+ size: 10
+ }
+ }
+ }
+ }
+}
+node {
+ name: "dynamic_partition"
+ op: "DynamicPartition"
+ input: "data"
+ input: "partitions"
+ attr {
+ key: "T"
+ value {
+ type: DT_FLOAT
+ }
+ }
+ attr {
+ key: "num_partitions"
+ value {
+ i: 4294967297
+ }
+ }
+}
+versions {
+ producer: 808
+}
diff --git a/src/frontends/tensorflow/tests/test_models/models_pbtxt/dynpart_zero_partitions.pbtxt b/src/frontends/tensorflow/tests/test_models/models_pbtxt/dynpart_zero_partitions.pbtxt
new file mode 100644
index 00000000000..64968df9f64
--- /dev/null
+++ b/src/frontends/tensorflow/tests/test_models/models_pbtxt/dynpart_zero_partitions.pbtxt
@@ -0,0 +1,61 @@
+node {
+ name: "data"
+ op: "Placeholder"
+ attr {
+ key: "dtype"
+ value {
+ type: DT_FLOAT
+ }
+ }
+ attr {
+ key: "shape"
+ value {
+ shape {
+ dim {
+ size: 10
+ }
+ }
+ }
+ }
+}
+node {
+ name: "partitions"
+ op: "Placeholder"
+ attr {
+ key: "dtype"
+ value {
+ type: DT_INT32
+ }
+ }
+ attr {
+ key: "shape"
+ value {
+ shape {
+ dim {
+ size: 10
+ }
+ }
+ }
+ }
+}
+node {
+ name: "dynamic_partition"
+ op: "DynamicPartition"
+ input: "data"
+ input: "partitions"
+ attr {
+ key: "T"
+ value {
+ type: DT_FLOAT
+ }
+ }
+ attr {
+ key: "num_partitions"
+ value {
+ i: 0
+ }
+ }
+}
+versions {
+ producer: 808
+}
diff --git a/src/frontends/tensorflow_common/src/op/dynamic_partition.cpp b/src/frontends/tensorflow_common/src/op/dynamic_partition.cpp
index 0b8a42ef8de..5ae4a67732c 100644
--- a/src/frontends/tensorflow_common/src/op/dynamic_partition.cpp
+++ b/src/frontends/tensorflow_common/src/op/dynamic_partition.cpp
@@ -40,6 +40,14 @@ OutputVector translate_dynamic_partition_op(const NodeContext& node) {
// retrieve num_partitions attribute
auto num_partitions = node.get_attribute<int64_t>("num_partitions");
+ TENSORFLOW_OP_VALIDATION(
+ node,
+ num_partitions > 0,
+ "DynamicPartition expects positive num_partitions attribute, got: " + std::to_string(num_partitions));
+ TENSORFLOW_OP_VALIDATION(
+ node,
+ num_partitions <= static_cast<int64_t>(std::numeric_limits<int32_t>::max()),
+ "DynamicPartition num_partitions attribute exceeds int32 range, got: " + std::to_string(num_partitions));
// compute how many slices are collected for each partition
// 1. initially assume that we collect zero slices for each partition