From d15b8bf5c5961a62017d8a9f3ff6c7998d4d925a Mon Sep 17 00:00:00 2001 From: "River.Li" Date: Wed, 25 Oct 2023 16:05:11 +0800 Subject: [PATCH] Fix zero dim with none zero strides issue --- src/inference/src/dev/make_tensor.cpp | 2 +- src/plugins/intel_cpu/src/infer_request.cpp | 10 ++++++---- .../memory_desc/cpu_blocked_memory_desc.cpp | 18 +++++++++--------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/inference/src/dev/make_tensor.cpp b/src/inference/src/dev/make_tensor.cpp index 1d23c62f86d957..2c0f33b352bcf6 100644 --- a/src/inference/src/dev/make_tensor.cpp +++ b/src/inference/src/dev/make_tensor.cpp @@ -77,7 +77,7 @@ class ViewTensor : public ITensor { auto& shape = get_shape(); if (m_strides.empty() && !shape.empty()) { m_strides.resize(shape.size()); - m_strides.back() = m_element_type.size(); + m_strides.back() = shape.back() == 0 ? 0 : m_element_type.size(); std::transform(shape.crbegin(), shape.crend() - 1, m_strides.rbegin(), diff --git a/src/plugins/intel_cpu/src/infer_request.cpp b/src/plugins/intel_cpu/src/infer_request.cpp index f6129f6d17b020..dd604b1b485109 100644 --- a/src/plugins/intel_cpu/src/infer_request.cpp +++ b/src/plugins/intel_cpu/src/infer_request.cpp @@ -624,10 +624,12 @@ void SyncInferRequest::init_tensor(const std::string& name) { } } m_outputs[name] = tensor; - auto desc = MemoryDescUtils::createCpuBlockedMemoryDesc(tensor); - if (!port_shape.is_dynamic() && !external_ptr.count(name) && - desc.isCompatible(output->second->getParentEdgesAtPort(0)[0]->getMemory().getDesc())) { - external_ptr[name] = tensor; + + if (!port_shape.is_dynamic() && !external_ptr.count(name)) { + auto desc = MemoryDescUtils::createCpuBlockedMemoryDesc(tensor); + if (desc.isCompatible(output->second->getParentEdgesAtPort(0)[0]->getMemory().getDesc())) { + external_ptr[name] = tensor; + } } // update tensors in case of multiple output ports with the same name for (const auto& out : get_outputs()) { diff --git a/src/plugins/intel_cpu/src/memory_desc/cpu_blocked_memory_desc.cpp b/src/plugins/intel_cpu/src/memory_desc/cpu_blocked_memory_desc.cpp index a9df79167521fe..266b3a2a6840ab 100644 --- a/src/plugins/intel_cpu/src/memory_desc/cpu_blocked_memory_desc.cpp +++ b/src/plugins/intel_cpu/src/memory_desc/cpu_blocked_memory_desc.cpp @@ -22,18 +22,18 @@ CpuBlockedMemoryDesc::CpuBlockedMemoryDesc(InferenceEngine::Precision prc, const const VectorDims& order, size_t offsetPadding, const VectorDims& offsetPaddingToData, const VectorDims& strides) : MemoryDesc(shape, Blocked), precision(prc) { if (std::any_of(order.begin(), order.end(), [](size_t val) { return val == Shape::UNDEFINED_DIM; })) { - IE_THROW() << "CpuBlockedMemoryDesc do not support undefined order."; + OPENVINO_THROW("CpuBlockedMemoryDesc do not support undefined order."); } if (std::any_of(blockedDims.begin() + shape.getRank(), blockedDims.end(), [](size_t val) { return val == Shape::UNDEFINED_DIM; })) { - IE_THROW() << "CpuBlockedMemoryDesc doesn't support undefined blockedDims."; + OPENVINO_THROW("CpuBlockedMemoryDesc doesn't support undefined blockedDims."); } if (shape.hasZeroDims()) { const auto& dims = shape.getDims(); for (size_t i = 0; i < shape.getRank(); i++) { if (dims[order[i]] == 0 && !dimsEqualWeak(blockedDims[i], 0)) { - IE_THROW() << "Can't create CpuBlockedMemoryDesc. Mistmatch zero dims in dims and blocked dims"; + OPENVINO_THROW("Can't create CpuBlockedMemoryDesc. Mistmatch zero dims in dims and blocked dims"); } } } @@ -61,13 +61,13 @@ CpuBlockedMemoryDesc::CpuBlockedMemoryDesc(InferenceEngine::Precision prc, const } } else { if (shape.hasZeroDims() && std::any_of(strides.begin(), strides.end(), [](size_t stride) { return stride != 0; } )) { - IE_THROW() << "Can't create CpuBlockedMemoryDesc with zero dim, but with non zero strides"; + OPENVINO_THROW("Can't create CpuBlockedMemoryDesc with zero dim, but with non zero strides"); } this->strides = strides; } if (!everyone_is(this->order.size(), this->blockedDims.size(), this->offsetPaddingToData.size(), this->strides.size())) { - IE_THROW() << "Order, blocked dims, offset padding to data and strides must have equals size"; + OPENVINO_THROW("Order, blocked dims, offset padding to data and strides must have equals size"); } } @@ -148,7 +148,7 @@ size_t CpuBlockedMemoryDesc::getOffset(const InferenceEngine::SizeVector& v) con size_t n_blocked_dims = order.size(); if (blockedDims.size() != n_blocked_dims || strides.size() != n_blocked_dims) { - IE_THROW() << "Cannot calculate offset. Incorrect primitive descriptor!"; + OPENVINO_THROW("Cannot calculate offset. Incorrect primitive descriptor!"); } InferenceEngine::SizeVector blockedShift(n_blocked_dims); for (size_t i = 1; i <= n_blocked_dims; i++) { @@ -240,7 +240,7 @@ bool CpuBlockedMemoryDesc::isTailCFormat() const { MemoryDescPtr CpuBlockedMemoryDesc::cloneWithNewDimsImp(const VectorDims &dims) const { if (std::any_of(dims.begin(), dims.end(), [](size_t x){ return Shape::UNDEFINED_DIM == x; })) { - IE_THROW() << "Can't clone desc if new dims are undefined"; + OPENVINO_THROW("Can't clone desc if new dims are undefined"); } // TODO [DS]: add stride recalculation for strided blobs @@ -249,7 +249,7 @@ MemoryDescPtr CpuBlockedMemoryDesc::cloneWithNewDimsImp(const VectorDims &dims) break; if (strides[i] != strides[i + 1] * blockedDims[i + 1]) - IE_THROW(NotImplemented) << "Can't clone desc with new dims for not dense tensor"; + OPENVINO_THROW("Can't clone desc with new dims for not dense tensor"); } VectorDims newBlockedDims(order.size()); @@ -298,7 +298,7 @@ size_t CpuBlockedMemoryDesc::getPaddedElementsCount() const { return 0; } if (std::any_of(blockedDims.begin(), blockedDims.end(), [](Dim dim) { return dim == Shape::UNDEFINED_DIM; })) { - IE_THROW() << "Can't compute padded elements count for non undefined blocked dims"; + OPENVINO_THROW("Can't compute padded elements count for non undefined blocked dims"); } return std::accumulate(blockedDims.begin(), blockedDims.end(), size_t{1}, std::multiplies()); }