|
| 1 | +from typing import Optional, Sequence, Union |
| 2 | + |
| 3 | +import tensorrt as trt |
| 4 | +from torch.fx.node import Target |
| 5 | +from torch_tensorrt.dynamo._SourceIR import SourceIR |
| 6 | +from torch_tensorrt.dynamo.conversion._ConversionContext import ConversionContext |
| 7 | +from torch_tensorrt.dynamo.conversion.converter_utils import get_trt_tensor |
| 8 | +from torch_tensorrt.fx.converters.converter_utils import ( |
| 9 | + has_dynamic_shape, |
| 10 | + set_layer_name, |
| 11 | +) |
| 12 | +from torch_tensorrt.fx.types import TRTTensor |
| 13 | + |
| 14 | +""" |
| 15 | +Note: IPaddingLayer is deprecated in TensorRT 8.2 and will be removed in TensorRT 10.0. |
| 16 | +Use ISliceLayer to pad the tensor, which supports new non-constant, reflects padding |
| 17 | +mode and clamp, and supports padding output with dynamic shape. |
| 18 | +""" |
| 19 | + |
| 20 | + |
| 21 | +def constant_padNd( |
| 22 | + ctx: ConversionContext, |
| 23 | + target: Union[Target, str], |
| 24 | + source_ir: Optional[SourceIR], |
| 25 | + name: str, |
| 26 | + input: TRTTensor, |
| 27 | + pad: Sequence[int], |
| 28 | + value: Union[int, float] = 0, |
| 29 | +) -> TRTTensor: |
| 30 | + if has_dynamic_shape(input.shape): |
| 31 | + assert input.shape[1] != -1, "Channel dim can't be dynamic for padding." |
| 32 | + |
| 33 | + rank = len(input.shape) |
| 34 | + |
| 35 | + if len(pad) // 2 > rank: |
| 36 | + raise RuntimeError( |
| 37 | + f"Trying to pad last {len(pad) // 2} dimension but the input only has {rank} dimension." |
| 38 | + ) |
| 39 | + |
| 40 | + start_list = [0] * rank |
| 41 | + new_shape = list(input.shape) |
| 42 | + |
| 43 | + for i in range(0, len(pad) // 2): |
| 44 | + start_list[-i - 1] = -pad[i * 2] |
| 45 | + new_shape[-i - 1] += pad[i * 2] + pad[i * 2 + 1] |
| 46 | + |
| 47 | + stride_list = [1] * rank |
| 48 | + layer = ctx.net.add_slice( |
| 49 | + input, |
| 50 | + start=tuple(start_list), |
| 51 | + shape=tuple(new_shape), |
| 52 | + stride=tuple(stride_list), |
| 53 | + ) |
| 54 | + value_const = get_trt_tensor(ctx, value, f"{name}_value", input.dtype) |
| 55 | + layer.set_input(4, value_const) |
| 56 | + layer.mode = trt.SliceMode.FILL |
| 57 | + |
| 58 | + set_layer_name(layer, target, name, source_ir) |
| 59 | + return layer.get_output(0) |
| 60 | + |
| 61 | + |
| 62 | +def reflection_padNd( |
| 63 | + ctx: ConversionContext, |
| 64 | + target: Union[Target, str], |
| 65 | + source_ir: Optional[SourceIR], |
| 66 | + name: str, |
| 67 | + input: TRTTensor, |
| 68 | + padding: Sequence[int], |
| 69 | +) -> TRTTensor: |
| 70 | + if has_dynamic_shape(input.shape): |
| 71 | + assert input.shape[1] != -1, "Channel dim can't be dynamic for padding." |
| 72 | + |
| 73 | + rank = len(input.shape) |
| 74 | + |
| 75 | + if len(padding) // 2 > rank: |
| 76 | + raise RuntimeError( |
| 77 | + f"Trying to pad last {len(padding) // 2} dimension but the input only has {rank} dimension." |
| 78 | + ) |
| 79 | + |
| 80 | + start_list = [0] * rank |
| 81 | + new_shape = list(input.shape) |
| 82 | + |
| 83 | + for i in range(0, len(padding) // 2): |
| 84 | + start_list[-i - 1] = -padding[i * 2] |
| 85 | + new_shape[-i - 1] += padding[i * 2] + padding[i * 2 + 1] |
| 86 | + |
| 87 | + stride_list = [1] * rank |
| 88 | + layer = ctx.net.add_slice( |
| 89 | + input, |
| 90 | + start=tuple(start_list), |
| 91 | + shape=tuple(new_shape), |
| 92 | + stride=tuple(stride_list), |
| 93 | + ) |
| 94 | + layer.mode = trt.SliceMode.REFLECT |
| 95 | + |
| 96 | + set_layer_name(layer, target, name, source_ir) |
| 97 | + return layer.get_output(0) |
| 98 | + |
| 99 | + |
| 100 | +def replication_padNd( |
| 101 | + ctx: ConversionContext, |
| 102 | + target: Union[Target, str], |
| 103 | + source_ir: Optional[SourceIR], |
| 104 | + name: str, |
| 105 | + input: TRTTensor, |
| 106 | + padding: Sequence[int], |
| 107 | +) -> TRTTensor: |
| 108 | + if has_dynamic_shape(input.shape): |
| 109 | + assert input.shape[1] != -1, "Channel dim can't be dynamic for padding." |
| 110 | + |
| 111 | + rank = len(input.shape) |
| 112 | + |
| 113 | + if len(padding) // 2 > rank: |
| 114 | + raise RuntimeError( |
| 115 | + f"Trying to pad last {len(padding) // 2} dimension but the input only has {rank} dimension." |
| 116 | + ) |
| 117 | + |
| 118 | + start_list = [0] * rank |
| 119 | + new_shape = list(input.shape) |
| 120 | + |
| 121 | + for i in range(0, len(padding) // 2): |
| 122 | + start_list[-i - 1] = -padding[i * 2] |
| 123 | + new_shape[-i - 1] += padding[i * 2] + padding[i * 2 + 1] |
| 124 | + |
| 125 | + stride_list = [1] * rank |
| 126 | + layer = ctx.net.add_slice( |
| 127 | + input, |
| 128 | + start=tuple(start_list), |
| 129 | + shape=tuple(new_shape), |
| 130 | + stride=tuple(stride_list), |
| 131 | + ) |
| 132 | + layer.mode = trt.SliceMode.CLAMP |
| 133 | + |
| 134 | + set_layer_name(layer, target, name, source_ir) |
| 135 | + return layer.get_output(0) |
| 136 | + |
| 137 | + |
| 138 | +def circular_padNd( |
| 139 | + ctx: ConversionContext, |
| 140 | + target: Union[Target, str], |
| 141 | + source_ir: Optional[SourceIR], |
| 142 | + name: str, |
| 143 | + input: TRTTensor, |
| 144 | + pad: Sequence[int], |
| 145 | +) -> TRTTensor: |
| 146 | + if has_dynamic_shape(input.shape): |
| 147 | + assert input.shape[1] != -1, "Channel dim can't be dynamic for padding." |
| 148 | + |
| 149 | + rank = len(input.shape) |
| 150 | + |
| 151 | + if len(pad) // 2 > rank: |
| 152 | + raise RuntimeError( |
| 153 | + f"Trying to pad last {len(pad) // 2} dimension but the input only has {rank} dimension." |
| 154 | + ) |
| 155 | + |
| 156 | + start_list = [0] * rank |
| 157 | + new_shape = list(input.shape) |
| 158 | + |
| 159 | + for i in range(0, len(pad) // 2): |
| 160 | + start_list[-i - 1] = -pad[i * 2] |
| 161 | + new_shape[-i - 1] += pad[i * 2] + pad[i * 2 + 1] |
| 162 | + |
| 163 | + stride_list = [1] * rank |
| 164 | + layer = ctx.net.add_slice( |
| 165 | + input, |
| 166 | + start=tuple(start_list), |
| 167 | + shape=tuple(new_shape), |
| 168 | + stride=tuple(stride_list), |
| 169 | + ) |
| 170 | + layer.mode = trt.SliceMode.WRAP |
| 171 | + |
| 172 | + set_layer_name(layer, target, name, source_ir) |
| 173 | + return layer.get_output(0) |
| 174 | + |
| 175 | + |
| 176 | +def pad( |
| 177 | + ctx: ConversionContext, |
| 178 | + target: Union[Target, str], |
| 179 | + source_ir: Optional[SourceIR], |
| 180 | + name: str, |
| 181 | + input: TRTTensor, |
| 182 | + pad: Sequence[int], |
| 183 | + mode: str = "constant", |
| 184 | + value: Optional[float] = None, |
| 185 | +) -> TRTTensor: |
| 186 | + if mode == "constant": |
| 187 | + return constant_padNd( |
| 188 | + ctx, |
| 189 | + target, |
| 190 | + source_ir, |
| 191 | + f"{name}_{mode}", |
| 192 | + input, |
| 193 | + pad, |
| 194 | + value if value is not None else 0, |
| 195 | + ) |
| 196 | + elif mode == "reflect": |
| 197 | + return reflection_padNd(ctx, target, source_ir, f"{name}_{mode}", input, pad) |
| 198 | + elif mode == "replicate": |
| 199 | + return replication_padNd(ctx, target, source_ir, f"{name}_{mode}", input, pad) |
| 200 | + elif mode == "circular": |
| 201 | + return circular_padNd(ctx, target, source_ir, f"{name}_{mode}", input, pad) |
| 202 | + else: |
| 203 | + raise RuntimeError( |
| 204 | + f'We currently only support for `mode` in ["constant", "reflect", "replicate", "circular"], but got {mode}' |
| 205 | + ) |
0 commit comments