Skip to content

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

[ARM] Fix bug in interpolate node in notebook image-to-image (#28957)

Nesterov Alexander9e8654e4ecf0
diff --git a/src/plugins/intel_cpu/src/nodes/executors/interpolate.hpp b/src/plugins/intel_cpu/src/nodes/executors/interpolate.hpp
index 11f17c1c212..18b4226194f 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/interpolate.hpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/interpolate.hpp
@@ -36,6 +36,14 @@ struct InterpolateAttrs {
InterpolateLayoutType layout;
std::vector<float> dataScales;
bool hasPad = false;
+ // Some FEs or preprocessing step resize spatial dimension for tensors with NHWC layout memory,
+ // but import them with a planar layout[abcd] with axis[1,2] for convenience. In this case, for pillow modes without
+ // pad, the nhwc layout path and the specific kernel(nhwc layout executor) can be used for this planar layout and
+ // axis settings(NCHWAsNHWC is true) to get better perf. To this end the following mapping is used:
+ // 1. logical shape alignment [abcd-nhwc] to [adbc-nchw].
+ // 2. axis alignment [1,2] to [2,3].
+ // 3. config planar layout support and treated it as channel_first layout.
+ bool NCHWAsNHWC = false;
};
inline VectorDims getPaddedInputShape(const VectorDims& srcDims,
diff --git a/src/plugins/intel_cpu/src/nodes/interpolate.cpp b/src/plugins/intel_cpu/src/nodes/interpolate.cpp
index 071ca306837..c7040f79580 100644
--- a/src/plugins/intel_cpu/src/nodes/interpolate.cpp
+++ b/src/plugins/intel_cpu/src/nodes/interpolate.cpp
@@ -2069,8 +2069,8 @@ Interpolate::Interpolate(const std::shared_ptr<ov::Node>& op, const GraphContext
if (isAxesSpecified) {
axes = ov::as_type_ptr<const ov::op::v0::Constant>(interp->get_input_node_shared_ptr(AXES_ID_V11))
->cast_vector<int>();
- if (dataRank == 4 && axes.size() == 2 && axes[0] == 1 && axes[1] == 2 && mayiuse(cpu::x64::sse41)) {
- NCHWAsNHWC = true;
+ if (dataRank == 4 && axes.size() == 2 && axes[0] == 1 && axes[1] == 2) {
+ interpAttrs.NCHWAsNHWC = true;
axes[0] = 2;
axes[1] = 3;
}
@@ -2111,7 +2111,7 @@ void Interpolate::getSupportedDescriptors() {
}
// correct pad
if (hasPad) {
- NCHWAsNHWC = false;
+ interpAttrs.NCHWAsNHWC = false;
auto correctPad = [&](std::vector<int> pad, int rank) {
int padLen = pad.size();
if (padLen == rank) {
@@ -2264,19 +2264,19 @@ void Interpolate::initSupportedPrimitiveDescriptors() {
if (dataRank == 4) {
if (mayiuse(cpu::x64::avx512_core)) {
- if (NCHWAsNHWC) {
+ if (interpAttrs.NCHWAsNHWC) {
pushDesc(LayoutType::ncsp, jit_avx512, true);
} else {
pushDesc(LayoutType::nspc, jit_avx512, true);
}
} else if (mayiuse(cpu::x64::avx2)) {
- if (NCHWAsNHWC) {
+ if (interpAttrs.NCHWAsNHWC) {
pushDesc(LayoutType::ncsp, jit_avx2, true);
} else {
pushDesc(LayoutType::nspc, jit_avx2, true);
}
} else if (mayiuse(cpu::x64::sse41)) {
- if (NCHWAsNHWC) {
+ if (interpAttrs.NCHWAsNHWC) {
pushDesc(LayoutType::ncsp, jit_sse42, true);
} else {
pushDesc(LayoutType::nspc, jit_sse42, true);
@@ -2441,7 +2441,7 @@ void Interpolate::prepareParams() {
VectorDims dstDims = dstDimsOrign;
// layoutAlignment
- if (NCHWAsNHWC && srcMemPtr->getDesc().hasLayoutType(LayoutType::ncsp)) {
+ if (interpAttrs.NCHWAsNHWC && srcMemPtr->getDesc().hasLayoutType(LayoutType::ncsp)) {
auto logicalShapeAlign = [](VectorDims& Dims) {
size_t C = Dims[3];
Dims[3] = Dims[2];
@@ -2463,7 +2463,8 @@ void Interpolate::prepareParams() {
std::vector<float> dataScales =
getScales(getPaddedInputShape(srcDims, interpAttrs.padBegin, interpAttrs.padEnd), dstDims);
- if (!NCHWAsNHWC && (getOutputShapeAtPort(0).getRank() > 2 && (dataScales[0] != 1.f || dataScales[1] != 1.f))) {
+ if (!interpAttrs.NCHWAsNHWC &&
+ (getOutputShapeAtPort(0).getRank() > 2 && (dataScales[0] != 1.f || dataScales[1] != 1.f))) {
THROW_CPU_NODE_ERR("only supports resize on spatial dimensions(depth, height and width)");
}
@@ -2503,7 +2504,7 @@ void Interpolate::prepareParams() {
bool isNearestLinearOrCubicSupported = isNearestLinearOrCubic && (isPlanarLayourAndSse41 || isAvx2AndF32);
bool isPillowModeSupported = isPillowMode && isByChannelLayout;
- if (isNearestLinearOrCubicSupported || isPillowModeSupported) {
+ if ((isNearestLinearOrCubicSupported || isPillowModeSupported) && mayiuse(cpu::x64::sse41)) {
executor = std::make_shared<InterpolateJitExecutor>(key.nodeAttrs,
key.srcDims,
key.dstDims,
@@ -4080,6 +4081,110 @@ void Interpolate::InterpolateRefExecutor::pillowRef(const uint8_t* in_ptr_,
});
}
+void Interpolate::InterpolateRefExecutor::pillowRefNCHWAsNHWC(const uint8_t* in_ptr_,
+ uint8_t* out_ptr_,
+ int B,
+ int C,
+ int IH,
+ int IW,
+ int OH,
+ int OW) {
+ size_t offset = 0;
+ int filterLenX = auxTable[offset];
+ int filterLenY = auxTable[offset + 1];
+ offset += 2;
+ auto* weightX = reinterpret_cast<float*>(&auxTable[offset]);
+ offset += filterLenX * OW;
+ auto* weightY = reinterpret_cast<float*>(&auxTable[offset]);
+ offset += filterLenY * OH;
+ auto* indexX = static_cast<int*>(&auxTable[offset]);
+ offset += 2 * OW;
+ auto* indexY = static_cast<int*>(&auxTable[offset]);
+
+ bool xPass = IW != OW;
+ bool yPass = IH != OH;
+
+ auto b_loop = [&](size_t b) {
+ const uint8_t* in_ptr_b = in_ptr_ + b * IH * IW * C * srcDataSize;
+ uint8_t* out_ptr_b = out_ptr_ + b * OH * OW * C * dstDataSize;
+
+ uint8_t* xpass_out_ptr_b = nullptr;
+ const uint8_t* ypass_in_ptr_b = nullptr;
+
+ if (xPass && yPass) {
+ size_t parallel_num = B;
+ size_t buffer_size = static_cast<size_t>(IH) * OW * C;
+ if (parallel_num < m_threads_num) {
+ xpass_out_ptr_b = static_cast<uint8_t*>(&pillow_working_buf[b * buffer_size * srcDataSize]);
+ } else {
+ size_t threadsIdx = parallel_get_thread_num();
+ xpass_out_ptr_b = static_cast<uint8_t*>(&pillow_working_buf[threadsIdx * buffer_size * srcDataSize]);
+ }
+ ypass_in_ptr_b = static_cast<const uint8_t*>(xpass_out_ptr_b);
+ } else if (xPass && !yPass) {
+ xpass_out_ptr_b = out_ptr_b;
+ } else if (!xPass && yPass) {
+ ypass_in_ptr_b = in_ptr_b;
+ } else if (!xPass && !yPass) {
+ cpu_memcpy(out_ptr_b, in_ptr_b, OH * OW * C * dstDataSize);
+ }
+
+ float result;
+ int f, filterS, filterL;
+ float* weight;
+
+ if (xPass) {
+ for (size_t ih = 0; ih < static_cast<size_t>(IH); ih++) {
+ for (size_t ow = 0; ow < static_cast<size_t>(OW); ow++) {
+ filterS = indexX[ow * 2];
+ filterL = indexX[ow * 2 + 1];
+ weight = reinterpret_cast<float*>(&weightX[ow * filterLenX]);
+ for (size_t c = 0; c < static_cast<size_t>(C); c++) {
+ result = 0.f;
+ for (f = 0; f < filterL; f++) {
+ float pixel =
+ getValue(in_ptr_b, ((ih * IW + (f + filterS)) * C + c) * srcDataSize, inputPrec);
+ result += pixel * weight[f];
+ }
+ if (!isFloatCompatible(outputPrec)) {
+ result =
+ static_cast<float>(static_cast<int>(result >= 0.0 ? result + 0.5f : result - 0.5f));
+ }
+ setValue(xpass_out_ptr_b, ((ih * OW + ow) * C + c) * dstDataSize, result, outputPrec);
+ }
+ }
+ }
+ }
+
+ if (yPass) {
+ for (size_t oh = 0; oh < static_cast<size_t>(OH); oh++) {
+ filterS = indexY[oh * 2];
+ filterL = indexY[oh * 2 + 1];
+ weight = reinterpret_cast<float*>(&weightY[oh * filterLenY]);
+ for (size_t ow = 0; ow < static_cast<size_t>(OW); ow++) {
+ for (size_t c = 0; c < static_cast<size_t>(C); c++) {
+ result = 0.f;
+ for (f = 0; f < filterL; f++) {
+ float pixel =
+ getValue(ypass_in_ptr_b, (((f + filterS) * OW + ow) * C + c) * srcDataSize, inputPrec);
+ result += pixel * weight[f];
+ }
+ if (!isFloatCompatible(outputPrec)) {
+ result =
+ static_cast<float>(static_cast<int>(result >= 0.0 ? result + 0.5f : result - 0.5f));
+ }
+ setValue(out_ptr_b, ((oh * OW + ow) * C + c) * dstDataSize, result, outputPrec);
+ }
+ }
+ }
+ }
+ };
+
+ parallel_nt_static(m_threads_num, [&](const int ithr, const int nthr) {
+ for_1d(ithr, nthr, B, b_loop);
+ });
+}
+
void Interpolate::InterpolateExecutorBase::create_pillow_working_buf(InterpolateLayoutType layout) {
if (srcDimPad5d[3] == dstDim5d[3] || srcDimPad5d[4] == dstDim5d[4]) {
return;
@@ -4290,7 +4395,11 @@ void Interpolate::InterpolateRefExecutor::exec(const uint8_t* in_ptr_,
}
case InterpolateMode::bilinear_pillow:
case InterpolateMode::bicubic_pillow: {
- pillowRef(in_ptr_, out_ptr_, N, C, IH, IW, OH, OW);
+ if (refInterpAttrs.NCHWAsNHWC) {
+ pillowRefNCHWAsNHWC(in_ptr_, out_ptr_, N, C, IH, IW, OH, OW);
+ } else {
+ pillowRef(in_ptr_, out_ptr_, N, C, IH, IW, OH, OW);
+ }
break;
}
default: {
diff --git a/src/plugins/intel_cpu/src/nodes/interpolate.h b/src/plugins/intel_cpu/src/nodes/interpolate.h
index 9142a2272ae..bfc3d07d9c6 100644
--- a/src/plugins/intel_cpu/src/nodes/interpolate.h
+++ b/src/plugins/intel_cpu/src/nodes/interpolate.h
@@ -99,14 +99,6 @@ public:
private:
bool is_version11 = true;
InterpolateAttrs interpAttrs;
- // Some FEs or preprocessing step resize spatial dimension for tensor with NHWC layout memory,
- // but imported as planar layout[abcd] with axis[1,2] for convenience. In this case, for pillow modes without pad
- // for now, nhwc layout path and the kernel(nhwc layout executor) can be used for this planar layout and axis
- // settings(NCHWAsNHWC is true) to get higher perf with
- // 1. logical shape alignment [abcd-nhwc] to [adbc-nchw].
- // 2. axis alignment [1,2] to [2,3].
- // 3. config planar layout support and treated it as channel_first layout.
- bool NCHWAsNHWC = false;
size_t dataRank = 0;
class InterpolateExecutorBase {
@@ -280,7 +272,8 @@ private:
const std::vector<float>& _dataScales)
: InterpolateExecutorBase(interpAttrs, srcDims, dstDims, _dataScales),
antialias(interpAttrs.antialias),
- dataScales(_dataScales) {}
+ dataScales(_dataScales),
+ refInterpAttrs(interpAttrs) {}
void exec(const uint8_t* in_ptr_, uint8_t* out_ptr_, const void* post_ops_data_) override;
@@ -315,6 +308,8 @@ private:
int kernel_width,
bool antialias);
void pillowRef(const uint8_t* in_ptr_, uint8_t* out_ptr_, int B, int C, int IH, int IW, int OH, int OW);
+ void
+ pillowRefNCHWAsNHWC(const uint8_t* in_ptr_, uint8_t* out_ptr_, int B, int C, int IH, int IW, int OH, int OW);
static float getValue(const uint8_t* base, size_t offset, ov::element::Type prec);
static void setValue(uint8_t* base, size_t offset, float value, ov::element::Type prec);
@@ -322,6 +317,7 @@ private:
private:
bool antialias;
std::vector<float> dataScales;
+ InterpolateAttrs refInterpAttrs;
};
void setPostOps(dnnl::primitive_attr& attr, const VectorDims& dims);
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/interpolate.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/interpolate.cpp
new file mode 100644
index 00000000000..0e3af63bd4d
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/interpolate.cpp
@@ -0,0 +1,105 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "custom/single_layer_tests/classes/interpolate.hpp"
+#include "common_test_utils/ov_tensor_utils.hpp"
+#include "openvino/core/preprocess/pre_post_process.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
+#include "utils/cpu_test_utils.hpp"
+#include "utils/fusing_test_utils.hpp"
+
+using namespace CPUTestUtils;
+
+namespace ov::test::Interpolate {
+namespace {
+
+// pillow modes: planar layout with axis[1,2] executed as nhwc layout case
+const std::vector<int64_t> defaultAxes4D_pillow_nchw_as_nhwc = {
+ {1, 2}
+};
+
+const std::vector<ShapeParams> shapeParams4D_Pillow_Smoke_nchw_as_nhwc = {
+ ShapeParams{
+ ov::op::v11::Interpolate::ShapeCalcMode::SCALES,
+ InputShape{{}, {{1, 4, 4, 3}}},
+ ov::test::utils::InputLayerType::CONSTANT,
+ {{2.0f, 4.0f}},
+ defaultAxes4D_pillow_nchw_as_nhwc
+ },
+ ShapeParams{
+ ov::op::v11::Interpolate::ShapeCalcMode::SIZES,
+ InputShape{{}, {{2, 16, 16, 4}}},
+ ov::test::utils::InputLayerType::CONSTANT,
+ {{2, 8}},
+ defaultAxes4D_pillow_nchw_as_nhwc
+ },
+ ShapeParams{
+ ov::op::v11::Interpolate::ShapeCalcMode::SCALES,
+ InputShape{{-1, -1, -1, {2, 20}}, {{1, 4, 4, 11}, {2, 6, 5, 7}, {1, 4, 4, 11}}},
+ ov::test::utils::InputLayerType::CONSTANT,
+ {{1.25f, 0.75f}},
+ defaultAxes4D_pillow_nchw_as_nhwc
+ },
+ ShapeParams{
+ ov::op::v11::Interpolate::ShapeCalcMode::SIZES,
+ InputShape{{-1, -1, -1, {2, 20}}, {{1, 4, 4, 17}, {2, 10, 12, 3}, {1, 4, 4, 17}}},
+ ov::test::utils::InputLayerType::CONSTANT,
+ {{6, 8}},
+ defaultAxes4D_pillow_nchw_as_nhwc
+ }
+};
+
+const std::vector<size_t> pads4D_nchw_as_nhwc = {
+ {0, 0, 0, 0}
+};
+
+const std::vector<double> cubeCoefsPillow = {
+ -0.5f,
+};
+
+// bilinear pillow and bicubic pillow test case supported in spec(ov ref)
+const std::vector<ov::op::v11::Interpolate::CoordinateTransformMode> coordinateTransformModesPillow_Smoke = {
+ ov::op::v11::Interpolate::CoordinateTransformMode::TF_HALF_PIXEL_FOR_NN,
+};
+
+const auto interpolateCasesBilinearPillow_Smoke_nchw_as_nhwc = ::testing::Combine(
+ ::testing::Values(ov::op::v11::Interpolate::InterpolateMode::BILINEAR_PILLOW),
+ ::testing::ValuesIn(coordinateTransformModesPillow_Smoke),
+ ::testing::ValuesIn(defNearestModes()),
+ ::testing::ValuesIn(antialias()),
+ ::testing::Values(pads4D_nchw_as_nhwc),
+ ::testing::Values(pads4D_nchw_as_nhwc),
+ ::testing::ValuesIn(cubeCoefsPillow));
+
+INSTANTIATE_TEST_SUITE_P(smoke_InterpolateBilinearPillow_LayoutAlign_Test, InterpolateLayerCPUTest,
+ ::testing::Combine(
+ interpolateCasesBilinearPillow_Smoke_nchw_as_nhwc,
+ ::testing::ValuesIn(shapeParams4D_Pillow_Smoke_nchw_as_nhwc),
+ ::testing::Values(ElementType::f32),
+ ::testing::Values(CPUSpecificParams{{nchw, x, x}, {nchw}, {"ref"}, "ref"}),
+ ::testing::Values(emptyFusingSpec),
+ ::testing::Values(ov::AnyMap())),
+ InterpolateLayerCPUTest::getTestCaseName);
+
+const auto interpolateCasesBicubicPillow_Smoke_nchw_as_nhwc = ::testing::Combine(
+ ::testing::Values(ov::op::v11::Interpolate::InterpolateMode::BICUBIC_PILLOW),
+ ::testing::ValuesIn(coordinateTransformModesPillow_Smoke),
+ ::testing::ValuesIn(defNearestModes()),
+ ::testing::ValuesIn(antialias()),
+ ::testing::Values(pads4D_nchw_as_nhwc),
+ ::testing::Values(pads4D_nchw_as_nhwc),
+ ::testing::ValuesIn(cubeCoefsPillow));
+
+INSTANTIATE_TEST_SUITE_P(smoke_InterpolateBicubicPillow_LayoutAlign_Test, InterpolateLayerCPUTest,
+ ::testing::Combine(
+ interpolateCasesBicubicPillow_Smoke_nchw_as_nhwc,
+ ::testing::ValuesIn(shapeParams4D_Pillow_Smoke_nchw_as_nhwc),
+ ::testing::Values(ElementType::f32),
+ ::testing::Values(CPUSpecificParams{{nchw, x, x}, {nchw}, {"ref"}, "ref"}),
+ ::testing::Values(emptyFusingSpec),
+ ::testing::Values(ov::AnyMap())),
+ InterpolateLayerCPUTest::getTestCaseName);
+
+} // namespace
+} // namespace ov::test::Interpolate