Hi:)
I have written an nn.Module that works fine if used as a plain Python object but as soon as I jit.script / compile it, it throws at runtime:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.DoubleTensor [10, 8]], which is output 0 of SelectBackward, is at version 3; expected version 2 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

Why does this only occur after compilation? Naively, I would understand if such an error occur if not compiled and then gets optimized away by compiling. But here the compiler seemingly introduces this error. Could this be a bug in the compiler?
Thanks for your help!
EDIT
I managed to bisect my code and reduce it to this snippet:
debug = False

model = MyFancyModel()
if not debug:
    model = torch.jit.script(model)
loss = torch.sum((model(x) - y)**2)
print('#1', loss)
loss.backward()  # works
print('#1 done.')

model = MyFancyModel()
if not debug:
    model = torch.jit.script(model)
loss = torch.sum((model(x) - y)**2)
print('#2', loss)
loss.backward()  # fails
print('#2 done.')

which works fine if debug is set to True but fails on the second call of loss.backward(). I am very confused. MyFancyModel() shouldn’t carry any static state and I don’t understand why (a) the second invocation of .backward() works w/o compiling but fails if the module is compiled, and (b) why does the first invocation works even with compiling!? Do you have any suggestions how to attack this problem?