Another weird bug of torchscript: it thinks my python bool is a tensor but it's not

TorchScript Errors are usually very informative.

EMPTY_FLOAT = torch.zeros(0).float()

class Bug(nn.Module):

    def __init__(self):
        super(Bug, self).__init__()

    def forward(self,
                my_bool: bool,
                x=EMPTY_FLOAT):

        if my_bool:
            y = x
        else:
            y = EMPTY_FLOAT

Then run

bug = Bug()
bug_script = torch.jit.script(bug)

Then comes the compile time error

RuntimeError: 
python value of type 'Tensor' cannot be used as a value:
  File "<ipython-input-310-549c5631a7a3>", line 15
    
        if my_bool:
        ~~~~~~~~~~~...  <--- HERE
            
            y = x

The problem seems to be EMPTY_FLOAT if I use torch.zeros(0).float() instead it compiles correctly.

Very strange, do you mind filing an issue on GitHub and we can follow up there? Thanks!

1 Like