Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions src/core/shape_inference/include/range_shape_inference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@ namespace op {

namespace ShapeInferRange {

template <class TRShape, typename std::enable_if<std::is_same<TRShape, PartialShape>::value>::type* = nullptr>
void dynamic_case(const Node* op,
jane-intel marked this conversation as resolved.
Show resolved Hide resolved
jane-intel marked this conversation as resolved.
Show resolved Hide resolved
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();
praasz marked this conversation as resolved.
Show resolved Hide resolved
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 dynamic_case(const Node* op,
jane-intel marked this conversation as resolved.
Show resolved Hide resolved
std::vector<TRShape>& output_shapes,
const double& start,
const double& step,
bool start_val,
bool step_val) {}
jane-intel marked this conversation as resolved.
Show resolved Hide resolved

template <class T, class TRShape = result_shape_t<T>>
std::vector<TRShape> range_shape_infer(const Node* op,
const std::vector<T>& input_shapes,
Expand All @@ -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) {
Expand All @@ -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)) {
Expand All @@ -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);
dynamic_case(op, output_shapes, start, step, start_val, step_val);
jane-intel marked this conversation as resolved.
Show resolved Hide resolved
}
return output_shapes;
}
Expand Down
18 changes: 18 additions & 0 deletions src/core/tests/type_prop/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Loading