@@ -204,104 +204,119 @@ nvinfer1::ITensor* clamp(
204
204
ConversionCtx* ctx,
205
205
nvinfer1::ITensor* x,
206
206
nvinfer1::ITensor* lower_bound,
207
- nvinfer1::ITensor* upper_bound) {
208
- auto max_layer = ctx->net ->addElementWise (*x, *lower_bound, nvinfer1::ElementWiseOperation::kMAX );
207
+ nvinfer1::ITensor* upper_bound,
208
+ std::string const & name) {
209
+
210
+ auto max_layer = add_elementwise (ctx, nvinfer1::ElementWiseOperation::kMAX , x, lower_bound, " max layer for " + name);
209
211
TORCHTRT_CHECK (max_layer, " Unable to create max layer for clamp" );
210
212
LOG_DEBUG (ctx->logger , " Create " << max_layer->getName () << " for clamp" );
211
213
auto max_itensor = max_layer->getOutput (0 );
212
214
213
- auto min_layer = ctx-> net -> addElementWise (*max_itensor, *upper_bound, nvinfer1::ElementWiseOperation::kMIN );
215
+ auto min_layer = add_elementwise (ctx, nvinfer1::ElementWiseOperation::kMIN , max_itensor, upper_bound, " min layer for " + name );
214
216
TORCHTRT_CHECK (min_layer, " Unable to create min layer for clamp" );
215
217
LOG_DEBUG (ctx->logger , " Create " << min_layer->getName () << " for clamp" );
216
218
auto min_itensor = min_layer->getOutput (0 );
217
219
return min_itensor;
218
220
}
219
221
220
222
// clamp x to [0, input_dim]
221
- nvinfer1::ITensor* clamp_to_input_dim (ConversionCtx* ctx, nvinfer1::ITensor* x, nvinfer1::ITensor* input_dim, int nbdims) {
222
- // auto nbdims = input_dim->getDimensions().d[0];
223
+ nvinfer1::ITensor* clamp_to_input_dim (
224
+ ConversionCtx* ctx,
225
+ nvinfer1::ITensor* x,
226
+ nvinfer1::ITensor* input_dim,
227
+ int nbdims,
228
+ std::string const & name) {
229
+
223
230
auto zero = torch::zeros ({nbdims}).to (torch::kI32 );
224
231
auto zero_itensor = tensor_to_const (ctx, zero);
225
232
auto one = torch::ones ({nbdims}).to (torch::kI32 );
226
233
auto one_itensor = tensor_to_const (ctx, one);
227
234
228
- auto upper_bound_layer = ctx-> net -> addElementWise (*input_dim, *one_itensor, nvinfer1::ElementWiseOperation::kSUB );
235
+ auto upper_bound_layer = add_elementwise (ctx, nvinfer1::ElementWiseOperation::kSUB , input_dim, one_itensor, " sub layer for " + name );
229
236
TORCHTRT_CHECK (upper_bound_layer, " Unable to create sub layer for clamp to inputDim" );
230
237
LOG_DEBUG (ctx->logger , " Create " << upper_bound_layer->getName () << " for clamp to inputDim" );
231
238
auto upper_bound = upper_bound_layer->getOutput (0 );
232
239
233
- auto max_layer = ctx-> net -> addElementWise (*x, *zero_itensor, nvinfer1::ElementWiseOperation::kMAX );
240
+ auto max_layer = add_elementwise (ctx, nvinfer1::ElementWiseOperation::kMAX , x, zero_itensor, " max layer for " + name );
234
241
TORCHTRT_CHECK (max_layer, " Unable to create max_layer for clamp to inputDim" );
235
242
LOG_DEBUG (ctx->logger , " Create " << max_layer->getName () << " for clamp to inputDim" );
236
243
auto max_itensor = max_layer->getOutput (0 );
237
244
238
- auto min_layer = ctx-> net -> addElementWise (*max_itensor, *upper_bound, nvinfer1::ElementWiseOperation::kMIN );
245
+ auto min_layer = add_elementwise (ctx, nvinfer1::ElementWiseOperation::kMIN , max_itensor, upper_bound, " min layer for " + name );
239
246
TORCHTRT_CHECK (min_layer, " Unable to create min_layer for clamp to inputDim" );
240
247
LOG_DEBUG (ctx->logger , " Create " << min_layer->getName () << " for clamp to inputDim" );
241
248
auto min_itensor = min_layer->getOutput (0 );
242
249
return min_itensor;
243
250
}
244
251
245
252
// return indices < 0 ? inputDims + indices : indices
246
- nvinfer1::ITensor* bump_if_negative (ConversionCtx* ctx, nvinfer1::ITensor* input_dim, nvinfer1::ITensor* indices, int nbdims) {
253
+ nvinfer1::ITensor* normalize_indices (
254
+ ConversionCtx* ctx,
255
+ nvinfer1::ITensor* input_dim,
256
+ nvinfer1::ITensor* indices,
257
+ int nbdims,
258
+ std::string const & name) {
259
+
247
260
auto zero = torch::zeros ({nbdims}).to (torch::kI32 );
248
261
auto neg = -torch::ones ({nbdims}).to (torch::kI32 );
249
262
auto zero_itensor = tensor_to_const (ctx, zero);
250
263
auto neg_itensor = tensor_to_const (ctx, neg);
251
264
// find the indices that = -1
252
- auto signs = clamp (ctx, indices, neg_itensor, zero_itensor);
265
+ auto signs = clamp (ctx, indices, neg_itensor, zero_itensor, " clamp layer for " + name );
253
266
254
267
// get the inputDim value where indices == -1, else 0
255
- auto mul = ctx-> net -> addElementWise (*signs, *input_dim, nvinfer1::ElementWiseOperation::kPROD );
256
- TORCHTRT_CHECK (mul, " Unable to create mul layer in bump_if_negative " );
257
- LOG_DEBUG (ctx->logger , " Create " << mul->getName () << " for bump_if_negative " );
268
+ auto mul = add_elementwise (ctx, nvinfer1::ElementWiseOperation::kPROD , signs, input_dim, " prod layer for " + name );
269
+ TORCHTRT_CHECK (mul, " Unable to create mul layer in normalize_indices " );
270
+ LOG_DEBUG (ctx->logger , " Create " << mul->getName () << " for normalize_indices " );
258
271
auto mul_itensor = mul->getOutput (0 );
259
272
260
273
// add the inputDim value to indices where indices == -1
261
- auto sub = ctx-> net -> addElementWise (*indices, *mul_itensor, nvinfer1::ElementWiseOperation::kSUB );
262
- TORCHTRT_CHECK (sub, " Unable to create sub layer in bump_if_negative " );
263
- LOG_DEBUG (ctx->logger , " Create " << sub->getName () << " for bump_if_negative " );
274
+ auto sub = add_elementwise (ctx, nvinfer1::ElementWiseOperation::kSUB , indices, mul_itensor, " sub layer for " + name );
275
+ TORCHTRT_CHECK (sub, " Unable to create sub layer in normalize_indices " );
276
+ LOG_DEBUG (ctx->logger , " Create " << sub->getName () << " for normalize_indices " );
264
277
auto sub_itensor = sub->getOutput (0 );
265
278
return sub_itensor;
266
279
}
267
280
268
- std::vector<nvinfer1::ITensor*> update_start_and_end (
281
+ std::vector<nvinfer1::ITensor*> normalize_start_and_end (
269
282
ConversionCtx* ctx,
270
283
nvinfer1::ITensor* in_shape,
271
284
nvinfer1::ITensor* in_start,
272
285
nvinfer1::ITensor* in_end,
273
- int nbdims) {
274
- auto start = bump_if_negative (ctx, in_shape, in_start, nbdims);
275
- auto out_start = clamp_to_input_dim (ctx, start, in_shape, nbdims);
276
- auto end = bump_if_negative (ctx, in_shape, in_end, nbdims);
277
- auto out_end = clamp_to_input_dim (ctx, end, in_shape, nbdims);
286
+ int nbdims,
287
+ std::string const & name) {
288
+ auto start = normalize_indices (ctx, in_shape, in_start, nbdims, " normalize start of " + name);
289
+ auto out_start = clamp_to_input_dim (ctx, start, in_shape, nbdims, " clamp start to inputDim for " + name);
290
+ auto end = normalize_indices (ctx, in_shape, in_end, nbdims, " normalize end of " + name);
291
+ auto out_end = clamp_to_input_dim (ctx, end, in_shape, nbdims, " clamp end to inputDim for " + name);
278
292
std::vector<nvinfer1::ITensor*> outputs;
279
293
outputs.push_back (out_start);
280
294
outputs.push_back (out_end);
281
295
return outputs;
282
296
}
283
297
284
298
// size = (end - start) / stride + 1, where range is [start, end], end is included
285
- nvinfer1::ITensor* calculate_output_size (
299
+ nvinfer1::ITensor* get_slice_size (
286
300
ConversionCtx* ctx,
287
301
nvinfer1::ITensor* start,
288
302
nvinfer1::ITensor* end,
289
303
nvinfer1::ITensor* stride,
290
- int nbdims) {
304
+ int nbdims,
305
+ std::string const & name) {
291
306
at::Tensor one_tensor = torch::ones ({nbdims}).to (torch::kI32 );
292
307
auto one_itensor = tensor_to_const (ctx, one_tensor);
293
308
294
- auto sub_layer = ctx-> net -> addElementWise (*end, *start, nvinfer1::ElementWiseOperation::kSUB );
309
+ auto sub_layer = add_elementwise (ctx, nvinfer1::ElementWiseOperation::kSUB , end, start, " get_slice_size sub layer for " + name );
295
310
TORCHTRT_CHECK (sub_layer, " Unable to create sub layer in calculate_output_size" );
296
311
LOG_DEBUG (ctx->logger , " Create " << sub_layer->getName () << " for calculate_output_size" );
297
312
auto sub_itensor = sub_layer->getOutput (0 );
298
313
299
- auto div_layer = ctx-> net -> addElementWise (*sub_itensor, *stride, nvinfer1::ElementWiseOperation::kDIV );
314
+ auto div_layer = add_elementwise (ctx, nvinfer1::ElementWiseOperation::kDIV , sub_itensor, stride, " get_slice_size div layer for " + name );
300
315
TORCHTRT_CHECK (div_layer, " Unable to create div layer in calculate_output_size" );
301
316
LOG_DEBUG (ctx->logger , " Create " << div_layer->getName () << " for calculate_output_size" );
302
317
auto div_itensor = div_layer->getOutput (0 );
303
318
304
- auto add_layer = ctx-> net -> addElementWise (*div_itensor, *one_itensor, nvinfer1::ElementWiseOperation::kSUM );
319
+ auto add_layer = add_elementwise (ctx, nvinfer1::ElementWiseOperation::kSUM , div_itensor, one_itensor, " get_slice_size sum layer for " + name );
305
320
TORCHTRT_CHECK (add_layer, " Unable to create add layer in calculate_output_size" );
306
321
LOG_DEBUG (ctx->logger , " Create " << add_layer->getName () << " for calculate_output_size" );
307
322
auto size_itensor = add_layer->getOutput (0 );
0 commit comments