-
Notifications
You must be signed in to change notification settings - Fork 364
feat: support conv dynamo converter #2252
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
activation, | ||
cast, | ||
condition, | ||
conv, | ||
elementwise, | ||
embedding, | ||
matmul, | ||
|
This file contains hidden or 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,129 @@ | ||
from typing import Optional, Sequence, Union | ||
|
||
import numpy as np | ||
|
||
# @manual=//deeplearning/trt/python:py_tensorrt | ||
import tensorrt as trt | ||
import torch | ||
from torch.fx.node import Target | ||
from torch_tensorrt.dynamo.conversion import impl | ||
from torch_tensorrt.dynamo.conversion.converter_utils import extend_attr_to_tuple | ||
from torch_tensorrt.fx.converters.converter_utils import ( | ||
SourceIR, | ||
get_dyn_range, | ||
get_trt_tensor, | ||
has_dynamic_shape, | ||
mark_as_int8_layer, | ||
set_layer_name, | ||
to_numpy, | ||
) | ||
from torch_tensorrt.fx.types import TRTNetwork, TRTTensor | ||
|
||
|
||
def convNd( | ||
network: TRTNetwork, | ||
target: Union[Target, str], | ||
source_ir: Optional[SourceIR], | ||
name: str, | ||
is_conv1d: bool, | ||
input: TRTTensor, | ||
weight: Union[TRTTensor, torch.Tensor], | ||
bias: Optional[Union[TRTTensor, torch.Tensor]], | ||
stride: Optional[Union[int, Sequence[int]]], | ||
padding: Optional[Union[int, Sequence[int]]], | ||
dilation: Optional[Union[int, Sequence[int]]], | ||
groups: Optional[int], | ||
scale: Optional[Union[torch.Tensor, float]] = None, | ||
zero_point: Optional[Union[torch.Tensor, float]] = None, | ||
) -> TRTTensor: | ||
if has_dynamic_shape(input.shape): | ||
assert input.shape[1] != -1, "Channel dim can't be dynamic for convolution." | ||
|
||
if is_conv1d: | ||
# Apply an unsqueeze operation to transform the conv1d problem into conv2d | ||
input = impl.unsqueeze.unsqueeze( | ||
network, target, source_ir, name + "_unsqueeze_conv1d", input, -1 | ||
) | ||
|
||
# Process bias terms | ||
if isinstance(bias, (torch.Tensor, np.ndarray)): | ||
# Transform the bias constant into a Numpy array | ||
bias = to_numpy(bias) | ||
|
||
elif isinstance(bias, TRTTensor): | ||
bias = get_trt_tensor(network, bias, f"{name}_bias") | ||
|
||
elif bias is not None: | ||
raise RuntimeError( | ||
f"Convolution {name} has bias of type {type(bias)}, Expected Torch Tensor or TRT Tensor" | ||
) | ||
|
||
# Process weight terms | ||
if network.has_explicit_precision or isinstance(weight, TRTTensor): | ||
weight = get_trt_tensor(network, weight, f"{name}_weight") | ||
# Append new dimension (unsqueeze) if the convolution is 1d | ||
if is_conv1d: | ||
input = impl.unsqueeze.unsqueeze( | ||
network, target, source_ir, name + "_unsqueeze_weight", weight, -1 | ||
) | ||
|
||
elif isinstance(weight, (torch.Tensor, np.ndarray)): | ||
# Transform the weight constant into a Numpy array | ||
weight = to_numpy(weight) | ||
|
||
# Append new dimension (unsqueeze) if the convolution is 1d | ||
if is_conv1d: | ||
weight = np.expand_dims(weight, -1) | ||
|
||
else: | ||
raise RuntimeError( | ||
f"Convolution {name} has weight of type {type(weight)}, Expect Optional[Tensor]" | ||
) | ||
|
||
# add conv layer | ||
conv_layer = network.add_convolution_nd( | ||
input=input, | ||
num_output_maps=weight.shape[0], | ||
kernel_shape=weight.shape[2:], | ||
kernel=trt.Weights() if isinstance(weight, TRTTensor) else weight, | ||
bias=trt.Weights() if isinstance(bias, TRTTensor) else bias, | ||
) | ||
|
||
# If the weight is a TRTTensor, set it as an input of the layer | ||
if isinstance(weight, TRTTensor): | ||
conv_layer.set_input(1, weight) | ||
|
||
# If the bias is a TRTTensor, set it as an input of the layer | ||
if isinstance(bias, TRTTensor): | ||
conv_layer.set_input(2, bias) | ||
|
||
# Expand parameters manually for Conv1D computations | ||
if is_conv1d: | ||
padding = tuple(padding) + (0,) | ||
stride = extend_attr_to_tuple(stride, 2) | ||
dilation = extend_attr_to_tuple(dilation, 2) | ||
|
||
set_layer_name(conv_layer, target, name, source_ir) | ||
|
||
# Set relevant attributes of convolution layer | ||
conv_layer.padding_nd = padding | ||
conv_layer.stride_nd = stride | ||
conv_layer.dilation_nd = dilation | ||
|
||
if groups is not None: | ||
conv_layer.num_groups = groups | ||
|
||
# Handle quantization cases | ||
if scale is not None and zero_point is not None: | ||
# Assume the dtype of activation is torch.quint8 | ||
mark_as_int8_layer(conv_layer, get_dyn_range(scale, zero_point, torch.quint8)) | ||
|
||
result = conv_layer.get_output(0) | ||
|
||
if is_conv1d: | ||
# Apply a squeeze operation to transform the conv2d problem back into conv1d | ||
result = impl.squeeze.squeeze( | ||
network, target, source_ir, name + "_squeeze_conv1d", result, -1 | ||
) | ||
|
||
return result |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.