@@ -199,67 +199,108 @@ nvinfer1::ITensor* tensor_to_const(ConversionCtx* ctx, at::Tensor t, const std::
199
199
return out;
200
200
}
201
201
202
- nvinfer1::ITensor* toITensor (ConversionCtx* ctx, const torch::jit::Node* n, at::Tensor* input) {
203
-
204
- auto weights = Weights (ctx, *input);
205
- // IConstantLayer to convert indices from Weights to ITensor
206
- auto const_layer = ctx->net ->addConstant (weights.shape , weights.data ); // shouln't use constant
207
- TORCHTRT_CHECK (const_layer, " Unable to create constant layer from node: " << *n);
208
- auto const_out = const_layer->getOutput (0 );
209
- return const_out;
210
- }
211
-
212
202
// clamp x to [lower_bound, upper_bound]
213
- nvinfer1::ITensor* clamp (ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* x,
203
+ nvinfer1::ITensor* clamp (ConversionCtx* ctx, nvinfer1::ITensor* x,
214
204
nvinfer1::ITensor* lower_bound, nvinfer1::ITensor* upper_bound) {
215
205
auto max_layer = ctx->net ->addElementWise (*x, *lower_bound, nvinfer1::ElementWiseOperation::kMAX );
206
+ TORCHTRT_CHECK (max_layer, " Unable to create max layer for clamp" );
207
+ LOG_DEBUG (ctx->logger , " Create " << max_layer->getName () << " for clamp" );
216
208
auto max_itensor = max_layer->getOutput (0 );
209
+
217
210
auto min_layer = ctx->net ->addElementWise (*max_itensor, *upper_bound, nvinfer1::ElementWiseOperation::kMIN );
211
+ TORCHTRT_CHECK (min_layer, " Unable to create min layer for clamp" );
212
+ LOG_DEBUG (ctx->logger , " Create " << min_layer->getName () << " for clamp" );
218
213
auto min_itensor = min_layer->getOutput (0 );
219
214
return min_itensor;
220
215
}
221
216
222
217
// clamp x to [0, input_dim]
223
- nvinfer1::ITensor* clamp_to_input_dim (ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* x,
218
+ nvinfer1::ITensor* clamp_to_input_dim (ConversionCtx* ctx, nvinfer1::ITensor* x,
224
219
nvinfer1::ITensor* input_dim) {
225
220
auto nbdims = input_dim->getDimensions ().d [0 ];
226
221
auto zero = torch::zeros ({nbdims}).to (torch::kI32 );
227
- auto zero_itensor = toITensor (ctx, n, & zero);
222
+ auto zero_itensor = tensor_to_const (ctx, zero);
228
223
auto one = torch::ones ({nbdims}).to (torch::kI32 );
229
- auto one_itensor = toITensor (ctx, n, &one);
224
+ auto one_itensor = tensor_to_const (ctx, one);
225
+
230
226
auto upper_bound_layer = ctx->net ->addElementWise (*input_dim, *one_itensor, nvinfer1::ElementWiseOperation::kSUB );
227
+ TORCHTRT_CHECK (upper_bound_layer, " Unable to create sub layer for clamp to inputDim" );
228
+ LOG_DEBUG (ctx->logger , " Create " << upper_bound_layer->getName () << " for clamp to inputDim" );
231
229
auto upper_bound = upper_bound_layer->getOutput (0 );
230
+
232
231
auto max_layer = ctx->net ->addElementWise (*x, *zero_itensor, nvinfer1::ElementWiseOperation::kMAX );
232
+ TORCHTRT_CHECK (max_layer, " Unable to create max_layer for clamp to inputDim" );
233
+ LOG_DEBUG (ctx->logger , " Create " << max_layer->getName () << " for clamp to inputDim" );
233
234
auto max_itensor = max_layer->getOutput (0 );
235
+
234
236
auto min_layer = ctx->net ->addElementWise (*max_itensor, *upper_bound, nvinfer1::ElementWiseOperation::kMIN );
237
+ TORCHTRT_CHECK (min_layer, " Unable to create min_layer for clamp to inputDim" );
238
+ LOG_DEBUG (ctx->logger , " Create " << min_layer->getName () << " for clamp to inputDim" );
235
239
auto min_itensor = min_layer->getOutput (0 );
236
240
return min_itensor;
237
241
}
238
242
239
243
240
244
// return indices < 0 ? inputDims + indices : indices
241
- nvinfer1::ITensor* bump_if_negtive (ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* input_dim,
245
+ nvinfer1::ITensor* bump_if_negtive (ConversionCtx* ctx, nvinfer1::ITensor* input_dim,
242
246
nvinfer1::ITensor* indices) {
243
247
auto nbdims = input_dim->getDimensions ().d [0 ];
244
248
auto zero = torch::zeros ({nbdims}).to (torch::kI32 );
245
249
auto neg = - torch::ones ({nbdims}).to (torch::kI32 );
246
- auto zero_itensor = toITensor (ctx, n, &zero);
247
- auto neg_itensor = toITensor (ctx, n, &neg);
248
- auto signs = clamp (ctx, n, indices, neg_itensor, zero_itensor);
250
+ auto zero_itensor = tensor_to_const (ctx, zero);
251
+ auto neg_itensor = tensor_to_const (ctx, neg);
252
+ // find the indices that = -1
253
+ auto signs = clamp (ctx, indices, neg_itensor, zero_itensor);
254
+
255
+ // get the inputDim value where indices == -1, else 0
249
256
auto mul = ctx->net ->addElementWise (*signs, *input_dim, nvinfer1::ElementWiseOperation::kPROD );
257
+ TORCHTRT_CHECK (mul, " Unable to create mul layer in bump_if_negtive" );
258
+ LOG_DEBUG (ctx->logger , " Create " << mul->getName () << " for bump_if_negtive" );
250
259
auto mul_itensor = mul->getOutput (0 );
260
+
261
+ // add the inputDim value to indices where indices == -1
251
262
auto sub = ctx->net ->addElementWise (*indices, *mul_itensor, nvinfer1::ElementWiseOperation::kSUB );
263
+ TORCHTRT_CHECK (sub, " Unable to create sub layer in bump_if_negtive" );
264
+ LOG_DEBUG (ctx->logger , " Create " << sub->getName () << " for bump_if_negtive" );
252
265
auto sub_itensor = sub->getOutput (0 );
253
266
return sub_itensor;
254
267
}
255
268
256
- void update_start_and_end (ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* in_shape,
257
- nvinfer1::ITensor* in_start, nvinfer1::ITensor* in_end,
258
- nvinfer1::ITensor** out_start, nvinfer1::ITensor** out_end) {
259
- auto start = bump_if_negtive (ctx, n, in_shape, in_start);
260
- *out_start = clamp_to_input_dim (ctx, n, start, in_shape);
261
- auto end = bump_if_negtive (ctx, n, in_shape, in_end);
262
- *out_end = clamp_to_input_dim (ctx, n, end, in_shape);
269
+ std::vector<nvinfer1::ITensor*> update_start_and_end (ConversionCtx* ctx, nvinfer1::ITensor* in_shape,
270
+ nvinfer1::ITensor* in_start, nvinfer1::ITensor* in_end) {
271
+ auto start = bump_if_negtive (ctx, in_shape, in_start);
272
+ auto out_start = clamp_to_input_dim (ctx, start, in_shape);
273
+ auto end = bump_if_negtive (ctx, in_shape, in_end);
274
+ auto out_end = clamp_to_input_dim (ctx, end, in_shape);
275
+ std::vector<nvinfer1::ITensor*> outputs;
276
+ outputs.push_back (out_start);
277
+ outputs.push_back (out_end);
278
+ return outputs;
279
+ }
280
+
281
+ // size = (end - start) / stride + 1, where range is [start, end], end is included
282
+ nvinfer1::ITensor* calculate_output_size (ConversionCtx* ctx, nvinfer1::ITensor* start, nvinfer1::ITensor* end,
283
+ nvinfer1::ITensor* stride, int nbdims) {
284
+
285
+ at::Tensor one_tensor = torch::ones ({nbdims}).to (torch::kI32 );
286
+ auto one_itensor = tensor_to_const (ctx, one_tensor);
287
+
288
+ auto sub_layer = ctx->net ->addElementWise (*end, *start, nvinfer1::ElementWiseOperation::kSUB );
289
+ TORCHTRT_CHECK (sub_layer, " Unable to create sub layer in calculate_output_size" );
290
+ LOG_DEBUG (ctx->logger , " Create " << sub_layer->getName () << " for calculate_output_size" );
291
+ auto sub_itensor = sub_layer->getOutput (0 );
292
+
293
+ auto div_layer = ctx->net ->addElementWise (*sub_itensor, *stride, nvinfer1::ElementWiseOperation::kDIV );
294
+ TORCHTRT_CHECK (div_layer, " Unable to create div layer in calculate_output_size" );
295
+ LOG_DEBUG (ctx->logger , " Create " << div_layer->getName () << " for calculate_output_size" );
296
+ auto div_itensor = div_layer->getOutput (0 );
297
+
298
+ auto add_layer = ctx->net ->addElementWise (*div_itensor, *one_itensor, nvinfer1::ElementWiseOperation::kSUM );
299
+ TORCHTRT_CHECK (add_layer, " Unable to create add layer in calculate_output_size" );
300
+ LOG_DEBUG (ctx->logger , " Create " << add_layer->getName () << " for calculate_output_size" );
301
+ auto size_itensor = add_layer->getOutput (0 );
302
+
303
+ return size_itensor;
263
304
}
264
305
265
306
bool is_dynamic_shape (nvinfer1::ITensor* tensor) {
0 commit comments