Skip to content

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

[CPU] Fix BatchToSpace handling of outputs containing dimension 1 (#26188)

Egor Duplenskiic7a19b341f6f
diff --git a/src/plugins/intel_cpu/src/nodes/batch_to_space.cpp b/src/plugins/intel_cpu/src/nodes/batch_to_space.cpp
index afaf3c7ca63..6979e939521 100644
--- a/src/plugins/intel_cpu/src/nodes/batch_to_space.cpp
+++ b/src/plugins/intel_cpu/src/nodes/batch_to_space.cpp
@@ -183,11 +183,11 @@ void BatchToSpace::batchToSpaceKernel() {
begin[1] = (blockShape[1] - 1 - oAdd[1]) / blockShape[1] / blockSize;
finish[1] = (outShape5D[1] - 1 - oAdd[1]) / blockShape[1] / blockSize;
begin[2] = (blockShape[2] - 1 - oAdd[2]) / blockShape[2];
- finish[2] = (outShape5D[2] - 1 - oAdd[2]) / blockShape[2];
+ finish[2] = (outShape5D[2] + blockShape[2] - 1 - oAdd[2]) / blockShape[2];
begin[3] = (blockShape[3] - 1 - oAdd[3]) / blockShape[3];
- finish[3] = (outShape5D[3] - 1 - oAdd[3]) / blockShape[3];
+ finish[3] = (outShape5D[3] + blockShape[3] - 1 - oAdd[3]) / blockShape[3];
begin[4] = (blockShape[4] - 1 - oAdd[4]) / blockShape[4];
- finish[4] = (outShape5D[4] - 1 - oAdd[4]) / blockShape[4];
+ finish[4] = (outShape5D[4] + blockShape[4] - 1 - oAdd[4]) / blockShape[4];
const int64_t addTmpOC = blocked ? 0lu : oAdd[1];
const int64_t addTmpOc = blocked ? oAdd[1] : 0lu;
indxStart[1] = begin[1] > indxStart[1] ? begin[1] : indxStart[1];
@@ -198,15 +198,15 @@ void BatchToSpace::batchToSpaceKernel() {
const size_t srcIdx1 = srcIdx0 + indxStart[1] * inSpatialStep * blockSize;
const size_t dstIdx1 = dstIdx0 + tmpOC * outSpatialStep * blockSize;
const size_t itEnd = blocked ? ((block - 1) * blockShape[1] + oAdd[1]) / blockSize : 0lu;
- for (size_t i2 = begin[2]; i2 < finish[2] + 1; ++i2) {
+ for (size_t i2 = begin[2]; i2 < finish[2]; ++i2) {
const int64_t tmpOd = i2 * blockShape[2] + oAdd[2];
const size_t srcIdx2 = srcIdx1 + i2 * inShape5D[3] * inShape5D[4] * blockSize;
const size_t dstIdx2 = dstIdx1 + tmpOd * outShape5D[3] * outShape5D[4] * blockSize;
- for (size_t i3 = begin[3]; i3 < finish[3] + 1; ++i3) {
+ for (size_t i3 = begin[3]; i3 < finish[3]; ++i3) {
const int64_t tmpOh = i3 * blockShape[3] + oAdd[3];
const size_t srcIdx3 = srcIdx2 + i3 * inShape5D[4] * blockSize;
const size_t dstIdx3 = dstIdx2 + tmpOh * outShape5D[4] * blockSize;
- for (size_t i4 = begin[4]; i4 < finish[4] + 1; ++i4) {
+ for (size_t i4 = begin[4]; i4 < finish[4]; ++i4) {
const int64_t tmpOw = i4 * blockShape[4] + oAdd[4];
const size_t srcIdx4 = srcIdx3 + i4 * blockSize;
const size_t dstIdx4 = dstIdx3 + tmpOw * blockSize;
diff --git a/src/plugins/intel_cpu/src/nodes/batch_to_space.h b/src/plugins/intel_cpu/src/nodes/batch_to_space.h
index 724ed9536ba..1b583f74bd7 100644
--- a/src/plugins/intel_cpu/src/nodes/batch_to_space.h
+++ b/src/plugins/intel_cpu/src/nodes/batch_to_space.h
@@ -16,6 +16,12 @@ public:
void getSupportedDescriptors() override {};
void initSupportedPrimitiveDescriptors() override;
+
+ // output shape can potentially be empty
+ bool isExecutable() const override {
+ return !hasEmptyInputTensors() && !hasEmptyOutputTensors();
+ }
+
void execute(dnnl::stream strm) override;
bool created() const override;
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/batch_to_space.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/batch_to_space.cpp
index 4af67e82587..4d1a4660b1b 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/batch_to_space.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/batch_to_space.cpp
@@ -77,7 +77,11 @@ public:
tensor = ov::Tensor(param_type, static_shape, blockShape.data());
break;
}
- case 2:
+ case 2: {
+ ASSERT_EQ(ov::shape_size(static_shape), cropsBegin.size());
+ tensor = ov::Tensor(param_type, static_shape, cropsBegin.data());
+ break;
+ }
case 3: {
ASSERT_EQ(ov::shape_size(static_shape), cropsEnd.size());
tensor = ov::Tensor(param_type, static_shape, cropsEnd.data());
@@ -402,6 +406,52 @@ INSTANTIATE_TEST_SUITE_P(smoke_DynamicBatchToSpaceCPULayerTestCaseWithBlocked2_5
dynamicBatchToSpaceParamsWithBlockedSet5D2,
BatchToSpaceCPULayerTest::getTestCaseName);
+std::vector<InputShape> dynamicInputShapesZeroDimOutput = {
+ {{-1, -1, -1, -1, -1}, {{2, 16, 1, 5, 5}}},
+ {{5}, {{5}, {5}, {5}}},
+ {{5}, {{5}, {5}, {5}}},
+ {{5}, {{5}, {5}, {5}}}
+};
+
+const std::vector<int64_t> blockShapeZeroDimOutput = {1, 1, 2, 1, 1};
+const std::vector<int64_t> cropsBeginZeroDimOutput = {0, 0, 0, 0, 0};
+const std::vector<int64_t> cropsEndZeroDimOutput = {0, 0, 2, 0, 0};
+
+const auto dynamicBatchToSpaceParamsSetZeroDimOutput = ::testing::Combine(::testing::Values(dynamicInputShapesZeroDimOutput),
+ ::testing::Values(blockShapeZeroDimOutput),
+ ::testing::Values(cropsBeginZeroDimOutput),
+ ::testing::Values(cropsEndZeroDimOutput),
+ ::testing::Values(ov::element::Type_t::f32),
+ ::testing::Values(CPUSpecificParams({ncdhw}, {ncdhw}, {}, {})));
+
+INSTANTIATE_TEST_SUITE_P(smoke_DynamicBatchToSpaceCPULayerTestCaseZeroDimOutput,
+ BatchToSpaceCPULayerTest,
+ dynamicBatchToSpaceParamsSetZeroDimOutput,
+ BatchToSpaceCPULayerTest::getTestCaseName);
+
+std::vector<InputShape> dynamicInputShapesOutputDimOne = {
+ {{-1, -1, -1, -1, -1}, {{2, 16, 1, 5, 5}}},
+ {{5}, {{5}, {5}, {5}}},
+ {{5}, {{5}, {5}, {5}}},
+ {{5}, {{5}, {5}, {5}}}
+};
+
+const std::vector<int64_t> blockShapeOutputDimOne = {1, 1, 2, 1, 1};
+const std::vector<int64_t> cropsBeginOutputDimOne = {0, 0, 0, 0, 0};
+const std::vector<int64_t> cropsEndOutputDimOne = {0, 0, 1, 0, 0};
+
+const auto dynamicBatchToSpaceParamsSetOutputDimOne = ::testing::Combine(::testing::Values(dynamicInputShapesOutputDimOne),
+ ::testing::Values(blockShapeOutputDimOne),
+ ::testing::Values(cropsBeginOutputDimOne),
+ ::testing::Values(cropsEndOutputDimOne),
+ ::testing::Values(ov::element::Type_t::f32),
+ ::testing::Values(CPUSpecificParams({ncdhw}, {ncdhw}, {}, {})));
+
+INSTANTIATE_TEST_SUITE_P(smoke_DynamicBatchToSpaceCPULayerTestCaseOutputDimOne,
+ BatchToSpaceCPULayerTest,
+ dynamicBatchToSpaceParamsSetOutputDimOne,
+ BatchToSpaceCPULayerTest::getTestCaseName);
+
} // namespace
} // namespace test
} // namespace ov