chainer.LinkHook

class chainer.LinkHook[source]

Base class of hooks for links.

LinkHook is a callback object that is registered to a Link. Registered link hooks are invoked before and after calling Link.forward() method of each link.

Link hooks that derive from LinkHook may override the following method:

By default, these methods do nothing.

Specifically, when the __call__() method of some link is invoked, forward_preprocess() (resp. forward_postprocess()) of all link hooks registered to this link are called before (resp. after) Link.forward() method of the link.

There are two ways to register LinkHook objects to Link objects.

The first one is to use with statement. Link hooks hooked in this way are registered to all links within with statement and are unregistered at the end of with statement.

Example

The following code is a simple example in which we measure the elapsed time of a part of forward propagation procedure with TimerHook, which is a subclass of LinkHook.

>>> class Model(chainer.Chain):
...   def __init__(self):
...     super(Model, self).__init__()
...     with self.init_scope():
...       self.l = L.Linear(10, 10)
...   def __call__(self, x1):
...     return F.exp(self.l(x1))
>>> model1 = Model()
>>> model2 = Model()
>>> x = chainer.Variable(np.zeros((1, 10), np.float32))
>>> with chainer.link_hooks.TimerHook() as m:
...   _ = model1(x)
...   y = model2(x)
>>> model3 = Model()
>>> z = model3(y)
>>> print('Total time : {}'.format(m.total_time()))
... # doctest:+ELLIPSIS
Total time : ...

In this example, we measure the elapsed times for each forward propagation of all functions in model1 and model2. Note that model3 is not a target measurement as TimerHook is unregistered before forward propagation of model3.

Note

Chainer stores the dictionary of registered link hooks as a thread local object. So, link hooks registered are different depending on threads.

The other one is to register directly to a Link object by calling its add_hook() method. Link hooks registered in this way can be removed by delete_hook() method. Contrary to former registration method, link hooks are registered only to the link which add_hook() is called.

Parameters

name (str) – Name of this link hook.

Methods

__enter__()[source]
__exit__(*_)[source]
added(link)[source]

Callback function invoked when the link hook is registered

Parameters

link (Link) – Link object to which the link hook is registered. None if the link hook is registered globally.

deleted(link)[source]

Callback function invoked when the link hook is unregistered

Parameters

link (Link) – Link object to which the link hook is unregistered. None if the link hook had been registered globally.

forward_postprocess(args)[source]

Callback function invoked after a forward call of a link.

Parameters

args

Callback data. It has the following attributes:

  • link (Link)

    Link object.

  • forward_name (str)

    Name of the forward method.

  • args (tuple)

    Non-keyword arguments given to the forward method.

  • kwargs (dict)

    Keyword arguments given to the forward method.

  • out

    Return value of the forward method.

forward_preprocess(args)[source]

Callback function invoked before a forward call of a link.

Parameters

args

Callback data. It has the following attributes:

  • link (Link)

    Link object.

  • forward_name (str)

    Name of the forward method.

  • args (tuple)

    Non-keyword arguments given to the forward method.

  • kwargs (dict)

    Keyword arguments given to the forward method.

Attributes

name = 'LinkHook'