Skip to content

Commit

Permalink
Fix zero dim with none zero strides issue
Browse files Browse the repository at this point in the history
  • Loading branch information
riverlijunjie committed Oct 25, 2023
1 parent 189deba commit d15b8bf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/inference/src/dev/make_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
10 changes: 6 additions & 4 deletions src/plugins/intel_cpu/src/infer_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Expand Down Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand Down Expand Up @@ -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<size_t>());
}
Expand Down

0 comments on commit d15b8bf

Please sign in to comment.