OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[CPU] Add ASCII diagram for MemoryInput/MemoryOutput memory mapping and reuse (#35340)
Aditya Goyalaede5eb976f8
diff --git a/src/plugins/intel_cpu/src/nodes/memory.cpp b/src/plugins/intel_cpu/src/nodes/memory.cppindex be1571d52fa..c9aaa38ddeb 100644--- a/src/plugins/intel_cpu/src/nodes/memory.cpp+++ b/src/plugins/intel_cpu/src/nodes/memory.cpp@@ -716,7 +716,95 @@ void MemoryInput::initOptimalPrimitiveDescriptor() {}}-// @todo add ascii diagramm for memory mapping / reuse+// Memory mapping / reuse for a MemoryInput <-> MemoryOutput sibling pair+//+// 1. Sibling pair and shared VariableState (double-buffer)+// ---------------------------------------------------------+//+// MemoryInput::makeState() always creates VariableStateDoubleBuffer.+// (MemoryInputSingle and MemoryInputSDPA use single-buffer variants+// where input_mem() == output_mem(); commit() does not swap buffers,+// but is still used to clear the reset-state flag.)+//+// +--------------------------- VariableStateDoubleBuffer ---------------++// | buf[0] buf[1] |+// | |+// | prime_mem() = buf[buffer_num] |+// | second_mem() = buf[buffer_num ^ 1] |+// | |+// | input_mem() --> prime_mem() output_mem() --> second_mem() |+// | commit(): buffer_num ^= 1 (toggles prime) |+// +---------------------------------------------------------------------++// | read ^ write+// v |+// +--------------+ child edge +--------------+ parent edge +----------------++// | MemoryInput | -----------> | Computation | ------------> | MemoryOutput |+// | (ReadValue) | | nodes | | (Assign) |+// +--------------+ +--------------+ +----------------++// | |+// +----------------------- sibling link ------------------------++// (both share the same VariableState)+//+//+// 2. ProxyMemoryBlock edge reuse (the "reuse" this diagram describes)+// -------------------------------------------------------------------+//+// MemoryInput::resolveInPlaceEdges (LOOK_UP):+// Creates one ProxyMemoryBlock and wraps it in Memory for every child edge.+// At runtime (runStatic / runDynamic):+//+// if internDesc compatible with assignedMem.desc:+// memBlock->setMemBlock(assignedMem->getMemoryBlock())+// --> child edge memory IS the state buffer (zero-copy path)+// else:+// memBlock->reset()+// dst->load(*src)+// --> child edge has its own block, data is copied from state+//+// MemoryOutput::resolveInPlaceEdges (LOOK_DOWN):+// Creates one ProxyMemoryBlock and wraps it in Memory for the parent edge.+// When state is assigned (assignExtMemory):+//+// if inpDesc compatible with extMemDesc:+// memBlock->setMemBlockResize(assignedMem->getMemoryBlock())+// --> parent edge memory IS the state buffer (zero-copy path)+// else:+// memBlock->reset()+// assignedMem->load(*inputMem) in runStatic+// --> parent edge has its own block, data is copied into state+//+//+// 3. MemoryInput with subgraph (ReadValueWithSubgraph)+// -----------------------------------------------------+//+// registerToAllocationContext() wires edges so data never copies across boundaries:+//+// outer parent edge[i] --sharedMemFrom--> subGraph input node[i] child edges+// outer child edge[i] <--sharedMemFrom-- subGraph output node[i] parent edge+//+// Resulting zero-copy flow:+//+// [outer parent nodes]+// | outer parent edge (owns memory)+// v+// +------------------++// | subGraph Input | (shared from outer parent edge - no allocation)+// | nodes |+// +------------------++// | inner edges (own memory)+// v+// +------------------++// | subGraph body |+// +------------------++// | inner output edge (owns memory)+// v+// +------------------++// | subGraph Output | (shared from outer child edge - no allocation)+// | node |+// +------------------++// | outer child edge (owns memory)+// v+// [outer child nodes]void MemoryInput::createPrimitive() {if (haveSubgraph()) {CPU_NODE_ASSERT(getParentEdges().size() == subGraph->inputsNumber(),