From a1f4975b310728c0042a47fda8a5c1d004c2745a Mon Sep 17 00:00:00 2001 From: MirceaDan99 Date: Thu, 12 Sep 2024 15:41:27 +0300 Subject: [PATCH] Refactor changes --- .../compiler/src/driver_compiler_adapter.cpp | 2 +- .../compiler/src/zero_compiler_in_driver.cpp | 62 +++++-------------- .../intel_npu/tools/compile_tool/main.cpp | 23 +++---- 3 files changed, 27 insertions(+), 60 deletions(-) diff --git a/src/plugins/intel_npu/src/compiler/src/driver_compiler_adapter.cpp b/src/plugins/intel_npu/src/compiler/src/driver_compiler_adapter.cpp index fb580b3e7caac1..4089c88f82566f 100644 --- a/src/plugins/intel_npu/src/compiler/src/driver_compiler_adapter.cpp +++ b/src/plugins/intel_npu/src/compiler/src/driver_compiler_adapter.cpp @@ -112,7 +112,7 @@ void LevelZeroCompilerAdapter::release(std::shared_ptr void LevelZeroCompilerAdapter::getCompiledNetwork( std::shared_ptr networkDescription, std::ostream& stream) { _logger.info("getCompiledNetwork - using adapter to perform getCompiledNetwork(networkDescription)"); - apiAdapter->getCompiledNetwork(std::move(networkDescription), stream); + apiAdapter->getCompiledNetwork(networkDescription, stream); } } // namespace driverCompilerAdapter 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 819e9c7559353c..c7e58d46179286 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 @@ -364,31 +364,17 @@ void LevelZeroCompilerInDriver::release(std::shared_ptrobj = std::move(moveStr); - this->setp(&this->obj[0], &this->obj[0] + this->obj.size()); - this->setg(&this->obj[0], &this->obj[0] + 1, &this->obj[0] + this->obj.size()); - this->pbump(this->obj.size()); - } - - std::string str() { - return this->obj; - } -private: - std::string obj; + CustomStringBuf(std::vector& 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()); + } }; -// new method for export_model(ostringstream) ? template void LevelZeroCompilerInDriver::getCompiledNetwork( std::shared_ptr networkDescription, std::ostream& stream) { - std::ostringstream* oStringStreamPtr = dynamic_cast(&stream); - std::vector blob; - std::string blobStr; - uint8_t* blobData; - size_t blobSize = -1; - 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); @@ -405,17 +391,10 @@ void LevelZeroCompilerInDriver::getCompiledNetwork( uint64_t(result), ". ", getLatestBuildError()); - - if (oStringStreamPtr != nullptr) { - blobStr.resize(blobSize); - blobData = reinterpret_cast(&blobStr[0]); - } else { - blob.resize(blobSize); - blobData = blob.data(); - } - + + std::const_pointer_cast(networkDescription)->compiledNetwork.resize(blobSize); // Get blob data - result = _graphDdiTableExt->pfnGetNativeBinary(graphHandle, &blobSize, blobData); + result = _graphDdiTableExt->pfnGetNativeBinary(graphHandle, &blobSize, std::const_pointer_cast(networkDescription)->compiledNetwork.data()); OPENVINO_ASSERT(result == ZE_RESULT_SUCCESS, "Failed to compile network. L0 pfnGetNativeBinary get blob data", @@ -427,29 +406,16 @@ void LevelZeroCompilerInDriver::getCompiledNetwork( ". ", getLatestBuildError()); _logger.info("LevelZeroCompilerInDriver getCompiledNetwork returning blob"); - // return blob; } else { _logger.info("return the blob from network description"); - if (oStringStreamPtr != nullptr) { - // some magic trick here so oStringStreamPtr->str(CustomStringThatWontCopyBuffer(networkDescription->compiledNetwork.data(), networkDescription->compiledNetwork.size()); - } else { - blobData = std::const_pointer_cast(networkDescription)->compiledNetwork.data(); - blobSize = networkDescription->compiledNetwork.size(); - } - // return networkDescription->compiledNetwork; } - if (oStringStreamPtr == nullptr) { - stream.write(reinterpret_cast(blobData), blobSize); + + std::ostringstream* oStringStreamPtr = dynamic_cast(&stream); + if (oStringStreamPtr != nullptr) { + CustomStringBuf customStringBuf(std::const_pointer_cast(networkDescription)->compiledNetwork); + oStringStreamPtr->rdbuf()->swap(customStringBuf); } else { - { - // Only for CXX17 - stream.rdbuf(new CustomStringBuf(std::move(blobStr))); - oStringStreamPtr->rdbuf()->swap(*dynamic_cast(stream.rdbuf())); - } - // By CXX20 we may use move semantics and avoid use of CustomStringBuf class - // oStringStreamPtr->str(std::move(blobStr)); - std::cout << oStringStreamPtr->str()[1] << std::endl; - std::cout << oStringStreamPtr->str()[2] << std::endl; + stream.write(reinterpret_cast(networkDescription->compiledNetwork.data()), networkDescription->compiledNetwork.size()); } } diff --git a/src/plugins/intel_npu/tools/compile_tool/main.cpp b/src/plugins/intel_npu/tools/compile_tool/main.cpp index a2b20922ae1f45..2f3f60231a9c40 100644 --- a/src/plugins/intel_npu/tools/compile_tool/main.cpp +++ b/src/plugins/intel_npu/tools/compile_tool/main.cpp @@ -572,18 +572,19 @@ int main(int argc, char* argv[]) { outputName = getFileNameFromPath(fileNameNoExt(FLAGS_m)) + ".blob"; } - // std::ofstream outputFile{outputName, std::ios::out | std::ios::binary}; + /*std::ofstream outputFile{outputName, std::ios::out | std::ios::binary}; + if (!outputFile.is_open()) { + std::cout << "Outputting file " << outputName << " can't be opened for writing" << std::endl; + return EXIT_FAILURE; + } else { + std::cout << "Writing into file - " << outputName << std::endl; + compiledModel.export_model(outputFile); + }*/ std::ostringstream oStringStream; - // if (!outputFile.is_open()) { - // std::cout << "Outputting file " << outputName << " can't be opened for writing" << std::endl; - // return EXIT_FAILURE; - // } else { - // std::cout << "Writing into file - " << outputName << std::endl; - // compiledModel.export_model(outputFile); - compiledModel.export_model(oStringStream); - std::cout << oStringStream.str()[1] << std::endl; - std::cout << oStringStream.str()[2] << std::endl; - // } + compiledModel.export_model(oStringStream); + auto str = oStringStream.str(); + std::cout << str[1] << std::endl; + std::cout << str[2] << std::endl; std::cout << "Done. LoadNetwork time elapsed: " << loadNetworkTimeElapsed.count() << " ms" << std::endl; } catch (const std::exception& error) { std::cerr << error.what() << std::endl;