Assertion and Testing

Chainer provides some facilities to make debugging easy.

Type checking utilities

FunctionNode uses a systematic type checking of the chainer.utils.type_check module. It enables users to easily find bugs of forward and backward implementations. You can find examples of type checking in some function implementations.

chainer.utils.type_check.Expr Abstract syntax tree of an expression.
chainer.utils.type_check.expect Evaluates and tests all given expressions.
chainer.utils.type_check.TypeInfo Type information of an input/gradient array.
chainer.utils.type_check.TypeInfoTuple Type information of input/gradient tuples.

Gradient checking utilities

Most function implementations are numerically tested by gradient checking. This method computes numerical gradients of forward routines and compares their results with the corresponding backward routines. It enables us to make the source of issues clear when we hit an error of gradient computations. The chainer.gradient_check module makes it easy to implement the gradient checking.

chainer.gradient_check.check_backward Test backward procedure of a given function.
chainer.gradient_check.check_double_backward Test twice differentiation of a given procedure.
chainer.gradient_check.numerical_grad Computes numerical gradient by finite differences.

Standard Assertions

The assertions have same names as NumPy’s ones. The difference from NumPy is that they can accept both numpy.ndarray and cupy.ndarray.

chainer.testing.assert_allclose Asserts if some corresponding element of x and y differs too much.
chainer.testing.assert_warns

Function testing utilities

Utilities for testing functions.

chainer.testing.FunctionTestCase A base class for function test cases.
chainer.testing.unary_math_function_unittest Decorator for testing unary mathematical Chainer functions.

Serialization testing utilities

Utilities for testing serializable objects.

chainer.testing.save_and_load Saves src and loads it to dst using a de/serializer.
chainer.testing.save_and_load_hdf5 Saves src to an HDF5 file and loads it to dst.
chainer.testing.save_and_load_npz Saves src to an NPZ file and loads it to dst.

Trainer Extension Testing Utilities

Utilities for testing trainer extensions.

chainer.testing.get_trainer_with_mock_updater Returns a Trainer object with mock updater.

Repeat decorators

These decorators have a decorated test run multiple times in a single invocation. Criteria of passing / failing of the test changes according to the type of decorators. See the documentation of each decorator for details.

chainer.testing.condition.repeat_with_success_at_least Decorator for multiple trial of the test case.
chainer.testing.condition.repeat Decorator that imposes the test to be successful in a row.
chainer.testing.condition.retry Decorator that imposes the test to be successful at least once.

Unit test annotation

Decorators for annotating unit tests.

chainer.testing.attr.gpu Decorator to indicate that GPU is required to run the test.
chainer.testing.attr.multi_gpu Decorator to indicate number of GPUs required to run the test.
chainer.testing.with_requires Run a test case only when given requirements are satisfied.
chainer.testing.fix_random Decorator that fixes random numbers in a test.