Skip to content

[OP][CPU] Fix SliceScatter issues with non-constant slice params #27482

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

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion src/core/shape_inference/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ struct TensorTransform : element::NotSupported<void> {
*/
template <class T, class TResult = std::vector<T>, class UnaryOperation>
TResult get_raw_data_as(const element::Type_t et, const void* const ptr, const size_t size, UnaryOperation&& func) {
OPENVINO_ASSERT(!!ptr, "ptr is Null");
// Based on OV ViewTensor, size 0 tensor is allowed to have nullptr memory ptr.
OPENVINO_ASSERT(size == 0 || ptr != nullptr, "ptr is Null");
TResult out;
auto out_it = std::inserter(out, out.end());

Expand Down
24 changes: 15 additions & 9 deletions src/plugins/intel_cpu/src/nodes/strided_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ StridedSlice::StridedSlice(const std::shared_ptr<ov::Node>& op, const GraphConte
if (inputShapes.size() > attrs.AXES_ID) {
isAxesSpecified = true;
}

hasConstAttrInputs = true;
for (size_t i = 0lu; i < op->get_input_size(); i++) {
isConstantInput[i] = ov::is_type<ov::op::v0::Constant>(op->get_input_node_shared_ptr(i));
if (!isConstantInput[i] && one_of(i, attrs.BEGIN_ID, attrs.END_ID, attrs.STRIDE_ID) &&
!attrs.isSliceScatterOp) {
shapeHasDataDependency = true;
if (!isConstantInput[i] && one_of(i, attrs.BEGIN_ID, attrs.END_ID, attrs.STRIDE_ID)) {
hasConstAttrInputs = false;
}
}
hasConstAttrInputs = !shapeHasDataDependency;
shapeHasDataDependency = attrs.isSliceScatterOp ? false : (!hasConstAttrInputs);
if (isAxesSpecified) {
hasConstAttrInputs &= isConstantInput[attrs.AXES_ID];
}
Expand Down Expand Up @@ -330,7 +329,7 @@ bool StridedSlice::isExecutable() const {
}

void StridedSlice::createPrimitive() {
if (inputShapesDefined() && isExecutable() && !shapeHasDataDependency) {
if (inputShapesDefined() && isExecutable() && !shapeHasDataDependency && hasConstAttrInputs) {
if (needPrepareParams()) {
prepareParams();
}
Expand Down Expand Up @@ -362,9 +361,16 @@ bool StridedSlice::needShapeInfer() const {
return Node::inputShapesModified() || shapeHasDataDependency;
}

void StridedSlice::execute(const dnnl::stream& strm) {
void StridedSlice::execute( const dnnl::stream& strm) {
if (!execPtr) {
THROW_CPU_NODE_ERR("doesn't have compiled executor!");
if (!isDynamicNode() && !hasConstAttrInputs) {
// SliceScatter due to not having data dependency on shape may not call prepareParams when start/stop/step
// values are non-constant in Static execution. In Slice and SliceScatter op, prepareParams would be called
// by createPrimitive (if const inputs) or by updateDynamicParams in case of dynamic node.
StridedSlice::prepareParams();
} else {
THROW_CPU_NODE_ERR("doesn't have compiled executor!");
}
}

execPtr->exec(srcMemory, dstMemory);
Expand Down Expand Up @@ -840,7 +846,7 @@ void StridedSlice::StridedSliceCommonExecutor::execSliceScatter(const std::vecto
const uint8_t* srcUpdates = srcMemory[1]->getDataAs<const uint8_t>();
uint8_t* dstData = dstMemory[0]->getDataAs<uint8_t>();
cpu_parallel_memcpy(dstData, srcData, srcMemory[0]->getSize());
if (srcMemory[1]->getSize() == 0) {
if (srcMemory[1]->getShape().hasZeroDims() || srcMemory[1]->getSize() == 0) {
// Updates are empty - do not apply
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ class SliceScatterLayerCPUTest : public testing::WithParamInterface<SliceScatter
in_data);
} else {
// Fill the slice input2~input5 with specified data.
tensor = ov::Tensor{ov::element::i64, targetInputStaticShapes[i], inputValues[i - 2]};
auto inputValue = inputValues[i - 2];
if (!inputValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please elaborate on the reason behind introducing this check? It looks like the current test configuration doesn't provide null pointers. But if it does, an input tensor is created but not initialized with valid values. So what is the expectation of using such an input?

tensor = ov::Tensor{ov::element::i64, targetInputStaticShapes[i]};
} else {
tensor = ov::Tensor{ov::element::i64, targetInputStaticShapes[i], inputValue};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is the reason why the tests don't cover the main issue, as is being resolved in this PR. The thing is that the SL tests in the static shapes configuration perform only one inference, thus the values on the variable inputs don't really change from infer to infer and SL tests don't spot the issue. But if we had had such tests, they would have revealed the design flaw of the current solution solution (please see the comment regarding execute method implementation). So apparently the SL tests should be extended to cover such a use case: the values on the variable inputs actually changes each inference. To this end even a dedicated test class may be introduced, when necessary.

}
}
inputs.insert({funcInput.get_node_shared_ptr(), tensor});
}
Expand Down Expand Up @@ -228,7 +233,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_CompareWithRefs_Plain_Static_2D,
SliceScatterLayerCPUTest,
::testing::Combine(::testing::Values(static_shapes_to_test_representation({{32, 16}})),
::testing::ValuesIn(paramsPlain2D),
::testing::Values(ov::test::utils::InputLayerType::CONSTANT),
::testing::ValuesIn(inputLayerTypes),
::testing::ValuesIn(inputPrecisions),
::testing::Values(emptyCPUSpec)),
SliceScatterLayerCPUTest::getTestCaseName);
Expand Down Expand Up @@ -319,7 +324,7 @@ INSTANTIATE_TEST_SUITE_P(
SliceScatterLayerCPUTest,
::testing::Combine(::testing::ValuesIn(static_shapes_to_test_representation(inputShapesStatic4D)),
::testing::ValuesIn(testCasesCommon4D),
::testing::Values(ov::test::utils::InputLayerType::CONSTANT),
::testing::ValuesIn(inputLayerTypes),
::testing::ValuesIn(inputPrecisions),
::testing::ValuesIn(CPUParamsCommon4D)),
SliceScatterLayerCPUTest::getTestCaseName);
Expand Down Expand Up @@ -407,7 +412,7 @@ INSTANTIATE_TEST_SUITE_P(
SliceScatterLayerCPUTest,
::testing::Combine(::testing::ValuesIn(static_shapes_to_test_representation(inputShapesStatic5D)),
::testing::ValuesIn(testCasesCommon5D),
::testing::Values(ov::test::utils::InputLayerType::CONSTANT),
::testing::ValuesIn(inputLayerTypes),
::testing::ValuesIn(inputPrecisions),
::testing::ValuesIn(CPUParamsCommon5D)),
SliceScatterLayerCPUTest::getTestCaseName);
Expand Down Expand Up @@ -441,7 +446,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_CompareWithRefs_Common_Full_Slice_5D,
SliceScatterLayerCPUTest,
::testing::Combine(::testing::ValuesIn(inputShapesFullSlice5D),
::testing::ValuesIn(testCasesFullSlice5D),
::testing::Values(ov::test::utils::InputLayerType::CONSTANT),
::testing::ValuesIn(inputLayerTypes),
::testing::ValuesIn(inputPrecisions),
::testing::ValuesIn(CPUParamsCommon5D)),
SliceScatterLayerCPUTest::getTestCaseName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ std::vector<SliceScatterParams> generateSliceScatterParamsUnsigned() {
reference_tests::Tensor{{1}, AXIS_ET, std::vector<AXIS_T>{0}},
reference_tests::Tensor{{4}, DATA_ET, std::vector<DATA_T>{1, 10, 3, 20}},
"1D_2_step_replace"),
SliceScatterParams(reference_tests::Tensor{{4}, DATA_ET, std::vector<DATA_T>{1, 2, 3, 4}},
reference_tests::Tensor{{4}, DATA_ET, std::vector<DATA_T>{10, 20, 30, 40}},
reference_tests::Tensor{{0}, IND_ET, std::vector<IND_T>{}},
reference_tests::Tensor{{0}, IND_ET, std::vector<IND_T>{}},
reference_tests::Tensor{{0}, IND_ET, std::vector<IND_T>{}},
reference_tests::Tensor{{4}, DATA_ET, std::vector<DATA_T>{10, 20, 30, 40}},
"1D_full_replace_empty_slice_params"),
SliceScatterParams(
reference_tests::Tensor{{4, 4},
DATA_ET,
Expand Down Expand Up @@ -348,6 +355,23 @@ std::vector<SliceScatterParams> generateSliceScatterParamsUnsigned() {
30, 31, 32, 33, 34, 35, 54, 55, 38, 39,
40, 41, 56, 57, 44, 45, 46, 47}},
"4D_partial_replace_even_axes"),
SliceScatterParams(
reference_tests::Tensor{{4, 2, 3, 2}, DATA_ET, std::vector<DATA_T>{}},
reference_tests::Tensor{{4, 2, 3, 2}, DATA_ET, std::vector<DATA_T>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47}},
reference_tests::Tensor{{0}, IND_ET, std::vector<IND_T>{}},
reference_tests::Tensor{{0}, IND_ET, std::vector<IND_T>{}},
reference_tests::Tensor{{0}, IND_ET, std::vector<IND_T>{}},
reference_tests::Tensor{{0}, IND_ET, std::vector<AXIS_T>{}},
reference_tests::Tensor{{4, 2, 3, 2}, DATA_ET, std::vector<DATA_T>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47}},
"4D_full_replace_empty_slice_params"),
};
return test_params;
}
Expand Down
Loading