How to use addSlice with dynamic input?


A clear and concise description of the bug or issue.


TensorRT Version:
GPU Type:
Nvidia Driver Version:
CUDA Version:
CUDNN Version:
Operating System + Version:
Ubuntu 18
Python Version (if applicable):
TensorFlow Version (if applicable):
PyTorch Version (if applicable):
Baremetal or Container (if container which image + tag):

Taking the channel splitting in shufflenet-v2 for instance, I could use the API addslice to make this layer in the case of fixed input size. However, i hope use dynamic input size now,

 const auto explicitBatch = 1U << static_cast<uint32_t>(NetworkDefinitionCreationFlag::kEXPLICIT_BATCH);
    INetworkDefinition *network = builder->createNetworkV2(explicitBatch);
    ITensor *data = network->addInput(INPUT_BLOB_NAME, dt, Dims4{-1, 3, INPUT_H, INPUT_W});

Then I used the addslice:
ISliceLayer *s1 = network->addSlice(input, Dims4{d.d[0], 0, 0, 0 }, Dims4{ d.d[0], d.d[1] / 2, d.d[2], d.d[3] }, Dims4{1, 1, 1, 1 });
ISliceLayer *s2 = network->addSlice(input, Dims4{d.d[0], d.d[1] / 2, 0, 0 }, Dims4{d.d[0], d.d[1] / 2, d.d[2], d.d[3] }, Dims4{1, 1, 1, 1 });

Expected Result:
the input for this layer AddSlice: [-1, 116, 40, 40]
two outputs for this layer should be: [-1, 58, 40, 40] and [-1, 58, 40, 40]
because the first dimension is -1 (placeholder for dynamic batch size), then raised this error:
[E] [TRT] (Unnamed Layer 20) [Slice]: slice size cannot have negative dimension, size = [-1,58,40,40]*

1 Like

Hi @290844930,
To use dynamic slice, we need to set start/size/stride with ISliceLayer::setInput().
Hope this link will help you.

Thank you.

1 Like

Can you give some examples? how to use setInput() deal with dynamic width/height Slicelayer

do you know how to call setInput() deal with dynamic width and height

Do you test whether you can output the correct result when stripe is equal to dims4 {1,1,1,1}? I tested it here. The result is different from the expected result

Can you give some examples? About this problem.