-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
op_teller.cc
2748 lines (2587 loc) · 91.6 KB
/
op_teller.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
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed 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.
#include "paddle/fluid/inference/tensorrt/op_teller.h"
#include <bitset>
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/data_layout.h"
#include "paddle/fluid/framework/op_meta_info_helper.h"
#include "paddle/fluid/framework/phi_utils.h"
#include "paddle/fluid/inference/tensorrt/dynamic_shape_infermeta_factory.h"
#include "paddle/phi/core/compat/op_utils.h"
#include "paddle/phi/core/kernel_factory.h"
namespace paddle {
namespace framework {
class OpDesc;
} // namespace framework
} // namespace paddle
namespace paddle {
namespace inference {
namespace tensorrt {
// Just tell by the op_types.
struct SimpleOpTypeSetTeller : public Teller {
SimpleOpTypeSetTeller() {
#if IS_TRT_VERSION_GE(7130)
// use TensorRT plugin
teller_set.insert("group_norm");
teller_set.insert("multiclass_nms3");
teller_set.insert("multiclass_nms");
int8_teller_set.insert("multiclass_nms3");
int8_teller_set.insert("multiclass_nms");
#endif
#if IS_TRT_VERSION_GE(7000)
teller_set.insert("tile");
teller_set.insert("flatten_contiguous_range");
int8_teller_set.insert("flatten_contiguous_range");
teller_set.insert("rnn");
int8_teller_set.insert("rnn");
teller_set.insert("fill_constant_batch_size_like");
int8_teller_set.insert("fill_constant_batch_size_like");
#endif
#if CUDA_VERSION >= 10020
teller_set.insert("reshape");
teller_set.insert("reshape2");
int8_teller_set.insert("reshape");
int8_teller_set.insert("reshape2");
#endif
#if IS_TRT_VERSION_GE(8000)
teller_set.insert("sparse_fc");
int8_teller_set.insert("sparse_fc");
teller_set.insert("sparse_multihead_matmul");
int8_teller_set.insert("sparse_multihead_matmul");
#endif
}
bool operator()(const framework::OpDesc& desc,
bool use_no_calib_int8 = false,
bool with_dynamic_shape = false) override {
const std::string op_type = desc.Type();
// do not support the op which is labeled the `skip_quant`
if ((desc.HasAttr("namescope") &&
PADDLE_GET_CONST(std::string, desc.GetAttr("op_namescope")) ==
"/skip_quant_2/") ||
desc.HasAttr("skip_quant"))
return false;
std::unordered_set<std::string> act_op_list = {
"relu", "relu6", "sigmoid",
"elu", "selu", "softsign",
"softplus", "stanh", "thresholded_relu",
"exp", "log", "sqrt",
"abs", "sin", "cos",
"tan", "tanh", "sinh",
"cosh", "asin", "acos",
"atan", "asinh", "atanh",
"ceil", "floor", "erf",
"reciprocal", "silu", "celu",
"tanh_shrink", "logsigmoid", "sign",
"logical_not"};
if (act_op_list.find(op_type) != act_op_list.end()) {
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
auto x_var_name = desc.Input("X")[0];
auto* x_var_desc = block->FindVar(x_var_name);
const auto x_shape = x_var_desc->GetShape();
if (x_shape.size() == 1) {
VLOG(3) << op_type
<< " op does not support input's dim is 1 in tensorrt.";
return false;
}
#if !IS_TRT_VERSION_GE(7000)
if (op_type == "erf") {
VLOG(3) << op_type << " op does not support tensorrt.";
return false;
}
#endif
}
// In static shape mode in TRT, we can't allow that op's input is a
// 1D-tensor So we filter it here. Some op like elementwise having "Y" too,
// but that is dealt with in the specified op, here just the common case
if (!with_dynamic_shape) {
std::string X_name;
auto inputs = desc.Inputs();
if (inputs.count("X") && !desc.Input("X").empty()) {
X_name = desc.Input("X")[0];
} else if (inputs.count("Input") && !desc.Input("Input").empty()) {
X_name = desc.Input("Input")[0];
}
auto* block = desc.Block();
if (block) {
auto* x_var_desc = block->FindVar(X_name);
// Can't get feed op's TensorDesc
if (op_type != "feed" && x_var_desc && !x_var_desc->Persistable()) {
const auto x_shape = x_var_desc->GetShape();
if (x_shape.size() == 1) return false;
}
}
}
if (op_type == "dropout") {
/*
* Some OpDescs Attribute support both constant value and dynamic
* runtime value (which is a Variable(s) type). But TensorRT maybe
* only support constant value Attribute, so we shall distinguish
* this case in time and return False in OpTeller.Tell().
* If Attribute is Variable(s), HasAttr() will return False
*/
if (!desc.HasAttr("dropout_prob", /*with_attr_var=*/false)) {
VLOG(3)
<< "Skip to convert into TRT while found Attribute('dropout_prob') "
"is Variable type in dropout.";
return false;
}
}
if (op_type == "pool2d") {
// If Attribute is Variable(s), HasAttr() will return False
if (!desc.HasAttr("ksize", /*with_attr_var=*/false)) {
VLOG(3) << "Skip to convert into TRT while found Attribute('ksize') is "
"Variable type in pool2d.";
return false;
}
std::vector<int> paddings =
PADDLE_GET_CONST(std::vector<int>, desc.GetAttr("paddings"));
if (paddings.size() > 2) {
return false;
}
if (desc.Input("X").size() != 1) {
VLOG(3) << "TRT Pool2d expect 1 input, but got "
<< desc.Input("X").size();
return false;
}
if (desc.Output("Out").size() != 1) {
VLOG(3) << "TRT Pool2d has only 1 output, but got "
<< desc.Output("Out").size();
return false;
}
if (desc.HasAttr("data_format")) {
std::string data_format =
PADDLE_GET_CONST(std::string, desc.GetAttr("data_format"));
if (data_format == "NHWC" || data_format == "NDHWC") {
return false;
}
}
if (!desc.HasAttr("pooling_type")) {
return false;
} else {
std::string pool_type =
PADDLE_GET_CONST(std::string, desc.GetAttr("pooling_type"));
if (pool_type != "max" && pool_type != "avg") {
VLOG(3) << "Wrong pool op type, the trt do not support the "
<< pool_type << " pool type.";
return false;
}
if (pool_type == "avg") {
if (desc.HasAttr("global_pooling")) {
if (!PADDLE_GET_CONST(bool, desc.GetAttr("global_pooling"))) {
if (desc.HasAttr("exclusive")) {
if (PADDLE_GET_CONST(bool, desc.GetAttr("exclusive"))) {
std::vector<int> ksize =
PADDLE_GET_CONST(std::vector<int>, desc.GetAttr("ksize"));
for (size_t i = 0; i < ksize.size(); i++) {
if (ksize[i] <= paddings[i]) {
VLOG(3) << "the padding size should be less than the "
"filter size "
"for exclusive-counting pooling.";
return false;
}
}
}
}
}
}
}
}
}
if (op_type == "conv2d" || op_type == "conv2d_transpose" ||
op_type == "conv2d_fusion" || op_type == "depthwise_conv2d" ||
op_type == "depthwise_conv2d_transpose") {
if (desc.Input("Input").size() != 1) {
VLOG(3) << "TRT Conv2d expect 1 input, but got "
<< desc.Input("Input").size() << " input.";
return false;
}
if (desc.Input("Filter").size() != 1) {
VLOG(3) << "TRT Conv2d expect 1 filter, but got "
<< desc.Input("Filter").size() << " filter.";
return false;
}
if (desc.HasAttr("enable_int8")) {
if (op_type == "conv2d" || op_type == "conv2d_fusion") {
if (!desc.HasAttr("Input_scale")) {
VLOG(3) << "Input scale not found. TRT int8"
" requires conv/deconv to have "
"input quantization scales.";
return false;
}
}
}
if (op_type == "conv2d_transpose" ||
op_type == "depthwise_conv2d_transpose") {
if (!desc.HasAttr("dilations")) {
return false;
} else {
const std::vector<int> dilations =
PADDLE_GET_CONST(std::vector<int>, desc.GetAttr("dilations"));
if (dilations[0] != 1 || dilations[1] != 1) {
VLOG(3) << "In conv2d_transpose, Dilations must be (1, 1) for "
"tensorRT, but given ("
<< dilations[0] << ", " << dilations[1] << ")";
return false;
}
}
}
if (desc.Output("Output").size() != 1) {
VLOG(3) << "TRT Conv2d expect 1 output, but got "
<< desc.Output("Output").size() << " output.";
return false;
}
// strides > 1 and 'SAME' is only supported by trt7.0 above
#if !IS_TRT_VERSION_GE(7000)
if (op_type == "conv2d" || op_type == "conv2d_fusion" ||
op_type == "depthwise_conv2d") {
if (desc.HasAttr("padding_algorithm") && with_dynamic_shape) {
auto padding_algorithm =
PADDLE_GET_CONST(std::string, desc.GetAttr("padding_algorithm"));
if (padding_algorithm == "SAME" && desc.HasAttr("strides")) {
const std::vector<int> strides =
PADDLE_GET_CONST(std::vector<int>, desc.GetAttr("strides"));
// there is no issue if strides.size() less than 2
if (strides.size() > 1) {
for (size_t i = 0; i < strides.size(); i++) {
if (strides[i] > 1) return false;
}
}
}
}
}
#endif
}
if (op_type == "deformable_conv") {
if (with_dynamic_shape) {
VLOG(3) << "Deformable conv trt plugin does not support dynamic shape";
return false;
}
auto* block = desc.Block();
auto input_name = desc.Input("Input")[0];
auto* input_desc = block->FindVar(input_name);
const auto input_shape = input_desc->GetShape();
if (input_shape.size() != 4) {
VLOG(3) << "Input of deformable conv should be 4-D Tensor, but got "
<< input_shape.size();
return false;
}
auto filter_name = desc.Input("Filter")[0];
auto* filter_desc = block->FindVar(filter_name);
const auto filter_shape = filter_desc->GetShape();
int groups = PADDLE_GET_CONST(int, desc.GetAttr("groups"));
if (input_shape[1] != filter_shape[1] * groups) {
VLOG(3) << "The number of input channels should be equal to filter "
<< "channels * groups. But got input channels "
<< input_shape[1] << "filter channels " << filter_shape[1];
return false;
}
const std::vector<int> strides =
PADDLE_GET_CONST(std::vector<int>, desc.GetAttr("strides"));
if (strides.size() != 2) {
VLOG(3) << "The size of strides should be 2, but got "
<< strides.size();
return false;
}
const std::vector<int> paddings =
PADDLE_GET_CONST(std::vector<int>, desc.GetAttr("paddings"));
if (paddings.size() != 2) {
VLOG(3) << "The size of paddings shoule be 2, but got "
<< paddings.size();
return false;
}
}
if (op_type == "bmm") {
if (!with_dynamic_shape) {
return false;
}
}
if (op_type == "sign") {
#if IS_TRT_VERSION_GE(8200)
if (!with_dynamic_shape) {
return false;
}
#else
VLOG(3) << "sign op is only supported by trt8.2 above ";
return false;
#endif
}
if (op_type == "logical_not") {
#if IS_TRT_VERSION_GE(8400)
if (!with_dynamic_shape) {
return false;
}
#else
VLOG(3) << "logical_not op is only supported by trt8.4 above because of "
"cast op";
return false;
#endif
}
if (op_type == "matmul_v2") {
if (!with_dynamic_shape) {
return false;
}
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
return true;
}
if (op_type == "matmul") {
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
// not support broadcast
auto* x_var_desc = block->FindVar(desc.Input("X")[0]);
auto* y_var_desc = block->FindVar(desc.Input("Y")[0]);
const auto x_shape = x_var_desc->GetShape();
const auto y_shape = y_var_desc->GetShape();
if (x_shape.size() != y_shape.size()) {
VLOG(3)
<< "matmul op not support broadcast, please check inputs'shape. ";
return false;
}
uint64_t dims = 2;
for (size_t i = 0; i < x_shape.size() - dims; ++i) {
if (x_shape[i] != y_shape[i] && (x_shape[i] == 1 || y_shape[i] == 1)) {
VLOG(3) << "matmul op not support broadcast, please check "
"inputs'shape[i]. ";
return false;
}
}
for (auto& param_name : desc.Inputs()) {
for (auto& var_name : param_name.second) {
auto* var_desc = block->FindVar(var_name);
const auto shape = var_desc->GetShape();
if (shape.size() < 3) {
VLOG(3)
<< "matmul op dims < 3 not supported in tensorrt, but got dims "
<< shape.size() << ", so jump it.";
return false;
}
}
}
}
if (op_type == "softmax") {
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
auto x_var_name = desc.Input("X")[0];
auto* x_var_desc = block->FindVar(x_var_name);
const auto x_shape = x_var_desc->GetShape();
}
if (op_type == "group_norm") {
bool has_attrs = (desc.HasAttr("epsilon") && desc.HasAttr("groups"));
if (has_attrs == false) return false;
auto registry = GetPluginRegistry();
if (registry == nullptr) return false;
std::string layout_str =
PADDLE_GET_CONST(std::string, desc.GetAttr("data_layout"));
if (layout_str != "NCHW") {
VLOG(3) << "Group norm trt plugin only support NCHW layout, but got "
<< layout_str;
return false;
}
}
if (op_type == "concat") {
if (!desc.HasAttr("axis")) {
return false;
}
int axis = PADDLE_GET_CONST(int, desc.GetAttr("axis"));
if (!with_dynamic_shape) {
if (axis == 0) return false;
}
auto concat_inputs = desc.Inputs();
if (concat_inputs.find("AxisTensor") != concat_inputs.end()) {
if (desc.Input("AxisTensor").size() >= 1) {
return false;
}
}
}
if (op_type == "transpose2" || op_type == "transpose") {
if (!desc.HasAttr("axis")) {
return false;
}
std::vector<int> axis =
PADDLE_GET_CONST(std::vector<int>, desc.GetAttr("axis"));
if (!with_dynamic_shape && axis[0] != 0) return false;
if (axis.size() >= nvinfer1::Dims::MAX_DIMS) return false;
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
auto x_var_name = desc.Input("X")[0];
auto* x_var_desc = block->FindVar(x_var_name);
const auto x_shape = x_var_desc->GetShape();
if (axis.size() != x_shape.size()) return false;
int dims = x_shape.size();
std::vector<int> perm(nvinfer1::Dims::MAX_DIMS);
for (int i = 0; i < dims; i++) {
perm[i] = axis[i];
}
auto is_valid_permutation = [&](int dims,
const std::vector<int>& permutation) {
std::bitset<nvinfer1::Dims::MAX_DIMS> found;
for (int i = 0; i < dims; ++i) {
const int x = permutation[i];
if ((x < 0) || (x >= dims) || found[x])
return false; // Out of bounds or duplicate
found.set(x);
}
return true;
};
if (!is_valid_permutation(dims, perm)) {
VLOG(3) << "Invalid permutation dimensions for trt transpose op "
"converter: duplicate or out of bound.";
return false;
}
}
if (op_type == "flatten2" || op_type == "flatten") {
if (!desc.HasAttr("axis")) {
return false;
} else {
#if IS_TRT_VERSION_GE(7130)
#else
if (with_dynamic_shape) return false;
#endif
int axis = PADDLE_GET_CONST(int, desc.GetAttr("axis"));
if (axis != 1) return false;
}
}
if (op_type == "flatten_contiguous_range") {
if (!with_dynamic_shape) {
int start_axis = PADDLE_GET_CONST(int, desc.GetAttr("start_axis"));
int stop_axis = PADDLE_GET_CONST(int, desc.GetAttr("stop_axis"));
auto x_var_name = desc.Input("X")[0];
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
auto* x_var_desc = block->FindVar(x_var_name);
const auto x_shape = x_var_desc->GetShape();
int dims = x_shape.size();
if (start_axis < 0) start_axis += dims;
if (start_axis == 0) {
VLOG(3) << "TRT flatten_contiguous_range not support the "
"batch-dimension being changed";
return false;
}
if (stop_axis < 0) stop_axis += dims;
for (int i = start_axis; i <= stop_axis; ++i) {
if (x_shape[i] < 0) {
VLOG(3) << "On TRT static shape,flatten_contiguous_range input dim "
"should be > 0";
return false;
}
}
}
}
if (op_type == "gather") {
auto gather_inputs = desc.Inputs();
if (gather_inputs.find("Axis") != gather_inputs.end()) {
if (desc.Input("Axis").size() >= 1) {
return false;
}
}
if (!with_dynamic_shape) {
return false;
} else {
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
auto index_var_name = desc.Input("Index")[0];
auto* index_var_desc = block->FindVar(index_var_name);
// The index input must be int32 datatype.
if (index_var_desc->GetDataType() !=
paddle::framework::proto::VarType_Type::VarType_Type_INT32) {
VLOG(3) << "gather op Index input data type must be int32";
return false;
}
#if !IS_TRT_VERSION_GE(7000)
auto* x_var_desc = block->FindVar(desc.Input("X")[0]);
const auto x_shape = x_var_desc->GetShape();
if (x_shape.size() == 1) {
VLOG(3) << "Gather does not support 1-dimensional input in tensorrt";
return false;
}
#endif
}
}
if (op_type == "gather_nd") {
if (!with_dynamic_shape) return false;
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
auto index_var_name = desc.Input("Index")[0];
auto* index_var_desc = block->FindVar(index_var_name);
// The index input must be int32 datatype.
if (index_var_desc->GetDataType() !=
paddle::framework::proto::VarType_Type::VarType_Type_INT32) {
VLOG(3) << "gather_nd op Index input data type must be int32";
return false;
}
#if IS_TRT_VERSION_LT(8200)
auto x_var_name = desc.Input("X")[0];
auto* x_var_desc = block->FindVar(x_var_name);
const auto index_shape = index_var_desc->GetShape();
const auto x_shape = x_var_desc->GetShape();
if (x_shape.size() <= 2) {
VLOG(3) << "gather_nd op requires the input's dimension to be greater "
"than 2";
return false;
}
if (x_shape.size() != index_shape.size()) {
VLOG(3) << "gather_nd op Index input dims size [" << index_shape.size()
<< " ] not equal to x dims size [" << x_shape.size() << "]";
return false;
}
#endif
}
if (op_type == "take_along_axis") {
#if IS_TRT_VERSION_GE(8200)
if (!with_dynamic_shape) return false;
auto* block = desc.Block();
auto input_var_name = desc.Input("Input")[0];
auto index_var_name = desc.Input("Index")[0];
auto* input_var_desc = block->FindVar(input_var_name);
auto* index_var_desc = block->FindVar(index_var_name);
// The index input must be int32 datatype.
if (index_var_desc->GetDataType() !=
paddle::framework::proto::VarType_Type::VarType_Type_INT32) {
VLOG(3) << "take_along_axis op Index input data type must be int32";
return false;
}
const auto input_shape = input_var_desc->GetShape();
const auto index_shape = index_var_desc->GetShape();
if (input_shape.size() != index_shape.size()) {
VLOG(3) << "take_along_axis op Index input dims size ["
<< index_shape.size() << " ] not equal to input dims size ["
<< input_shape.size() << "]";
return false;
}
#else
VLOG(3) << "take_along_axis op is only supported by trt8.2 above ";
return false;
#endif
}
if (op_type == "anchor_generator") {
if (!with_dynamic_shape) return false;
}
if (op_type == "yolo_box") {
if (with_dynamic_shape) return false;
bool has_attrs =
(desc.HasAttr("class_num") && desc.HasAttr("anchors") &&
desc.HasAttr("downsample_ratio") && desc.HasAttr("conf_thresh") &&
desc.HasAttr("clip_bbox") && desc.HasAttr("scale_x_y"));
if (!has_attrs) return false;
}
if (op_type == "yolo_box_head") {
if (with_dynamic_shape) return false;
bool has_attrs = desc.HasAttr("class_num") && desc.HasAttr("anchors");
if (!has_attrs) return false;
}
if (op_type == "arg_max") {
if (!desc.HasAttr("axis", /*with_attr_var=*/false)) {
VLOG(3) << "Skip to convert into TRT while found Attribute('axis') is "
"Variable type in arg_max.";
return false;
}
int axis = desc.HasAttr("axis")
? PADDLE_GET_CONST(int64_t, desc.GetAttr("axis"))
: -1;
bool flatten = PADDLE_GET_CONST(bool, desc.GetAttr("flatten"));
int dtype = PADDLE_GET_CONST(int, desc.GetAttr("dtype"));
if (axis == 0 || flatten || dtype != 2) return false;
}
if (op_type == "affine_channel") {
if (!desc.HasAttr("data_layout")) return false;
auto data_layout = phi::StringToDataLayout(
PADDLE_GET_CONST(std::string, desc.GetAttr("data_layout")));
if (data_layout != phi::DataLayout::kNCHW) return false;
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
auto x_var_name = desc.Input("X")[0];
auto* x_var_desc = block->FindVar(x_var_name);
const auto x_shape = x_var_desc->GetShape();
if (x_shape.size() == 2) {
return false;
}
}
if (op_type == "multiclass_nms" || op_type == "multiclass_nms3") {
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
auto multiclass_nms_inputs = desc.Inputs();
if (multiclass_nms_inputs.find("RoisNum") !=
multiclass_nms_inputs.end()) {
if (desc.Input("RoisNum").size() >= 1) {
return false;
}
}
for (auto& param_name : multiclass_nms_inputs) {
for (auto& var_name : param_name.second) {
auto* var_desc = block->FindVar(var_name);
const auto shape = var_desc->GetShape();
if (shape.size() != 3) {
VLOG(3) << "multiclass_nms op dims != 3 not supported in tensorrt, "
"but got dims "
<< shape.size() << ", so jump it.";
return false;
}
}
}
bool has_attrs =
(desc.HasAttr("background_label") &&
desc.HasAttr("score_threshold") && desc.HasAttr("nms_top_k") &&
desc.HasAttr("keep_top_k") && desc.HasAttr("normalized"));
if (has_attrs == false) return false;
// TODO(wangxinxin08): tricky solution because the outputs of batchedNMS
// plugin are not constient with those of multiclass_nms3
if (desc.HasAttr("nms_eta") == false) return false;
auto nms_eta = PADDLE_GET_CONST(float, desc.GetAttr("nms_eta"));
if (nms_eta <= 1.0) return false;
auto nms_top_k = PADDLE_GET_CONST(int, desc.GetAttr("nms_top_k"));
if (nms_top_k < 0) return false;
auto keep_top_k = PADDLE_GET_CONST(int, desc.GetAttr("keep_top_k"));
if (keep_top_k < 0) return false;
auto registry = GetPluginRegistry();
if (registry == nullptr) return false;
}
if (op_type == "nearest_interp") {
std::vector<std::string> attrs{
"interp_method", "align_corners", "scale", "out_h", "out_w"};
for (auto const attr : attrs) {
if (!desc.HasAttr(attr)) return false;
}
if (desc.HasAttr("data_layout")) {
auto data_layout = phi::StringToDataLayout(
PADDLE_GET_CONST(std::string, desc.GetAttr("data_layout")));
if (data_layout != phi::DataLayout::kNCHW &&
data_layout != phi::DataLayout::kNHWC)
return false;
}
auto interp_method =
PADDLE_GET_CONST(std::string, desc.GetAttr("interp_method"));
if (interp_method != "nearest") return false;
auto scale = PADDLE_GET_CONST(float, desc.GetAttr("scale"));
auto out_h = PADDLE_GET_CONST(int, desc.GetAttr("out_h"));
auto out_w = PADDLE_GET_CONST(int, desc.GetAttr("out_w"));
auto align_corners =
PADDLE_GET_CONST(bool, desc.GetAttr("align_corners"));
if (!(scale > 0.f && (out_h <= 0 && out_w <= 0))) {
if (out_h <= 0) {
VLOG(3) << "out_h must be greater than 0 if scale is not set.";
return false;
}
if (out_w <= 0) {
VLOG(3) << "out_w must be greater than 0 if scale is not set.";
return false;
}
}
if ((scale <= 0.f) && with_dynamic_shape) {
VLOG(3) << "dynamic shape not support scale not set.";
return false;
}
// When align_corners = true, the paddle's and trt_layer's results has
// diff
if (align_corners && scale != 1) {
return false;
}
}
if (op_type == "nearest_interp_v2") {
std::vector<std::string> attrs{"data_layout",
"interp_method",
"align_corners",
"scale",
"out_h",
"out_w"};
for (auto const attr : attrs) {
if (!desc.HasAttr(attr)) return false;
}
auto data_layout = phi::StringToDataLayout(
PADDLE_GET_CONST(std::string, desc.GetAttr("data_layout")));
if (data_layout != phi::DataLayout::kNCHW &&
data_layout != phi::DataLayout::kNHWC)
return false;
auto interp_method =
PADDLE_GET_CONST(std::string, desc.GetAttr("interp_method"));
if (interp_method != "nearest") return false;
auto scale = PADDLE_GET_CONST(std::vector<float>, desc.GetAttr("scale"));
auto out_h = PADDLE_GET_CONST(int, desc.GetAttr("out_h"));
auto out_w = PADDLE_GET_CONST(int, desc.GetAttr("out_w"));
if (!(out_h > 0 && out_w > 0)) {
if (scale.size() < 2) return false;
if (scale[0] <= 0.f || scale[1] <= 0.f) {
VLOG(3) << "scale factor must be greater than 0 if out_h or out_w is "
"not set.";
return false;
}
}
}
if (op_type == "bilinear_interp_v2") {
std::vector<std::string> attrs{"data_layout",
"interp_method",
"align_corners",
"scale",
"out_h",
"out_w"};
for (auto const attr : attrs) {
if (!desc.HasAttr(attr)) {
VLOG(3) << "The op_type " << op_type << " doesn't have the attr "
<< attr << " and return false";
return false;
}
}
auto resize_inputs = desc.Inputs();
if (resize_inputs.find("SizeTensor") != resize_inputs.end()) {
if (desc.Input("SizeTensor").size() >= 1) {
VLOG(3)
<< "The Paddle-TRT doesn't support the SizeTensor for op_type "
<< op_type;
return false;
}
}
if (resize_inputs.find("OutSize") != resize_inputs.end()) {
if (!with_dynamic_shape) {
VLOG(3) << "Static shape don't support the OutSize for op_type "
<< op_type;
return false;
}
}
auto data_layout = phi::StringToDataLayout(
PADDLE_GET_CONST(std::string, desc.GetAttr("data_layout")));
if (data_layout != phi::DataLayout::kNCHW &&
data_layout != phi::DataLayout::kNHWC) {
VLOG(3) << "The op_type " << op_type
<< " is not NCHW or NHWC return false";
return false;
}
auto interp_method =
PADDLE_GET_CONST(std::string, desc.GetAttr("interp_method"));
if (interp_method != "bilinear") {
VLOG(3) << "The interp_method of op_type " << op_type
<< " is not bilinear";
return false;
}
auto align_corners =
PADDLE_GET_CONST(bool, desc.GetAttr("align_corners"));
if (align_corners != false) {
VLOG(3)
<< "The bilinear_interp_v2 only supports align_corners with false.";
return false;
}
bool has_scale_input_size =
(resize_inputs.find("Scale") != resize_inputs.end());
if (has_scale_input_size && desc.Input("Scale").size() != 1) {
const std::vector<float> scale =
PADDLE_GET_CONST(std::vector<float>, desc.GetAttr("scale"));
if (scale.size() <= 1) {
if (!desc.HasAttr("out_h") || !desc.HasAttr("out_w")) {
VLOG(3) << "The op_type " << op_type
<< " doesn't have Scale and the scale size <=1 and without "
"out_h / out_w, it will return false";
return false;
}
auto out_h = PADDLE_GET_CONST(int, desc.GetAttr("out_h"));
auto out_w = PADDLE_GET_CONST(int, desc.GetAttr("out_w"));
if (!(out_h <= 0 && out_w <= 0)) {
if (out_h <= 0) {
VLOG(3) << "The op_type " << op_type
<< "'s out_h must be greater than 0 if scale is not set.";
return false;
}
if (out_w <= 0) {
VLOG(3) << "The op_type " << op_type
<< "'s out_w must be greater than 0 if scale is not set.";
return false;
}
}
} else {
for (size_t i = 0; i < scale.size(); i++) {
if (scale[i] <= 0 && with_dynamic_shape) {
VLOG(3) << "dynamic shape not support Attr(scale[" << i << "]) "
<< scale[i]
<< " less than 1 and Input(Scale) vector not set.";
return false;
}
}
}
}
}
if (op_type == "hard_swish") {
if (desc.Input("X").size() != 1) {
VLOG(3) << "HardSwish op has only 1 input, but got "
<< desc.Input("X").size();
return false;
}
if (desc.Output("Out").size() != 1) {
VLOG(3) << "HardSwish op has only 1 output, but got "
<< desc.Output("Out").size();
return false;
}
}
if (op_type == "squeeze2") {
// If Attribute is Variable(s), HasAttr() will return False
if (!desc.HasAttr("axes", /*with_attr_var=*/false)) {
VLOG(3) << "Skip to convert into TRT while found Attribute('axes') is "
"Variable type in squeeze2.";
return false;
}
std::vector<int> axes;
if (desc.HasAttr("axes")) {
axes = PADDLE_GET_CONST(std::vector<int>, desc.GetAttr("axes"));
}
if (axes.size() == 0) {
VLOG(3) << "The necessary attributes of the squeeze2 operator axes is "
"missing.";
return false;
}
if (!with_dynamic_shape) {
if (std::find(axes.begin(), axes.end(), 0) != axes.end()) {
VLOG(3) << "Invalid squeeze axes. Axes having batch axis is not "
"supported in static shape";
return false;
}
}
}
if (op_type == "unsqueeze2") {
std::vector<int> axes;
if (desc.HasAttr("axes")) {
axes = PADDLE_GET_CONST(std::vector<int>, desc.GetAttr("axes"));
}
if (axes.size() == 0) {
VLOG(3) << "The necessary attributes of the squeeze2 operator axes is "
"missing.";
return false;
}
if (!with_dynamic_shape) {
if (std::find(axes.begin(), axes.end(), 0) != axes.end()) {
VLOG(3) << "Invalid squeeze axes. Axes having batch axis is not "
"supported in static shape";
return false;
}
}
}
if (op_type == "batch_norm") {
const std::vector<std::string> bn_inputs = {
"X", "Bias", "Mean", "Scale", "Variance"};
for (unsigned int i = 0; i < bn_inputs.size(); i++) {
if (desc.Input(bn_inputs[i]).size() != 1) {
VLOG(3) << "Invalid " << bn_inputs[i]
<< "'s size of batch_norm TRT "
"converter. Expected 1, received "
<< desc.Input(bn_inputs[i]).size() << ".";
return false;
}
}
auto batch_norm_inputs = desc.Inputs();
if (batch_norm_inputs.find("MomentumTensor") != batch_norm_inputs.end()) {
if (desc.Input("MomentumTensor").size() >= 1) {