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

[CPU][Ref] Fix Reduce ops to produce stable (zero val) output for empty input #27423

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ ov::matcher_pass_callback ConvertReduceBase::convert_reduce_to_pooling() {
return [&](ov::pass::pattern::Matcher& m) {
auto reduce = std::dynamic_pointer_cast<T>(m.get_match_root());

if (!reduce || transformation_callback(reduce)) {
if (!reduce || transformation_callback(reduce) || ov::shape_size(reduce->input_value(0).get_shape()) == 0) {
return false;
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/reference/include/openvino/reference/reduce_l1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ void reduce_l1(InputIt in, OutputIt out, const Shape& in_shape, const AxisSet& r

const auto out_shape = ov::util::reduce(in_shape, reduction_axes);
std::fill(out, std::next(out, shape_size(out_shape)), T(0));
if (shape_size(in_shape) == 0) {
return;
}

const auto in_strides = row_major_strides(in_shape);
const auto out_strides = row_major_strides(out_shape);
Expand Down
3 changes: 3 additions & 0 deletions src/core/reference/include/openvino/reference/reduce_l2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ void reduce_l2(InputIt in, OutputIt out, const Shape& in_shape, const AxisSet& r
const auto out_shape = ov::util::reduce(in_shape, reduction_axes);
const auto out_last = std::next(out, shape_size(out_shape));
std::fill(out, out_last, T(0));
if (shape_size(in_shape) == 0) {
return;
}

const auto in_strides = row_major_strides(in_shape);
const auto out_strides = row_major_strides(out_shape);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ void reduce_max(const T* in, T* out, const Shape& in_shape, const AxisSet& reduc
constexpr auto min_value = std::numeric_limits<T>::lowest();

const auto out_shape = util::reduce(in_shape, reduction_axes);

if (shape_size(in_shape) == 0) {
std::fill_n(out, shape_size(out_shape), T{0});
return;
}

std::fill(out, std::next(out, shape_size(out_shape)), min_value);

const auto in_strides = row_major_strides(in_shape);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ void reduce_mean(const T* in, T* out, const Shape& in_shape, const AxisSet& redu
reduce_sum(in, out, in_shape, reduction_axes);

const auto out_shape = util::reduce(in_shape, reduction_axes);
if (shape_size(in_shape) == 0) {
return;
}

const auto out_size = shape_size(out_shape);
const auto count = static_cast<T>(shape_size(in_shape) / out_size);
std::transform(out, std::next(out, out_size), out, [count](const T value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ void reduce_min(const T* in, T* out, const Shape& in_shape, const AxisSet& reduc
std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : std::numeric_limits<T>::max();

const auto out_shape = util::reduce(in_shape, reduction_axes);

if (shape_size(in_shape) == 0) {
std::fill_n(out, shape_size(out_shape), T{0});
return;
}

std::fill(out, out + shape_size(out_shape), max_value);

const auto in_strides = row_major_strides(in_shape);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ namespace reference {
template <typename T>
void reduce_prod(const T* arg, T* out, const Shape& in_shape, const AxisSet& reduction_axes) {
const auto out_shape = util::reduce(in_shape, reduction_axes);
std::fill(out, out + shape_size(out_shape), T(1));

if (shape_size(in_shape) == 0) {
std::fill(out, out + shape_size(out_shape), T(0));
return;
}

std::fill(out, out + shape_size(out_shape), T(1));
const auto in_strides = row_major_strides(in_shape);
const auto out_strides = row_major_strides(out_shape);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ void reduce_sum(const T* in, T* out, const Shape& in_shape, const AxisSet& reduc
const auto out_size = shape_size(out_shape);
std::vector<T> cs(out_size, T{0});
std::fill(out, std::next(out, out_size), T{0});
if (shape_size(in_shape) == 0) {
return;
}

const auto in_strides = row_major_strides(in_shape);
const auto out_strides = row_major_strides(out_shape);
Expand Down
9 changes: 8 additions & 1 deletion src/plugins/intel_cpu/src/nodes/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2089,7 +2089,7 @@ void Reduce::initSupportedPrimitiveDescriptors() {
}

bool Reduce::isExecutable() const {
return !isInputTensorAtPortEmpty(REDUCE_DATA);
return !isOutputTensorAtPortEmpty(0);
}

void Reduce::prepareParams() {
Expand Down Expand Up @@ -2274,6 +2274,13 @@ void Reduce::execute(dnnl::stream strm) {
const uint8_t *src_data = srcMemPtr->getDataAs<const uint8_t>();
uint8_t *dst_data = dstMemPtr->getDataAs<uint8_t>();

const auto& src_shape = getSrcMemoryAtPort(REDUCE_DATA)->getStaticDims();
if ((shape_size(src_shape) == 0 || srcMemPtr->getSize() == 0) && dstMemPtr->getSize() > 0) {
// If input is empty fill ouptut with zero
std::fill_n(dst_data, dstMemPtr->getSize(), uint8_t{0});
return;
}

if (jit_mode) {
if (is_hybrid_layout) {
dst_data = reinterpret_cast<uint8_t *>(prc_mem.get_data_handle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ std::vector<std::vector<ov::test::InputShape>> inputShapes_SingleBatch = {
{{{}, {{1, 19, 2, 9}}}},
};

std::vector<std::vector<ov::test::InputShape>> inputShapes_Dynmic_ZeroDim = {
{{{-1, -1, -1, -1}, {{2, 0, 3, 9}}}},
{{{2, 0, -1, -1}, {{2, 0, 3, 9}}}},
{{{2, 0, -1, -1}, {{2, 0, 3, 0}}}}
};

std::vector<CPUSpecificParams> cpuParams_4D = {
CPUSpecificParams({nchw}, {nchw}, {}, {}),
CPUSpecificParams({nhwc}, {nhwc}, {}, {}),
Expand Down Expand Up @@ -103,6 +109,20 @@ const auto params_MultiAxis_4D_dynamic = testing::Combine(
testing::Values(emptyFusingSpec),
testing::ValuesIn(additionalConfig()));

const auto params_MultiAxis_4D_dynamic_with_zero = testing::Combine(
testing::Combine(
testing::Values(std::vector<int>{0, 1}),
testing::Values(ov::test::utils::OpType::VECTOR),
testing::ValuesIn(keepDims()),
testing::ValuesIn(reductionTypes()),
testing::ValuesIn(inpOutPrc()),
testing::Values(ElementType::undefined),
testing::Values(ElementType::undefined),
testing::ValuesIn(inputShapes_Dynmic_ZeroDim)),
testing::Values(emptyCPUSpec),
testing::Values(emptyFusingSpec),
testing::ValuesIn(additionalConfig()));

const auto params_Int32 = testing::Combine(
testing::Combine(
testing::ValuesIn(axes()),
Expand Down Expand Up @@ -145,6 +165,13 @@ INSTANTIATE_TEST_SUITE_P(
ReduceCPULayerTest::getTestCaseName
);

INSTANTIATE_TEST_SUITE_P(
smoke_Reduce_MultiAxis_4D_dynamic_with_zero_CPU,
ReduceCPULayerTest,
params_MultiAxis_4D_dynamic_with_zero,
ReduceCPULayerTest::getTestCaseName
);

INSTANTIATE_TEST_SUITE_P(
smoke_Reduce_Int32_CPU,
ReduceCPULayerTest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const std::vector<std::vector<size_t>> input_shapes = {
std::vector<size_t>{3, 5, 7, 9},
};

const std::vector<std::vector<size_t>> input_shapes_0_dim = {
std::vector<size_t>{2, 0, 4, 1},
std::vector<size_t>{8, 0, 4, 0},
std::vector<size_t>{0, 0, 0, 0},
};

const std::vector<std::vector<size_t>> input_shapes_one_axis = {
std::vector<size_t>{10, 20, 30, 40},
std::vector<size_t>{3, 5, 7, 9},
Expand Down Expand Up @@ -167,6 +173,16 @@ const auto params_reduction_types = testing::Combine(
testing::Values(ov::test::utils::DEVICE_CPU)
);

const auto params_empty_input = testing::Combine(
testing::ValuesIn(axes),
testing::Values(op_types[1]),
testing::ValuesIn(keep_dims),
testing::ValuesIn(reduction_types),
testing::Values(model_types[0]),
testing::ValuesIn(input_shapes_0_dim),
testing::Values(ov::test::utils::DEVICE_CPU)
);

const auto params_reduction_types_logical = testing::Combine(
testing::Values(std::vector<int>{0, 1, 3}),
testing::Values(op_types[1]),
Expand Down Expand Up @@ -250,6 +266,13 @@ INSTANTIATE_TEST_SUITE_P(
ReduceOpsLayerTest::getTestCaseName
);

INSTANTIATE_TEST_SUITE_P(
smoke_Reduce_ReductionTypes_EmptyTensor,
ReduceOpsLayerTest,
params_empty_input,
ReduceOpsLayerTest::getTestCaseName
);

INSTANTIATE_TEST_SUITE_P(
smoke_ReduceLogical_ReductionTypes,
ReduceOpsLayerTest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ std::vector<ReductionParams> generateReductionParams(const bool keep_dims) {
reference_tests::Tensor(reduce(Shape{3, 2, 2}, AxisSet{2}, keep_dims),
element::Type(IN_ET),
std::vector<T>{3, 7, 11, 15, 19, 23}))};
if (keep_dims == false) {
params.push_back(ReductionParams(
ReductionType::L1,
keep_dims,
std::vector<int64_t>{1, 2},
reference_tests::Tensor({2, 0, 4}, element::Type(IN_ET), std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8}),
reference_tests::Tensor(Shape{2},
element::Type(IN_ET),
std::vector<T>{0, 0}))); // Keep dims false, tensor filled with zero
}
return params;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ std::vector<ReductionParams> generateReductionParams(const bool keep_dims) {
reference_tests::Tensor(reduce(Shape{3, 2, 2}, AxisSet{2}, keep_dims),
element::Type(IN_ET),
std::vector<T>{2.23606798, 5.0, 7.81024968, 10.63014581, 13.45362405, 16.2788206}))};
if (keep_dims == false) {
params.push_back(ReductionParams(
ReductionType::L2,
keep_dims,
std::vector<int64_t>{1, 2},
reference_tests::Tensor({2, 0, 4}, element::Type(IN_ET), std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8}),
reference_tests::Tensor(Shape{2},
element::Type(IN_ET),
std::vector<T>{0, 0}))); // Keep dims false, tensor filled with zero
}
return params;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ std::vector<ReductionParams> generateReductionParams(const bool keep_dims) {
reference_tests::Tensor(reduce(Shape{3, 3, 3}, AxisSet{0, 1, 2}, keep_dims),
element::Type(IN_ET),
std::vector<T>{27}))};
if (keep_dims == false) {
params.push_back(ReductionParams(
ReductionType::Max,
keep_dims,
std::vector<int64_t>{1, 2},
reference_tests::Tensor({2, 0, 4}, element::Type(IN_ET), std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8}),
reference_tests::Tensor(Shape{2},
element::Type(IN_ET),
std::vector<T>{0, 0}))); // Keep dims false, tensor filled with zero
}
return params;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ std::vector<ReductionParams> generateReductionParams(const bool keep_dims) {
reference_tests::Tensor(reduce(Shape{3, 2}, AxisSet{1}, keep_dims),
element::Type(IN_ET),
std::vector<T>{1.5, 3.5, 5.5}))};
if (keep_dims == false) {
params.push_back(ReductionParams(
ReductionType::Mean,
keep_dims,
std::vector<int64_t>{1, 2},
reference_tests::Tensor({2, 0, 4}, element::Type(IN_ET), std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8}),
reference_tests::Tensor(Shape{2},
element::Type(IN_ET),
std::vector<T>{0, 0}))); // Keep dims false, tensor filled with zero
}
return params;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ std::vector<ReductionParams> generateReductionParams(const bool keep_dims) {
reference_tests::Tensor(reduce(Shape{3, 3, 3}, AxisSet{0, 1, 2}, keep_dims),
element::Type(IN_ET),
std::vector<T>{1}))};
if (keep_dims == false) {
params.push_back(ReductionParams(
ReductionType::Min,
keep_dims,
std::vector<int64_t>{1, 2},
reference_tests::Tensor({2, 0, 4}, element::Type(IN_ET), std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8}),
reference_tests::Tensor(Shape{2},
element::Type(IN_ET),
std::vector<T>{0, 0}))); // Keep dims false, tensor filled with zero
}
return params;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ std::vector<ReductionParams> generateReductionParams(const bool keep_dims) {
19 * 20 * 21,
22 * 23 * 24,
25 * 26 * 27}))};
if (keep_dims == false) {
params.push_back(ReductionParams(
ReductionType::Prod,
keep_dims,
std::vector<int64_t>{1, 2},
reference_tests::Tensor({2, 0, 4}, element::Type(IN_ET), std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8}),
reference_tests::Tensor(Shape{2},
element::Type(IN_ET),
std::vector<T>{0, 0}))); // Keep dims false, tensor filled with zero
}
return params;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ std::vector<ReductionParams> generateReductionParams(const bool keep_dims) {
reference_tests::Tensor(reduce(Shape{3, 3, 3, 3, 3}, AxisSet{0, 1, 2, 3, 4}, keep_dims),
element::Type(IN_ET),
std::vector<T>{243}))};
if (keep_dims == false) {
params.push_back(ReductionParams(
ReductionType::Sum,
keep_dims,
std::vector<int64_t>{1, 2},
reference_tests::Tensor({2, 0, 4}, element::Type(IN_ET), std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8}),
reference_tests::Tensor(Shape{2},
element::Type(IN_ET),
std::vector<T>{0, 0}))); // Keep dims false, tensor filled with zero
}
return params;
}

Expand Down
Loading