Skip to content

[TF:TRT] Enable dynamic shape for "slice" and "strided slice" operations. - #51475

Merged
copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
christopherbate:tf-trt-dynamic-strided-slice
Sep 30, 2021
Merged

copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
christopherbate:tf-trt-dynamic-strided-slice

Conversation

@christopherbate

@christopherbate christopherbate commented Aug 12, 2021 •

Copy link
Copy Markdown
Contributor

Enables dynamic shape support during conversion of Tensorflow "slice" and "strided slice" operations to TensorRT ISliceLayer.

Moves existing StridedSliceHelper function to dedicated ops/slice_ops.[h|cc] file and refactors it extensively to support dynamic shape requirements.
To support the creation of ancillary graphs in the TensorRT network, a helper class TRTNetworkBuilder is added to quickly create familiar operations (e.g. Nonzero, Sign, ZerosLike) via composition of those layers exposed by TensorRT.
Converter functions which utilize StridedSliceHlper (ConvertStridedSlice, ConvertSlice, ConvertSplit) are updated to use the new helper and gain dynamic shape support.
Tests for ConvertStridedSlice are updated to include better documentation and additional tests for dynamic shape. A negative test for new_axis_mask is added.

@google-ml-butler google-ml-butler Bot added the size:L CL Change Size: Large label Aug 12, 2021
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Aug 12, 2021
@google-cla google-cla Bot added the cla: yes label Aug 12, 2021
@christopherbate
christopherbate force-pushed the tf-trt-dynamic-strided-slice branch from 22643fb to de46f1e Compare August 13, 2021 01:44
@rthadur rthadur self-assigned this Aug 13, 2021
@gbaned gbaned assigned gbaned and unassigned rthadur Aug 17, 2021
@sanjoy
sanjoy requested review from bixia1 and removed request for sanjoy August 18, 2021 20:46
@sanjoy

sanjoy commented Aug 18, 2021

Copy link
Copy Markdown
Contributor

@christopherbate is this ready to review, or is it still in a draft state?

@christopherbate

Copy link
Copy Markdown
Contributor Author

Still in draft, will be ready by end of week

@christopherbate christopherbate changed the title TF-TRT: Enable dynamic shape for "slice' and "strided slice" operations. [TF:TRT] Enable dynamic shape for "slice' and "strided slice" operations. Aug 24, 2021
@christopherbate christopherbate changed the title [TF:TRT] Enable dynamic shape for "slice' and "strided slice" operations. [TF:TRT] Enable dynamic shape for "slice" and "strided slice" operations. Aug 24, 2021
@christopherbate
christopherbate force-pushed the tf-trt-dynamic-strided-slice branch from de46f1e to 072e889 Compare August 24, 2021 21:46
@christopherbate

Copy link
Copy Markdown
Contributor Author

@bixia1 This is ready for review

@christopherbate
christopherbate marked this pull request as ready for review August 24, 2021 21:47
@christopherbate
christopherbate force-pushed the tf-trt-dynamic-strided-slice branch 2 times, most recently from ca28a20 to 6bfe721 Compare August 27, 2021 15:58
@bixia1

bixia1 commented Aug 30, 2021

Copy link
Copy Markdown
Contributor

I only reviewed part of the change in the tests and I am still working on reviewing the change to the converter.

It would be nice if you can add some details to the PR description to describe the change in this PR, such as how you extend the converter to support dynamic shapes and the tests you added.

@christopherbate

Copy link
Copy Markdown
Contributor Author

@bixia1 I applied changes to address your comments as additional commits for readability, will squash once approved

@christopherbate
christopherbate force-pushed the tf-trt-dynamic-strided-slice branch from efe055d to b8cdd0a Compare September 3, 2021 16:49
@christopherbate

christopherbate commented Sep 3, 2021 •

Copy link
Copy Markdown
Contributor Author

rebased on TOT and fixed preprocessor guard issue

@christopherbate

Copy link
Copy Markdown
Contributor Author

I added a commit to completely remove usage of TensorRT ISelectLayer as it is not supported with static version of TensorRT. Was able to work around it by replacing implementation of NonZero and AbsInt operations with slightly different implementations.

#include "tensorflow/core/platform/status.h"
#include "third_party/tensorrt/NvInfer.h"

// Use this macro within functions which return a Status or StatusOR<T> to check

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.

s/which/that/

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.

Fixed.

return errors::Internal(__FILE__, ":", __LINE__, " TRT_ENSURE failure"); \
}

// Check that a Status or StatusOr<T> object does not carry an error message.

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.

s/Check/Checks/


// Check that a Status or StatusOr<T> object does not carry an error message.
// For pointer-containing StatusOr<T*>, use the below TRT_ENSURE_PTR_OK macro.
// If it does have an error, return an errors::Internal instance

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.

s/return/returns/

}

// Check that a Status or StatusOr<T> object does not carry an error message.
// For pointer-containing StatusOr<T*>, use the below TRT_ENSURE_PTR_OK macro.

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 move this line to the end of the comment block. Otherwise, the meaning of "it" in the the following sentence is not clear.

x.status().ToString()); \
}

// Check that a StatusOr<T*>object does not carry an error, and that the

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.

s/Check/Checks/

this->Constant<int32>(-1, input->getDimensions().nbDims);
TRT_ENSURE_PTR_OK(neg_one);

// Make all negaitve elements into -1, positive and zero elements

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.

s/Make/Turn/

this->Max(input, (*neg_one)->getOutput(0));
TRT_ENSURE_PTR_OK(max);

// Make all positive elements into +1, negative and zero elements

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.

s/Make/Turn/

// Adds an Add operation to the network.
StatusOr<nvinfer1::IElementWiseLayer*> Add(nvinfer1::ITensor* lhs,
nvinfer1::ITensor* rhs) noexcept {
TRT_ENSURE(network_);

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.

My previous was shown as outdated, copied it back to here
Can we avoid TRT_EXPECT(network_) in all the functions that use it by making
"network_" "nvinfer1::INetworkDefinition* const network_" and only check it in the constructor?

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.

I don't think that check provides the same ability to catch mis-use of this class. It's definitely possible for the pointer to become null as it is not owned by the class.

@bixia1 bixia1 Sep 15, 2021 •

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.

Sorry I don't understand what you are talking.
When we create TRTNetworkBuilder build(x, y);
build->network_ will get a copy of the pointer value and will stay the same unless a method of TRTNetworkBuilder modifies it. If we can make "nvinfer1::INetworkDefinition* const network_", we can kind of prevent this modification from happening.
Can you explain how build->network_ becomes null?

@christopherbate christopherbate Sep 21, 2021 •

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.

Fixed, had a brain fart 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.

To test nullity of INetworkDefinition at construction, I made constructor private and added a public Create method which returns a StatusOr<TRTNetworkBuilder>

@bixia1 bixia1 Sep 27, 2021 •

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.

Sorry, I don't see the change you said you have made. The constructor is still public, and I don't see the Create method. Also, all builder routines, such as Add and Min, still have the check. Did I miss something? @

// functions, so this function multiplies the input by a zero value to side
// step knowing the shape. For static inputs, this can be optimized using one
// of the TRTNetworkBuilder::Constant function instead.
StatusOr<nvinfer1::IElementWiseLayer*> ZerosLikeDyamic(

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 couldn't find any use of the routine.
Dynamic is mis-spelled as Dyamic.

This routine get a tensor with the same shape as the input and with value 0. It is hard to correlate its name ZerosLikeDynamic with its function. Maybe something like "ZerosWithShape"?

@christopherbate

Copy link
Copy Markdown
Contributor Author

I went through once again and have pushed fixes for all comments minus the comment about scoping where I have asked for clarification on the style/rules.

@christopherbate
christopherbate force-pushed the tf-trt-dynamic-strided-slice branch from faf3f3e to 9a69cc6 Compare September 28, 2021 03:23
@gbaned
gbaned requested review from bixia1 and removed request for bixia1 September 28, 2021 13:22
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Sep 28, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Sep 28, 2021
@christopherbate

Copy link
Copy Markdown
Contributor Author

Googler refactored int64's to int64_t's, creating conflict just now. Do I need to rebase or do you guys handle this internal?

@bixia1

bixia1 commented Sep 28, 2021

Copy link
Copy Markdown
Contributor

Please rebase.

@christopherbate
christopherbate force-pushed the tf-trt-dynamic-strided-slice branch from 9a69cc6 to 2b851df Compare September 28, 2021 20:37
@google-ml-butler google-ml-butler Bot removed the ready to pull PR ready for merge process label Sep 28, 2021
@christopherbate

Copy link
Copy Markdown
Contributor Author

rebased

@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Sep 28, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Sep 28, 2021
@gbaned gbaned removed awaiting review Pull request awaiting review ready to pull PR ready for merge process labels Sep 29, 2021
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Sep 29, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Sep 29, 2021
@copybara-service
copybara-service Bot merged commit 050940d into tensorflow:master Sep 30, 2021
@google-ml-butler google-ml-butler Bot removed the ready to pull PR ready for merge process label Sep 30, 2021
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 size:L CL Change Size: Large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants