Skip to content

Commit

Permalink
[CPU] Fix empty memory creation
Browse files Browse the repository at this point in the history
dnnl::memory created with an empty dnnl::memory_desc is not empty.
Default constructor should be used instead.
Adjust rest of memory logic to allow an empty dnnl::memory object
  • Loading branch information
EgorDuplensky committed Feb 14, 2025
1 parent 5755945 commit 3a2ea53
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/plugins/intel_cpu/src/cpu_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,13 +429,16 @@ StaticMemory::StaticMemory(dnnl::engine eng, MemoryDescPtr desc, const void* dat

try {
auto dnnl_desc = MemoryDescUtils::convertToDnnlMemoryDesc(m_pMemDesc);
// ========================
// Equivalent of constructor memory(const primitive_desc &desc, void *hdl)
// but with ability to skip pads zeroing.
m_prim = dnnl::memory(dnnl_desc->getDnnlDesc(), m_eng, DNNL_MEMORY_NONE);
//
// ========================
m_prim.set_data_handle(m_pMemBlock->getRawPtr());
const auto& memory_desc = dnnl_desc->getDnnlDesc();
if (memory_desc.is_zero()) {
// dnnl memory created using an empty memory_desc is not empty, so use a default constructor
m_prim = dnnl::memory();
} else {
m_prim = dnnl::memory(memory_desc, m_eng, DNNL_MEMORY_NONE);
m_prim.set_data_handle(m_pMemBlock->getRawPtr());
}
} catch (const std::exception& exc) {
dnnlErrorCtx = exc.what();
}
Expand Down Expand Up @@ -485,9 +488,6 @@ MemoryBlockPtr StaticMemory::getMemoryBlock() const {

// oneDNN specifics for backward compatibility
dnnl::memory StaticMemory::getPrimitive() const {
if (!m_prim) {
OPENVINO_THROW("Couldn't create dnnl::memory object: ", dnnlErrorCtx);
}
return m_prim;
}

Expand Down Expand Up @@ -590,6 +590,10 @@ bool mbind_move(const MemoryCPtr& mem, int numaNodeID) {
}

bool mbind_move(const dnnl::memory& mem, int numaNodeID) {
if (!mem) {
return true;
}

void* data = mem.get_data_handle();
auto desc = mem.get_desc();
auto size = desc.get_size();
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/intel_cpu/tests/unit/dnnl_memory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ TEST(StaticMemoryTest, UnsupportedDnnlPrecision) {
OV_ASSERT_NO_THROW(raw_data_ptr = testMemory->getData());
ASSERT_FALSE(nullptr == raw_data_ptr);
dnnl_memory = dnnl::memory();
ASSERT_THROW(dnnl_memory = testMemory->getPrimitive(), ov::Exception);
ASSERT_FALSE(dnnl_memory);
OV_ASSERT_NO_THROW(dnnl_memory = testMemory->getPrimitive());
ASSERT_TRUE(dnnl_memory.get(true) == nullptr);
}

0 comments on commit 3a2ea53

Please sign in to comment.