Skip to content

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

[Range] Symbol propagation for range from 0 to A (#27505)

Evgenya Nugmanovab5a4ad16be8b
diff --git a/src/core/shape_inference/include/range_shape_inference.hpp b/src/core/shape_inference/include/range_shape_inference.hpp
index 3be56a4543a..5d754810d9b 100644
--- a/src/core/shape_inference/include/range_shape_inference.hpp
+++ b/src/core/shape_inference/include/range_shape_inference.hpp
@@ -11,6 +11,31 @@ namespace op {
namespace ShapeInferRange {
+template <class TRShape, typename std::enable_if<std::is_same<TRShape, PartialShape>::value>::type* = nullptr>
+void symbol_propagation(const Node* op,
+ std::vector<TRShape>& output_shapes,
+ const double& start,
+ const double& step,
+ bool start_val,
+ bool step_val) {
+ output_shapes[0] = ov::PartialShape::dynamic(1);
+ if (op->get_input_size() == 3 && step_val && step == 1) {
+ auto start_symbol = op->input_value(0).get_tensor().get_value_symbol();
+ auto stop_symbol = op->input_value(1).get_tensor().get_value_symbol();
+ if (start_val && start == 0 && !stop_symbol.empty()) {
+ output_shapes[0][0].set_symbol(stop_symbol[0]);
+ }
+ }
+}
+
+template <class TRShape, typename std::enable_if<!std::is_same<TRShape, PartialShape>::value>::type* = nullptr>
+void symbol_propagation(const Node* op,
+ std::vector<TRShape>& output_shapes,
+ const double& start,
+ const double& step,
+ bool start_val,
+ bool step_val) {}
+
template <class T, class TRShape = result_shape_t<T>>
std::vector<TRShape> range_shape_infer(const Node* op,
const std::vector<T>& input_shapes,
@@ -35,12 +60,18 @@ std::vector<TRShape> range_shape_infer(const Node* op,
NODE_VALIDATION_CHECK(op, start_val->size() == 1);
start = (*start_val)[0];
NODE_VALIDATION_CHECK(op, std::isfinite(start) && !std::isnan(start), "'start' cannot be nan or infinite.");
+ if (output_is_integral)
+ // all inputs must be casted to output_type before the rounding for casting values are done towards zero
+ start = std::trunc(start);
}
if (stop_val) {
NODE_VALIDATION_CHECK(op, stop_val->size() == 1);
stop = (*stop_val)[0];
NODE_VALIDATION_CHECK(op, std::isfinite(stop) && !std::isnan(stop), "'stop' cannot be nan or infinite.");
+ if (output_is_integral)
+ // all inputs must be casted to output_type before the rounding for casting values are done towards zero
+ stop = std::trunc(stop);
}
if (step_val) {
@@ -52,18 +83,13 @@ std::vector<TRShape> range_shape_infer(const Node* op,
NODE_VALIDATION_CHECK(op,
std::isfinite(step) && !std::isnan(step) && step != 0,
"'step' cannot be zero, nan, or infinite.");
+ if (output_is_integral)
+ // all inputs must be casted to output_type before the rounding for casting values are done towards zero
+ step = std::trunc(step);
}
auto output_shapes = std::vector<TRShape>(1);
if (start_val && stop_val && step_val) {
- // all inputs must be casted to output_type before
- // the rounding for casting values are done towards zero
- if (output_is_integral) {
- start = std::trunc(start);
- stop = std::trunc(stop);
- step = std::trunc(step);
- }
-
// the number of elements is: max(ceil((stop − start) / step), 0)
double span;
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
@@ -76,7 +102,7 @@ std::vector<TRShape> range_shape_infer(const Node* op,
output_shapes[0] = TRShape{static_cast<uint32_t>(strided)};
} else {
- output_shapes[0] = ov::PartialShape::dynamic(1);
+ symbol_propagation(op, output_shapes, start, step, start_val, step_val);
}
return output_shapes;
}
diff --git a/src/core/tests/type_prop/range.cpp b/src/core/tests/type_prop/range.cpp
index c37f5987047..d44fe3c3bcc 100644
--- a/src/core/tests/type_prop/range.cpp
+++ b/src/core/tests/type_prop/range.cpp
@@ -895,3 +895,21 @@ INSTANTIATE_TEST_SUITE_P(type_prop,
RangeParams{-1, 1, 0.25, PartialShape{8}},
RangeParams{-1, 0.875, 0.25, PartialShape{8}}),
PrintToDummyParamName());
+
+TEST(type_prop, range_symbol_start_0_stop_A_step_1) {
+ auto stop_symbol = std::make_shared<ov::Symbol>();
+ auto source_shape = PartialShape::dynamic(1);
+ source_shape[0].set_symbol(stop_symbol);
+ auto symbol_source =
+ make_shared<ov::op::v0::ShapeOf>(make_shared<ov::op::v0::Parameter>(element::i64, source_shape));
+
+ auto start = make_shared<ov::op::v0::Constant>(element::i64, Shape{}, 0);
+ auto stop = make_shared<ov::op::v8::Gather>(symbol_source,
+ make_shared<ov::op::v0::Constant>(element::i64, Shape{}, 0),
+ make_shared<ov::op::v0::Constant>(element::i64, Shape{}, 0));
+ auto step = make_shared<ov::op::v0::Constant>(element::i64, Shape{}, 1);
+
+ auto range = make_shared<op::v0::Range>(start, stop, step);
+
+ ASSERT_TRUE(ov::symbol::are_equal(range->get_output_partial_shape(0)[0].get_symbol(), stop_symbol));
+}