TensorRT 3.0 Python API did not provide initialization function for Permutation class

Hi,

I am manually constructing the network in Python API.

When using the ShuffleLayer, for the function set_first_transpose(), a Permutation type should be passed in. However, I think the current API does not support initialization of the Permutation type. Is it?

Thank you!

Hi,

Here is a pseudo code for your reference:

network = builder.create_network()
...
shuf = network.add_shuffle(conv100)

# Get the default permutation from get_first_transpose()
perm = shuf.get_first_transpose()
perm_order = return_swig_array(perm, size=nbDims)

# Change the order of the permutation here
perm_order[0] = permute_order[0]
perm_order[1] = permute_order[1]
perm_order[2] = permute_order[2]

# Set the permutation using set_first_transpose()
shuf.set_first_transpose(perm)
...

Thanks.

Hi,
I’d like to use the shuffle layer as well but your pseudo code didn’t help much :)

  • what’s the return_swig_array function?
  • is perm actually changed since you modify perm_order?

A more accurate example that shows also the reshape operation would be very useful.
Thanks.

The pseudo code doesn’t really help since return_swig_array is not a real function.
Please provide us with a working solution.

Thanks.

Hi,

Here is a complete sample for your reference:

import numpy as np
import tensorrt as trt
import ctypes
import pycuda.driver as cuda


def mkinput(inputsize=(3, 4, 6)):
    np.random.seed(791110)
    input = np.random.rand(*inputsize)
    input = input.astype(np.float32)
    return input


def return_swig_array(data_ptr, size):
    data = ctypes.c_int * size
    data = data.from_address(int(data_ptr))
    return data


def create_engine(inputsize=(3, 4, 6), permute_order=(0, 2, 1)):
    G_LOGGER = trt.infer.ConsoleLogger(trt.infer.LogSeverity.ERROR)
    builder = trt.infer.create_infer_builder(G_LOGGER)

    network = builder.create_network()
    data = network.add_input("data", trt.infer.DataType.FLOAT, inputsize)

    wt = np.random.randn(3*3*3*2).astype(np.float32)
    bias = np.zeros(2, dtype=np.float32)
    conv0 = network.add_convolution(data, 2, (3, 3), wt, bias)
    conv0.set_padding((1, 1))

    nbDims = conv0.get_output(0).get_dimensions().nbDims
    shuf = network.add_shuffle(conv0.get_output(0))

    perm = shuf.get_first_transpose()
    perm_order = return_swig_array(perm, size=nbDims)

    # Change the order of the permutation here
    perm_order[0] = permute_order[0]
    perm_order[1] = permute_order[1]
    perm_order[2] = permute_order[2]

    shuf.set_first_transpose(perm)

    network.mark_output(conv0.get_output(0))
    network.mark_output(shuf.get_output(0))

    builder.set_max_batch_size(1)
    builder.set_max_workspace_size(1 << 30)
    engine = builder.build_cuda_engine(network)

    network.destroy()
    builder.destroy()

    return engine


def inference(engine, input, outputsize):
    output1 = np.empty(outputsize[0], dtype=np.float32)
    output2 = np.empty(outputsize[1], dtype=np.float32)

    d_input = cuda.mem_alloc(1 * input.size * input.dtype.itemsize)
    d_output1 = cuda.mem_alloc(1 * output1.size * output1.dtype.itemsize)
    d_output2 = cuda.mem_alloc(1 * output2.size * output2.dtype.itemsize)

    bindings = [int(d_input), int(d_output1), int(d_output2)]

    context = engine.create_execution_context()

    stream = cuda.Stream()
    input = input.ravel()

    cuda.memcpy_htod_async(d_input, input, stream)

    context.execute(1, bindings)

    cuda.memcpy_dtoh_async(output1, d_output1, stream)
    cuda.memcpy_dtoh_async(output2, d_output2, stream)

    stream.synchronize()

    context.destroy()
    engine.destroy()

    return output1, output2


if __name__ == '__main__':
    input_size = (3, 4, 6)
    permute_order = (0, 2, 1)

    input_blob = mkinput(input_size)
    engine = create_engine(input_size, permute_order)

    output_conv_size = (2, 4, 6)
    output_tran_size = (output_conv_size[permute_order[0]], output_conv_size[permute_order[1]], output_conv_size[permute_order[2]])
    output_after_conv, output_after_shuf = inference(engine, input_blob, outputsize=[output_conv_size, output_tran_size])

    print('Permute order: {}'.format(permute_order))
    print('Shape after convolution: {}'.format(output_after_conv.shape))
    print('Shape after transpose: {}'.format(output_after_shuf.shape))

Thanks.