Skip to content

Commit abce82d

Browse files
CR fix
1 parent 9c3af2a commit abce82d

File tree

6 files changed

+73
-59
lines changed

6 files changed

+73
-59
lines changed

src/plugins/intel_cpu/src/graph.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "nodes/convert.h"
2828
#include "nodes/subgraph.h"
2929
#include "nodes/fullyconnected.h"
30-
#include "output_mem_mgr.h"
3130

3231
#include <ie_algorithm.hpp>
3332
#include <blob_factory.hpp>
@@ -903,7 +902,7 @@ void Graph::AllocateWithReuse() {
903902
if (isOutGrp) {
904903
IE_ASSERT(isOutGrp==1); // reuse_io_tensors false
905904
grpMemMngr =
906-
std::make_shared<OutputMemoryMngr>(grpMemMngr);
905+
std::make_shared<ProxyMemoryMngr>(grpMemMngr);
907906
DEBUG_LOG(grpMemMngr, " ", this);
908907

909908
// Store the output memory managers.
@@ -912,7 +911,7 @@ void Graph::AllocateWithReuse() {
912911
const auto child = edge->getChild();
913912
if (child->getType() == Type::Output) {
914913
for (auto &output : outputNodesMap) {
915-
if (output.second == child) outputNodesMemMngrMap[output.first] = grpMemMngr;
914+
if (output.second == child) outputNodesMemMngrMap[output.first] = std::static_pointer_cast<ProxyMemoryMngr>(grpMemMngr);
916915
}
917916
}
918917
}

src/plugins/intel_cpu/src/graph.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <memory>
2020
#include <atomic>
2121

22+
#include "proxy_mem_mgr.h"
23+
2224
namespace ov {
2325
namespace intel_cpu {
2426

@@ -248,7 +250,7 @@ class Graph {
248250
std::map<std::string, NodePtr> inputNodesMap;
249251
std::map<std::string, NodePtr> outputNodesMap;
250252

251-
std::map<std::string, MemoryMngrPtr> outputNodesMemMngrMap;
253+
std::map<std::string, ProxyMemoryMngrPtr> outputNodesMemMngrMap;
252254

253255
// these node pointers (from graphNodes) are to avoid regular checking for
254256
// constantness of nodes in Infer methods and calls of

src/plugins/intel_cpu/src/infer_request.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "memory_desc/dnnl_blocked_memory_desc.h"
2626
#include <transformations/utils/utils.hpp>
2727
#include <ie_ngraph_utils.hpp>
28-
#include "output_mem_mgr.h"
28+
#include "proxy_mem_mgr.h"
2929

3030
namespace ov {
3131
namespace intel_cpu {
@@ -269,26 +269,24 @@ void InferRequestBase::changeDefaultPtr() {
269269
bool canBeInPlace = true;
270270
// TODO: filter
271271

272-
auto inferrequest = std::dynamic_pointer_cast<InferRequest>(this->shared_from_this());
273-
OPENVINO_ASSERT(inferrequest, "should be a InferRequest instance");
274-
275-
// share intel_cpu::Tensor to Graph by injecting to corresponding OutputMemoryMngr instance.
276-
OutputMemoryMngrPtr outputMemMngr;
272+
// share intel_cpu::Tensor to Graph by injecting to corresponding ProxyMemoryMngr instance.
273+
ProxyMemoryMngrPtr outputMemMngr;
277274
const auto &outMemMngrMap = graph->outputNodesMemMngrMap;
278275
auto itr = outMemMngrMap.find(it.first);
279276
if (itr != outMemMngrMap.end()) {
280-
outputMemMngr = std::dynamic_pointer_cast<OutputMemoryMngr>(itr->second);
277+
outputMemMngr = itr->second;
281278
OPENVINO_ASSERT(outputMemMngr, "output memmanager should not be empty.");
282279
} else {
283280
OPENVINO_THROW("Cannot find output memmanager for output " + it.first + " !");
284281
}
285282

286283
if (canBeInPlace) {
287284
auto tt = std::get<0>(outputsTensor2BlobMap[it.first]);
288-
outputMemMngr->setTensor(tt);
285+
auto memptr = tt->get_memory();
286+
outputMemMngr->setManager(memptr->getMemoryMngr());
289287
DEBUG_LOG("setTensor ", tt, " graph ", graph, " inferrequest ", this);
290288
} else {
291-
outputMemMngr->setTensor(nullptr);
289+
outputMemMngr->setManager(nullptr);
292290
DEBUG_LOG("setTensor nullptr", " graph ", graph, " inferrequest ", this);
293291
}
294292

src/plugins/intel_cpu/src/output_mem_mgr.cpp

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (C) 2018-2023 Intel Corporation
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
5+
#include "proxy_mem_mgr.h"
6+
#include "utils/debug_capabilities.h"
7+
8+
using namespace ov::intel_cpu;
9+
10+
void ProxyMemoryMngr::setManager(MemoryMngrPtr _pMngr) {
11+
m_validated = (_pMngr != m_pMngr);
12+
if (_pMngr) {
13+
m_pMngr = _pMngr;
14+
} else {
15+
m_pMngr = m_pOrigMngr;
16+
}
17+
}
18+
19+
void* ProxyMemoryMngr::getRawPtr() const noexcept {
20+
{
21+
std::lock_guard<std::mutex> guard(m_lock);
22+
if (m_validated) {
23+
m_pMngr->resize(m_Size);
24+
m_validated = false;
25+
}
26+
}
27+
return m_pMngr->getRawPtr();
28+
}
29+
30+
void ProxyMemoryMngr::setExtBuff(void* ptr, size_t size) {
31+
return m_pMngr->setExtBuff(ptr, size);
32+
}
33+
34+
bool ProxyMemoryMngr::resize(size_t size) {
35+
m_Size = size;
36+
m_validated = false;
37+
return m_pMngr->resize(size);
38+
}
39+
40+
bool ProxyMemoryMngr::hasExtBuffer() const noexcept {
41+
return m_pMngr->hasExtBuffer();
42+
}
43+
44+
void ProxyMemoryMngr::registerMemory(Memory* memPtr) {
45+
m_pMngr->registerMemory(memPtr);
46+
}
47+
48+
void ProxyMemoryMngr::unregisterMemory(Memory* memPtr) {
49+
m_pMngr->unregisterMemory(memPtr);
50+
}

src/plugins/intel_cpu/src/output_mem_mgr.h renamed to src/plugins/intel_cpu/src/proxy_mem_mgr.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace ov {
1313
namespace intel_cpu {
1414

15-
class OutputMemoryMngr : public IMemoryMngrObserver {
15+
class ProxyMemoryMngr : public IMemoryMngrObserver {
1616
public:
17-
explicit OutputMemoryMngr(MemoryMngrPtr pMngr) : m_pMngr(pMngr), m_pOrigMngr(pMngr) {
17+
explicit ProxyMemoryMngr(MemoryMngrPtr pMngr) : m_pMngr(pMngr), m_pOrigMngr(pMngr) {
1818
OPENVINO_ASSERT(m_pOrigMngr, "Memory manager is uninitialized");
1919
}
2020

@@ -26,15 +26,21 @@ class OutputMemoryMngr : public IMemoryMngrObserver {
2626
void registerMemory(Memory* memPtr) override;
2727
void unregisterMemory(Memory* memPtr) override;
2828

29-
void setTensor(std::shared_ptr<Tensor> tensor);
29+
void setManager(MemoryMngrPtr _pMngr);
3030

3131
private:
3232
// We keep the original MemMngr as may fallback to copy output.
3333
const MemoryMngrPtr m_pOrigMngr;
3434
MemoryMngrPtr m_pMngr;
35+
36+
// WA: resize stage might work because there is no shape change,
37+
// but the underlying actual memory manager changes.
38+
mutable std::mutex m_lock;
39+
mutable size_t m_Size = 0ul;
40+
mutable bool m_validated = false;
3541
};
36-
using OutputMemoryMngrPtr = std::shared_ptr<OutputMemoryMngr>;
37-
using OutputMemoryMngrCPtr = std::shared_ptr<const OutputMemoryMngr>;
42+
using ProxyMemoryMngrPtr = std::shared_ptr<ProxyMemoryMngr>;
43+
using ProxyMemoryMngrCPtr = std::shared_ptr<const ProxyMemoryMngr>;
3844

3945
} // namespace intel_cpu
4046
} // namespace ov

0 commit comments

Comments
 (0)