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] Use std::filesystem::path for paths management (#33342)
Arseniy Obolenskiy9bc4fc696647
diff --git a/src/plugins/intel_cpu/src/graph_dumper.cpp b/src/plugins/intel_cpu/src/graph_dumper.cppindex b6bdc357260..993ebde39f0 100644--- a/src/plugins/intel_cpu/src/graph_dumper.cpp+++ b/src/plugins/intel_cpu/src/graph_dumper.cpp@@ -8,6 +8,7 @@#include <chrono>#include <cstddef>#include <cstdint>+#include <filesystem>#include <fstream>#include <iomanip>#include <ios>@@ -40,7 +41,7 @@namespace ov::intel_cpu {void serializeToCout(const Graph& graph);-void serializeToXML(const Graph& graph, const std::string& path);+void serializeToXML(const Graph& graph, const std::filesystem::path& path);namespace {@@ -236,24 +237,25 @@ std::shared_ptr<ov::Model> dump_graph_as_ie_ngraph_net(const Graph& graph) {#ifdef CPU_DEBUG_CAPSvoid serialize(const Graph& graph) {- const std::string& path = graph.getConfig().debugCaps.execGraphPath;+ const std::string& pathStr = graph.getConfig().debugCaps.execGraphPath;- if (path.empty()) {+ if (pathStr.empty()) {return;}- if (path == "cout") {+ if (pathStr == "cout") {serializeToCout(graph);- } else if (!path.compare(path.size() - 4, 4, ".xml")) {+ } else if (std::filesystem::path p{pathStr}; p.extension() == ".xml") {static int g_idx = 0;- std::string xmlPath = std::string(path, 0, path.size() - 4) + "_" + std::to_string(g_idx++) + ".xml";+ const auto xmlPath =+ p.parent_path() / (p.stem().string() + "_" + std::to_string(g_idx++) + p.extension().string());serializeToXML(graph, xmlPath);} else {- OPENVINO_THROW("Unknown serialize format. Should be either 'cout' or '*.xml'. Got ", path);+ OPENVINO_THROW("Unknown serialize format. Should be either 'cout' or '*.xml'. Got ", pathStr);}}-void serializeToXML(const Graph& graph, const std::string& path) {+void serializeToXML(const Graph& graph, const std::filesystem::path& path) {if (path.empty()) {return;}@@ -393,10 +395,9 @@ void average_counters(const Graph& graph) {}static int graphIndex = 0;- std::string fileName = path + "_" + std::to_string(graphIndex++) + ".csv";-- std::ofstream file;- file.open(fileName);+ std::filesystem::path fileName{path};+ fileName += "_" + std::to_string(graphIndex++) + ".csv";+ std::ofstream file(fileName);// table structure is identical to the benchmark_app average_counters reportconst std::string header = "layerName;execStatus;layerType;execType;realTime (ms);cpuTime (ms);";diff --git a/src/plugins/intel_cpu/src/plugin.cpp b/src/plugins/intel_cpu/src/plugin.cppindex e293de8660b..7fb0263fd99 100644--- a/src/plugins/intel_cpu/src/plugin.cpp+++ b/src/plugins/intel_cpu/src/plugin.cpp@@ -6,6 +6,7 @@#include <cstddef>#include <cstring>+#include <filesystem>#include <fstream>#include <istream>#include <memory>@@ -107,7 +108,7 @@ static std::string getDeviceFullName() {}return s.substr(start, end - start + 1);};- auto read_first_line = [&](const char* path) -> std::string {+ auto read_first_line = [&](const std::filesystem::path& path) -> std::string {std::ifstream f(path);if (!f.is_open()) {return {};diff --git a/src/plugins/intel_cpu/src/utils/blob_dump.cpp b/src/plugins/intel_cpu/src/utils/blob_dump.cppindex 9900e20a032..e578118c149 100644--- a/src/plugins/intel_cpu/src/utils/blob_dump.cpp+++ b/src/plugins/intel_cpu/src/utils/blob_dump.cpp@@ -10,6 +10,7 @@#include <cstddef>#include <cstdint>+#include <filesystem>#include <fstream>#include <ios>#include <istream>@@ -281,32 +282,24 @@ BlobDumper BlobDumper::read(std::istream& stream) {return res;}-BlobDumper BlobDumper::read(const std::string& file_path) {- std::ifstream file;- file.open(file_path);+BlobDumper BlobDumper::read(const std::filesystem::path& file_path) {+ std::ifstream file(file_path, std::ios::binary);OPENVINO_ASSERT(file.is_open(), "Dumper cannot open file ", file_path);-- auto res = read(file);- file.close();- return res;+ return read(file);}-void BlobDumper::dump(const std::string& dump_path) const {- std::ofstream dump_file;- dump_file.open(dump_path, std::ios::binary);+void BlobDumper::dump(const std::filesystem::path& dump_path) const {+ std::ofstream dump_file(dump_path, std::ios::binary);OPENVINO_ASSERT(dump_file.is_open(), "Dumper cannot create dump file ", dump_path);dump(dump_file);- dump_file.close();}-void BlobDumper::dumpAsTxt(const std::string& dump_path) const {- std::ofstream dump_file;- dump_file.open(dump_path);+void BlobDumper::dumpAsTxt(const std::filesystem::path& dump_path) const {+ std::ofstream dump_file(dump_path);OPENVINO_ASSERT(dump_file.is_open(), "Dumper cannot create dump file ", dump_path);dumpAsTxt(dump_file);- dump_file.close();}} // namespace ov::intel_cpudiff --git a/src/plugins/intel_cpu/src/utils/blob_dump.h b/src/plugins/intel_cpu/src/utils/blob_dump.hindex 1b46be4f209..7874ececabc 100644--- a/src/plugins/intel_cpu/src/utils/blob_dump.h+++ b/src/plugins/intel_cpu/src/utils/blob_dump.h@@ -7,6 +7,7 @@#include <cpu_memory.h>#include <cstdint>+#include <filesystem>#include <istream>#include <memory>#include <oneapi/dnnl/dnnl_common.hpp>@@ -43,13 +44,13 @@ public:explicit BlobDumper(MemoryPtr _memory) : memory(std::move(_memory)) {}- static BlobDumper read(const std::string& file_path);+ static BlobDumper read(const std::filesystem::path& file_path);static BlobDumper read(std::istream& stream);- void dump(const std::string& dump_path) const;+ void dump(const std::filesystem::path& dump_path) const;void dump(std::ostream& stream) const;- void dumpAsTxt(const std::string& dump_path) const;+ void dumpAsTxt(const std::filesystem::path& dump_path) const;void dumpAsTxt(std::ostream& stream) const;[[nodiscard]] void* getDataPtr() const {diff --git a/src/plugins/intel_cpu/src/utils/ngraph_transformation.hpp b/src/plugins/intel_cpu/src/utils/ngraph_transformation.hppindex 1458e2d8f29..a607381ee41 100644--- a/src/plugins/intel_cpu/src/utils/ngraph_transformation.hpp+++ b/src/plugins/intel_cpu/src/utils/ngraph_transformation.hpp@@ -3,6 +3,7 @@//#pragma once#include <bitset>+#include <filesystem>#include <functional>#include <memory>#include <string>@@ -72,29 +73,36 @@ private:}void dump(const std::string&& postfix) {static int num = 0; // just to keep dumped IRs ordered in filesystem- const auto pathAndName =- config.dumpIR.dir + "/ir_" + std::to_string(num) + '_' + infoMap.at(type).name + postfix;+ const std::filesystem::path dumpDir{config.dumpIR.dir};+ const auto pathAndName = dumpDir / ("ir_" + std::to_string(num) + '_' + infoMap.at(type).name + postfix);- ov::util::create_directory_recursive(config.dumpIR.dir);+ ov::util::create_directory_recursive(dumpDir);ov::pass::Manager serializer;if (config.dumpIR.format.filter[DebugCapsConfig::IrFormatFilter::XmlBin]) {- serializer.register_pass<ov::pass::Serialize>(pathAndName + ".xml", "");+ auto xmlPath = pathAndName;+ xmlPath.replace_extension(".xml");+ serializer.register_pass<ov::pass::Serialize>(xmlPath, std::filesystem::path{});}if (config.dumpIR.format.filter[DebugCapsConfig::IrFormatFilter::Xml]) {- std::string xmlFile(pathAndName + ".xml");+ auto xmlFile = pathAndName;+ xmlFile.replace_extension(".xml");- serializer.register_pass<ov::pass::Serialize>(xmlFile, NULL_STREAM);+ serializer.register_pass<ov::pass::Serialize>(xmlFile, std::filesystem::path{NULL_STREAM});}if (config.dumpIR.format.filter[DebugCapsConfig::IrFormatFilter::Svg]) {- serializer.register_pass<ov::pass::VisualizeTree>(pathAndName + ".svg");+ auto svgFile = pathAndName;+ svgFile.replace_extension(".svg");+ serializer.register_pass<ov::pass::VisualizeTree>(svgFile);}if (config.dumpIR.format.filter[DebugCapsConfig::IrFormatFilter::Dot]) {- serializer.register_pass<ov::pass::VisualizeTree>(pathAndName + ".dot");+ auto dotFile = pathAndName;+ dotFile.replace_extension(".dot");+ serializer.register_pass<ov::pass::VisualizeTree>(dotFile);}serializer.run_passes(model);diff --git a/src/plugins/intel_cpu/src/utils/node_dumper.cpp b/src/plugins/intel_cpu/src/utils/node_dumper.cppindex b1bfe1ceadc..5bce3bc568a 100644--- a/src/plugins/intel_cpu/src/utils/node_dumper.cpp+++ b/src/plugins/intel_cpu/src/utils/node_dumper.cpp@@ -3,6 +3,7 @@//#include <algorithm>#include <cstddef>+#include <filesystem>#include <iostream>#include "cpu_types.h"@@ -90,7 +91,7 @@ static bool shouldBeDumped(const NodePtr& node, const DebugCapsConfig& config, creturn true;}-static void dump(const BlobDumper& bd, const std::string& file, const DebugCapsConfig& config) {+static void dump(const BlobDumper& bd, const std::filesystem::path& file, const DebugCapsConfig& config) {switch (config.blobDumpFormat) {case DebugCapsConfig::FORMAT::BIN: {bd.dump(file);@@ -106,6 +107,7 @@ static void dump(const BlobDumper& bd, const std::string& file, const DebugCapsC}static void dumpInternalBlobs(const NodePtr& node, const DebugCapsConfig& config) {+ const std::filesystem::path blobDumpDir{config.blobDumpDir};std::string nodeName = node->getName();formatNodeName(nodeName);@@ -114,7 +116,7 @@ static void dumpInternalBlobs(const NodePtr& node, const DebugCapsConfig& configfor (size_t i = 0; i < internalBlobs.size(); i++) {const auto& blb = internalBlobs[i];std::string file_name = NameFromType(node->getType()) + "_" + nodeName + "_blb" + std::to_string(i) + ".ieb";- auto dump_file = config.blobDumpDir + "/#" + std::to_string(node->getExecIndex()) + "_" + file_name;+ auto dump_file = blobDumpDir / ("#" + std::to_string(node->getExecIndex()) + "_" + file_name);if (blb->getDesc().getPrecision() == ov::element::u1) {continue;@@ -125,14 +127,10 @@ static void dumpInternalBlobs(const NodePtr& node, const DebugCapsConfig& config}}-static std::string createDumpFilePath(const std::string& blobDumpDir, const std::string& fileName, int execIndex) {- auto execIndexStr = std::to_string(execIndex);- std::string dump_file;- dump_file.reserve(blobDumpDir.size() + execIndexStr.size() + fileName.size() + 4);-- dump_file.append(blobDumpDir).append("/#").append(execIndexStr).append("_").append(fileName);-- return dump_file;+static std::filesystem::path createDumpFilePath(const std::filesystem::path& blobDumpDir,+ const std::string& fileName,+ int execIndex) {+ return blobDumpDir / ("#" + std::to_string(execIndex) + "_" + fileName);}void dumpInputBlobs(const NodePtr& node, const DebugCapsConfig& config, int count) {@@ -140,6 +138,7 @@ void dumpInputBlobs(const NodePtr& node, const DebugCapsConfig& config, int counreturn;}+ const std::filesystem::path blobDumpDir{config.blobDumpDir};std::string nodeName = node->getName();formatNodeName(nodeName);@@ -157,7 +156,7 @@ void dumpInputBlobs(const NodePtr& node, const DebugCapsConfig& config, int counfile_name = file_name.substr(file_name.size() - 240);}- std::string dump_file = createDumpFilePath(config.blobDumpDir, file_name, node->getExecIndex());+ auto dump_file = createDumpFilePath(blobDumpDir, file_name, node->getExecIndex());std::cout << "Dump inputs: " << dump_file << '\n';@@ -178,6 +177,7 @@ void dumpOutputBlobs(const NodePtr& node, const DebugCapsConfig& config, int coureturn;}+ const std::filesystem::path blobDumpDir{config.blobDumpDir};std::string nodeName = node->getName();formatNodeName(nodeName);@@ -194,7 +194,7 @@ void dumpOutputBlobs(const NodePtr& node, const DebugCapsConfig& config, int coufile_name = file_name.substr(file_name.size() - 240);}- std::string dump_file = createDumpFilePath(config.blobDumpDir, file_name, node->getExecIndex());+ auto dump_file = createDumpFilePath(blobDumpDir, file_name, node->getExecIndex());std::cout << "Dump outputs: " << dump_file << '\n';