Skip to content

Define TensorRT network with dynamic shapes - #36439

Merged
tensorflow-copybara merged 23 commits into
tensorflow:masterfrom
tfeher:trt_build_with_dynamic_shapes
Feb 11, 2020
Merged

tensorflow-copybara merged 23 commits into
tensorflow:masterfrom
tfeher:trt_build_with_dynamic_shapes

Conversation

@tfeher

@tfeher tfeher commented Feb 3, 2020 •

Copy link
Copy Markdown
Contributor

This PR builds enables TRT network creation using dynamic shapes.

Dynamic shapes can be only used in the explicit batch mode (experimental). This PR only implements TensorRT network definition using dynamic shapes. To actually build the engine and run inference, one needs to specify optimization profiles. That is implemented in the next PR. Therefore TRT Engine creation is expected to fail if explicit batch mode is selected and the input tensors have unknown dimensions. This is tested in trt_engine_op_test.

This PR is not supposed to change any existing behavior: inference in implicit batch mode, or in explicit mode with known (static) shapes should still work as before.

Note
With the introduction of dynamic shapes, TF-TRT will have two different strategies to handle input with different shapes. (Confusingly the doc sometimes refers both as dynamic shapes). From the user point of view they are similar: the aim is to use TensorRT inference for inputs whose shape is unknown at the time when we call trt.convert().

  • The existing strategy is dynamic engine creation. It means that TensorRT engines can be created during runtime to handle different input shapes.

    If is_dynamic_op is True, each TRTEngineOp will contain a serialized
    subgraph GraphDef, which will be converted to a TRT engine at execution time
    and the TRT engine will be cached for future usage. A new TRT engine will be
    created each time when none of the cached engines match the input shapes. If
    it fails to execute the TRT engine or the number of cached engines reaches
    maximum_cached_engines, the op will fall back to call the corresponding TF
    function.
    If is_dynamic_op is False, each TRTEngineOp will contain a serialized TRT
    engine created from the corresponding subgraph. No more engines will be
    created on the fly, and the op will fall back to call the corresponding TF
    function when it fails to execute the engine.

    # Whether to generate dynamic TRT ops which will build the TRT network
    # and engine at run time.
    # i.e. Since TensorRT version < 6.0 does not support dynamic dimensions
    # other than the batch dimension, when the TensorFlow graph has a
    # non-batch dimension of dynamic size, we would need to enable this
    # option. This option should be set to True in TF 2.0.
    "is_dynamic_op",

    Dynamic engine creation is the only supported ongine creation in TF v2. See the relevant doc:
    Note that in V2, is_dynamic_op=False is not supported, meaning TRT engines
    will be built only when the corresponding TRTEngineOp is executed. But we

  • This PR introduces dynamic input shapes (supported since TensorRT version 6), which is another way to deal with input data that has a shape not known at conversion time: we can define the TensorRT network with unknown input shapes. This way a single engine can handle input with different sizes. We still need to know the expected range of each input dimension (shape optimization profiles) at engine creation time.

@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 15:10
@gbaned gbaned self-assigned this Feb 4, 2020
@gbaned gbaned added the comp:gpu:tensorrt Issues specific to TensorRT label Feb 4, 2020

@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 constructive comments. I have addressed the issues, please have a look!

@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 additional comments. I have addressed the issues.


#if IS_TRT_VERSION_GE(6, 0, 0, 0)
// For dynamic input shapes, we need to use TRT ops to build the new shape.
// If the remaining dimensions of squeeze operation have dynamic sizes, we

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.

Add "a" between "of" and "squeeze operation"


// Array of all input shapes, collected from the input_shapes attribute when
// constructing the TRTEngineOp. The input_shapes attribute is set during
// graph conversion time. This data is used to retrive which input dimensions

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.

retrieve, missing "e"

return errors::InvalidArgument(
"Input tensor with shape ", shape.DebugString(),
" has an unknown non-batch dimension at dim ", d);
// Following checks are only used during TRT engine creation time. In implicit

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.

Following checks are only used during TRT engine creation time.
Do you mean this routine itself is called to validate tensors during TRT engine creation time? Can you move this sentence and make it a document for the routine?

@tensorflow-bot tensorflow-bot Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Feb 11, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Feb 11, 2020
tensorflow-copybara pushed a commit that referenced this pull request Feb 11, 2020
PiperOrigin-RevId: 294525798
Change-Id: Ied3dd9c188bef8f22dff1a6864615a5d59991d7a
@tensorflow-copybara
tensorflow-copybara merged commit b75a622 into tensorflow:master Feb 11, 2020
tensorflow-copybara pushed a commit that referenced this pull request Feb 12, 2020
PiperOrigin-RevId: 294560539
Change-Id: Ia85988659cac35e571caaf6ece69ad506736b438
@tfeher
tfeher deleted the trt_build_with_dynamic_shapes 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