Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_constant incompatible function arguments #313

Open
fused-byte opened this issue May 10, 2020 · 11 comments
Open

add_constant incompatible function arguments #313

fused-byte opened this issue May 10, 2020 · 11 comments

Comments

@fused-byte
Copy link

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 NVIDIA/TensorRT#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]])

@ctyfang
Copy link

ctyfang commented May 28, 2020

Did you ever figure out a fix for this?

@yuzhiyiliu
Copy link

I encountered the same problem!

@fused-byte
Copy link
Author

fused-byte commented Jul 9, 2020

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.

@yuzhiyiliu
Copy link

        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)

@Jaftem
Copy link

Jaftem commented Jul 27, 2020

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?

@yuzhiyiliu
Copy link

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

@DuyguSerbes
Copy link

@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.

@ghazalehtrb
Copy link

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])

@herry123435
Copy link

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!

@ghazalehtrb
Copy link

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

@deepindeed2022
Copy link

Omg, I still have this issue in swinv2_tiny_window8_256 model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants