-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathtensorrt_ops.cc
1412 lines (1308 loc) · 65.1 KB
/
tensorrt_ops.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
103
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
150
151
152
153
154
155
156
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
188
189
190
191
192
193
194
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
227
228
229
230
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
261
262
263
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
293
294
295
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
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
359
360
361
362
363
364
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* \file runtime/contrib/tensorrt/tensorrt_ops.cc
* \brief Converters from Relay ops into TensorRT layers. Converters should
* inherit from TensorRTOpConverter and implement the Convert() method.
*/
#include "tensorrt_ops.h"
#include <algorithm>
#include <cmath>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "../json/json_node.h"
#include "NvInfer.h"
#include "tensorrt_utils.h"
namespace tvm {
namespace runtime {
namespace contrib {
TensorRTOpConverter::TensorRTOpConverter(std::string op_name,
const std::vector<TensorRTInputType>& input_types,
bool variable_input_count)
: op_name(std::move(op_name)),
input_types(input_types),
variable_input_count(variable_input_count) {}
nvinfer1::ITensor* TensorRTOpConverter::Reshape(TensorRTOpConverterParams* params,
nvinfer1::ITensor* input,
const std::vector<int>& new_shape) const {
auto layer = params->network->addShuffle(*input);
ICHECK(layer != nullptr);
layer->setReshapeDimensions(VectorToTrtDims(new_shape));
layer->setOutputType(0, input->getType());
return layer->getOutput(0);
}
nvinfer1::ITensor* TensorRTOpConverter::Transpose(TensorRTOpConverterParams* params,
nvinfer1::ITensor* input,
const std::vector<int>& order) const {
auto layer = params->network->addShuffle(*input);
ICHECK(layer != nullptr);
nvinfer1::Permutation perm;
if (TRT_HAS_IMPLICIT_BATCH(params)) {
// Batch dimension cannot be modified.
ICHECK_EQ(input->getDimensions().nbDims, order.size() - 1);
ICHECK_EQ(order[0], 0);
for (size_t i = 0; i + 1 < order.size(); ++i) {
perm.order[i] = order[i + 1] - 1;
}
} else {
ICHECK_EQ(input->getDimensions().nbDims, order.size());
for (size_t i = 0; i < order.size(); ++i) {
perm.order[i] = order[i];
}
}
layer->setFirstTranspose(perm);
return layer->getOutput(0);
}
int TensorRTOpConverter::ConvertAxis(TensorRTOpConverterParams* params, int axis,
int input_rank) const {
// Add 1 for missing batch dim.
if (TRT_HAS_IMPLICIT_BATCH(params)) {
input_rank += 1;
}
ICHECK(axis >= -input_rank && axis < input_rank);
if (axis < 0) axis += input_rank;
if (TRT_HAS_IMPLICIT_BATCH(params)) {
// Can't modify batch dimenson.
ICHECK_NE(axis, 0);
// Subtract 1 for implicit batch dim.
axis -= 1;
}
return axis;
}
nvinfer1::ITensor* TensorRTOpConverter::CreateScalar(
TensorRTOpConverterParams* params, float value, const nvinfer1::Dims& broadcast_to_dims) const {
nvinfer1::Dims dims;
dims.nbDims = broadcast_to_dims.nbDims;
std::fill_n(dims.d, dims.nbDims, 1);
float* values = new float[1];
values[0] = value;
const nvinfer1::DataType weight_type = params->inputs.at(1).weight.type;
nvinfer1::Weights weights{weight_type, static_cast<void*>(values), 1};
params->trt_weights->push_back(weights);
return params->network->addConstant(dims, weights)->getOutput(0);
}
void TensorRTOpConverter::GetPadding(const std::vector<std::string>& padding,
bool* use_asymmetric_padding, nvinfer1::DimsHW* prepadding,
nvinfer1::DimsHW* postpadding) const {
ICHECK(padding.size() == 1 || padding.size() == 2 || padding.size() == 4);
if (padding.size() == 4) {
// four int : padding width in the order of (top, left, bottom, right).
*prepadding = nvinfer1::DimsHW(std::stoi(padding[0]), std::stoi(padding[1]));
*postpadding = nvinfer1::DimsHW(std::stoi(padding[2]), std::stoi(padding[3]));
*use_asymmetric_padding = true;
} else if (padding.size() == 2) {
// two int : bottom, right will use same padding as top, left
*prepadding = nvinfer1::DimsHW(std::stoi(padding[0]), std::stoi(padding[1]));
*postpadding = *prepadding;
*use_asymmetric_padding = false;
} else {
// one int : same padding used on all sides
*prepadding = nvinfer1::DimsHW(std::stoi(padding[0]), std::stoi(padding[0]));
*postpadding = *prepadding;
*use_asymmetric_padding = false;
}
}
void TensorRTOpConverter::GetPadding3D(const std::vector<std::string>& padding,
bool* use_asymmetric_padding, nvinfer1::Dims* prepadding,
nvinfer1::Dims* postpadding) const {
ICHECK(padding.size() == 1 || padding.size() == 3 || padding.size() == 6);
if (padding.size() == 6) {
// six int : padding width in the order of (front, top, left, back, bottom, right)
*prepadding =
nvinfer1::Dims3(std::stoi(padding[0]), std::stoi(padding[1]), std::stoi(padding[2]));
*postpadding =
nvinfer1::Dims3(std::stoi(padding[3]), std::stoi(padding[4]), std::stoi(padding[5]));
*use_asymmetric_padding = true;
} else if (padding.size() == 3) {
// three int : back, bottom, right will use same padding as front, top, left
*prepadding =
nvinfer1::Dims3(std::stoi(padding[0]), std::stoi(padding[1]), std::stoi(padding[2]));
*postpadding = *prepadding;
*use_asymmetric_padding = false;
} else {
// one int : same padding used on all sides
*prepadding =
nvinfer1::Dims3(std::stoi(padding[0]), std::stoi(padding[0]), std::stoi(padding[0]));
*postpadding = *prepadding;
*use_asymmetric_padding = false;
}
}
class ActivationOpConverter : public TensorRTOpConverter {
public:
explicit ActivationOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor}) {}
~ActivationOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
static const std::unordered_map<std::string, nvinfer1::ActivationType> op_map = {
{"nn.relu", nvinfer1::ActivationType::kRELU},
{"sigmoid", nvinfer1::ActivationType::kSIGMOID},
{"tanh", nvinfer1::ActivationType::kTANH},
#if TRT_VERSION_GE(5, 1, 5)
{"clip", nvinfer1::ActivationType::kCLIP},
{"nn.leaky_relu", nvinfer1::ActivationType::kLEAKY_RELU},
#endif
};
auto it = op_map.find(op_name);
ICHECK(it != op_map.end()) << "Unsupported activation type " << op_name;
nvinfer1::IActivationLayer* act_layer =
params->network->addActivation(*params->inputs.at(0).tensor, it->second);
#if TRT_VERSION_GE(5, 1, 5)
if (op_name == "clip") {
float a_min = std::stof(params->node.GetAttr<std::vector<std::string>>("a_min")[0]);
float a_max = std::stof(params->node.GetAttr<std::vector<std::string>>("a_max")[0]);
act_layer->setAlpha(a_min);
act_layer->setBeta(a_max);
} else if (op_name == "nn.leaky_relu") {
float alpha = std::stof(params->node.GetAttr<std::vector<std::string>>("alpha")[0]);
act_layer->setAlpha(alpha);
}
#endif
ICHECK(act_layer != nullptr);
params->outputs.push_back(act_layer->getOutput(0));
}
};
class ElementWiseBinaryOpConverter : public TensorRTOpConverter {
public:
explicit ElementWiseBinaryOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor, kTensor}) {}
~ElementWiseBinaryOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
static const std::unordered_map<std::string, nvinfer1::ElementWiseOperation> op_map = {
{"add", nvinfer1::ElementWiseOperation::kSUM},
{"subtract", nvinfer1::ElementWiseOperation::kSUB},
{"multiply", nvinfer1::ElementWiseOperation::kPROD},
{"divide", nvinfer1::ElementWiseOperation::kDIV},
{"power", nvinfer1::ElementWiseOperation::kPOW},
{"maximum", nvinfer1::ElementWiseOperation::kMAX},
{"minimum", nvinfer1::ElementWiseOperation::kMIN}};
auto it = op_map.find(op_name);
ICHECK(it != op_map.end()) << "Unsupported elementwise type " << op_name;
// Broadcast
auto input0 = params->inputs.at(0).tensor;
auto input0_dims = TrtDimsToVector(input0->getDimensions());
auto input1 = params->inputs.at(1).tensor;
auto input1_dims = TrtDimsToVector(input1->getDimensions());
const bool need_broadcast = input0_dims.size() != input1_dims.size();
if (need_broadcast) {
if (input0_dims.size() < input1_dims.size()) {
std::vector<int> new_shape(input0_dims);
while (new_shape.size() < input1_dims.size()) new_shape.insert(new_shape.begin(), 1);
input0 = Reshape(params, input0, new_shape);
} else if (input1_dims.size() < input0_dims.size()) {
std::vector<int> new_shape(input1_dims);
while (new_shape.size() < input0_dims.size()) new_shape.insert(new_shape.begin(), 1);
input1 = Reshape(params, input1, new_shape);
}
}
nvinfer1::IElementWiseLayer* elemwise_layer =
params->network->addElementWise(*input0, *input1, it->second);
ICHECK(elemwise_layer != nullptr);
params->outputs.push_back(elemwise_layer->getOutput(0));
}
};
class Conv1DOpConverter : public TensorRTOpConverter {
public:
explicit Conv1DOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor, kWeight}) {}
~Conv1DOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input_tensor = params->inputs.at(0).tensor;
auto input_dims = TrtDimsToVector(input_tensor->getDimensions());
auto weight_shape = params->inputs.at(1).weight_shape;
ICHECK_EQ(params->node.GetAttr<std::vector<std::string>>("data_layout")[0], "NCW");
ICHECK_EQ(params->node.GetAttr<std::vector<std::string>>("kernel_layout")[0], "OIW");
auto str_strides = params->node.GetAttr<std::vector<std::string>>("strides");
auto str_dilation = params->node.GetAttr<std::vector<std::string>>("dilation");
auto str_padding = params->node.GetAttr<std::vector<std::string>>("padding");
int groups = std::stoi(params->node.GetAttr<std::vector<std::string>>("groups")[0]);
int channels = weight_shape[0];
if (params->node.HasAttr("channels") &&
!params->node.GetAttr<std::vector<std::string>>("channels")[0].empty()) {
channels = std::stoi(params->node.GetAttr<std::vector<std::string>>("channels")[0]);
}
auto shuffle_layer = params->network->addShuffle(*input_tensor);
std::vector<int> new_shape = {input_dims[0], input_dims[1], 1};
shuffle_layer->setReshapeDimensions(VectorToTrtDims(new_shape));
input_tensor = shuffle_layer->getOutput(0);
const auto kernel_size = nvinfer1::DimsHW(weight_shape[2], 1);
const nvinfer1::DataType weight_type = params->inputs.at(1).weight.type;
nvinfer1::Weights bias{weight_type, nullptr, 0};
auto conv_layer = params->network->addConvolution(*input_tensor, channels, kernel_size,
params->inputs.at(1).weight, bias);
ICHECK(conv_layer != nullptr);
conv_layer->setPadding(nvinfer1::DimsHW(std::stoi(str_padding[0]), 0));
ICHECK_EQ(str_strides.size(), 1);
const auto strides = nvinfer1::DimsHW(std::stoi(str_strides[0]), 1);
conv_layer->setStride(strides);
ICHECK_EQ(str_dilation.size(), 1);
const auto dilation = nvinfer1::DimsHW(std::stoi(str_dilation[0]), 1);
conv_layer->setDilation(dilation);
conv_layer->setNbGroups(groups);
input_tensor = conv_layer->getOutput(0);
auto conv_output_dims = TrtDimsToVector(input_tensor->getDimensions());
std::vector<int> back_shape = {0, 0};
auto shuffle_back_layer = params->network->addShuffle(*input_tensor);
shuffle_back_layer->setReshapeDimensions(VectorToTrtDims(back_shape));
params->outputs.push_back(shuffle_back_layer->getOutput(0));
}
};
class Conv2DOpConverter : public TensorRTOpConverter {
public:
explicit Conv2DOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor, kWeight}) {}
~Conv2DOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input_tensor = params->inputs.at(0).tensor;
auto input_dims = TrtDimsToVector(input_tensor->getDimensions());
auto weight_shape = params->inputs.at(1).weight_shape;
ICHECK_EQ(params->node.GetAttr<std::vector<std::string>>("data_layout")[0], "NCHW");
ICHECK(params->node.GetAttr<std::vector<std::string>>("out_layout")[0] == "" ||
params->node.GetAttr<std::vector<std::string>>("out_layout")[0] == "NCHW");
ICHECK_EQ(params->node.GetAttr<std::vector<std::string>>("kernel_layout")[0], "OIHW");
auto str_strides = params->node.GetAttr<std::vector<std::string>>("strides");
auto str_dilation = params->node.GetAttr<std::vector<std::string>>("dilation");
auto str_padding = params->node.GetAttr<std::vector<std::string>>("padding");
int groups = std::stoi(params->node.GetAttr<std::vector<std::string>>("groups")[0]);
int channels = weight_shape[0];
if (params->node.HasAttr("channels") &&
!params->node.GetAttr<std::vector<std::string>>("channels")[0].empty()) {
channels = std::stoi(params->node.GetAttr<std::vector<std::string>>("channels")[0]);
}
// TRT conv2d op doesn't support asymmetric padding before 5.1, so we
// workaround by adding a padding layer before the pooling op.
nvinfer1::DimsHW prepadding, postpadding;
bool use_asymmetric_padding;
GetPadding(str_padding, &use_asymmetric_padding, &prepadding, &postpadding);
#if !TRT_VERSION_GE(5, 1, 5)
if (use_asymmetric_padding) {
auto pad_layer = params->network->addPadding(*input_tensor, prepadding, postpadding);
ICHECK(pad_layer != nullptr);
input_tensor = pad_layer->getOutput(0);
// No need for conv op to do any padding.
use_asymmetric_padding = false;
prepadding = nvinfer1::DimsHW(0, 0);
}
#endif
const auto kernel_size = nvinfer1::DimsHW(weight_shape[2], weight_shape[3]);
const nvinfer1::DataType weight_type = params->inputs.at(1).weight.type;
nvinfer1::Weights bias{weight_type, nullptr, 0};
auto conv_layer = params->network->addConvolution(*input_tensor, channels, kernel_size,
params->inputs.at(1).weight, bias);
ICHECK(conv_layer != nullptr);
conv_layer->setName(params->LayerName().c_str());
if (use_asymmetric_padding) {
#if TRT_VERSION_GE(5, 1, 5)
conv_layer->setPrePadding(prepadding);
conv_layer->setPostPadding(postpadding);
#endif
} else {
conv_layer->setPadding(prepadding);
}
ICHECK_EQ(str_strides.size(), 2);
const auto strides = nvinfer1::DimsHW(std::stoi(str_strides[0]), std::stoi(str_strides[1]));
conv_layer->setStride(strides);
ICHECK_EQ(str_dilation.size(), 2);
const auto dilation = nvinfer1::DimsHW(std::stoi(str_dilation[0]), std::stoi(str_dilation[1]));
conv_layer->setDilation(dilation);
conv_layer->setNbGroups(groups);
params->outputs.push_back(conv_layer->getOutput(0));
}
};
#if TRT_VERSION_GE(6, 0, 1)
class Conv3DOpConverter : public TensorRTOpConverter {
public:
explicit Conv3DOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor, kWeight}) {}
~Conv3DOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input_tensor = params->inputs.at(0).tensor;
auto input_dims = TrtDimsToVector(input_tensor->getDimensions());
auto weight_shape = params->inputs.at(1).weight_shape;
ICHECK_EQ(params->node.GetAttr<std::vector<std::string>>("data_layout")[0], "NCDHW");
ICHECK(params->node.GetAttr<std::vector<std::string>>("out_layout")[0] == "" ||
params->node.GetAttr<std::vector<std::string>>("out_layout")[0] == "NCDHW");
ICHECK_EQ(params->node.GetAttr<std::vector<std::string>>("kernel_layout")[0], "OIDHW");
auto str_strides = params->node.GetAttr<std::vector<std::string>>("strides");
auto str_dilation = params->node.GetAttr<std::vector<std::string>>("dilation");
auto str_padding = params->node.GetAttr<std::vector<std::string>>("padding");
int groups = std::stoi(params->node.GetAttr<std::vector<std::string>>("groups")[0]);
nvinfer1::Dims prepadding, postpadding;
bool use_asymmetric_padding;
GetPadding3D(str_padding, &use_asymmetric_padding, &prepadding, &postpadding);
const int num_outputs =
std::stoi(params->node.GetAttr<std::vector<std::string>>("channels")[0]);
const auto kernel_size = nvinfer1::Dims3(weight_shape[2], weight_shape[3], weight_shape[4]);
const nvinfer1::DataType weight_type = params->inputs.at(1).weight.type;
nvinfer1::Weights bias{weight_type, nullptr, 0};
auto conv_layer = params->network->addConvolutionNd(*input_tensor, num_outputs, kernel_size,
params->inputs.at(1).weight, bias);
ICHECK(conv_layer != nullptr);
if (use_asymmetric_padding) {
conv_layer->setPrePadding(prepadding);
conv_layer->setPostPadding(postpadding);
} else {
conv_layer->setPaddingNd(prepadding);
}
ICHECK_EQ(str_strides.size(), 3);
const auto strides = nvinfer1::Dims3(std::stoi(str_strides[0]), std::stoi(str_strides[1]),
std::stoi(str_strides[2]));
conv_layer->setStrideNd(strides);
ICHECK_EQ(str_dilation.size(), 3);
const auto dilation = nvinfer1::Dims3(std::stoi(str_dilation[0]), std::stoi(str_dilation[1]),
std::stoi(str_dilation[2]));
conv_layer->setDilationNd(dilation);
conv_layer->setNbGroups(groups);
params->outputs.push_back(conv_layer->getOutput(0));
}
};
#endif // TRT_VERSION_GE(6, 0, 1)
class DenseOpConverter : public TensorRTOpConverter {
public:
explicit DenseOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor, kWeight}) {}
~DenseOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input_tensor = params->inputs.at(0).tensor;
auto input_dims = TrtDimsToVector(input_tensor->getDimensions());
ICHECK(input_dims.size() > 0 && input_dims.size() <= 3);
const size_t required_rank = TRT_HAS_IMPLICIT_BATCH(params) ? 3 : 4;
const bool need_reshape_on_input = input_dims.size() != required_rank;
if (need_reshape_on_input) {
// Add dims of size 1 until rank is required_rank.
std::vector<int> new_shape(input_dims);
while (new_shape.size() < required_rank) new_shape.insert(new_shape.end(), 1);
input_tensor = Reshape(params, input_tensor, new_shape);
}
// Weights are in KC format.
ICHECK_EQ(params->inputs.at(1).weight_shape.size(), 2);
const int num_units = params->inputs.at(1).weight_shape[0];
const nvinfer1::DataType weight_type = params->inputs.at(1).weight.type;
nvinfer1::Weights bias{weight_type, nullptr, 0};
nvinfer1::IFullyConnectedLayer* fc_layer = params->network->addFullyConnected(
*input_tensor, num_units, params->inputs.at(1).weight, bias);
ICHECK(fc_layer != nullptr);
auto output_tensor = fc_layer->getOutput(0);
if (need_reshape_on_input) {
// Remove added dims.
input_dims[input_dims.size() - 1] = num_units;
output_tensor = Reshape(params, output_tensor, input_dims);
}
params->outputs.push_back(output_tensor);
}
};
class BatchNormOpConverter : public TensorRTOpConverter {
public:
explicit BatchNormOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor, kWeight, kWeight, kWeight, kWeight}) {}
~BatchNormOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input = params->inputs.at(0).tensor;
auto gamma = params->inputs.at(1).weight;
auto beta = params->inputs.at(2).weight;
auto mean = params->inputs.at(3).weight;
auto var = params->inputs.at(4).weight;
ICHECK_EQ(gamma.count, beta.count);
ICHECK_EQ(gamma.count, mean.count);
ICHECK_EQ(gamma.count, var.count);
const float epsilon = std::stof(params->node.GetAttr<std::vector<std::string>>("epsilon")[0]);
const int axis = std::stoi(params->node.GetAttr<std::vector<std::string>>("axis")[0]);
const bool scale = std::stoi(params->node.GetAttr<std::vector<std::string>>("scale")[0]);
const bool center = std::stoi(params->node.GetAttr<std::vector<std::string>>("center")[0]);
auto input_dims = TrtDimsToVector(input->getDimensions());
const size_t min_rank = TRT_HAS_IMPLICIT_BATCH(params) ? 3 : 4;
const size_t max_rank = TRT_HAS_IMPLICIT_BATCH(params) ? 4 : 5;
ICHECK_LE(input_dims.size(), max_rank);
const bool need_reshape = input_dims.size() < min_rank;
const bool need_transpose = axis != 1;
// Reshape if needed
if (need_reshape) {
// Add dims of size 1 until rank is required_rank.
std::vector<int> new_shape(input_dims);
while (new_shape.size() < min_rank) new_shape.insert(new_shape.end(), 1);
input = Reshape(params, input, new_shape);
}
// Transpose if needed.
const int input_rank_with_batch =
input->getDimensions().nbDims + (TRT_HAS_IMPLICIT_BATCH(params) ? 1 : 0);
ICHECK(input_rank_with_batch == 4 || input_rank_with_batch == 5);
std::vector<int> transpose_order(input_rank_with_batch);
if (need_transpose) {
// Move axis dim to first dim after batch.
for (int i = 0; i < input_rank_with_batch; ++i) {
transpose_order[i] = i;
}
transpose_order[1] = axis;
transpose_order[axis] = 1;
input = Transpose(params, input, transpose_order);
}
void* weight_scale_ptr = new float[gamma.count];
const nvinfer1::DataType weight_type_scale = params->inputs.at(1).weight.type;
nvinfer1::Weights weight_scale{weight_type_scale, weight_scale_ptr, gamma.count};
params->trt_weights->push_back(weight_scale);
void* weight_shift_ptr = new float[gamma.count];
const nvinfer1::DataType weight_type_shift = params->inputs.at(2).weight.type;
nvinfer1::Weights weight_shift{weight_type_shift, weight_shift_ptr, gamma.count};
params->trt_weights->push_back(weight_shift);
const nvinfer1::DataType weight_type_power = params->inputs.at(3).weight.type;
nvinfer1::Weights power{weight_type_power, nullptr, 0};
// fill in the content of weights for the Scale layer
const float* gamma_ptr = reinterpret_cast<const float*>(gamma.values);
const float* beta_ptr = reinterpret_cast<const float*>(beta.values);
const float* mean_ptr = reinterpret_cast<const float*>(mean.values);
const float* var_ptr = reinterpret_cast<const float*>(var.values);
float* scale_ptr = reinterpret_cast<float*>(weight_scale_ptr);
float* shift_ptr = reinterpret_cast<float*>(weight_shift_ptr);
for (int i = 0; i < gamma.count; ++i) {
scale_ptr[i] = 1.0 / std::sqrt(var_ptr[i] + epsilon);
if (scale) {
scale_ptr[i] *= gamma_ptr[i];
}
shift_ptr[i] = -mean_ptr[i] * scale_ptr[i];
if (center) {
shift_ptr[i] += beta_ptr[i];
}
}
#if TRT_VERSION_GE(6, 0, 1)
const int channel_dim = TRT_HAS_IMPLICIT_BATCH(params) ? 0 : 1;
nvinfer1::IScaleLayer* scale_layer = params->network->addScaleNd(
*input, nvinfer1::ScaleMode::kCHANNEL, weight_shift, weight_scale, power, channel_dim);
#else
ICHECK_EQ(input->getDimensions().nbDims, 3);
nvinfer1::IScaleLayer* scale_layer = params->network->addScale(
*input, nvinfer1::ScaleMode::kCHANNEL, weight_shift, weight_scale, power);
#endif
ICHECK(scale_layer != nullptr);
auto output = scale_layer->getOutput(0);
if (need_transpose) {
output = Transpose(params, output, transpose_order);
}
if (need_reshape) {
output = Reshape(params, output, input_dims);
}
params->outputs.push_back(output);
}
};
class LayerNormOpConverter : public TensorRTOpConverter {
public:
explicit LayerNormOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor, kWeight, kWeight}) {}
~LayerNormOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input = params->inputs.at(0).tensor;
auto gamma_input = params->inputs.at(1).weight;
auto beta_input = params->inputs.at(2).weight;
ICHECK_EQ(gamma_input.count, beta_input.count);
const float epsilon = std::stof(params->node.GetAttr<std::vector<std::string>>("epsilon")[0]);
const bool scale = std::stoi(params->node.GetAttr<std::vector<std::string>>("scale")[0]);
const bool center = std::stoi(params->node.GetAttr<std::vector<std::string>>("center")[0]);
const int input_rank = input->getDimensions().nbDims;
const int original_axis = std::stoi(params->node.GetAttr<std::vector<std::string>>("axis")[0]);
const int axis = ConvertAxis(params, original_axis, input_rank);
std::vector<int> weight_shape(input_rank, 1);
weight_shape[axis] = gamma_input.count;
auto gamma =
params->network->addConstant(VectorToTrtDims(weight_shape), gamma_input)->getOutput(0);
auto beta =
params->network->addConstant(VectorToTrtDims(weight_shape), beta_input)->getOutput(0);
// Compute mean
auto mean_layer = params->network->addReduce(*input, nvinfer1::ReduceOperation::kAVG, 1 << axis,
/*keepdims=*/true);
ICHECK(mean_layer != nullptr);
auto mean = mean_layer->getOutput(0);
// Compute variance
auto diff_layer =
params->network->addElementWise(*input, *mean, nvinfer1::ElementWiseOperation::kSUB);
ICHECK(diff_layer != nullptr);
auto square_layer =
params->network->addElementWise(*diff_layer->getOutput(0), *diff_layer->getOutput(0),
nvinfer1::ElementWiseOperation::kPROD);
ICHECK(square_layer != nullptr);
auto var_layer = params->network->addReduce(
*square_layer->getOutput(0), nvinfer1::ReduceOperation::kAVG, 1 << axis, /*keepdims=*/true);
ICHECK(var_layer != nullptr);
auto var = var_layer->getOutput(0);
// sqrt(var + epsilon)
auto epsilon_tensor = CreateScalar(params, epsilon, var->getDimensions());
auto denom_add_layer = params->network->addElementWise(*var, *epsilon_tensor,
nvinfer1::ElementWiseOperation::kSUM);
ICHECK(denom_add_layer != nullptr);
auto denom_layer =
params->network->addUnary(*denom_add_layer->getOutput(0), nvinfer1::UnaryOperation::kSQRT);
ICHECK(denom_layer != nullptr);
// (input - mean) / sqrt(var + epsilon)
auto output_layer =
params->network->addElementWise(*diff_layer->getOutput(0), *denom_layer->getOutput(0),
nvinfer1::ElementWiseOperation::kDIV);
ICHECK(output_layer != nullptr);
auto output = output_layer->getOutput(0);
if (scale) {
auto scale_layer =
params->network->addElementWise(*output, *gamma, nvinfer1::ElementWiseOperation::kPROD);
ICHECK(scale_layer != nullptr);
output = scale_layer->getOutput(0);
}
if (center) {
auto center_layer =
params->network->addElementWise(*output, *beta, nvinfer1::ElementWiseOperation::kSUM);
ICHECK(center_layer != nullptr);
output = center_layer->getOutput(0);
}
params->outputs.push_back(output);
}
};
class BatchFlattenOpConverter : public TensorRTOpConverter {
public:
explicit BatchFlattenOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor}) {}
~BatchFlattenOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
std::vector<int> new_shape{-1};
if (!TRT_HAS_IMPLICIT_BATCH(params)) {
new_shape.insert(new_shape.begin(), params->inputs.at(0).tensor->getDimensions().d[0]);
}
params->outputs.push_back(Reshape(params, params->inputs.at(0).tensor, new_shape));
}
};
class SoftmaxOpConverter : public TensorRTOpConverter {
public:
explicit SoftmaxOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor}) {}
~SoftmaxOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input = params->inputs.at(0).tensor;
const int input_rank = input->getDimensions().nbDims;
const int original_axis = std::stoi(params->node.GetAttr<std::vector<std::string>>("axis")[0]);
const int axis = ConvertAxis(params, original_axis, input_rank);
nvinfer1::ISoftMaxLayer* softmax_layer = params->network->addSoftMax(*input);
softmax_layer->setAxes(1 << axis);
ICHECK(softmax_layer != nullptr);
params->outputs.push_back(softmax_layer->getOutput(0));
}
};
class PoolingOpConverter : public TensorRTOpConverter {
public:
explicit PoolingOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor}) {}
~PoolingOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input = params->inputs.at(0).tensor;
static const std::unordered_map<std::string, nvinfer1::PoolingType> op_map = {
{"nn.max_pool2d", nvinfer1::PoolingType::kMAX},
{"nn.avg_pool2d", nvinfer1::PoolingType::kAVERAGE}};
auto it = op_map.find(op_name);
ICHECK(it != op_map.end()) << "Unsupported pooling type " << op_name << " in TensorRT";
ICHECK_EQ(params->node.GetAttr<std::vector<std::string>>("layout")[0], "NCHW");
auto str_pool_size = params->node.GetAttr<std::vector<std::string>>("pool_size");
auto str_padding = params->node.GetAttr<std::vector<std::string>>("padding");
auto str_strides = params->node.GetAttr<std::vector<std::string>>("strides");
nvinfer1::DimsHW prepadding, postpadding;
bool use_asymmetric_padding;
GetPadding(str_padding, &use_asymmetric_padding, &prepadding, &postpadding);
bool ceil_mode = std::stoi(params->node.GetAttr<std::vector<std::string>>("ceil_mode")[0]);
// TRT pooling op doesn't support asymmetric padding before 5.1, so we
// workaround by adding a padding layer before the pooling op.
#if !TRT_VERSION_GE(5, 1, 5)
if (use_asymmetric_padding) {
auto pad_layer = params->network->addPadding(*input, prepadding, postpadding);
ICHECK(pad_layer != nullptr);
input = pad_layer->getOutput(0);
// No need for pooling op to do any padding.
use_asymmetric_padding = false;
prepadding = nvinfer1::DimsHW(0, 0);
}
#endif
nvinfer1::DimsHW window_size =
nvinfer1::DimsHW(std::stoi(str_pool_size[0]), std::stoi(str_pool_size[1]));
auto pool_layer = params->network->addPooling(*input, it->second, window_size);
ICHECK(pool_layer != nullptr);
nvinfer1::DimsHW strides =
nvinfer1::DimsHW(std::stoi(str_strides[0]), std::stoi(str_strides[1]));
pool_layer->setStride(strides);
if (use_asymmetric_padding) {
#if TRT_VERSION_GE(5, 1, 5)
pool_layer->setPrePadding(prepadding);
pool_layer->setPostPadding(postpadding);
#endif
} else {
pool_layer->setPadding(prepadding);
}
if (op_name == "nn.avg_pool2d") {
bool count_include_pad =
std::stoi(params->node.GetAttr<std::vector<std::string>>("count_include_pad")[0]);
// count_include_pad=True is useless if there is no padding. TRT doesn't
// like count_include_pad in combination with strides even when there is
// no padding or assymetric padding even, so turn off inclusive to avoid
// error message. Note: Padding will always be symmetric with
// count_include_pad since partitioner will prevent unsupported case.
if (prepadding.h() == 0 && prepadding.w() == 0) {
count_include_pad = false;
}
pool_layer->setAverageCountExcludesPadding(!count_include_pad);
}
#if TRT_VERSION_GE(5, 1, 5)
if (ceil_mode) {
pool_layer->setPaddingMode(nvinfer1::PaddingMode::kEXPLICIT_ROUND_UP);
}
#else
ICHECK(!ceil_mode);
#endif
params->outputs.push_back(pool_layer->getOutput(0));
}
};
#if TRT_VERSION_GE(6, 0, 1)
class Pooling3DOpConverter : public TensorRTOpConverter {
public:
explicit Pooling3DOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor}) {}
~Pooling3DOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input = params->inputs.at(0).tensor;
static const std::unordered_map<std::string, nvinfer1::PoolingType> op_map = {
{"nn.max_pool3d", nvinfer1::PoolingType::kMAX},
{"nn.avg_pool3d", nvinfer1::PoolingType::kAVERAGE}};
auto it = op_map.find(op_name);
ICHECK(it != op_map.end()) << "Unsupported pooling type " << op_name << " in TensorRT";
ICHECK_EQ(params->node.GetAttr<std::vector<std::string>>("layout")[0], "NCDHW");
auto str_pool_size = params->node.GetAttr<std::vector<std::string>>("pool_size");
auto str_padding = params->node.GetAttr<std::vector<std::string>>("padding");
auto str_strides = params->node.GetAttr<std::vector<std::string>>("strides");
nvinfer1::DimsHW prepadding, postpadding;
bool use_asymmetric_padding;
GetPadding3D(str_padding, &use_asymmetric_padding, &prepadding, &postpadding);
bool ceil_mode = std::stoi(params->node.GetAttr<std::vector<std::string>>("ceil_mode")[0]);
nvinfer1::Dims window_size = nvinfer1::Dims3(
std::stoi(str_pool_size[0]), std::stoi(str_pool_size[1]), std::stoi(str_pool_size[2]));
auto pool_layer = params->network->addPoolingNd(*input, it->second, window_size);
ICHECK(pool_layer != nullptr);
nvinfer1::Dims strides = nvinfer1::Dims3(std::stoi(str_strides[0]), std::stoi(str_strides[1]),
std::stoi(str_strides[2]));
pool_layer->setStrideNd(strides);
if (use_asymmetric_padding) {
pool_layer->setPrePadding(prepadding);
pool_layer->setPostPadding(postpadding);
} else {
pool_layer->setPaddingNd(prepadding);
}
if (op_name == "nn.avg_pool3d") {
bool count_include_pad =
std::stoi(params->node.GetAttr<std::vector<std::string>>("count_include_pad")[0]);
pool_layer->setAverageCountExcludesPadding(!count_include_pad);
}
if (ceil_mode) {
pool_layer->setPaddingMode(nvinfer1::PaddingMode::kEXPLICIT_ROUND_UP);
}
params->outputs.push_back(pool_layer->getOutput(0));
}
};
#endif // TRT_VERSION_GE(6, 0, 1)
class GlobalPoolingOpConverter : public TensorRTOpConverter {
public:
explicit GlobalPoolingOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor}) {}
~GlobalPoolingOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input_tensor = params->inputs.at(0).tensor;
auto input_dims = TrtDimsToVector(input_tensor->getDimensions());
static const std::unordered_map<std::string, nvinfer1::PoolingType> op_map = {
{"nn.global_max_pool2d", nvinfer1::PoolingType::kMAX},
{"nn.global_avg_pool2d", nvinfer1::PoolingType::kAVERAGE}};
auto it = op_map.find(op_name);
ICHECK(it != op_map.end()) << "Unsupported pooling type " << op_name << " in TensorRT";
ICHECK_EQ(params->node.GetAttr<std::vector<std::string>>("layout")[0], "NCHW");
const int h = TRT_HAS_IMPLICIT_BATCH(params) ? input_dims[1] : input_dims[2];
const int w = TRT_HAS_IMPLICIT_BATCH(params) ? input_dims[2] : input_dims[3];
auto pool_layer =
params->network->addPooling(*input_tensor, it->second, nvinfer1::DimsHW(h, w));
ICHECK(pool_layer != nullptr);
params->outputs.push_back(pool_layer->getOutput(0));
}
};
class ExpandDimsOpConverter : public TensorRTOpConverter {
public:
explicit ExpandDimsOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor}) {}
~ExpandDimsOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input_tensor = params->inputs.at(0).tensor;
auto input_dims = TrtDimsToVector(input_tensor->getDimensions());
const int original_axis = std::stoi(params->node.GetAttr<std::vector<std::string>>("axis")[0]);
const int num_newaxis =
std::stoi(params->node.GetAttr<std::vector<std::string>>("num_newaxis")[0]);
const int axis = ConvertAxis(params, original_axis, input_dims.size() + 1);
for (int i = 0; i < num_newaxis; ++i) {
input_dims.insert(input_dims.begin() + axis, 1);
}
params->outputs.push_back(Reshape(params, params->inputs.at(0).tensor, input_dims));
}
};
class SqueezeOpConverter : public TensorRTOpConverter {
public:
explicit SqueezeOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor}) {}
~SqueezeOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input_tensor = params->inputs.at(0).tensor;
auto input_dims = TrtDimsToVector(input_tensor->getDimensions());
auto str_axis = params->node.GetAttr<std::vector<std::string>>("axis");
for (size_t i = 0; i < str_axis.size(); ++i) {
const int axis = ConvertAxis(params, std::stoi(str_axis[i]), input_dims.size());
input_dims[axis] = 0;
}
input_dims.erase(std::remove(input_dims.begin(), input_dims.end(), 0), input_dims.end());
params->outputs.push_back(Reshape(params, params->inputs.at(0).tensor, input_dims));
}
};
class UnaryOpConverter : public TensorRTOpConverter {
public:
explicit UnaryOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor}) {}
~UnaryOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
// The following ops are supported by TRT but don't exist in relay yet:
// recip, tan, sinh, cosh, asin, acos, asinh, acosh, atanh
static const std::unordered_map<std::string, nvinfer1::UnaryOperation> op_map = {
{"exp", nvinfer1::UnaryOperation::kEXP},
{"log", nvinfer1::UnaryOperation::kLOG},
{"sqrt", nvinfer1::UnaryOperation::kSQRT},
{"abs", nvinfer1::UnaryOperation::kABS},
{"negative", nvinfer1::UnaryOperation::kNEG},
#if TRT_VERSION_GE(5, 1, 5)
{"sin", nvinfer1::UnaryOperation::kSIN},
{"cos", nvinfer1::UnaryOperation::kCOS},
{"atan", nvinfer1::UnaryOperation::kATAN},
{"ceil", nvinfer1::UnaryOperation::kCEIL},
{"floor", nvinfer1::UnaryOperation::kFLOOR},
#endif
#if TRT_VERSION_GE(7, 0, 0)
{"erf", nvinfer1::UnaryOperation::kERF},
#endif
};
auto it = op_map.find(op_name);
ICHECK(it != op_map.end()) << "Unsupported unary type " << op_name;
nvinfer1::IUnaryLayer* unary_layer =
params->network->addUnary(*params->inputs.at(0).tensor, it->second);
ICHECK(unary_layer != nullptr);
params->outputs.push_back(unary_layer->getOutput(0));
}
};
class ConcatOpConverter : public TensorRTOpConverter {
public:
explicit ConcatOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {}, /*variable_input_count=*/true) {}
~ConcatOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
const int num_inputs = params->inputs.size();
ICHECK_GT(num_inputs, 0);
const int input_rank = params->inputs[0].tensor->getDimensions().nbDims;
std::vector<nvinfer1::ITensor*> input_tensors;
for (auto input : params->inputs) {
ICHECK_EQ(input.type, kTensor);
ICHECK_EQ(input_rank, input.tensor->getDimensions().nbDims);
input_tensors.push_back(input.tensor);
}
const int original_axis = std::stoi(params->node.GetAttr<std::vector<std::string>>("axis")[0]);
const int axis = ConvertAxis(params, original_axis, input_rank);
nvinfer1::IConcatenationLayer* concat_layer =
params->network->addConcatenation(input_tensors.data(), input_tensors.size());
ICHECK(concat_layer != nullptr);
concat_layer->setAxis(axis);
params->outputs.push_back(concat_layer->getOutput(0));
}
};
#if TRT_VERSION_GE(5, 1, 5)
class SplitOpConverter : public TensorRTOpConverter {
public:
explicit SplitOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor}) {}
~SplitOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input = params->inputs.at(0).tensor;
auto input_dims = TrtDimsToVector(input->getDimensions());
const int original_axis = std::stoi(params->node.GetAttr<std::vector<std::string>>("axis")[0]);
const int axis = ConvertAxis(params, original_axis, input_dims.size());
auto indices_or_sections =
params->node.GetAttr<std::vector<std::string>>("indices_or_sections");
auto mode = params->node.GetAttr<std::vector<std::string>>("mode")[0];
std::vector<int> split_starts;
std::vector<int> split_sizes;
if (mode == "sections") {
int sections = std::stoi(indices_or_sections[0]);
int size = input_dims[axis] / sections;
for (int i = 0; i < sections; i++) {
split_starts.push_back(i * size);
split_sizes.push_back(size);
}
} else {
int last_index = 0;
for (size_t i = 0; i < indices_or_sections.size(); ++i) {
int index = std::stoi(indices_or_sections[i]);
split_starts.push_back(last_index);
split_sizes.push_back(index - last_index);
last_index = index;
}
split_starts.push_back(last_index);
split_sizes.push_back(input_dims[axis] - last_index);
}
std::vector<int> start(input_dims.size(), 0);
std::vector<int> size(input_dims.begin(), input_dims.end());
std::vector<int> strides(input_dims.size(), 1);
for (size_t i = 0; i < split_sizes.size(); ++i) {
start[axis] = split_starts[i];
size[axis] = split_sizes[i];
auto slice_layer = params->network->addSlice(*input, VectorToTrtDims(start),
VectorToTrtDims(size), VectorToTrtDims(strides));
params->outputs.push_back(slice_layer->getOutput(0));
}
}
};
#endif
class BiasAddOpConverter : public TensorRTOpConverter {
public:
explicit BiasAddOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor, kWeight}) {}
~BiasAddOpConverter() = default;
void Convert(TensorRTOpConverterParams* params) const {
auto input_tensor = params->inputs.at(0).tensor;
auto input_dims = TrtDimsToVector(input_tensor->getDimensions());
size_t required_rank = TRT_HAS_IMPLICIT_BATCH(params) ? 3 : 4;
const size_t input_nbDims = input_tensor->getDimensions().nbDims;
int axis = std::stoi(params->node.GetAttr<std::vector<std::string>>("axis")[0]);
if (axis == -1) {
// Make sure there are 2 dimensions after channel dimension,
if (input_nbDims + 2 > required_rank) required_rank = input_nbDims + 2;
axis = input_nbDims - 1;
} else if (TRT_HAS_IMPLICIT_BATCH(params)) {
axis -= 1;
}
ICHECK(input_dims.size() > 0 && input_dims.size() <= required_rank);
const bool need_reshape_on_input = input_dims.size() != required_rank;
if (need_reshape_on_input) {
// Add dims of size 1 until rank is required_rank.
std::vector<int> new_shape(input_dims);
while (new_shape.size() < required_rank) new_shape.insert(new_shape.end(), 1);
input_tensor = Reshape(params, input_tensor, new_shape);
}
const nvinfer1::DataType weight_type = params->inputs.at(1).weight.type;
nvinfer1::Weights scale{weight_type, nullptr, 0};
nvinfer1::Weights power{weight_type, nullptr, 0};
nvinfer1::IScaleLayer* scale_layer =
params->network->addScaleNd(*input_tensor, nvinfer1::ScaleMode::kCHANNEL,
params->inputs.at(1).weight, scale, power, axis);
ICHECK(scale_layer != nullptr);
auto output_tensor = scale_layer->getOutput(0);
if (need_reshape_on_input) {
// Remove added dims.
output_tensor = Reshape(params, output_tensor, input_dims);
}
params->outputs.push_back(output_tensor);
}
};
class Conv2DTransposeOpConverter : public TensorRTOpConverter {
public:
explicit Conv2DTransposeOpConverter(std::string op_name)
: TensorRTOpConverter(std::move(op_name), {kTensor, kWeight}) {}
~Conv2DTransposeOpConverter() = default;