tensorrt_custom_ops.md 14.7 KB
Newer Older
1
## TensorRT自定义算子
Zaida Zhou's avatar
Zaida Zhou committed
2

3
4
<!-- TOC -->

5
- [TensorRT自定义算子](#tensorrt%E8%87%AA%E5%AE%9A%E4%B9%89%E7%AE%97%E5%AD%90)
6
  - [MMCVRoIAlign](#mmcvroialign)
7
8
9
10
11
    - [描述](#%E6%8F%8F%E8%BF%B0)
    - [模型参数](#%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0)
    - [输入](#%E8%BE%93%E5%85%A5)
    - [输出](#%E8%BE%93%E5%87%BA)
    - [类型约束](#%E7%B1%BB%E5%9E%8B%E7%BA%A6%E6%9D%9F)
12
  - [ScatterND](#scatternd)
13
14
15
16
17
    - [描述](#%E6%8F%8F%E8%BF%B0-1)
    - [模型参数](#%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0-1)
    - [输入](#%E8%BE%93%E5%85%A5-1)
    - [输出](#%E8%BE%93%E5%87%BA-1)
    - [类型约束](#%E7%B1%BB%E5%9E%8B%E7%BA%A6%E6%9D%9F-1)
18
  - [NonMaxSuppression](#nonmaxsuppression)
19
20
21
22
23
    - [描述](#%E6%8F%8F%E8%BF%B0-2)
    - [模型参数](#%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0-2)
    - [输入](#%E8%BE%93%E5%85%A5-2)
    - [输出](#%E8%BE%93%E5%87%BA-2)
    - [类型约束](#%E7%B1%BB%E5%9E%8B%E7%BA%A6%E6%9D%9F-2)
24
  - [MMCVDeformConv2d](#mmcvdeformconv2d)
25
26
27
28
29
    - [描述](#%E6%8F%8F%E8%BF%B0-3)
    - [模型参数](#%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0-3)
    - [输入](#%E8%BE%93%E5%85%A5-3)
    - [输出](#%E8%BE%93%E5%87%BA-3)
    - [类型约束](#%E7%B1%BB%E5%9E%8B%E7%BA%A6%E6%9D%9F-3)
30
  - [grid_sampler](#grid_sampler)
31
32
33
34
35
    - [描述](#%E6%8F%8F%E8%BF%B0-4)
    - [模型参数](#%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0-4)
    - [输入](#%E8%BE%93%E5%85%A5-4)
    - [输出](#%E8%BE%93%E5%87%BA-4)
    - [类型约束](#%E7%B1%BB%E5%9E%8B%E7%BA%A6%E6%9D%9F-4)
36
  - [cummax](#cummax)
37
38
39
40
41
    - [描述](#%E6%8F%8F%E8%BF%B0-5)
    - [模型参数](#%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0-5)
    - [输入](#%E8%BE%93%E5%85%A5-5)
    - [输出](#%E8%BE%93%E5%87%BA-5)
    - [类型约束](#%E7%B1%BB%E5%9E%8B%E7%BA%A6%E6%9D%9F-5)
42
  - [cummin](#cummin)
43
44
45
46
47
    - [描述](#%E6%8F%8F%E8%BF%B0-6)
    - [模型参数](#%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0-6)
    - [输入](#%E8%BE%93%E5%85%A5-6)
    - [输出](#%E8%BE%93%E5%87%BA-6)
    - [类型约束](#%E7%B1%BB%E5%9E%8B%E7%BA%A6%E6%9D%9F-6)
48
  - [MMCVInstanceNormalization](#mmcvinstancenormalization)
49
50
51
52
53
    - [描述](#%E6%8F%8F%E8%BF%B0-7)
    - [模型参数](#%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0-7)
    - [输入](#%E8%BE%93%E5%85%A5-7)
    - [输出](#%E8%BE%93%E5%87%BA-7)
    - [类型约束](#%E7%B1%BB%E5%9E%8B%E7%BA%A6%E6%9D%9F-7)
54
  - [MMCVModulatedDeformConv2d](#mmcvmodulateddeformconv2d)
55
56
57
58
59
    - [描述](#%E6%8F%8F%E8%BF%B0-8)
    - [模型参数](#%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0-8)
    - [输入](#%E8%BE%93%E5%85%A5-8)
    - [输出](#%E8%BE%93%E5%87%BA-8)
    - [类型约束](#%E7%B1%BB%E5%9E%8B%E7%BA%A6%E6%9D%9F-8)
60
61
62
63
64
65
66
67
68
69
70

<!-- TOC -->

### MMCVRoIAlign

#### 描述

在特征图上计算RoIAlign,在多数双阶段目标检测模型的bbox_head中使用

#### 模型参数

71
72
73
74
75
76
77
| 类型      | 参数名              | 描述                                  |
| ------- | ---------------- | ----------------------------------- |
| `int`   | `output_height`  | roi特征的输出高度                          |
| `int`   | `output_width`   | roi特征的输出宽度                          |
| `float` | `spatial_scale`  | 输入检测框的缩放系数                          |
| `int`   | `sampling_ratio` | 输出的采样率。`0`表示使用密集采样                  |
| `str`   | `mode`           | 池化方式。 `avg``max`                   |
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
| `int`   | `aligned`        | 如果`aligned=1`,则像素会进行-0.5的偏移以达到更好的对齐 |

#### 输入

<dl>
<dt><tt>inputs[0]</tt>: T</dt>
<dd>输入特征图;形状为(N, C, H, W)的四维张量,其中N为batch大小,C为输入通道数,H和W为输入特征图的高和宽。</dd>
<dt><tt>inputs[1]</tt>: T</dt>
<dd>需要进行池化的感兴趣区域;形状为(num_rois, 5)的二维张量,内容为[[batch_index, x1, y1, x2, y2], ...]。rois的坐标为输入特征图的坐标系。</dd>
</dl>

#### 输出

<dl>
<dt><tt>outputs[0]</tt>: T</dt>
<dd>池化的输出;形状为(num_rois, C, output_height, output_width)的四维张量。每个输出特征feat[i]都与输入感兴趣区域rois[i]一一对应。<dd>
</dl>
#### 类型约束

- T:tensor(float32, Linear)

### ScatterND

#### 描述

103
ScatterND接收三个输入,分别为秩为r >= 1的`data`,秩为q >= 1的`indices`以及秩为 q + r - indices.shape\[-1\] -1 的`update`。输出的计算方式为:首先创建一个`data`的拷贝,然后根据`indces`的值使用`update`对拷贝的`data`进行更新。注意`indices`中不应该存在相同的条目,也就是说对同一个位置进行一次以上的更新是不允许的。
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

输出的计算方式可以参考如下代码:

```python
  output = np.copy(data)
  update_indices = indices.shape[:-1]
  for idx in np.ndindex(update_indices):
      output[indices[idx]] = updates[idx]
```

#### 模型参数



#### 输入

<dl>
<dt><tt>inputs[0]</tt>: T</dt>
<dd>秩为r >= 1的输入`data`</dd>

<dt><tt>inputs[1]</tt>: tensor(int32, Linear)</dt>
<dd>秩为q >= 1的输入`update`</dd>

<dt><tt>inputs[2]</tt>: T</dt>
<dd>秩为 q + r - indices.shape[-1] -1 的输入`update`</dd>
</dl>

#### 输出

<dl>
<dt><tt>outputs[0]</tt>: T</dt>
<dd>秩为r >= 1的输出张量</dd>
</dl>

#### 类型约束

- T:tensor(float32, Linear), tensor(int32, Linear)

### NonMaxSuppression

#### 描述

根据IoU阈值对候选框进行非极大值抑制。

#### 模型参数

150
151
152
153
154
155
156
| 类型      | 参数名                          | 描述                                                                            |
| ------- | ---------------------------- | ----------------------------------------------------------------------------- |
| `int`   | `center_point_box`           | 0 - 候选框的格式为\[y1, x1, y2, x2\], 1-候选框的格式为\[x_center, y_center, width, height\] |
| `int`   | `max_output_boxes_per_class` | 每一类最大的输出检测框个数。默认为0,输出检测框个数等于输入候选框数                                            |
| `float` | `iou_threshold`              | 用来判断候选框重合度的阈值,取值范围\[0, 1\]。默认值为0                                              |
| `float` | `score_threshold`            | 用来判断候选框是否合法的阈值                                                                |
| `int`   | `offset`                     | 检测框长宽计算方式为(x2 - x1 + offset),可选值0或1                                           |
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

#### 输入

<dl>
<dt><tt>inputs[0]</tt>: T</dt>
<dd>输入候选框。形状为(num_batches, spatial_dimension, 4)的三维张量</dd>
<dt><tt>inputs[1]</tt>: T</dt>
<dd>输入得分。形状为(num_batches, num_classes, spatial_dimension)的三维张量</dd>
</dl>

#### 输出

<dl>
<dt><tt>outputs[0]</tt>: tensor(int32, Linear)</dt>
<dd>被选中的候选框索引。形状为(num_selected_indices, 3)的二维张量。每一行内容为[batch_index, class_index, box_index]。</dd>
<dd>其中 num_selected_indices=num_batches* num_classes* min(max_output_boxes_per_class, spatial_dimension)。</dd>
<dd>所有未被选中的候选框索引都会被填充为-1</dd>
</dl>

#### 类型约束

- T:tensor(float32, Linear)

### MMCVDeformConv2d

#### 描述

在输入特征上计算Deformable Convolution,请阅读[Deformable Convolutional Network](https://arxiv.org/abs/1703.06211)了解更多细节。

#### 模型参数

188
189
190
191
192
193
194
| 类型             | 参数名                | 描述                                                      |
| -------------- | ------------------ | ------------------------------------------------------- |
| `list of ints` | `stride`           | 卷积的步长 (sH, sW)                                          |
| `list of ints` | `padding`          | 输入特征填充大小 (padH, padW)                                   |
| `list of ints` | `dilation`         | 卷积核各元素间隔 (dH, dW)                                       |
| `int`          | `deformable_group` | 可变偏移量的分组                                                |
| `int`          | `group`            | 卷积分组数,`input_channel`会根据这个值被分为数个分组进行计算                  |
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
| `int`          | `im2col_step`      | 可变卷积使用im2col计算卷积。输入与偏移量会以im2col_step为步长分块计算,减少临时空间的使用量。 |

#### 输入

<dl>
<dt><tt>inputs[0]</tt>: T</dt>
<dd>输入特征;形状为(N, C, inH, inW)的四维张量,其中N为batch大小,C为输入通道数,inH和inW为输入特征图的高和宽</dd>
<dt><tt>inputs[1]</tt>: T</dt>
<dd>输入偏移量;形状为(N, deformable_group* 2* kH* kW, outH, outW)的四维张量,kH和kW为输入特征图的高和宽,outH和outW为输入特征图的高和宽</dd>
<dt><tt>inputs[2]</tt>: T</dt>
<dd>输入权重;形状为(output_channel, input_channel, kH, kW)的四维张量</dd>
</dl>

#### 输出

<dl>
<dt><tt>outputs[0]</tt>: T</dt>
<dd>输出特征;形状为(N, output_channel, outH, outW)的四维张量</dd>
</dl>

#### 类型约束

- T:tensor(float32, Linear)

### grid_sampler

#### 描述

根据`grid`的像素位置对`input`进行网格采样。

#### 模型参数

227
228
229
230
| 类型    | 参数名                  | 描述                                                                                               |
| ----- | -------------------- | ------------------------------------------------------------------------------------------------ |
| `int` | `interpolation_mode` | 计算输出使用的插值模式。(0: `bilinear` , 1: `nearest`)                                                       |
| `int` | `padding_mode`       | 边缘填充模式。(0: `zeros`, 1: `border`, 2: `reflection`)                                                |
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
| `int` | `align_corners`      | 如果`align_corners=1`,则极值(`-1``1`)会被当做输入边缘像素的中心点。如果`align_corners=0`,则它们会被看做是边缘像素的边缘点,减小分辨率对采样的影响 |

#### 输入

<dl>
<dt><tt>inputs[0]</tt>: T</dt>
<dd>输入特征;形状为(N, C, inH, inW)的四维张量,其中N为batch大小,C为输入通道数,inH和inW为输入特征图的高和宽</dd>
<dt><tt>inputs[1]</tt>: T</dt>
<dd>输入网格;形状为(N, outH, outW, 2)的四维张量,outH和outW为输出的高和宽 </dd>
</dl>

#### 输出

<dl>
<dt><tt>outputs[0]</tt>: T</dt>
<dd>输出特征;形状为(N, C, outH, outW)的四维张量</dd>
</dl>

#### 类型约束

- T:tensor(float32, Linear)

### cummax

#### 描述

返回一个元组(`values`, `indices`),其中`values``input``dim`维的累计最大值,`indices`为第`dim`维最大值位置。请阅读[torch.cummax](https://pytorch.org/docs/stable/generated/torch.cummax.html)了解更多细节。

#### 模型参数

261
262
263
| 类型    | 参数名   | 描述        |
| ----- | ----- | --------- |
| `int` | `dim` | 进行累计计算的维度 |
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292

#### 输入

<dl>
<dt><tt>inputs[0]</tt>: T</dt>
<dd>输入张量;可以使任意形状</dd>
</dl>

#### 输出

<dl>
<dt><tt>outputs[0]</tt>: T</dt>
<dd>`input``dim`维的累计最大值,形状与`input`相同。类型和`input`一致</dd>
<dt><tt>outputs[1]</tt>: (int32, Linear)</dt>
<dd>`dim`维最大值位置,形状与`input`相同</dd>
</dl>

#### 类型约束

- T:tensor(float32, Linear)

### cummin

#### 描述

返回一个元组(`values`, `indices`),其中`values``input``dim`维的累计最小值,`indices`为第`dim`维最小值位置。请阅读[torch.cummin](https://pytorch.org/docs/stable/generated/torch.cummin.html)了解更多细节。

#### 模型参数

293
294
295
| 类型    | 参数名   | 描述        |
| ----- | ----- | --------- |
| `int` | `dim` | 进行累计计算的维度 |
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

#### 输入

<dl>
<dt><tt>inputs[0]</tt>: T</dt>
<dd>输入张量;可以使任意形状</dd>
</dl>

#### 输出

<dl>
<dt><tt>outputs[0]</tt>: T</dt>
<dd>`input``dim`维的累计最小值,形状与`input`相同。类型和`input`一致</dd>
<dt><tt>outputs[1]</tt>: (int32, Linear)</dt>
<dd>`dim`维最小值位置,形状与`input`相同</dd>
</dl>

#### 类型约束

- T:tensor(float32, Linear)

### MMCVInstanceNormalization

#### 描述

对特征计算instance normalization,请阅读[Instance Normalization: The Missing Ingredient for Fast Stylization](https://arxiv.org/abs/1607.08022)了解更多详细信息。

#### 模型参数

325
326
| 类型      | 参数名       | 描述                |
| ------- | --------- | ----------------- |
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
| `float` | `epsilon` | 用来避免除0错误。默认为1e-05 |

#### 输入

<dl>
<dt><tt>inputs[0]</tt>: T</dt>
<dd>输入特征。形状为(N, C, H, W)的四维张量,其中N为batch大小,C为输入通道数,H和W为输入特征图的高和宽</dd>
<dt><tt>inputs[1]</tt>: T</dt>
<dd>输入缩放系数。形状为(C,)的一维张量</dd>
<dt><tt>inputs[2]</tt>: T</dt>
<dd>输入偏移量。形状为(C,)的一维张量</dd>
</dl>

#### 输出

<dl>
<dt><tt>outputs[0]</tt>: T</dt>
<dd>输出特征。形状为(N, C, H, W)的四维张量</dd>
</dl>

#### 类型约束

- T:tensor(float32, Linear)

### MMCVModulatedDeformConv2d

#### 描述

在输入特征上计算Modulated Deformable Convolution,请阅读[Deformable ConvNets v2: More Deformable, Better Results](https://arxiv.org/abs/1811.11168?from=timeline)了解更多细节。

#### 模型参数

359
360
361
362
363
364
| 类型             | 参数名                 | 描述                                     |
| -------------- | ------------------- | -------------------------------------- |
| `list of ints` | `stride`            | 卷积的步长 (sH, sW)                         |
| `list of ints` | `padding`           | 输入特征填充大小 (padH, padW)                  |
| `list of ints` | `dilation`          | 卷积核各元素间隔 (dH, dW)                      |
| `int`          | `deformable_groups` | 可变偏移量的分组,通常置位1即可                       |
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
| `int`          | `groups`            | 卷积分组数,`input_channel`会根据这个值被分为数个分组进行计算 |

#### 输入

<dl>
<dt><tt>inputs[0]</tt>: T</dt>
<dd>输入特征;形状为(N, C, inH, inW)的四维张量,其中N为batch大小,C为输入通道数,inH和inW为输入特征图的高和宽</dd>
<dt><tt>inputs[1]</tt>: T</dt>
<dd>输入偏移量;形状为(N, deformable_group* 2* kH* kW, outH, outW)的四维张量,kH和kW为输入特征图的高和宽,outH和outW为输入特征图的高和宽</dd>
<dt><tt>inputs[2]</tt>: T</dt>
<dd>输入掩码;形状为(N, deformable_group* kH* kW, outH, outW)的四维张量</dd>
<dt><tt>inputs[3]</tt>: T</dt>
<dd>输入权重;形状为(output_channel, input_channel, kH, kW)的四维张量</dd>
<dt><tt>inputs[4]</tt>: T, optional</dt>
<dd>输入偏移量;形状为(output_channel)的一维张量</dd>
</dl>

#### 输出

<dl>
<dt><tt>outputs[0]</tt>: T</dt>
<dd>输出特征;形状为(N, output_channel, outH, outW)的四维张量</dd>
</dl>

#### 类型约束

- T:tensor(float32, Linear)