From c794c88ae0fa5a12d237cb502393928c7315520a Mon Sep 17 00:00:00 2001 From: stevenlix <38092805+stevenlix@users.noreply.github.com> Date: Fri, 11 Sep 2020 09:12:56 -0700 Subject: [PATCH] Solve name conflict in TensorRT engine caching (#5128) * fix hash conflict * Add verbose for engine deserialization and destroy old engine memory if new engine is generated * update parser * Update tensorrt_execution_provider.cc * use a better hash algorithm * Update tensorrt_execution_provider.cc --- .gitmodules | 6 +++--- cmake/external/onnx-tensorrt | 2 +- .../tensorrt/tensorrt_execution_provider.cc | 13 +++++-------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/.gitmodules b/.gitmodules index 17f5c5d8b1..fee8b1c840 100644 --- a/.gitmodules +++ b/.gitmodules @@ -56,12 +56,12 @@ [submodule "cmake/external/libprotobuf-mutator"] path = cmake/external/libprotobuf-mutator url = https://github.com/google/libprotobuf-mutator.git -[submodule "cmake/external/onnx-tensorrt"] - path = cmake/external/onnx-tensorrt - url = https://github.com/onnx/onnx-tensorrt.git [submodule "cmake/external/flatbuffers"] path = cmake/external/flatbuffers url = https://github.com/google/flatbuffers.git [submodule "cmake/external/SafeInt/safeint"] path = cmake/external/SafeInt/safeint url = https://github.com/gwang-msft/SafeInt.git +[submodule "cmake/external/onnx-tensorrt"] + path = cmake/external/onnx-tensorrt + url = https://github.com/onnx/onnx-tensorrt.git diff --git a/cmake/external/onnx-tensorrt b/cmake/external/onnx-tensorrt index 088554a5fb..fbebb14474 160000 --- a/cmake/external/onnx-tensorrt +++ b/cmake/external/onnx-tensorrt @@ -1 +1 @@ -Subproject commit 088554a5fbee9ba183c05c09c1abe986034e9208 +Subproject commit fbebb144744b3be8defecd8478d74940056df305 diff --git a/onnxruntime/core/providers/tensorrt/tensorrt_execution_provider.cc b/onnxruntime/core/providers/tensorrt/tensorrt_execution_provider.cc index d546f63ded..0e922f7c34 100644 --- a/onnxruntime/core/providers/tensorrt/tensorrt_execution_provider.cc +++ b/onnxruntime/core/providers/tensorrt/tensorrt_execution_provider.cc @@ -47,9 +47,9 @@ std::string GetEnginePath(const ::std::string& root, const std::string& name) { } std::string GetVecHash(const ::std::vector& vec) { - std::size_t ret = 0; + std::size_t ret = vec.size(); for (auto& i : vec) { - ret ^= std::hash()(i); + ret ^= i + 0x9e3779b9 + (ret << 6) + (ret >> 2); } return std::to_string(ret); } @@ -1026,22 +1026,22 @@ common::Status TensorrtExecutionProvider::Provider_Compile(const std::vectortrt_node_name_with_precision + "_" + GetVecHash(input_shapes); std::string cached_path = GetEnginePath(trt_state->engine_cache_path, trt_node_name_with_precision_shape); std::ifstream plan_file(cached_path, std::ios::binary | std::ios::in); + trt_state->context->reset(); + trt_state->engine->reset(); if (plan_file && trt_state->engine_cache_enable) { plan_file.seekg(0, std::ios::end); int engine_size = plan_file.tellg(); plan_file.seekg(0, std::ios::beg); std::unique_ptr engine_buf{new char[engine_size]}; plan_file.read((char*)engine_buf.get(), engine_size); - auto runtime_ = trt_state->runtime; - trt_state->engine->reset(); *(trt_state->engine) = tensorrt_ptr::unique_pointer( runtime_->deserializeCudaEngine(engine_buf.get(), engine_size, nullptr)); if (trt_state->engine->get() == nullptr) { return ORT_MAKE_STATUS(ONNXRUNTIME, EP_FAIL, "TensorRT EP Failed to Build Engine."); } + LOGS_DEFAULT(VERBOSE) << "[TensorRT EP] DeSerialized " + cached_path; trt_engine = trt_state->engine->get(); - } else { auto trt_config = tensorrt_ptr::unique_pointer(trt_builder->createBuilderConfig()); trt_config->setMaxWorkspaceSize(*(trt_state->max_workspace_size_ptr)); @@ -1049,8 +1049,6 @@ common::Status TensorrtExecutionProvider::Provider_Compile(const std::vectorfp16_enable_ptr) && trt_builder->platformHasFastFp16()) { trt_config->setFlag(nvinfer1::BuilderFlag::kFP16); } - trt_state->context->reset(); - trt_state->engine->reset(); *(trt_state->engine) = tensorrt_ptr::unique_pointer( trt_builder->buildEngineWithConfig(*trt_state->network, *trt_config)); @@ -1066,7 +1064,6 @@ common::Status TensorrtExecutionProvider::Provider_Compile(const std::vectorcontext->reset(); *(trt_state->context) = tensorrt_ptr::unique_pointer( trt_state->engine->get()->createExecutionContext()); if (trt_state->context->get() == nullptr) {