@@ -61,39 +61,46 @@ ILayer* UpsampleTRTPluginLayerBuilder::AddToNetwork(INetworkDefinition* network)
61
61
out_shape_tensor = concat (network, nc, size);
62
62
}
63
63
64
+
65
+ // Dim Mode Special Case:
66
+ // Cases When Both N,C and H+W are dynamic
67
+ // In this case, We cannot turn to Scale mode.
68
+ // Also layer->SetOutputDimensions() API does not accept -1 as dim
69
+ // Have to use TNN Upsample Plugin.
70
+ // e.g [-1,2,-1,-1]
71
+ if (input_blobs_.size () == 1 && !paramlist->dims .empty ()) {
72
+ // In this case, network->addResize should not be called. GO Plugin
73
+ auto trt_dim = input_tensor->getDimensions ();
74
+ if (trt_dim.d [0 ] <= 0 || trt_dim.d [1 ] <= 0 ) {
75
+ LOGI (" WARNING: Dynamic NCHW Upsample with fixed dims param is NOT SUPPORTED by TensorRT, use TNN Upsample Plugin instead.\n " );
76
+ return TensorRTPluginLayerBuilder::AddToNetwork (network);
77
+ }
78
+ }
79
+
64
80
IResizeLayer* layer = network->addResize (*input_tensor);
65
81
if (layer != nullptr ) {
66
82
layer->setName (layer_name_.c_str ());
67
83
if (input_blobs_.size () == 1 ) {
68
84
if (!paramlist->dims .empty ()) {
69
85
auto trt_dim = input_tensor->getDimensions ();
70
86
if (trt_dim.nbDims != 4 ) {
71
- LOGE (" Upsample with 1 input only support 4d input." );
87
+ LOGE (" Upsample with 1 input only support 4d input.\n " );
72
88
return nullptr ;
73
89
}
74
90
75
91
// trt_dim may have one of the following values:
76
92
// [-1,3,32,32], [-1,2,-1,-1], [1,16,256,256]
77
93
if (trt_dim.d [0 ] <= 0 || trt_dim.d [1 ] <= 0 ) {
78
94
// Cases When At least One of N, C be dynamic
79
- if (trt_dim.d [2 ] > 0 && trt_dim.d [3 ] > 0 ) {
80
- // Cases when H,W are fixed, turn to scale mode
81
- // e.g [-1,3,32,32]
82
- float scale[4 ];
83
- scale[0 ] = 1 ;
84
- scale[1 ] = 1 ;
85
- scale[2 ] = paramlist->dims [0 ] / float (trt_dim.d [2 ]);
86
- scale[3 ] = paramlist->dims [1 ] / float (trt_dim.d [3 ]);
87
- layer->setScales (scale, 4 );
88
- } else {
89
- // Cases When Both N,C and H+W are dynamic
90
- // In this case, We cannot turn to Scale mode.
91
- // Also layer->SetOutputDimensions() API does not accept -1 as dim
92
- // Have to use TNN Upsample Plugin.
93
- // e.g [-1,2,-1,-1]
94
- LOGI (" WARNING: Dynamic NCHW Upsample with fixed dims provided, NOT SUPPORTED by TensorRT, use TNN Upsample Plugin instead." );
95
- return TensorRTPluginLayerBuilder::AddToNetwork (network);
96
- }
95
+ // and H,W are fixed, turn to scale mode
96
+ // Here trt_dim.d[2] > 0 && trt_dim.d[3] > 0
97
+ // e.g [-1,3,32,32]
98
+ float scale[4 ];
99
+ scale[0 ] = 1 ;
100
+ scale[1 ] = 1 ;
101
+ scale[2 ] = paramlist->dims [0 ] / float (trt_dim.d [2 ]);
102
+ scale[3 ] = paramlist->dims [1 ] / float (trt_dim.d [3 ]);
103
+ layer->setScales (scale, 4 );
97
104
} else {
98
105
// Cases When Both N and C are fixed
99
106
// e.g [1,16,256,256]
@@ -107,17 +114,30 @@ ILayer* UpsampleTRTPluginLayerBuilder::AddToNetwork(INetworkDefinition* network)
107
114
paramlist->dims [0 ], paramlist->dims [1 ]);
108
115
layer->setOutputDimensions (dims);
109
116
} else {
110
- LOGE (" Upsample with 1 input Fix N,C + Fixed dims does not have standard positive dim, Unsupported." );
117
+ LOGE (" Upsample with 1 input Fix N,C + Fixed dims does not have standard positive dim, Unsupported.\n " );
111
118
return nullptr ;
112
119
}
113
120
}
114
121
} else {
115
- float scale[4 ];
116
- scale[0 ] = 1 ;
117
- scale[1 ] = 1 ;
118
- scale[2 ] = paramlist->scales [1 ];
119
- scale[3 ] = paramlist->scales [0 ];
120
- layer->setScales (scale, 4 );
122
+ if (output_dims.size () == 4 ) {
123
+ float scale[4 ];
124
+ scale[0 ] = 1 ;
125
+ scale[1 ] = 1 ;
126
+ scale[2 ] = paramlist->scales [1 ];
127
+ scale[3 ] = paramlist->scales [0 ];
128
+ layer->setScales (scale, 4 );
129
+ } else if (output_dims.size () == 5 ) {
130
+ float scale[5 ];
131
+ scale[0 ] = 1 ;
132
+ scale[1 ] = 1 ;
133
+ scale[2 ] = paramlist->scales [2 ];
134
+ scale[3 ] = paramlist->scales [1 ];
135
+ scale[4 ] = paramlist->scales [0 ];
136
+ layer->setScales (scale, 5 );
137
+ } else {
138
+ LOGE (" Upsample with 1 input and scale param only support 2d or 3d now.\n " );
139
+ return nullptr ;
140
+ }
121
141
}
122
142
} else if (input_blobs_.size () == 2 ) {
123
143
// set resize layer input with shape tensor
@@ -127,12 +147,12 @@ ILayer* UpsampleTRTPluginLayerBuilder::AddToNetwork(INetworkDefinition* network)
127
147
auto input_tensor2 = std::dynamic_pointer_cast<TensorRTTensor>(input_foreign_tensor2)->GetTensor ();
128
148
layer->setInput (1 , *input_tensor2);
129
149
} else {
130
- float scale[4 ];
131
- scale[0 ] = 1 ;
132
- scale[1 ] = 1 ;
133
- scale[2 ] = paramlist->scales [1 ];
134
- scale[3 ] = paramlist->scales [0 ];
135
- layer->setScales (scale, 4 );
150
+ float scale[4 ];
151
+ scale[0 ] = 1 ;
152
+ scale[1 ] = 1 ;
153
+ scale[2 ] = paramlist->scales [1 ];
154
+ scale[3 ] = paramlist->scales [0 ];
155
+ layer->setScales (scale, 4 );
136
156
}
137
157
layer->setResizeMode (paramlist->mode == 1 ? ResizeMode::kNEAREST : ResizeMode::kLINEAR );
138
158
layer->setAlignCorners (paramlist->align_corners );
0 commit comments