Skip to content

🐛 [Bug] Slicing from ModuleList with Negative Value #1473

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

Closed
collinmccarthy opened this issue Nov 22, 2022 · 2 comments
Closed

🐛 [Bug] Slicing from ModuleList with Negative Value #1473

collinmccarthy opened this issue Nov 22, 2022 · 2 comments
Assignees
Labels
bug Something isn't working component: converters Issues re: Specific op converters No Activity

Comments

@collinmccarthy
Copy link

Bug Description

(Related to #1087 which works for me, but this bug still remains).

I'm working with EfficientDet, and I'm having an issue during compilation with this line:

def forward(self, x: List[torch.Tensor]) -> List[torch.Tensor]:
    for fn in self.fnode:
        x.append(fn(x))
            
    return x[-self.num_levels::]  # <-- ERROR HERE

which gives the error

RuntimeError: [Error thrown at ./core/conversion/var/Var_inl.h:38] Expected ivalue->isInt() to be true but got false
Requested unwrapping of arg IValue assuming it was l however type is NoneType

The solution was to change the code above to

def forward(self, x: List[torch.Tensor]) -> List[torch.Tensor]:
    for fn in self.fnode:
        x.append(fn(x))
            
    # return x[-self.num_levels::]  # <-- ERROR HERE
    return [x[i] for i in range(-self.num_levels, 0)]  # WORKS

I have a MWE to reproduce the issue below.

To Reproduce

Run the following, which outputs the same error as shown above.

from typing import List
import torch
import torch.nn as nn
import torch_tensorrt

class Node(nn.Module):
    def __init__(self):
        super().__init__()
        self.node = nn.Linear(10, 10)

    def forward(self, x: List[torch.Tensor]) -> torch.Tensor:
        return self.node(x[-1])

class Decoder(nn.Module):
    def __init__(self, num_levels: int):
        super().__init__()
        self.keep_levels = num_levels
        self.nodes = nn.ModuleList([Node() for _ in range(num_levels + 2)])

    def forward(self, in_features: List[torch.Tensor]) -> List[torch.Tensor]:
        for node in self.nodes:
            in_features.append(node(in_features))

        return in_features[-self.keep_levels:]  # Doesn't work
        # return in_features[self.num_levels-self.keep_levels:self.num_levels]  # Works
        # return [in_features[i] for i in range(-self.keep_levels, 0)] # Works

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        num_levels = 3
        self.levels = nn.ModuleList([nn.Linear(10, 10) for _ in range(num_levels)])
        self.decoder = Decoder(num_levels)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        features: List[torch.Tensor] = []
        for level in self.levels:
            features.append(level(x))

        return self.decoder(features)[-1]

def reproduce_error():
    torch_tensorrt.logging.set_reportable_log_level(torch_tensorrt.logging.Level.Graph)
    model = Model().eval().cuda()

    x = torch.randn(1, 10).cuda()
    test_output = model.forward(x)
    print(test_output)
    print(torch.jit.script(model).graph)
    trt_model = torch_tensorrt.compile(model, inputs=[x])
    converted_output = trt_model.forward(x)
    print(converted_output)

reproduce_error()

Expected behavior

No error message, should print an tensor as output.

Environment

Build information about Torch-TensorRT can be found by turning on debug messages

  • Torch-TensorRT Version (e.g. 1.0.0): 1.3.0a0+0471f2d2
  • PyTorch Version (e.g. 1.0): 1.13.0.dev20221003+cu116
  • CPU Architecture: x86_64
  • OS (e.g., Linux): Ubuntu 20.04 LTS
  • How you installed PyTorch (conda, pip, libtorch, source): pip
  • Build command you used (if compiling from source):
  • Are you using local sources or building from archives: local
  • Python version: 3.10
  • CUDA version: 11.6
  • GPU models and configuration: TitanV
  • Any other relevant information:
@collinmccarthy collinmccarthy added the bug Something isn't working label Nov 22, 2022
@narendasan narendasan added the component: converters Issues re: Specific op converters label Dec 5, 2022
@peri044
Copy link
Collaborator

peri044 commented Dec 13, 2022

I think the error might be coming from here

int64_t end = args.at(n->input(2)).unwrapToInt();
int64_t step = args.at(n->input(3)).unwrapToInt();

end and step are expecting integers but the debug log of your program shows

%4 : int = prim::Constant[value=1]() # test.py:24:15
  %3 : NoneType = prim::Constant()
  %2 : int = prim::Constant[value=-3]()
%44 : Tensor[] = aten::slice(%features.1, %2, %3, %4) # test.py:24:15

%3 is NoneType which might be the problem here

@github-actions
Copy link

This issue has not seen activity for 90 days, Remove stale label or comment or this will be closed in 10 days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working component: converters Issues re: Specific op converters No Activity
Projects
None yet
Development

No branches or pull requests

4 participants