-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
2 deletions.
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
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,25 @@ | ||
from torch2trt_dynamic.plugins import create_torchunfold_plugin | ||
from torch2trt_dynamic.torch2trt_dynamic import (get_arg, tensorrt_converter, | ||
trt_) | ||
|
||
|
||
@tensorrt_converter('torch.nn.functional.unfold') | ||
def convert_unfold(ctx): | ||
input = ctx.method_args[0] | ||
kernel_size = get_arg(ctx, 'kernel_size', pos=1, default=0) | ||
dilation = get_arg(ctx, 'dilation', pos=2, default=1) | ||
padding = get_arg(ctx, 'padding', pos=3, default=0) | ||
stride = get_arg(ctx, 'stride', pos=4, default=1) | ||
output = ctx.method_return | ||
input_trt = trt_(ctx.network, input) | ||
|
||
plugin = create_torchunfold_plugin( | ||
'unfold_' + str(id(input)), | ||
kernel_size=kernel_size, | ||
dilation=dilation, | ||
padding=padding, | ||
stride=stride) | ||
|
||
layer = ctx.network.add_plugin_v2(inputs=[input_trt], plugin=plugin) | ||
|
||
output._trt = layer.get_output(0) |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import numpy as np | ||
import tensorrt as trt | ||
|
||
|
||
def create_torchunfold_plugin(layer_name, kernel_size, dilation, padding, | ||
stride): | ||
|
||
creator = trt.get_plugin_registry().get_plugin_creator( | ||
'TorchUnfoldPluginDynamic', '1', '') | ||
|
||
pfc = trt.PluginFieldCollection() | ||
|
||
if isinstance(kernel_size, int): | ||
kernel_size = (kernel_size, kernel_size) | ||
pf_kernel_size = trt.PluginField('kernel_size', | ||
np.array(kernel_size, dtype=np.int32), | ||
trt.PluginFieldType.INT32) | ||
pfc.append(pf_kernel_size) | ||
|
||
if isinstance(dilation, int): | ||
dilation = (dilation, dilation) | ||
pf_dilation = trt.PluginField('dilation', | ||
np.array(dilation, dtype=np.int32), | ||
trt.PluginFieldType.INT32) | ||
pfc.append(pf_dilation) | ||
|
||
if isinstance(padding, int): | ||
padding = (padding, padding) | ||
pf_padding = trt.PluginField('padding', np.array(padding, dtype=np.int32), | ||
trt.PluginFieldType.INT32) | ||
pfc.append(pf_padding) | ||
|
||
if isinstance(stride, int): | ||
stride = (stride, stride) | ||
pf_stride = trt.PluginField('stride', np.array(stride, dtype=np.int32), | ||
trt.PluginFieldType.INT32) | ||
pfc.append(pf_stride) | ||
|
||
return creator.create_plugin(layer_name, pfc) |