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


class OpTestMeta(type):
Y
Yu Yang 已提交
7 8
    """
    Operator Test ClassMeta.
9 10

    It injects `test_all` method into user's OperatorTest class, to make Python
Y
Yu Yang 已提交
11
    unittest module run that method.
12

Y
Yu Yang 已提交
13 14
    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.
15

Y
Yu Yang 已提交
16 17 18
    See `test_add_two_op` for example usage.
    """

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

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

            for place in places:
Y
Yancey1989 已提交
30
                for in_name, in_dup in Operator.get_op_inputs(self.type):
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
                    if hasattr(self, 'inputs') and in_name in self.inputs:
                        kwargs[in_name] = []
                        if in_dup:
                            arrays = self.inputs[in_name]
                            for index, arr in enumerate(arrays):
                                var = scope.new_var(in_name + str(index))
                                tensor = var.get_tensor()
                                tensor.set_dims(arr.shape)
                                tensor.set(arr, place)
                                kwargs[in_name].append(in_name + str(index))
                        else:
                            kwargs[in_name] = in_name
                            var = scope.new_var(in_name).get_tensor()
                            arr = self.inputs[in_name]
                            var.set_dims(arr.shape)
                            var.set(arr, place)
Q
qijun 已提交
47 48
                    else:
                        kwargs[in_name] = "@EMPTY@"
Y
Yu Yang 已提交
49

Y
Yancey1989 已提交
50
                for out_name, out_dup in Operator.get_op_outputs(self.type):
D
dangqingqing 已提交
51 52 53 54
                    if not hasattr(self, "outputs"):
                        raise ValueError(
                            "The test op must set self.outputs dict.")
                    if out_name not in self.outputs:
D
dangqingqing 已提交
55
                        raise ValueError("The %s is not in self.outputs dict." %
D
dangqingqing 已提交
56 57 58
                                         (out_name))
                    kwargs[out_name] = out_name
                    scope.new_var(out_name).get_tensor()
Y
Yu Yang 已提交
59

Y
Yu Yang 已提交
60
                for attr_name in Operator.get_op_attr_names(self.type):
D
dangqingqing 已提交
61 62
                    if hasattr(self, "attrs") and attr_name in self.attrs:
                        kwargs[attr_name] = self.attrs[attr_name]
Y
Yu Yang 已提交
63

Y
Yu Yang 已提交
64
                op = Operator(self.type, **kwargs)
65 66
                if isinstance(place, core.GPUPlace) and not op.support_gpu():
                    return
Y
Yu Yang 已提交
67

Q
qijun 已提交
68
                op.infer_shape(scope)
Y
Yu Yang 已提交
69

Q
qijun 已提交
70 71
                ctx = core.DeviceContext.create(place)
                op.run(scope, ctx)
Y
Yu Yang 已提交
72

Y
Yancey1989 已提交
73
                for out_name, out_dup in Operator.get_op_outputs(self.type):
Q
qijun 已提交
74
                    actual = numpy.array(scope.find_var(out_name).get_tensor())
D
dangqingqing 已提交
75
                    expect = self.outputs[out_name]
Q
Qiao Longfei 已提交
76
                    self.assertTrue(
77
                        numpy.allclose(
D
dangqingqing 已提交
78
                            actual, expect, atol=1e-05),
79
                        "output name: " + out_name + " has diff")
Y
Yu Yang 已提交
80 81 82

        obj.test_all = test_all
        return obj