From a901b0c6792038eadd6f08a04940d04c50d7817c Mon Sep 17 00:00:00 2001 From: Bo Wang Date: Wed, 6 Jul 2022 12:13:20 -0700 Subject: [PATCH 1/5] fix: add a warning msg when renaming itensors Signed-off-by: Bo Wang --- core/conversion/conversion.cpp | 2 +- core/conversion/conversionctx/ConversionCtx.cpp | 11 ++++++++++- core/conversion/conversionctx/ConversionCtx.h | 4 ++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/conversion/conversion.cpp b/core/conversion/conversion.cpp index 0fa805b7fe..c4baadd815 100644 --- a/core/conversion/conversion.cpp +++ b/core/conversion/conversion.cpp @@ -188,7 +188,7 @@ void AddInputs( ctx->input_is_dynamic = true; } - ctx->value_tensor_map[in] = trt_in; + ctx->AddNamedTensor(in, trt_in); ctx->num_inputs += 1; } diff --git a/core/conversion/conversionctx/ConversionCtx.cpp b/core/conversion/conversionctx/ConversionCtx.cpp index 0d7b7084d9..b0fc77e19d 100644 --- a/core/conversion/conversionctx/ConversionCtx.cpp +++ b/core/conversion/conversionctx/ConversionCtx.cpp @@ -130,8 +130,11 @@ ConversionCtx::~ConversionCtx() { } nvinfer1::ITensor* ConversionCtx::AssociateValueAndTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { + if (!AddNamedTensor(value, tensor)) { + LOG_WARNING( + "Trying to rewrite the name " << value->debugName() << " to a named ITensor " << tensor->getName() << "."); + } tensor->setName(value->debugName().c_str()); - this->value_tensor_map[value] = tensor; return tensor; } @@ -140,6 +143,12 @@ torch::jit::IValue* ConversionCtx::AssociateValueAndIValue(const torch::jit::Val return &this->evaluated_value_map[value]; } +bool ConversionCtx::AddNamedTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { + value_tensor_map[value] = tensor; + auto ret = named_tensors.insert(tensor); + return ret.second; +} + std::string ConversionCtx::SerializeEngine() { #if NV_TENSORRT_MAJOR > 7 auto serialized_network = builder->buildSerializedNetwork(*net, *cfg); diff --git a/core/conversion/conversionctx/ConversionCtx.h b/core/conversion/conversionctx/ConversionCtx.h index 11a06b0f20..86de5c3e42 100644 --- a/core/conversion/conversionctx/ConversionCtx.h +++ b/core/conversion/conversionctx/ConversionCtx.h @@ -46,6 +46,7 @@ struct ConversionCtx { ConversionCtx(BuilderSettings settings); std::string SerializeEngine(); nvinfer1::ITensor* AssociateValueAndTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); + bool AddNamedTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); torch::jit::IValue* AssociateValueAndIValue(const torch::jit::Value* value, torch::jit::IValue tensor); bool CheckLayerAddition(const torch::jit::Node* n); @@ -69,6 +70,9 @@ struct ConversionCtx { std::unordered_map value_tensor_map; std::unordered_map evaluated_value_map; + + // record already named ITensors to prevent rewriting another name to the same tensor + std::unordered_set named_tensors; }; } // namespace conversion From c169a81aec08f8b274fcd3b23ae1f9061ebe4f3c Mon Sep 17 00:00:00 2001 From: Bo Wang Date: Thu, 14 Jul 2022 16:00:03 -0700 Subject: [PATCH 2/5] chore: change the function names Signed-off-by: Bo Wang --- core/conversion/conversion.cpp | 2 +- core/conversion/conversionctx/ConversionCtx.cpp | 6 +++--- core/conversion/conversionctx/ConversionCtx.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/conversion/conversion.cpp b/core/conversion/conversion.cpp index c4baadd815..a4a872920f 100644 --- a/core/conversion/conversion.cpp +++ b/core/conversion/conversion.cpp @@ -188,7 +188,7 @@ void AddInputs( ctx->input_is_dynamic = true; } - ctx->AddNamedTensor(in, trt_in); + ctx->RecordNewTensor(in, trt_in); ctx->num_inputs += 1; } diff --git a/core/conversion/conversionctx/ConversionCtx.cpp b/core/conversion/conversionctx/ConversionCtx.cpp index b0fc77e19d..868cfc373b 100644 --- a/core/conversion/conversionctx/ConversionCtx.cpp +++ b/core/conversion/conversionctx/ConversionCtx.cpp @@ -130,7 +130,7 @@ ConversionCtx::~ConversionCtx() { } nvinfer1::ITensor* ConversionCtx::AssociateValueAndTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { - if (!AddNamedTensor(value, tensor)) { + if (!RecordNewTensor(value, tensor)) { LOG_WARNING( "Trying to rewrite the name " << value->debugName() << " to a named ITensor " << tensor->getName() << "."); } @@ -143,9 +143,9 @@ torch::jit::IValue* ConversionCtx::AssociateValueAndIValue(const torch::jit::Val return &this->evaluated_value_map[value]; } -bool ConversionCtx::AddNamedTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { +bool ConversionCtx::RecordNewTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { value_tensor_map[value] = tensor; - auto ret = named_tensors.insert(tensor); + auto ret = known_tensors.insert(tensor); return ret.second; } diff --git a/core/conversion/conversionctx/ConversionCtx.h b/core/conversion/conversionctx/ConversionCtx.h index 86de5c3e42..122aa4488a 100644 --- a/core/conversion/conversionctx/ConversionCtx.h +++ b/core/conversion/conversionctx/ConversionCtx.h @@ -46,7 +46,7 @@ struct ConversionCtx { ConversionCtx(BuilderSettings settings); std::string SerializeEngine(); nvinfer1::ITensor* AssociateValueAndTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); - bool AddNamedTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); + bool RecordNewTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); torch::jit::IValue* AssociateValueAndIValue(const torch::jit::Value* value, torch::jit::IValue tensor); bool CheckLayerAddition(const torch::jit::Node* n); @@ -72,7 +72,7 @@ struct ConversionCtx { std::unordered_map evaluated_value_map; // record already named ITensors to prevent rewriting another name to the same tensor - std::unordered_set named_tensors; + std::unordered_set known_tensors; }; } // namespace conversion From cb5c06b9f5cd1b9e9d719c664bd7e8d3e5a0de26 Mon Sep 17 00:00:00 2001 From: Bo Wang Date: Fri, 22 Jul 2022 12:29:19 -0700 Subject: [PATCH 3/5] refactor: refactor the RecordNewTensor function Signed-off-by: Bo Wang --- core/conversion/conversionctx/ConversionCtx.cpp | 14 +++++++------- core/conversion/conversionctx/ConversionCtx.h | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/conversion/conversionctx/ConversionCtx.cpp b/core/conversion/conversionctx/ConversionCtx.cpp index 868cfc373b..ee19d97709 100644 --- a/core/conversion/conversionctx/ConversionCtx.cpp +++ b/core/conversion/conversionctx/ConversionCtx.cpp @@ -130,11 +130,8 @@ ConversionCtx::~ConversionCtx() { } nvinfer1::ITensor* ConversionCtx::AssociateValueAndTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { - if (!RecordNewTensor(value, tensor)) { - LOG_WARNING( - "Trying to rewrite the name " << value->debugName() << " to a named ITensor " << tensor->getName() << "."); - } - tensor->setName(value->debugName().c_str()); + RecordNewTensor(value, tensor); + return tensor; } @@ -143,10 +140,13 @@ torch::jit::IValue* ConversionCtx::AssociateValueAndIValue(const torch::jit::Val return &this->evaluated_value_map[value]; } -bool ConversionCtx::RecordNewTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { +void ConversionCtx::RecordNewTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { value_tensor_map[value] = tensor; auto ret = known_tensors.insert(tensor); - return ret.second; + if (!ret) { + LOG_WARNING( + "Trying to record the value " << value->debugName() << " with the ITensor " << tensor->getName() << " again."); + } } std::string ConversionCtx::SerializeEngine() { diff --git a/core/conversion/conversionctx/ConversionCtx.h b/core/conversion/conversionctx/ConversionCtx.h index 122aa4488a..352270d264 100644 --- a/core/conversion/conversionctx/ConversionCtx.h +++ b/core/conversion/conversionctx/ConversionCtx.h @@ -46,7 +46,7 @@ struct ConversionCtx { ConversionCtx(BuilderSettings settings); std::string SerializeEngine(); nvinfer1::ITensor* AssociateValueAndTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); - bool RecordNewTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); + void RecordNewTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); torch::jit::IValue* AssociateValueAndIValue(const torch::jit::Value* value, torch::jit::IValue tensor); bool CheckLayerAddition(const torch::jit::Node* n); From 8b66d3dd32d3cf882444ed604f25c66ddcaa5e12 Mon Sep 17 00:00:00 2001 From: Bo Wang Date: Fri, 22 Jul 2022 12:47:49 -0700 Subject: [PATCH 4/5] chore: fix a typo Signed-off-by: Bo Wang --- core/conversion/conversionctx/ConversionCtx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/conversion/conversionctx/ConversionCtx.cpp b/core/conversion/conversionctx/ConversionCtx.cpp index ee19d97709..79d848175b 100644 --- a/core/conversion/conversionctx/ConversionCtx.cpp +++ b/core/conversion/conversionctx/ConversionCtx.cpp @@ -143,7 +143,7 @@ torch::jit::IValue* ConversionCtx::AssociateValueAndIValue(const torch::jit::Val void ConversionCtx::RecordNewTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { value_tensor_map[value] = tensor; auto ret = known_tensors.insert(tensor); - if (!ret) { + if (!ret.second) { LOG_WARNING( "Trying to record the value " << value->debugName() << " with the ITensor " << tensor->getName() << " again."); } From 74d4df13817ee85ab6928bd9bbab2ecbf48f46cb Mon Sep 17 00:00:00 2001 From: Dheeraj Peri Date: Sat, 23 Jul 2022 04:34:16 -0700 Subject: [PATCH 5/5] chore: Minor renaming Signed-off-by: Dheeraj Peri --- core/conversion/conversion.cpp | 2 +- core/conversion/conversionctx/ConversionCtx.cpp | 6 +++--- core/conversion/conversionctx/ConversionCtx.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/conversion/conversion.cpp b/core/conversion/conversion.cpp index a4a872920f..3211e7dd98 100644 --- a/core/conversion/conversion.cpp +++ b/core/conversion/conversion.cpp @@ -188,7 +188,7 @@ void AddInputs( ctx->input_is_dynamic = true; } - ctx->RecordNewTensor(in, trt_in); + ctx->RecordNewITensor(in, trt_in); ctx->num_inputs += 1; } diff --git a/core/conversion/conversionctx/ConversionCtx.cpp b/core/conversion/conversionctx/ConversionCtx.cpp index 79d848175b..96f6e95f8e 100644 --- a/core/conversion/conversionctx/ConversionCtx.cpp +++ b/core/conversion/conversionctx/ConversionCtx.cpp @@ -130,7 +130,7 @@ ConversionCtx::~ConversionCtx() { } nvinfer1::ITensor* ConversionCtx::AssociateValueAndTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { - RecordNewTensor(value, tensor); + RecordNewITensor(value, tensor); return tensor; } @@ -140,9 +140,9 @@ torch::jit::IValue* ConversionCtx::AssociateValueAndIValue(const torch::jit::Val return &this->evaluated_value_map[value]; } -void ConversionCtx::RecordNewTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { +void ConversionCtx::RecordNewITensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) { value_tensor_map[value] = tensor; - auto ret = known_tensors.insert(tensor); + auto ret = seen_itensors.insert(tensor); if (!ret.second) { LOG_WARNING( "Trying to record the value " << value->debugName() << " with the ITensor " << tensor->getName() << " again."); diff --git a/core/conversion/conversionctx/ConversionCtx.h b/core/conversion/conversionctx/ConversionCtx.h index 352270d264..e89aa50731 100644 --- a/core/conversion/conversionctx/ConversionCtx.h +++ b/core/conversion/conversionctx/ConversionCtx.h @@ -46,7 +46,7 @@ struct ConversionCtx { ConversionCtx(BuilderSettings settings); std::string SerializeEngine(); nvinfer1::ITensor* AssociateValueAndTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); - void RecordNewTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); + void RecordNewITensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor); torch::jit::IValue* AssociateValueAndIValue(const torch::jit::Value* value, torch::jit::IValue tensor); bool CheckLayerAddition(const torch::jit::Node* n); @@ -72,7 +72,7 @@ struct ConversionCtx { std::unordered_map evaluated_value_map; // record already named ITensors to prevent rewriting another name to the same tensor - std::unordered_set known_tensors; + std::unordered_set seen_itensors; }; } // namespace conversion