Skip to content

Improve TensorRT binding index query - #36434

Merged
tensorflow-copybara merged 8 commits into
tensorflow:masterfrom
tfeher:trt_binding_indices
Feb 6, 2020
Merged

tensorflow-copybara merged 8 commits into
tensorflow:masterfrom
tfeher:trt_binding_indices

Conversation

@tfeher

@tfeher tfeher commented Feb 3, 2020 •

Copy link
Copy Markdown
Contributor

This PR improves the TensorRT binding index query in a way that makes it compatible with optimization profiles.

Currently profiles are not used in TF-TRT, therefore this PR does not change any existing behavior. It is just a preparatory step to implement dynamic shapes with profiles.

TensorRT bindings are an array of pointers to input and output buffers for the network.
To execute TensorRT inference we have to specify the bindings.

To specify the pointers for the bindings, we have to query the binding index for each input and output tensor, and fill the data pointer in the bindings array at the correct location specified by the binding index.

Since TensorRT 6, an engine can have a optimization profiles, and each profile has it's own set of bindings. This PR make binding index query compatible with multiple optimization profiles.

@tensorflow-bot tensorflow-bot Bot added the size:L CL Change Size: Large label Feb 3, 2020
@tfeher
tfeher requested a review from bixia1 February 3, 2020 13:34
@gbaned gbaned self-assigned this Feb 4, 2020
@gbaned gbaned added the comp:gpu:tensorrt Issues specific to TensorRT label Feb 4, 2020

@bixia1 bixia1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me. Would you please remove the use of "PR #36379" in this PR description as it is not important.

}
}

// Get the binding index of a tensor in an engine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Gets. Also please add a blank line after this line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

corrected

Status GetTrtBindingIndex(const char* tensor_name, int profile_idx,
const nvinfer1::ICudaEngine* cuda_engine,
int* binding_idx) {
*binding_idx = cuda_engine->getBindingIndex(tensor_name);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we add this comment?
// This is binding_index_within_profile.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Described the binding index numbering scheme and explained what getBindingIndex returns.

Comment on lines +564 to +565
// If we have more then one optimization profiles then the binding idx
// depends on the profile number

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we replace this comment with this:
binding_index_within_engine = binding_index_within_profile + profile_index * bindings_per_profile

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added this comment.

StrCat("Input node ", input_name, " not found, at ", name());
LOG(ERROR) << msg;
ctx->SetStatus(errors::NotFound(msg));
int binding_index = -1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we have to initialize binding_index here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, it is not absolutely necessary. I have removed the initialization.

// Get the binding index of a tensor in an engine.
// The binding index is looked up using the tensor's name and the profile idx.
// Profile idx should be set to zero, if we do not have optimization profiles.
Status GetTrtBindingIndex(const char* tensor_name, int profile_idx,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's replace 'idx' with index so that we use index consistently.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@tfeher tfeher left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @bixia1 for the review. I have addressed the issues.

}
}

// Get the binding index of a tensor in an engine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

corrected

// Get the binding index of a tensor in an engine.
// The binding index is looked up using the tensor's name and the profile idx.
// Profile idx should be set to zero, if we do not have optimization profiles.
Status GetTrtBindingIndex(const char* tensor_name, int profile_idx,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Status GetTrtBindingIndex(const char* tensor_name, int profile_idx,
const nvinfer1::ICudaEngine* cuda_engine,
int* binding_idx) {
*binding_idx = cuda_engine->getBindingIndex(tensor_name);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Described the binding index numbering scheme and explained what getBindingIndex returns.

Comment on lines +564 to +565
// If we have more then one optimization profiles then the binding idx
// depends on the profile number

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added this comment.

StrCat("Input node ", input_name, " not found, at ", name());
LOG(ERROR) << msg;
ctx->SetStatus(errors::NotFound(msg));
int binding_index = -1;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, it is not absolutely necessary. I have removed the initialization.

@tensorflow-bot tensorflow-bot Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Feb 6, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Feb 6, 2020
tensorflow-copybara pushed a commit that referenced this pull request Feb 6, 2020
PiperOrigin-RevId: 293660474
Change-Id: I7868bb49ddd32ade542c8562cf89841fcdf8bf98
@tensorflow-copybara
tensorflow-copybara merged commit a1cc097 into tensorflow:master Feb 6, 2020
@tfeher
tfeher deleted the trt_binding_indices branch May 16, 2022 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes comp:gpu:tensorrt Issues specific to TensorRT ready to pull PR ready for merge process size:L CL Change Size: Large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants