op_test_util.py 2.3 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7
import paddle.v2.framework.core as core
import unittest
import numpy
import paddle.v2.framework.create_op_creation_methods as creation


class OpTestMeta(type):
Y
Yu Yang 已提交
8 9 10 11 12 13 14 15 16 17 18 19
    """
    Operator Test ClassMeta.
    
    It injects `test_all` method into user's OperatorTest class, to make Python 
    unittest module run that method.
    
    The `test_all` read what value is stored in `self`. It use self's values to
    create and run a operator, and check whether that op is OK or not.
    
    See `test_add_two_op` for example usage.
    """

Y
Yu Yang 已提交
20 21 22 23 24 25 26 27
    def __new__(cls, name, bases, attrs):
        obj = super(OpTestMeta, cls).__new__(cls, name, bases, attrs)

        def test_all(self):
            func = getattr(creation.op_creations, self.type, None)
            self.assertIsNotNone(func)

            scope = core.Scope(None)
Q
qijun 已提交
28
            place = core.CPUPlace()
Y
Yu Yang 已提交
29 30 31 32 33 34 35 36
            kwargs = dict()

            for in_name in func.all_input_args:
                if hasattr(self, in_name):
                    kwargs[in_name] = in_name
                    var = scope.create_var(in_name).get_tensor()
                    arr = getattr(self, in_name)
                    var.set_dims(arr.shape)
Q
qijun 已提交
37
                    var.set(arr, place)
Y
Yu Yang 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
                else:
                    kwargs[in_name] = "@EMPTY@"

            for out_name in func.all_output_args:
                if hasattr(self, out_name):
                    kwargs[out_name] = out_name
                    scope.create_var(out_name).get_tensor()

            for attr_name in func.all_attr_args:
                if hasattr(self, attr_name):
                    kwargs[attr_name] = getattr(self, attr_name)

            op = func(**kwargs)

            op.infer_shape(scope)

            ctx = core.DeviceContext.cpu_context()
            op.run(scope, ctx)

            for out_name in func.all_output_args:
                actual = numpy.array(scope.get_var(out_name).get_tensor())
                expect = getattr(self, out_name)
Q
qijun 已提交
60 61 62 63
                # TODO(qijun) The default decimal is 7, but numpy.dot and eigen.mul
                # has some diff, and could not pass unittest. So I set decimal 3 here.
                # And I will check this in future.
                numpy.testing.assert_almost_equal(actual, expect, decimal=3)
Y
Yu Yang 已提交
64 65 66

        obj.test_all = test_all
        return obj