def __call__(self, *args, **kwargs): # TODO(niboshi): Support link hooks for other forward methods. hooks = chainer._get_link_hooks() if self._n_local_link_hooks > 0: hooks = collections.OrderedDict(hooks) hooks.update(self.local_link_hooks) hooks = hooks.values() # avoid six for performance # Call forward_preprocess hook if hooks: cb_args = link_hook._ForwardPreprocessCallbackArgs( self, 'forward', args, kwargs) for hook in hooks: hook.forward_preprocess(cb_args) # Call the forward function # (See #5078) super().__call__ is used when the method is injected by a # mixin class. To keep backward compatibility, the injected one is # prioritized over forward(). forward = getattr(super(Link, self), '__call__', None) if forward is None: forward = self.forward out = forward(*args, **kwargs) # Call forward_postprocess hook if hooks: cb_args = link_hook._ForwardPostprocessCallbackArgs( self, 'forward', args, kwargs, out) for hook in hooks: hook.forward_postprocess(cb_args) return out
def __call__(self, *args, **kwargs): # TODO(niboshi): Support link hooks for other forward methods. hooks = chainer._get_link_hooks() if self._n_local_link_hooks > 0: hooks = collections.OrderedDict(hooks) hooks.update(self.local_link_hooks) hooks = hooks.values() # avoid six for performance # Call forward_preprocess hook if hooks: cb_args = link_hook._ForwardPreprocessCallbackArgs( self, 'forward', args, kwargs) for hook in hooks: hook.forward_preprocess(cb_args) # Call the forward function # (See #5078) super().__call__ is used when the method is injected by a # mixin class. To keep backward compatibility, the injected one is # prioritized over forward(). forward = getattr(super(Link, self), '__call__', None) if forward is None: forward = self.forward out = forward(*args, **kwargs) # Call forward_postprocess hook if hooks: cb_args = link_hook._ForwardPostprocessCallbackArgs( self, 'forward', args, kwargs, out) for hook in hooks: hook.forward_postprocess(cb_args) return out