The assigned one will be rewritten to the assigned one. Below are the experimental results.
Colab
#Define func
def func(x, y):
  return x - y
func(1, 2) # -1
#Define func0
def func0():
  return None
func0() #No output
#Substitute the function name for the function name
func0 = func
func(1, 3) # -2
func0(1, 3) # -2
For some reason, the __call__method of the Module class of PyTorch was set to be set with _call_impl once and then assigned to __call__ as described above.
Can anyone please tell me why PyTorch made such a troublesome description (seriously). Below is the source code.
Python:torch.nn.modules.module
def _call_impl(self, *input, **kwargs):
        for hook in itertools.chain(
                _global_forward_pre_hooks.values(),
                self._forward_pre_hooks.values()):
            result = hook(self, input)
            if result is not None:
                if not isinstance(result, tuple):
                    result = (result,)
                input = result
        if torch._C._get_tracing_state():
            result = self._slow_forward(*input, **kwargs)
        else:
            result = self.forward(*input, **kwargs)
        for hook in itertools.chain(
                _global_forward_hooks.values(),
                self._forward_hooks.values()):
            hook_result = hook(self, input, result)
            if hook_result is not None:
                result = hook_result
        if (len(self._backward_hooks) > 0) or (len(_global_backward_hooks) > 0):
            var = result
            while not isinstance(var, torch.Tensor):
                if isinstance(var, dict):
                    var = next((v for v in var.values() if isinstance(v, torch.Tensor)))
                else:
                    var = var[0]
            grad_fn = var.grad_fn
            if grad_fn is not None:
                for hook in itertools.chain(
                        _global_backward_hooks.values(),
                        self._backward_hooks.values()):
                    wrapper = functools.partial(hook, self)
                    functools.update_wrapper(wrapper, hook)
                    grad_fn.register_hook(wrapper)
        return result
    __call__ : Callable[..., Any] = _call_impl
        Recommended Posts