register_forward_hooks (and register_backward_hooks) currently aren’t supported in TorchScript. If you’d like to see them added, please file a feature request on GitHub.
In your particular case it sounds like you’re inheriting from ScriptModule as the way to access the TorchScript compiler. An API change in v1.2.0 lets you compile nn.Modules without making them inherit from ScriptModule directly, see these docs for details:
import torch
import torch.nn as nn
class M(nn.Module):
def __init__(self):
super().__init__()
self.conv = nn.Conv2d(5, 5, 2)
def forward(self, x):
return self.conv(x) + 10
def my_hook(self, *args):
print("Hello from my_hook")
m = M()
m.conv.register_forward_hook(my_hook)
# `my_hook` will be called
m(torch.randn(5, 5, 2, 2))
a_scripted_module = torch.jit.script(m)
# `my_hook` will NOT be called, forward hooks are lost
# when an `nn.Module` is compiled
a_scripted_module(torch.randn(5, 5, 2, 2))