-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support 1D, 2D, and 3D avg and max pooling dynamo converters #2317
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
139ad9b
feat: support 1D, 2D, and 3D avg and max pooling dynamo converters
zewenli98 8e89105
Merge branch 'pytorch:main' into pool_dynamo_converters
zewenli98 1ddd88f
fix bugs and add 1d support
zewenli98 767e8fb
Merge branch 'pool_dynamo_converters' of https://github.com/zewenli98…
zewenli98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
matmul, | ||
normalization, | ||
permutation, | ||
pool, | ||
reduce, | ||
select, | ||
shape, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from typing import Optional, Sequence, Union | ||
|
||
import tensorrt as trt | ||
from torch.fx.node import Target | ||
from torch_tensorrt.dynamo._SourceIR import SourceIR | ||
from torch_tensorrt.dynamo.conversion.converter_utils import extend_attr_to_tuple | ||
from torch_tensorrt.fx.converters.converter_utils import ( | ||
has_dynamic_shape, | ||
set_layer_name, | ||
) | ||
from torch_tensorrt.fx.types import TRTNetwork, TRTTensor | ||
|
||
|
||
def avg_poolNd( | ||
network: TRTNetwork, | ||
target: Union[Target, str], | ||
source_ir: Optional[SourceIR], | ||
name: str, | ||
input: TRTTensor, | ||
kernel_size: Sequence[int], | ||
stride: Union[int, Sequence[int]], | ||
padding: Union[int, Sequence[int]] = 0, | ||
ceil_mode: bool = False, | ||
count_include_pad: bool = True, | ||
divisor_override: Optional[int] = None, | ||
) -> TRTTensor: | ||
if has_dynamic_shape(input.shape): | ||
assert input.shape[1] != -1, "Channel dim can't be dynamic for pooling." | ||
|
||
if ceil_mode is not False: | ||
raise RuntimeError("ceil_mode is not yet supported!") | ||
|
||
if divisor_override is not None: | ||
raise RuntimeError("divisor_override is not yet supported!") | ||
|
||
dim = len(kernel_size) | ||
|
||
kernel_size = extend_attr_to_tuple(kernel_size, dim) | ||
|
||
if stride == []: | ||
stride = kernel_size | ||
else: | ||
stride = extend_attr_to_tuple(stride, dim) | ||
|
||
padding = extend_attr_to_tuple(padding, dim) | ||
|
||
# add average pooling layer | ||
pool_layer = network.add_pooling_nd( | ||
input=input, | ||
type=trt.PoolingType.AVERAGE, | ||
window_size=kernel_size, | ||
) | ||
|
||
pool_layer.stride_nd = stride | ||
pool_layer.padding_nd = padding | ||
pool_layer.average_count_excludes_padding = not count_include_pad | ||
|
||
set_layer_name(pool_layer, target, name, source_ir) | ||
return pool_layer.get_output(0) | ||
|
||
|
||
def max_poolNd( | ||
network: TRTNetwork, | ||
target: Union[Target, str], | ||
source_ir: Optional[SourceIR], | ||
name: str, | ||
input: TRTTensor, | ||
kernel_size: Sequence[int], | ||
stride: Union[int, Sequence[int]], | ||
padding: Union[int, Sequence[int]] = 0, | ||
dilation: Union[int, Sequence[int]] = 1, | ||
ceil_mode: bool = False, | ||
) -> TRTTensor: | ||
if has_dynamic_shape(input.shape): | ||
assert input.shape[1] != -1, "Channel dim can't be dynamic for pooling." | ||
|
||
if dilation != 1: | ||
raise RuntimeError("dilation is not yet supported!") | ||
|
||
if ceil_mode is not False: | ||
raise RuntimeError("ceil_mode is not yet supported!") | ||
|
||
dim = len(kernel_size) | ||
|
||
kernel_size = extend_attr_to_tuple(kernel_size, dim) | ||
|
||
if stride == []: | ||
stride = kernel_size | ||
else: | ||
stride = extend_attr_to_tuple(stride, dim) | ||
|
||
padding = extend_attr_to_tuple(padding, dim) | ||
|
||
# add max pooling layer | ||
pool_layer = network.add_pooling_nd( | ||
input=input, | ||
type=trt.PoolingType.MAX, | ||
window_size=kernel_size, | ||
) | ||
|
||
pool_layer.stride_nd = stride | ||
pool_layer.padding_nd = padding | ||
|
||
set_layer_name(pool_layer, target, name, source_ir) | ||
return pool_layer.get_output(0) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could
max_pool1d
support be added here as well? SchemaThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add
torch.ops.aten.max_pool1d.default
but it won't be used. Even fortorch.nn.AvgPool1d
, it still callstorch.ops.aten.avg_pool2d.default
, as you can see in the test file: https://github.com/pytorch/TensorRT/pull/2317/files#diff-9fce39bc42c66d2866c41665779cab7da0a4d3fe54576925e2b66c17a1cf1ebfR20-R43But anyways, the 1d schema looks same as others, so I added here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for that - I plan to add a lowering pass which will lead us to that converter, so it will still be helpful.