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][Debug Caps] Enable perf counters collection for exec graph (#35429)
Vladislav Golubev257665c9e23b
diff --git a/src/plugins/intel_cpu/docs/debug_capabilities/exec_graph_serialization.md b/src/plugins/intel_cpu/docs/debug_capabilities/exec_graph_serialization.mdindex 1139bb0e937..e0490054fd9 100644--- a/src/plugins/intel_cpu/docs/debug_capabilities/exec_graph_serialization.md+++ b/src/plugins/intel_cpu/docs/debug_capabilities/exec_graph_serialization.md@@ -2,8 +2,9 @@Execution graph serialization is disabled by default and controlled by environment variable **OV_CPU_EXEC_GRAPH_PATH**:```sh- OV_CPU_EXEC_GRAPH_PATH=<option> binary ...+OV_CPU_EXEC_GRAPH_PATH=<option> binary ...```+Possible serialization options:* cout\Serialize to console output.@@ -11,3 +12,21 @@ Serialize to console output.Serialize graph into .xml and .bin files. Can be opened using, for example, *netron* app.* \<path\>_index.dot\Serialize graph into .dot file. Can be inspected using, for example, *graphviz* tools.++## Output filename++Output files are named `<option>_<index>_<model_name>.<ext>`, where `index` reflects compilation order. Example with `OV_CPU_EXEC_GRAPH_PATH=exec.xml`:+```+exec_0_ModelA.xml+exec_1_ModelB.xml+```++## Two-phase serialization++Each graph is serialized twice:+1. **At compile time** — written immediately after graph construction with `not_executed` perf counters. Useful for diagnosing crashes during the first inference.+2. **After inference** — the same file is overwritten with real per-layer execution times when the compiled model is destroyed.++## Multi-stream++All streams of the same model share one output file. The last stream to be destroyed overwrites it, which is fine since all streams run the same graph.diff --git a/src/plugins/intel_cpu/src/config.cpp b/src/plugins/intel_cpu/src/config.cppindex 546f1235211..9b16b0d553c 100644--- a/src/plugins/intel_cpu/src/config.cpp+++ b/src/plugins/intel_cpu/src/config.cpp@@ -48,8 +48,9 @@ Config::Config() {* configuration properties*/void Config::applyDebugCapsProperties() {- // always enable perf counters for verbose, performance summary and average counters- if (!debugCaps.verbose.empty() || debugCaps.summaryPerf || !debugCaps.averageCountersPath.empty()) {+ // always enable perf counters for verbose, performance summary, average counters and exec graph serialization+ if (!debugCaps.verbose.empty() || debugCaps.summaryPerf || !debugCaps.averageCountersPath.empty() ||+ !debugCaps.execGraphPath.empty()) {collectPerfCounters = true;}}diff --git a/src/plugins/intel_cpu/src/graph.cpp b/src/plugins/intel_cpu/src/graph.cppindex 735fd3d204d..de02f08f540 100644--- a/src/plugins/intel_cpu/src/graph.cpp+++ b/src/plugins/intel_cpu/src/graph.cpp@@ -104,6 +104,7 @@ namespace ov::intel_cpu {Graph::~Graph() {CPU_DEBUG_CAP_ENABLE(summary_perf(*this));CPU_DEBUG_CAP_ENABLE(average_counters(*this));+ CPU_DEBUG_CAP_ENABLE(serialize(*this));}template <typename NET>diff --git a/src/plugins/intel_cpu/src/graph_dumper.cpp b/src/plugins/intel_cpu/src/graph_dumper.cppindex c6ef03fbd85..12f0fb1e021 100644--- a/src/plugins/intel_cpu/src/graph_dumper.cpp+++ b/src/plugins/intel_cpu/src/graph_dumper.cpp@@ -19,6 +19,7 @@#include <oneapi/dnnl/dnnl.hpp>#include <sstream>#include <string>+#include <unordered_map>#include <utility>#include <vector>@@ -231,6 +232,10 @@ std::shared_ptr<ov::Model> dump_graph_as_ie_ngraph_net(const Graph& graph) {#ifdef CPU_DEBUG_CAPSvoid serialize(const Graph& graph) {+ if (!graph.getGraphContext()) {+ return;+ }+const std::string& pathStr = graph.getConfig().debugCaps.execGraphPath;if (pathStr.empty()) {@@ -249,14 +254,28 @@ void serialize(const Graph& graph) {OPENVINO_THROW("Unknown serialize format. Should be either 'cout', '*.xml' or '*.dot'. Got ", pathStr);}- static int g_idx = 0;- const auto indexedFileName = p.stem().string() + "_" + std::to_string(g_idx++) + ext.string();- const auto indexedPath = p.parent_path() / indexedFileName;+ // Exec graph serialization happens twice per graph instance:+ // 1. At compile time (Graph::Activate): the graph structure is written with+ // "not_executed" perf counters. This ensures a file is always present even+ // if a crash occurs during inference.+ // 2. At destruction (Graph::~Graph): the same file is overwritten with real+ // perf counters collected during inference.+ //+ // All streams of the same model share a single file. A monotonically increasing+ // index is assigned once per unique model name so that the order of compilation+ // is visible in the filename and multiple models don't overwrite each other.+ // Example with OV_CPU_EXEC_GRAPH_PATH=exec.xml:+ // modelA (any stream) -> exec_0_A.xml+ // modelB (any stream) -> exec_1_B.xml+ static std::unordered_map<std::string, size_t> numPerModel;+ const auto idx = numPerModel.emplace(graph.GetName(), numPerModel.size()).first->second;+ const auto fileName = p.stem().string() + "_" + std::to_string(idx) + "_" + graph.GetName() + ext.string();+ const auto filePath = p.parent_path() / fileName;if (ext == ".xml") {- serializeToXML(graph, indexedPath);+ serializeToXML(graph, filePath);} else {- serializeToDot(graph, indexedPath);+ serializeToDot(graph, filePath);}}