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

[Draft][CPU][Ref] Support Reduce ops with empty input #27603

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
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
52 changes: 47 additions & 5 deletions src/plugins/intel_cpu/src/nodes/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2020,6 +2020,7 @@ void Reduce::initSupportedPrimitiveDescriptors() {
config.outConfs[0].setMemDesc(creatorsMap.at(outFormat)->createSharedDesc(outPrecision, getOutputShapeAtPort(0)));

if (useAclExecutor) {
#if defined (OV_CPU_WITH_ACL)
std::vector<MemoryDescPtr> srcMemoryDescs;
for (size_t i = 0; i < config.inConfs.size(); i++) {
srcMemoryDescs.push_back(config.inConfs[i].getMemDesc());
Expand All @@ -2033,7 +2034,16 @@ void Reduce::initSupportedPrimitiveDescriptors() {
std::make_shared<ExecutorContext>(context, getImplPriority()));
if (!factory->isEmpty()) {
supportedPrimitiveDescriptors.push_back({config, impl_type, factory});
} else {
bool apply_ref = customImplPriorities.size() > 0 && customImplPriorities[0] == ref;
// For the case of empty input, transformations ConvertReduceProd(Min, Max, Sum) are disabled to avoid empty output.
// So these 4 reduce modes are not supported for such case, then factory->isEmpty() returns true. Though we don't
// actually need these acl kernels in execution, supportedPrimitiveDescriptors mustn't be empty otherwise we get error.
if (!apply_ref) {
supportedPrimitiveDescriptors.push_back({config, impl_type});
}
}
#endif
} else {
supportedPrimitiveDescriptors.push_back({config, impl_type});
}
Expand Down Expand Up @@ -2089,10 +2099,14 @@ void Reduce::initSupportedPrimitiveDescriptors() {
}

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

void Reduce::prepareParams() {
auto srcMemPtr = getSrcMemoryAtPort(REDUCE_DATA);
const auto& src_shape = srcMemPtr->getStaticDims();
empty_input = shape_size(src_shape) == 0 || srcMemPtr->getSize() == 0;
#if defined (OV_CPU_WITH_ACL)
if (canUseAclExecutor) {
std::vector<MemoryDescPtr> srcMemoryDescs;
for (size_t i = 0; i < getParentEdges().size(); i++) {
Expand All @@ -2102,11 +2116,25 @@ void Reduce::prepareParams() {
dstMemoryDescs.push_back(getDstMemoryAtPort(0)->getDescPtr());

auto selectedPD = getSelectedPrimitiveDescriptor();
aclExecPtr = selectedPD->getExecutorFactoryAs<ReduceExecutorFactory>()->makeExecutor(reduceAttrs, srcMemoryDescs, dstMemoryDescs, {});
selectedPD->setImplementationType(aclExecPtr->getImplType());

if (!empty_input) {
aclExecPtr = selectedPD->getExecutorFactoryAs<ReduceExecutorFactory>()->makeExecutor(reduceAttrs, srcMemoryDescs, dstMemoryDescs, {});
selectedPD->setImplementationType(aclExecPtr->getImplType());
} else {
selectedPD->setImplementationType(acl);
}
return;
} else {
auto selectedPD = getSelectedPrimitiveDescriptor();
if (!empty_input) {
// ref
selectedPD->setImplementationType(ref);
} else {
// unsupported reduce mode (prod, min, max, sum) for empty input
selectedPD->setImplementationType(acl);
return;
}
}
#endif

src_dims = getParentEdgeAt(REDUCE_DATA)->getMemory().getDesc().getShape().getDims();
std::vector<int> reduce_axes;
Expand Down Expand Up @@ -2274,11 +2302,24 @@ void Reduce::execute(dnnl::stream strm) {
const uint8_t *src_data = srcMemPtr->getDataAs<const uint8_t>();
uint8_t *dst_data = dstMemPtr->getDataAs<uint8_t>();

if (empty_input) {
if (dstMemPtr->getSize() > 0) {
init_dst_data(dst_data, dstMemPtr->getSize());
#if defined(OPENVINO_ARCH_X86_64)
if (attr.get()->post_ops_.len() != 0) {
reduce_kernel_post_process(dst_data);
}
#endif
}
return;
}

if (jit_mode) {
if (is_hybrid_layout) {
dst_data = reinterpret_cast<uint8_t *>(prc_mem.get_data_handle());
}
reduce_type(src_data, dst_data);
#if defined (OV_CPU_WITH_ACL)
} else if (aclExecPtr) {
std::vector<MemoryCPtr> srcMemory;
for (size_t i = 0; i < getParentEdges().size(); i++) {
Expand All @@ -2288,6 +2329,7 @@ void Reduce::execute(dnnl::stream strm) {
dstMemory.push_back(getDstMemoryAtPort(0));

aclExecPtr->exec(srcMemory, dstMemory, postOpsDataPtrs.data());
#endif
} else {
if (layout == ReduceLayoutType::reduce_ncsp) {
auto in_ptr = reinterpret_cast<const float *>(src_data);
Expand Down Expand Up @@ -2725,7 +2767,7 @@ inline void Reduce::reduce_kernel_process(const uint8_t *in_p, uint8_t *out_p, s

inline void Reduce::reduce_kernel_post_process(uint8_t *out_ptr) {
const uint8_t *in_ptr = fuse_low_precision ? static_cast<uint8_t *>(&intermediate_buf[0]) : nullptr;
const size_t integerDivisor = IB * IC * ID * IH * IW / (OB * OC * OD * OH * OW);
const size_t integerDivisor = empty_input ? 1 : IB * IC * ID * IH * IW / (OB * OC * OD * OH * OW);
const float divisor = static_cast<float>(integerDivisor);
if (layout == ReduceLayoutType::reduce_ncsp) {
parallel_for2d(OB, OC, [&](size_t ob, size_t oc) {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/intel_cpu/src/nodes/reduce.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class Reduce : public Node {
bool ReduceCDW_opt = false;
bool use_aux_kernel = false;
bool set_use_aux_kernel = false;
bool empty_input = false;
bool ReduceN, ReduceC, ReduceD, ReduceH, ReduceW;
size_t IB, IC, ID, IH, IW;
size_t OB, OC, OD, OH, OW;
Expand Down Expand Up @@ -188,9 +189,11 @@ class Reduce : public Node {

std::string errorPrefix;

#if defined (OV_CPU_WITH_ACL)
ReduceAttrs reduceAttrs;
bool canUseAclExecutor = false;
std::shared_ptr<ReduceExecutor> aclExecPtr = nullptr;
#endif
};

} // namespace node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ ov::matcher_pass_callback ov::intel_cpu::ConvertReduceMultiAxisBase::convert_red
if (!reduction_axes) {
return false;
}
if (!reduce->is_dynamic() && ov::shape_size(input0.get_shape()) == 0) {
return false;
}
if (ov::shape_size(input1.get_shape()) <= 1) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,25 @@ const std::vector<ov::test::utils::ReductionType>& reductionTypes() {
return reductionTypes;
}

const std::vector<ov::test::utils::ReductionType>& reductionTypesArithmetic() {
static const std::vector<ov::test::utils::ReductionType> reductionTypesArithmetic = {
ov::test::utils::ReductionType::Mean,
ov::test::utils::ReductionType::Sum,
ov::test::utils::ReductionType::Prod,
ov::test::utils::ReductionType::L1,
ov::test::utils::ReductionType::L2,
};
return reductionTypesArithmetic;
}

const std::vector<ov::test::utils::ReductionType>& reductionTypesCompare() {
static const std::vector<ov::test::utils::ReductionType> reductionTypesCompare = {
ov::test::utils::ReductionType::Max,
ov::test::utils::ReductionType::Min,
};
return reductionTypesCompare;
}

const std::vector<ElementType>& inpOutPrc() {
static const std::vector<ElementType> inpOutPrc = {ElementType::f32};
return inpOutPrc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const std::vector<std::vector<int>>& axes();
const std::vector<std::vector<int>>& axesND();
const std::vector<ov::test::utils::OpType>& opTypes();
const std::vector<utils::ReductionType>& reductionTypes();
const std::vector<utils::ReductionType>& reductionTypesArithmetic();
const std::vector<utils::ReductionType>& reductionTypesCompare();
const std::vector<ElementType>& inpOutPrc();
const std::vector<std::map<std::string, ov::element::Type>> additionalConfig();
const std::vector<std::map<std::string, ov::element::Type>> additionalConfigFP32();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ std::vector<std::vector<ov::test::InputShape>> inputShapes_5D = {
{{{}, {{2, 19, 2, 2, 9}}}},
};

std::vector<std::vector<ov::test::InputShape>> inputShapes_5D_ZeroDim = {
{{{}, {{2, 19, 0, 2, 9}}}},
{{{}, {{2, 19, 0, 2, 0}}}},
};

const std::vector<std::vector<int>> axes5D = {
{2, 4},
{1, 2, 4},
Expand All @@ -44,6 +49,34 @@ const auto params_MultiAxis_5D = testing::Combine(
testing::Values(emptyFusingSpec),
testing::ValuesIn(additionalConfig()));

const auto params_MultiAxis_5D_ZeroDim = testing::Combine(
testing::Combine(
testing::ValuesIn(axes5D),
testing::Values(ov::test::utils::OpType::VECTOR),
testing::Values(true),
testing::ValuesIn(reductionTypesArithmetic()),
testing::ValuesIn(inpOutPrc()),
testing::Values(ElementType::undefined),
testing::Values(ElementType::undefined),
testing::ValuesIn(inputShapes_5D_ZeroDim)),
testing::ValuesIn(filterCPUSpecificParams(cpuParams_5D)),
testing::Values(emptyFusingSpec),
testing::ValuesIn(additionalConfig()));

const auto params_MultiAxis_5D_ZeroDim_Compare = testing::Combine(
testing::Combine(
testing::ValuesIn(axes5D),
testing::Values(ov::test::utils::OpType::VECTOR),
testing::Values(true),
testing::ValuesIn(reductionTypesCompare()),
testing::ValuesIn(inpOutPrc()),
testing::Values(ElementType::undefined),
testing::Values(ElementType::undefined),
testing::ValuesIn(inputShapes_5D_ZeroDim)),
testing::ValuesIn(filterCPUSpecificParams(cpuParams_5D)),
testing::Values(emptyFusingSpec),
testing::ValuesIn(additionalConfigFP32()));

const std::vector<std::vector<int>> axes5D_ref = {
{0}
};
Expand Down Expand Up @@ -79,6 +112,20 @@ INSTANTIATE_TEST_SUITE_P(
ReduceCPULayerTest::getTestCaseName
);

INSTANTIATE_TEST_SUITE_P(
smoke_Reduce_MultiAxis_5D_ZeroDim_CPU,
ReduceCPULayerTest,
params_MultiAxis_5D_ZeroDim,
ReduceCPULayerTest::getTestCaseName
);

INSTANTIATE_TEST_SUITE_P(
smoke_Reduce_MultiAxis_5D_ZeroDim_Compare_CPU,
ReduceCPULayerTest,
params_MultiAxis_5D_ZeroDim_Compare,
ReduceCPULayerTest::getTestCaseName
);

// Reference implementation testing of ACL unsupported case
INSTANTIATE_TEST_SUITE_P(
smoke_Reduce_MultiAxis_5D_CPU_ref,
Expand Down
Loading
Loading