question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

another 'Tensor' object has no attribute '_trt'

See original GitHub issue

Hi Trying out EfficientNet_b1 for jetson

seems like I get the following warning and error

Warning: Encountered known unsupported method torch.nn.functional.has_torch_function_variadic
Warning: Encountered known unsupported method torch.nn.functional.linear

  File "model_convert.py", line 24, in <module>
    model_trt = torch2trt(model, [x])
  File "/usr/local/lib/python3.6/dist-packages/torch2trt-0.2.0-py3.6.egg/torch2trt/torch2trt.py", line 546, in torch2trt
  File "/usr/local/lib/python3.6/dist-packages/torch2trt-0.2.0-py3.6.egg/torch2trt/torch2trt.py", line 407, in mark_outputs
AttributeError: 'Tensor' object has no attribute '_trt'

I am aware SiLU is not implemented for onnx so I have changed them to ReLU but it seems like there is another error

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jaybdubcommented, Jun 15, 2021

Hi @ooodragon94,

I was able to convert the model by registering converters for torch.nn.functional.silu and torch.nn.functional.linear

Below is the program I used to convert the model.

import timm
import torch
from torch2trt import torch2trt, tensorrt_converter, get_arg, trt, add_missing_trt_tensors, torch_dtype_to_trt

# REGISTER NEW CONVERTERS
@tensorrt_converter('torch.nn.functional.silu')
def convert_silu(ctx):
    input = get_arg(ctx, 'input', pos=0, default=None)
    output = ctx.method_return
    input_trt = add_missing_trt_tensors(ctx.network, [input])[0]
    
    layer = ctx.network.add_activation(input_trt, trt.ActivationType.SIGMOID)
    layer = ctx.network.add_elementwise(input_trt, layer.get_output(0), trt.ElementWiseOperation.PROD)
    
    output._trt = layer.get_output(0)
    
@tensorrt_converter('torch.nn.functional.linear')
def convert_Linear(ctx):
    input = ctx.method_args[0]
    weight = get_arg(ctx, 'weight', 1, None)
    bias = get_arg(ctx, 'bias', 2, None)
    input_trt = add_missing_trt_tensors(ctx.network, [input])[0]
    output = ctx.method_return

    # reshape to ...xNx1x1
    layer = ctx.network.add_shuffle(input_trt)
    layer.reshape_dims = tuple(input_trt.shape) + (1, 1) 

    bias_trt = trt.Weights(torch_dtype_to_trt(weight.dtype))
    if bias is not None:
        bias_trt = bias.detach().cpu().numpy()
        
    # add fully connected
    layer = ctx.network.add_fully_connected(
        input=layer.get_output(0),
        num_outputs=int(weight.shape[0]),
        kernel=weight.detach().cpu().numpy(),
        bias=bias_trt)

    # reshape back to N
    layer = ctx.network.add_shuffle(layer.get_output(0))
    layer.reshape_dims = tuple(output.shape[1:])

    output._trt = layer.get_output(0)


# CONVERT MODEL
model = timm.models.efficientnet_b1(pretrained=True).cuda().eval()
data = torch.zeros(1, 3, 224, 224).cuda()
model_trt = torch2trt(model, [data])

# TEST
data = torch.randn(1, 3, 224, 224).cuda()
print(torch.max(torch.abs(model_trt(data) - model(data)))

Please let me know if this works for you and I’ll work on a pull-request to integrate these converters into the project.

Best, John

0reactions
ooodragon94commented, Jun 25, 2021

Your explanation rocks. Thank you so much for your kind and thorough response!

Read more comments on GitHub >

github_iconTop Results From Across the Web

'Tensor' object has no attribute '_trt' #15 - GitHub
I get error AttributeError: 'Tensor' object has no attribute '_trt' when I meet MaxPool2d.
Read more >
'Tensor' object has no attribute 'numpy' while extending keras ...
AttributeError : 'Tensor' object has no attribute 'numpy' while extending keras sequential model ; # encoders is a bunch of Dense layers encoder....
Read more >
AttributeError: 'Tensor' object has no attribute '_trt'的一种可能性
1. 遇到的问题在自己搭建模型的时候,遇到了这个问题,这个问题的意思,归根结底是:keras中定义的tensor和tensorflow(theano) ...
Read more >
'Tensor' object has no attribute or method 'forward' - jit
Hi everyone. I am working on the CLIP project. I am trying to parse models from it, script them and save them, but...
Read more >
IExecutionContext — NVIDIA TensorRT Standard Python API ...
Trivially true if network has no dynamically shaped input tensors. ... Application-implemented error reporting interface for TensorRT objects.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found