From 64926cf59387f47d0f8456ee364f0c66a874d262 Mon Sep 17 00:00:00 2001 From: MirceaDan99 Date: Thu, 12 Sep 2024 18:18:27 +0300 Subject: [PATCH] Implement custom `std::ostringstream::str()` function --- .../compiler/src/zero_compiler_in_driver.cpp | 43 +++++++++++++------ .../utils/customstringbuf/customstringbuf.hpp | 37 ++++++++++++++++ .../tools/compile_tool/CMakeLists.txt | 1 + .../intel_npu/tools/compile_tool/main.cpp | 2 + 4 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 src/plugins/intel_npu/src/utils/include/intel_npu/utils/customstringbuf/customstringbuf.hpp diff --git a/src/plugins/intel_npu/src/compiler/src/zero_compiler_in_driver.cpp b/src/plugins/intel_npu/src/compiler/src/zero_compiler_in_driver.cpp index c7e58d46179286..66a70bf8ba4642 100644 --- a/src/plugins/intel_npu/src/compiler/src/zero_compiler_in_driver.cpp +++ b/src/plugins/intel_npu/src/compiler/src/zero_compiler_in_driver.cpp @@ -2,6 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // +#include + #include "zero_compiler_in_driver.hpp" #include @@ -362,24 +364,20 @@ void LevelZeroCompilerInDriver::release(std::shared_ptr& blob) { - this->setp(reinterpret_cast(blob.data()), reinterpret_cast(blob.data()) + blob.size()); - this->setg(reinterpret_cast(blob.data()), reinterpret_cast(blob.data()) + 1, reinterpret_cast(blob.data()) + blob.size()); - this->pbump(blob.size()); - } -}; - template void LevelZeroCompilerInDriver::getCompiledNetwork( std::shared_ptr networkDescription, std::ostream& stream) { + std::ostringstream* oStringStreamPtr = dynamic_cast(&stream); + uint8_t* blobData; + std::string blobStr; + if (networkDescription->metadata.graphHandle != nullptr && networkDescription->compiledNetwork.size() == 0) { _logger.info("LevelZeroCompilerInDriver getCompiledNetwork get blob from graphHandle"); ze_graph_handle_t graphHandle = static_cast(networkDescription->metadata.graphHandle); // Get blob size first + size_t blobSize = -1; auto result = _graphDdiTableExt->pfnGetNativeBinary(graphHandle, &blobSize, nullptr); OPENVINO_ASSERT(result == ZE_RESULT_SUCCESS, @@ -392,9 +390,16 @@ void LevelZeroCompilerInDriver::getCompiledNetwork( ". ", getLatestBuildError()); - std::const_pointer_cast(networkDescription)->compiledNetwork.resize(blobSize); + if (oStringStreamPtr != nullptr) { + blobStr.resize(blobSize); + blobData = reinterpret_cast(&blobStr[0]); + } else { + std::const_pointer_cast(networkDescription)->compiledNetwork.resize(blobSize); + blobData = std::const_pointer_cast(networkDescription)->compiledNetwork.data(); + } + // Get blob data - result = _graphDdiTableExt->pfnGetNativeBinary(graphHandle, &blobSize, std::const_pointer_cast(networkDescription)->compiledNetwork.data()); + result = _graphDdiTableExt->pfnGetNativeBinary(graphHandle, &blobSize, blobData); OPENVINO_ASSERT(result == ZE_RESULT_SUCCESS, "Failed to compile network. L0 pfnGetNativeBinary get blob data", @@ -410,10 +415,20 @@ void LevelZeroCompilerInDriver::getCompiledNetwork( _logger.info("return the blob from network description"); } - std::ostringstream* oStringStreamPtr = dynamic_cast(&stream); if (oStringStreamPtr != nullptr) { - CustomStringBuf customStringBuf(std::const_pointer_cast(networkDescription)->compiledNetwork); - oStringStreamPtr->rdbuf()->swap(customStringBuf); + CustomStringBuf* customStringBufPtr = new CustomStringBuf(std::move(blobStr)); + stream.rdbuf(customStringBufPtr); + oStringStreamPtr->rdbuf()->swap(*customStringBufPtr); + + int index = stream.xalloc(); + stream.pword(index) = customStringBufPtr; + stream.register_callback([](std::ios_base::event evt, std::ios_base& str, int idx){ + if (evt == std::ios_base::erase_event) + { + CustomStringBuf* ptr = static_cast(str.pword(idx)); + delete ptr; + } + }, index); } else { stream.write(reinterpret_cast(networkDescription->compiledNetwork.data()), networkDescription->compiledNetwork.size()); } diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/customstringbuf/customstringbuf.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/customstringbuf/customstringbuf.hpp new file mode 100644 index 00000000000000..c16b7e155afebe --- /dev/null +++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/customstringbuf/customstringbuf.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include + +namespace intel_npu { + +class CustomStringBuf : public std::stringbuf { +public: + CustomStringBuf(std::string&& str) { + this->_str = std::move(str); + this->setp(&this->_str[0], &this->_str[0] + this->_str.size()); + this->setg(&this->_str[0], &this->_str[1], &this->_str[0] + this->_str.size()); + this->pbump(this->_str.size()); + } + + std::string&& str() { + return std::move(_str); + } +private: + std::string _str; +}; + +} // namespace intel_npu + +namespace std { + +template<> +ostringstream::_Mystr ostringstream::str() const { + intel_npu::CustomStringBuf* customStringBufPtr = dynamic_cast(ostream::rdbuf()); + if (customStringBufPtr != nullptr) { + return customStringBufPtr->str(); + } else { + return this->rdbuf()->str(); + } +} + +} // namespace std \ No newline at end of file diff --git a/src/plugins/intel_npu/tools/compile_tool/CMakeLists.txt b/src/plugins/intel_npu/tools/compile_tool/CMakeLists.txt index 66ff751b9f5162..51621e62c59007 100644 --- a/src/plugins/intel_npu/tools/compile_tool/CMakeLists.txt +++ b/src/plugins/intel_npu/tools/compile_tool/CMakeLists.txt @@ -20,6 +20,7 @@ ov_add_target(ADD_CPPLINT TYPE EXECUTABLE NAME ${TARGET_NAME} ROOT ${CMAKE_CURRENT_SOURCE_DIR} + INCLUDES "${NPU_PLUGIN_SOURCE_DIR}/src/utils/include" LINK_LIBRARIES PRIVATE openvino::runtime diff --git a/src/plugins/intel_npu/tools/compile_tool/main.cpp b/src/plugins/intel_npu/tools/compile_tool/main.cpp index 2f3f60231a9c40..95c19a897ee9c2 100644 --- a/src/plugins/intel_npu/tools/compile_tool/main.cpp +++ b/src/plugins/intel_npu/tools/compile_tool/main.cpp @@ -12,6 +12,8 @@ #include #include +#include + #include #include "openvino/core/partial_shape.hpp"