Skip to content

Enable explicit batch mode with optimization profiles - #36080

Closed
pooyadavoodi wants to merge 95 commits into
tensorflow:masterfrom
pooyadavoodi:add_profiles
Closed

pooyadavoodi wants to merge 95 commits into
tensorflow:masterfrom
pooyadavoodi:add_profiles

Conversation

@pooyadavoodi

@pooyadavoodi pooyadavoodi commented Jan 21, 2020 •

Copy link
Copy Markdown
  • This PR adds a new experimental feature to TF-TRT.
  • This features enables the explicit batch mode in TF-TRT which comes with two main advantages: 1) 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 reshape layers can change the batch dimension dynamically, 2) TRT networks can be created with dynamic shapes (-1 dims), while optimization profiles are generated automatically out of the users data to hint TRT for optimizing the built engines.
  • Currently this feature does not support building TRT engines during runtime, which means the only single engine that is built during build() can be used during runtime. This makes it important for users to feed data to build() with all interesting shapes so that the single engine can execute those shapes.
  • 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.
  • 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.

An example on how to use this feature:

    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()

    input_shapes = [(2, 4), (3, 9), (64, 128)]
    def my_input_fn():
      for x in input_shapes:
        yield (np.random.normal(size=x).astype(np.float32),)

    converter.build(input_fn=my_input_fn)

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

Pooya Davoodi and others added 30 commits January 20, 2020 15:17
The EngineContext in TRT cache resource now handles a vector of contexts, one for each profile.
…g.bzl

TF doesn't compile without this commit.
@gbaned

gbaned commented Feb 11, 2020

Copy link
Copy Markdown
Contributor

@pooyadavoodi Can you please resolve conflicts? Thanks!

@googlebot

Copy link
Copy Markdown

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@tfeher

tfeher commented Feb 11, 2020 •

Copy link
Copy Markdown
Contributor

Thanks @gbaned for the message, I have updated the PR. Please note that PRs #36660 and #36664 have to merged before this one.

@gbaned

gbaned commented Feb 12, 2020

Copy link
Copy Markdown
Contributor

@pooyadavoodi, @tfeher Can you please sign cla ? Thank you!

@tfeher

tfeher commented Feb 12, 2020

Copy link
Copy Markdown
Contributor

@googlebot I consent

@tfeher

tfeher commented Feb 13, 2020

Copy link
Copy Markdown
Contributor

The CLA is failing because the original PR author has left the company and is not on the corporate CLA anymore. Since this PR is anyways broken into smaller pieces, I have taken the last piece still contained here and moved into a separate PR #36729.

This PR can be closed.

For completeness, here is a list of PRs that were created out of this one:
1 Enable explicit batch mode #36379
2. Improve binding index query #36434
3. Add binding size specification #36435
4. Define networks with dynamic shapes #36439
5. Add optimizaton profiles #36660
6. Execution context managment #36664
7. Test deserialization of opt profiles (will be added independently of the rest in a separate PR).
8. TensorRT profile generation mode #36729

@gbaned

gbaned commented Feb 14, 2020

Copy link
Copy Markdown
Contributor

Thanks @tfeher
Closing this PR, as its change has been moved to #36729

@gbaned gbaned closed this Feb 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: no size:XL CL Change Size:Extra Large stat:awaiting response Status - Awaiting response from author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants