Skip to content

Enable TF-TRT explicit batch mode - #36379

Closed
tfeher wants to merge 20 commits into
tensorflow:masterfrom
tfeher:trt_explicit_batch_mode
Closed

tfeher wants to merge 20 commits into
tensorflow:masterfrom
tfeher:trt_explicit_batch_mode

Conversation

@tfeher

@tfeher tfeher commented Jan 31, 2020 •

Copy link
Copy Markdown
Contributor

This PR adds a new experimental feature to TF-TRT: explicit batch mode. Explicit batch mode is the first step on the way to enable dynamic shapes in TF-TRT (PR #36080).

The main advantage of explicit batch mode is the following: the batch dimension is not a special dimension in this mode, which means a single TRT network can have layers with different batch sizes and more importantly operators can manipulate the batch dimension. A unit test demonstrates this for the squeeze op.

Currently not all TF-TRT ops support explicit batch mode, it is expected to use this feature for elementwise ops. We are soon going to enable all the existing converters to work with this feature.

Currently this feature does not support INT8 calibration.

This feature is not supposed to change any of the existing behavior of TF-TRT in any of the various modes.

The ground work for this PR was already laid down by @pooyadavoodi in PR #34293. The current PR:

  • updates the TRTEngineOp to use newer TensorRT API that allows explicit batch mode (enqueueV2)
  • update the converter of squeeze op
  • add a unit test to demonstrate explicit batch mode using the squeeze op

To turn on this experimental feature, one has to use a rewriter config. Here is an example how to do it:

    root = _model()
    save.save(root, input_saved_model_dir,
              {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: root.run})

    rewriter_config_with_trt = rewriter_config_pb2.RewriterConfig()
    rewriter_config_with_trt.optimizers.extend(
        ["constfold", "layout", "constfold"])
    rewriter_config_with_trt.meta_optimizer_iterations = (
        rewriter_config_pb2.RewriterConfig.ONE)
    optimizer = rewriter_config_with_trt.custom_optimizers.add()
    rewriter_config_with_trt.custom_optimizers.add().name = "constfold"
    optimizer.name = "TensorRTOptimizer"
    optimizer.parameter_map["minimum_segment_size"].i = 1
    optimizer.parameter_map["is_dynamic_op"].b = True
    optimizer.parameter_map["maximum_cached_engines"].i = 1
    optimizer.parameter_map["use_implicit_batch"].b = False

    conversion_params = trt_convert.DEFAULT_TRT_CONVERSION_PARAMS._replace(
        rewriter_config_template=rewriter_config_with_trt)
    converter = trt_convert.TrtGraphConverterV2(input_saved_model_dir=input_saved_model_dir,
                                    conversion_params=conversion_params)
    converter.convert()

    output_saved_model_dir = self.mkdtemp()
    converter.save(output_saved_model_dir=output_saved_model_dir)

Tagging @bixia1 for review.

@tfeher

tfeher commented Feb 3, 2020 •

Copy link
Copy Markdown
Contributor Author

Update: this message was intended for the discussion of #36080, I have also added this comment there.

Here is a list of PRs related to the current one #36080. They incrementally add changes that shall make #36080 smaller. Each PR is based on the previous one in the list, please review them in the given order:
1 #36379
2. #36434
3. #36435
4. #36439
5. Add optimizaton profiles (Work in progress)
6. Handle multiple contexts (work in progress)
7. Test deserialization of opt profiles (work in progress)

After these the current #36080 will be reduced to the following: Enable build mode to generate optimization profiles for dynamic shapes.

@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.

Thanks for the work! Looks good to me, I only have some minor comments.

@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 all the issues please have a look.

@rthadur
rthadur requested a review from bixia1 February 4, 2020 17:55
bixia1
bixia1 previously approved these changes 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.

I only see some minor formatting issue, which will hopefully be automatically fixed when the code is imported to google3.

@tensorflow-bot tensorflow-bot Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Feb 4, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Feb 4, 2020
@tensorflow-bot tensorflow-bot Bot removed the ready to pull PR ready for merge process label Feb 5, 2020
@tensorflow-bot tensorflow-bot Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Feb 5, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Feb 5, 2020
tensorflow-copybara pushed a commit that referenced this pull request Feb 5, 2020
PiperOrigin-RevId: 293412014
Change-Id: I5a0852d415b149e2e39a2e41eb953ad1c8b911d2
@rthadur

rthadur commented Feb 6, 2020

Copy link
Copy Markdown
Contributor

Seems auto-merge is not happening but the changes are now committed so we can close this. Thank you for the PR.

@rthadur rthadur closed this Feb 6, 2020
@tfeher
tfeher deleted the trt_explicit_batch_mode 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 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.

5 participants