JIT function changed the default encoding of the built-in `open`

This is a piece of reproduced code.

import sys, torch
@torch.jit.script
def magic_func(x, y):
    return torch.tanh(x + y)
x, y = torch.rand(8, device="cuda"), torch.rand(8, device="cuda")
before_encoding = open(sys.argv[0]).encoding
for _ in range(int(sys.argv[1])):
    output = magic_func(x, y)
after_encoding = open(sys.argv[0]).encoding
print(f'{before_encoding} vs. {after_encoding}')

Test results:

$ python utf8.py 3
UTF-8 vs. ANSI_X3.4-1968
$ python utf8.py 2
UTF-8 vs. ANSI_X3.4-1968
$ python utf8.py 1
UTF-8 vs. UTF-8

What’s going on here?

This seems to be an nvrtc issue and you might need to set the encoding explicitly to restore it again as a workaround.

1 Like