Skip to content

fix: converter renaming already named tensors #1167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/conversion/conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
11 changes: 10 additions & 1 deletion core/conversion/conversionctx/ConversionCtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ ConversionCtx::~ConversionCtx() {
}

nvinfer1::ITensor* ConversionCtx::AssociateValueAndTensor(const torch::jit::Value* value, nvinfer1::ITensor* tensor) {
if (!AddNamedTensor(value, tensor)) {
LOG_WARNING(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should put the warning in AddNamedTensor so there is no way to get around the warning.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with this. We should warn inside the RecordNewTensor so that it will be generic if we use this call elsewhere.

"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;
}

Expand All @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should call this something like RecordNewITensor or IsNewITensor since its not actually naming the tensor, it is just noting a new ITensor*

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just RecordITensor() ?. It will record or else warn if the ITensor is new.

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);
Expand Down
4 changes: 4 additions & 0 deletions core/conversion/conversionctx/ConversionCtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -69,6 +70,9 @@ struct ConversionCtx {

std::unordered_map<const torch::jit::Value*, nvinfer1::ITensor*> value_tensor_map;
std::unordered_map<const torch::jit::Value*, torch::jit::IValue> evaluated_value_map;

// record already named ITensors to prevent rewriting another name to the same tensor
std::unordered_set<nvinfer1::ITensor*> named_tensors;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call this known_itensors or seen_itensors something?

};

} // namespace conversion
Expand Down