pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
84.6k stars 22.79k forks source link

`torch.jit.is_scripting()` not set when scripting a Module #67146

Open awaelchli opened 3 years ago

awaelchli commented 3 years ago

🐛 Bug

The function torch.jit.is_scripting() returns constant False when accessed in a property implemented in a Module.

To Reproduce

class Model(torch.nn.Module):
    @property
    def hello(self):
        if not torch.jit.is_scripting():  # does not work
            warnings.warn("this property is deprecated")

        # prints False
        print(torch.jit.is_scripting())
        return "hello"

    def forward(self, x):
        if torch.jit.is_scripting():  # works
            return x + 1
        return x

jitted_model = torch.jit.script(Model())
print(jitted_model(torch.tensor(2)))  # output: 3

def function(x):
    if torch.jit.is_scripting(): # works
        return x + 1
    return x

jitted_function = torch.jit.script(function)
print(jitted_function(torch.tensor(2)))  # output: 3

This shows a warning despite

  1. the property hello is never used in the code
  2. the guarding with if not torch.jit.is_scripting() is not working

Expected behavior

The code above should not produce any warning from the hello property, because it is never used.

There should be a way to detect if scripting is turned on. The function torch.jit.is_scripting() should return True even outside the forward() function when scripting a Module.

Environment

PyTorch version: 1.10.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 10.15.7 (x86_64)
GCC version: Could not collect
Clang version: 12.0.0 (clang-1200.0.32.29)
CMake version: version 3.20.1
Libc version: N/A

Python version: 3.8.11 (default, Aug  6 2021, 08:56:27)  [Clang 10.0.0 ] (64-bit runtime)
Python platform: macOS-10.15.7-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:

[pip3] torch==1.10.0
[pip3] torchaudio==0.10.0
[pip3] torchmetrics==0.5.0
[pip3] torchvision==0.11.1

Additional context

daniellepintz commented 2 years ago

@ansley @suo @tugsbayasgalan would it be possible to get some help on this issue?

rohitgr7 commented 2 years ago

hey PyTorch team, is there any update on this?

carmocca commented 2 years ago

TorchScript is no longer supported, so I don't think we'll get a core dev to fix this. If we can get a fix in, I assume it will get approved though.

carmocca commented 2 years ago

@suo any chance we can get your thoughts or direction for working on a fix? Thank you!

dashesy commented 1 year ago

This is a bug. There is a code that is throwing this error

NotSupportedError: keyword-arg expansion is not supported:

and I cannot special case the scripting path. I use torch._C._get_tracing_state() to special case when tracing, but it also is False when scripting. And I have to use scripting because of a for-loop

awaelchli commented 1 year ago

If you come across this issue and need a workaround, I had success with this:

import inspect
import os

def is_scripting():
    return any(os.path.join("torch", "jit") in frameinfo.filename for frameinfo in inspect.stack())

This isn't very smart, but should help when in desparate need :)