1
1
#include " NvInfer.h"
2
2
#include " core/conversion/converters/converters.h"
3
3
#include " core/conversion/tensorcontainer/TensorContainer.h"
4
- #include " core/util/converter_util.h"
5
4
#include " core/util/prelude.h"
6
5
#include " core/util/trt_util.h"
7
6
#include " torch/torch.h"
@@ -16,25 +15,12 @@ namespace converters {
16
15
namespace impl {
17
16
namespace {
18
17
19
- void addSliceInput (nvinfer1::Dims& dims, int idx, ConversionCtx* ctx, nvinfer1::ISliceLayer* slice) {
20
- int32_t rank = static_cast <int32_t >(dims.nbDims );
21
- int32_t * tmp = new int32_t [rank];
22
- for (int i = 0 ; i < rank; i++)
23
- tmp[i] = dims.d [i];
24
- const nvinfer1::Dims d{1 , {rank}};
25
- const nvinfer1::Weights w{nvinfer1::DataType::kINT32 , tmp, rank};
26
- auto t = ctx->net ->addConstant (d, w)->getOutput (0 );
27
- slice->setInput (idx, *t);
28
- }
29
-
30
18
nvinfer1::ITensor* concat (int max_rank, int old_rank, ConversionCtx* ctx, nvinfer1::ITensor* tensor) {
31
19
if (max_rank - old_rank > 0 ) {
32
- int32_t * tmp = new int32_t [max_rank - old_rank];
33
- for (int i = 0 ; i < (max_rank - old_rank); i++)
34
- tmp[i] = 1 ;
35
- auto max_rank_tensor = util::arrToTensor (tmp, max_rank - old_rank, ctx);
20
+ torch::Tensor thOne = torch::tensor (std::vector<int32_t >(max_rank - old_rank, 1 ), torch::kInt32 );
21
+ auto one_tensor = tensor_to_const (ctx, thOne);
36
22
auto in_shape_tensor = ctx->net ->addShape (*tensor)->getOutput (0 );
37
- nvinfer1::ITensor* const args[2 ] = {max_rank_tensor , in_shape_tensor};
23
+ nvinfer1::ITensor* const args[2 ] = {one_tensor , in_shape_tensor};
38
24
return ctx->net ->addConcatenation (args, 2 )->getOutput (0 );
39
25
} else { // max_rank - old_rank == 0
40
26
return ctx->net ->addShape (*tensor)->getOutput (0 );
@@ -166,7 +152,6 @@ bool add_expand_dynamic(
166
152
167
153
// Dimensions are right alignment. Eg: an input of [3, 1] and max_rank = 4, the result of concat is [1, 1, 3, 1]
168
154
auto new_input_shape_tensor = concat (max_rank, input_rank, ctx, in);
169
- // LOG_DEBUG("Expand layer output tensor shape: " << new_output_shape_tensor->getDimensions());
170
155
auto new_output_shape_tensor = expandedDimsTensor;
171
156
172
157
// Add a reshape layer to expand dims
@@ -176,6 +161,8 @@ bool add_expand_dynamic(
176
161
// Start the slicing from beginning of tensor since this is an expand layer
177
162
std::vector<int64_t > start_vec (max_rank, 0 );
178
163
nvinfer1::Dims starts_dim = util::toDims (c10::IntArrayRef (start_vec));
164
+ at::Tensor thStart = torch::tensor (util::toVec (starts_dim), torch::kInt32 );
165
+ auto starts = tensor_to_const (ctx, thStart);
179
166
180
167
// compute sizes = max(x,y).
181
168
auto sizes =
@@ -186,18 +173,17 @@ bool add_expand_dynamic(
186
173
187
174
// Compute (x > 1 ? 1 : 0) for x in newDims, assuming positive x, using only TensorRT operations.
188
175
// min(1, sub(input_shape, 1))
189
- int32_t * one_vector_tmp = new int32_t [1 ];
190
- one_vector_tmp[0 ] = 1 ;
191
- auto one_vector = util::arrToTensor (one_vector_tmp, 1 , ctx);
192
- auto x_sub_one = ctx->net ->addElementWise (*new_input_shape_tensor, *one_vector, nvinfer1::ElementWiseOperation::kSUB )
176
+ torch::Tensor thOne = torch::tensor ({1 }, torch::kInt32 );
177
+ auto one_tensor = tensor_to_const (ctx, thOne);
178
+ auto x_sub_one = ctx->net ->addElementWise (*new_input_shape_tensor, *one_tensor, nvinfer1::ElementWiseOperation::kSUB )
193
179
->getOutput (0 );
194
- auto strides = ctx->net ->addElementWise (*one_vector , *x_sub_one, nvinfer1::ElementWiseOperation::kMIN )->getOutput (0 );
180
+ auto strides = ctx->net ->addElementWise (*one_tensor , *x_sub_one, nvinfer1::ElementWiseOperation::kMIN )->getOutput (0 );
195
181
nvinfer1::Dims strides_dim{-1 , {}};
196
182
strides_dim.nbDims = max_rank;
197
183
198
- // Slice layer does the expansion in TRT. Desired output size is specified by expandedDimsTensor
184
+ // Slice layer does the expansion in TRT. Desired output size is specified by sizes input at index 2.
199
185
auto slice = ctx->net ->addSlice (*shuffle->getOutput (0 ), starts_dim, sizes_dim, strides_dim);
200
- addSliceInput (starts_dim, 1 , ctx, slice );
186
+ slice-> setInput ( 1 , *starts );
201
187
slice->setInput (2 , *sizes);
202
188
slice->setInput (3 , *strides);
203
189
@@ -219,11 +205,8 @@ auto expand_registrations TRTORCH_UNUSED =
219
205
auto expandedDims = util::toDims (expanded_size);
220
206
LOG_DEBUG (" (expand layer) Expand input from " << input_dims << " to " << expandedDims);
221
207
if (ctx->input_is_dynamic ) {
222
- int expanded_size_rank = static_cast <int >(expanded_size.size ());
223
- int32_t * tmp = new int32_t [expanded_size_rank];
224
- for (int i = 0 ; i < expanded_size_rank; i++)
225
- tmp[i] = expanded_size[i];
226
- auto expandedDimsTensor = util::arrToTensor (tmp, expanded_size_rank, ctx);
208
+ at::Tensor thExpanded_size = torch::tensor (expanded_size.vec (), torch::kInt32 );
209
+ auto expandedDimsTensor = tensor_to_const (ctx, thExpanded_size);
227
210
return add_expand_dynamic (ctx, n, in, expandedDimsTensor, expandedDims, true );
228
211
} else {
229
212
return add_expand (ctx, n, in, expandedDims);
0 commit comments