Repository: pytorch
Issue: TracerWarning When Using Tensor Size in Torchscript Trace
Labels: oncall: jit,triaged

## 🐛 Bug

When the size of a tensor during a Torchscript trace is used to compare the size of a dimension against a Python integer, a tracer warning is issued stating that converting a tensor to a Python boolean might cause the trace to be incorrect. A tensor is not being converted to a boolean, the output from size() or shape is a torch.Size object and not a tensor so this operation should be safe or the warning message is not correct.

## To Reproduce

The following script will reproduce the behaviour, specifically the second assert where the size of dimension 0 is being compared to the Python literal 0:

    import torch
    class JITTest(torch.nn.Module):
        def forward(self,x):
            size=x.size() # or x.shape
            assert not isinstance(size,torch.Tensor)
            assert size[0]>0 # <- TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect.
            return x
            
    net=JITTest()
    scripted=torch.jit.trace(net,torch.rand((10,10)))

Output is: 

        TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!

This appears to be stating that `size` is a tensor and that the operation `size[0] > 0` is implicitly converting the tensor to a boolean using the `>` operator. Either trace has misidentified `size` as a tensor or this operation is not properly traceable but the issued warning incorrectly states what the problem is.

## Expected behaviour

I expected no warning to be issued and that any operation on the size of a tensor to be correct. I would expect code like the following to produce a correct trace:

    if x.shape[0] == 1:
        do_something_for_single_channel()
    else:
        do_something_for_multi_channel()

## Environment

PyTorch version: 1.3.1
Is debug build: No
CUDA used to build PyTorch: 10.1.243

OS: Ubuntu 16.04.6 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
CMake version: version 3.12.1

Python version: 3.7
Is CUDA available: Yes
CUDA runtime version: Could not collect
GPU models and configuration: 
GPU 0: GeForce GTX 980
GPU 1: TITAN X (Pascal)

Nvidia driver version: 418.67
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.5.1

Versions of relevant libraries:
[pip] Could not collect
[conda] _pytorch_select           0.1                       cpu_0  
[conda] blas                      1.0                         mkl  
[conda] ignite                    0.2.1                    py37_0    pytorch
[conda] mkl                       2019.4                      243  
[conda] mkl-service               2.3.0            py37he904b0f_0  
[conda] mkl_fft                   1.0.15           py37ha843d7b_0  
[conda] mkl_random                1.1.0            py37hd6b4f25_0  
[conda] pytorch                   1.3.1           py3.7_cuda10.1.243_cudnn7.6.3_0    pytorch
[conda] torchvision               0.4.2           cpu_py37h9ec355b_0


cc @suo