Modify output forward hooks

According to the V1.1.0 docs it’s possible to modify a module output using forward hooks.

However sourcecode is not consistent:

    def __call__(self, *input, **kwargs):
        for hook in self._forward_pre_hooks.values():
            hook(self, input)
        if torch._C._get_tracing_state():
            result = self._slow_forward(*input, **kwargs)
            result = self.forward(*input, **kwargs)
        for hook in self._forward_hooks.values():
            hook_result = hook(self, input, result)
            if hook_result is not None:
                raise RuntimeError(
                    "forward hooks should never return any values, but '{}'"
                    "didn't return None".format(hook))

As you can see if hook_result is not None, it throws an error.
What did I miss?

1 Like