op_test_util.py 2.7 KB
Newer Older
Y
Yu Yang 已提交
1 2 3
import paddle.v2.framework.core as core
import unittest
import numpy
Y
Yu Yang 已提交
4
from paddle.v2.framework.op import Operator
Y
Yu Yang 已提交
5 6 7


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
    def __new__(cls, name, bases, attrs):
        obj = super(OpTestMeta, cls).__new__(cls, name, bases, attrs)

        def test_all(self):
Y
Yu Yang 已提交
24
            scope = core.Scope()
Y
Yu Yang 已提交
25
            kwargs = dict()
Y
Yu Yang 已提交
26
            places = [core.CPUPlace()]
Q
Qiao Longfei 已提交
27
            if core.is_compile_gpu() and core.Operator.support_gpu(self.type):
Q
qijun 已提交
28 29 30
                places.append(core.GPUPlace(0))

            for place in places:
Y
Yu Yang 已提交
31
                for in_name in Operator.get_op_input_names(self.type):
Y
Yu Yang 已提交
32
                    if hasattr(self, "inputs") and in_name in self.inputs:
Q
qijun 已提交
33
                        kwargs[in_name] = in_name
Q
qijun 已提交
34
                        var = scope.new_var(in_name).get_tensor()
D
dangqingqing 已提交
35
                        arr = self.inputs[in_name]
Q
qijun 已提交
36 37 38 39
                        var.set_dims(arr.shape)
                        var.set(arr, place)
                    else:
                        kwargs[in_name] = "@EMPTY@"
Y
Yu Yang 已提交
40

Y
Yu Yang 已提交
41
                for out_name in Operator.get_op_output_names(self.type):
D
dangqingqing 已提交
42 43 44 45
                    if not hasattr(self, "outputs"):
                        raise ValueError(
                            "The test op must set self.outputs dict.")
                    if out_name not in self.outputs:
D
dangqingqing 已提交
46
                        raise ValueError("The %s is not in self.outputs dict." %
D
dangqingqing 已提交
47 48 49
                                         (out_name))
                    kwargs[out_name] = out_name
                    scope.new_var(out_name).get_tensor()
Y
Yu Yang 已提交
50

Y
Yu Yang 已提交
51
                for attr_name in Operator.get_op_attr_names(self.type):
D
dangqingqing 已提交
52 53
                    if hasattr(self, "attrs") and attr_name in self.attrs:
                        kwargs[attr_name] = self.attrs[attr_name]
Y
Yu Yang 已提交
54

Y
Yu Yang 已提交
55
                op = Operator(self.type, **kwargs)
Y
Yu Yang 已提交
56

Q
qijun 已提交
57
                op.infer_shape(scope)
Y
Yu Yang 已提交
58

Q
qijun 已提交
59 60
                ctx = core.DeviceContext.create(place)
                op.run(scope, ctx)
Y
Yu Yang 已提交
61

Y
Yu Yang 已提交
62
                for out_name in Operator.get_op_output_names(self.type):
Q
qijun 已提交
63
                    actual = numpy.array(scope.find_var(out_name).get_tensor())
D
dangqingqing 已提交
64
                    expect = self.outputs[out_name]
Q
Qiao Longfei 已提交
65 66 67
                    self.assertTrue(
                        numpy.allclose(actual, expect),
                        "output name: " + out_name + "has diff")
Y
Yu Yang 已提交
68 69 70

        obj.test_all = test_all
        return obj