torch2trt icon indicating copy to clipboard operation
torch2trt copied to clipboard

add_constant incompatible function arguments

Open fused-byte opened this issue 4 years ago • 11 comments

Hi,

I am trying to convert my YOLO model trained on Pytorch into tensorRT compatible model. On following the steps I get the following error message. I saw other people getting the same error but in a different scenario. I hope somebody can put light on the reason for this error as the stack trace doesn't give much information. It would be great if anyone can provide a solution to this.

I tried this https://github.com/NVIDIA/TensorRT/issues/213 to fix my problem but it doesn't work.

Warning: Encountered known unsupported method torch.arange Warning: Encountered known unsupported method torch.Tensor.repeat Traceback (most recent call last): File "convert.py", line 12, in model_trt = torch2trt(model, [data]) File "/usr/local/lib/python2.7/dist-packages/torch2trt/torch2trt.py", line 377, in torch2trt outputs = module(*inputs) File "/home/nvidia/.local/lib/python2.7/site-packages/torch/nn/modules/module.py", line 532, in call result = self.forward(*input, **kwargs) File "/home/nvidia/pipeline/src/as1819/perception/cone_detection_camera/lib/utils/models.py", line 254, in forward x = module[0](x, img_size) File "/home/nvidia/.local/lib/python2.7/site-packages/torch/nn/modules/module.py", line 532, in call result = self.forward(*input, **kwargs) File "/home/nvidia/pipeline/src/as1819/perception/cone_detection_camera/lib/utils/models.py", line 129, in forward create_grids(self, img_size, nG) File "/home/nvidia/pipeline/src/as1819/perception/cone_detection_camera/lib/utils/models.py", line 277, in create_grids grid_x = torch.arange(nG).repeat((nG, 1)).view((1, 1, nG, nG)).float() File "/usr/local/lib/python2.7/dist-packages/torch2trt/torch2trt.py", line 202, in wrapper converter'converter' File "/usr/local/lib/python2.7/dist-packages/torch2trt/converters/view.py", line 10, in convert_view input_trt = trt_(ctx.network, input) File "/usr/local/lib/python2.7/dist-packages/torch2trt/torch2trt.py", line 135, in trt_ t._trt = network.add_constant(shape, weight).get_output(0) TypeError: add_constant(): incompatible function arguments. The following argument types are supported: 1. (self: tensorrt.tensorrt.INetworkDefinition, shape: tensorrt.tensorrt.Dims, weights: tensorrt.tensorrt.Weights) -> tensorrt.tensorrt.IConstantLayer Invoked with: <tensorrt.tensorrt.INetworkDefinition object at 0x7ee1ad3b70>, (13, 13), array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]])

fused-byte avatar May 10 '20 14:05 fused-byte

Did you ever figure out a fix for this?

ctyfang avatar May 28 '20 02:05 ctyfang

I encountered the same problem!

yuzhiyiliu avatar Jul 09 '20 07:07 yuzhiyiliu

I first converted my model into .onnx format and then simplified it using this repo https://github.com/daquexian/onnx-simplifier and then to .trt using this repo.

I did not find a way or help from this repo to resolve the issue.

fused-byte avatar Jul 09 '20 07:07 fused-byte

        shape = tuple(t.shape) #  don't exclude batch when adding constants...?
        weight = t.detach().cpu().numpy()
        weight = trt.Weights(np.ascontiguousarray(weight, dtype=np.float32)) # fixed
        t._trt = network.add_constant(shape, weight).get_output(0)

yuzhiyiliu avatar Jul 09 '20 08:07 yuzhiyiliu

Hi,

I recently upgraded from PyTorch 1.3 and TRT 6 to PyTorch 1.6 and TRT 7 and this problem has also come up (what versions are you guys using?). @yuzhiyiliu's fix worked for me, but I'm also seeing the same issue on line 159 of torch2trt.py:

 scalar = t * torch.ones(shape, dtype=dtype).cpu().numpy()
 trt_tensor = network.add_constant(shape, scalar).get_output(0)

I'm going to guess there are probably other areas of the code that are also broken.

Is this a PyTorch version compatibility issue and can we see this fixed soon?

Jaftem avatar Jul 27 '20 22:07 Jaftem

Tell you secretly, the YOLO layer has no weight and does not to be converted.

yuzhiyiliu avatar Jul 28 '20 02:07 yuzhiyiliu

@Jaftem Could you solve the issue on line 159? I applied @yuzhiyiliu's solution for the weight but for the scalar, I could not find the way.

DuyguSerbes avatar Sep 21 '21 07:09 DuyguSerbes

Hi, I'm getting a similar error at this line:

divisor = torch.tensor(list(range(1, input_shape[2] + 1))*input_shape[1], device='cuda').view([1, input_shape[1], input_shape[2], 1, 1])

I can't export to onnx because apparently adaptive pooling is not supported there. Does anyone know how to fix this? What does the error even mean?

TypeError: add_constant(): incompatible function arguments. The following argument types are supported:
    1. (self: tensorrt.tensorrt.INetworkDefinition, shape: tensorrt.tensorrt.Dims, weights: tensorrt.tensorrt.Weights) -> tensorrt.tensorrt.IConstantLayer

Invoked with: <tensorrt.tensorrt.INetworkDefinition object at 0x7f39cf3653b0>, (384,), array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,
        2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,
        3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,
        4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,  4,
        5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,  4,  5,
        6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,  4,  5,  6,
        7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,  4,  5,  6,  7,
        8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,  4,  5,  6,  7,  8,
        9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,  4,  5,  6,  7,  8,  9,
       10, 11, 12, 13, 14, 15, 16,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10,
       11, 12, 13, 14, 15, 16,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11,
       12, 13, 14, 15, 16,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,
       13, 14, 15, 16,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,
       14, 15, 16,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
       15, 16,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
       16,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,
        2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,
        3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,
        4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,  4,
        5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,  4,  5,
        6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  1,  2,  3,  4,  5,  6,
        7,  8,  9, 10, 11, 12, 13, 14, 15, 16])

ghazalehtrb avatar Aug 16 '22 17:08 ghazalehtrb

To @ghazalehtrb and other guys, did you solve this issue? I encountered to same problem with @ghazalehtrb, but couldn't find what to do from anywhere!

herry123435 avatar Sep 22 '22 03:09 herry123435

@herry123435 Unfortunately no! I still have this issue, I ended up changing the whole network for now.

ghazalehtrb avatar Sep 27 '22 16:09 ghazalehtrb

Omg, I still have this issue in swinv2_tiny_window8_256 model.

deepindeed2022 avatar Feb 10 '23 10:02 deepindeed2022