op_test.py 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import unittest
import numpy as np
import itertools
import paddle.v2.framework.core as core
from paddle.v2.framework.op import Operator


def grad_var_name(var_name):
    return var_name + "@GRAD"


Q
qijun 已提交
12
def create_op(scope, op_type, inputs, outputs, attrs):
13 14
    kwargs = dict()

Q
qijun 已提交
15
    for in_name, in_dup in Operator.get_op_inputs(op_type):
16 17 18 19
        if in_name in inputs:
            kwargs[in_name] = []
            if in_dup:
                sub_in = inputs[in_name]
Q
qijun 已提交
20
                for sub_in_name, _ in sub_in:
21 22 23 24 25 26
                    var = scope.new_var(sub_in_name)
                    kwargs[in_name].append(sub_in_name)
            else:
                var = scope.new_var(in_name)
                kwargs[in_name].append(in_name)

Q
qijun 已提交
27
    for out_name, out_dup in Operator.get_op_outputs(op_type):
28 29 30
        if out_name in outputs:
            kwargs[out_name] = []
            if out_dup:
31 32 33 34
                sub_out = outputs[out_name]
                for sub_out_name, _ in sub_out:
                    var = scope.new_var(sub_out_name)
                    kwargs[out_name].append(sub_out_name)
35 36 37 38
            else:
                var = scope.new_var(out_name)
                kwargs[out_name].append(out_name)

Q
qijun 已提交
39
    for attr_name in Operator.get_op_attr_names(op_type):
Q
qijun 已提交
40 41
        if attr_name in attrs:
            kwargs[attr_name] = attrs[attr_name]
42

43 44 45 46
    return Operator(op_type, **kwargs)


def set_input(scope, op, inputs, place):
Q
qijun 已提交
47
    for in_name, in_dup in Operator.get_op_inputs(op.type()):
48 49 50
        if in_name in inputs:
            if in_dup:
                sub_in = inputs[in_name]
51
                for sub_in_name, sub_in_val in sub_in:
52 53
                    var = scope.find_var(sub_in_name)
                    tensor = var.get_tensor()
54 55
                    sub_in_array = sub_in_val[0] \
                        if isinstance(sub_in_val, tuple) else sub_in_val
Q
qijun 已提交
56 57
                    tensor.set_dims(sub_in_array.shape)
                    tensor.set(sub_in_array, place)
58 59
                    if isinstance(sub_in_val, tuple):
                        tensor.set_lod(sub_in_val[1])
60 61 62
            else:
                var = scope.find_var(in_name)
                tensor = var.get_tensor()
63 64 65 66 67 68
                in_val = inputs[in_name]
                in_array = in_val[0] if isinstance(in_val, tuple) else in_val
                tensor.set_dims(in_array.shape)
                tensor.set(in_array, place)
                if isinstance(in_val, tuple):
                    tensor.set_lod(in_val[1])
69 70 71


def set_output_grad(scope, op, outputs, place):
72 73 74 75 76 77 78 79 80 81 82 83 84
    def __set_tensor__(name):
        out_tensor = scope.find_var(name).get_tensor()
        grad_tensor = scope.new_var(grad_var_name(name)).get_tensor()
        out_dtype = out_tensor.dtype()
        if out_dtype == core.DataType.FP64:
            data = np.ones(out_tensor.shape(), dtype=np.float64)
        elif out_dtype == core.DataType.FP32:
            data = np.ones(out_tensor.shape(), dtype=np.float32)
        else:
            raise ValueError("Not supported data type " + str(out_dtype))

        grad_tensor.set(data, place)

Q
qijun 已提交
85
    for out_name, out_dup in Operator.get_op_outputs(op.type()):
86 87 88
        if out_name in outputs:
            if out_dup:
                sub_out = outputs[out_name]
89
                for sub_out_name, _ in sub_out:
90
                    __set_tensor__(sub_out_name)
91
            else:
92
                __set_tensor__(out_name)
93 94 95 96 97 98


def get_numeric_gradient(scope,
                         op,
                         inputs,
                         input_to_check,
Y
Yancey 已提交
99
                         output_names,
100 101 102 103 104 105 106 107 108 109 110 111
                         delta=0.005,
                         in_place=False):
    set_input(scope, op, inputs, core.CPUPlace())

    tensor_to_check = scope.find_var(input_to_check).get_tensor()

    def product(dim):
        return reduce(lambda a, b: a * b, dim, 1)

    ctx = core.DeviceContext.create(core.CPUPlace())

    def get_output():
Y
Yancey 已提交
112 113 114 115 116
        sum = 0.0
        for output_name in output_names:
            op.run(scope, ctx)
            sum += np.array(scope.find_var(output_name).get_tensor()).sum()
        return sum
117 118 119

    tensor_to_check = scope.find_var(input_to_check).get_tensor()
    tensor_size = product(tensor_to_check.get_dims())
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    tensor_to_check_dtype = tensor_to_check.dtype()
    if tensor_to_check_dtype == core.DataType.FP32:
        tensor_to_check_dtype = np.float32
    elif tensor_to_check_dtype == core.DataType.FP64:
        tensor_to_check_dtype = np.float64
    else:
        raise ValueError("Not supported data type " + str(
            tensor_to_check_dtype))

    gradient_flat = np.zeros(shape=(tensor_size, ), dtype=tensor_to_check_dtype)

    def __get_elem__(tensor, i):
        if tensor_to_check_dtype == np.float32:
            return tensor.get_float_element(i)
        else:
            return tensor.get_double_element(i)

    def __set_elem__(tensor, i, e):
        if tensor_to_check_dtype == np.float32:
            tensor.set_float_element(i, e)
        else:
            tensor.set_double_element(i, e)

143 144 145 146
    # we only compute gradient of one element each time.
    # we use a for loop to compute the gradient of every element.
    for i in xrange(tensor_size):
        if in_place:
Q
qijun 已提交
147
            set_input(scope, op, inputs, core.CPUPlace())
148 149

        # get one input element throw it's index i.
150
        origin = __get_elem__(tensor_to_check, i)
151 152
        # add delta to it, run op and then get the sum of the result tensor.
        x_pos = origin + delta
153
        __set_elem__(tensor_to_check, i, x_pos)
154 155 156
        y_pos = get_output()

        if in_place:
Q
qijun 已提交
157
            set_input(scope, op, inputs, core.CPUPlace())
158 159

        x_neg = origin - delta
160
        __set_elem__(tensor_to_check, i, x_neg)
161 162
        y_neg = get_output()

163
        __set_elem__(tensor_to_check, i, origin)
164 165 166 167 168 169 170
        gradient_flat[i] = (y_pos - y_neg) / delta / 2

    return gradient_flat.reshape(tensor_to_check.get_dims())


def get_backward_op(scope, op, no_grad_set):
    backward_op = core.Operator.backward(op, no_grad_set)
Q
qijun 已提交
171
    for input in backward_op.input_vars():
172 173
        var = scope.new_var(input)
        var.get_tensor()
Q
qijun 已提交
174
    for output in backward_op.output_vars():
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        var = scope.new_var(output)
        var.get_tensor()
    return backward_op


def get_gradient(scope, op, inputs, outputs, grad_name, place,
                 no_grad_set=None):
    ctx = core.DeviceContext.create(place)

    set_input(scope, op, inputs, place)

    op.run(scope, ctx)

    if no_grad_set is None:
        no_grad_set = set()

    backward_op = get_backward_op(scope, op, no_grad_set)
    set_output_grad(scope, op, outputs, place)

    backward_op.run(scope, ctx)

    out = np.array(scope.find_var(grad_name).get_tensor())
    return out


class OpTest(unittest.TestCase):
201
    def check_output_with_place(self, place, atol):
202
        self.scope = core.Scope()
Q
qijun 已提交
203
        op_inputs = self.inputs if hasattr(self, "inputs") else dict()
204
        op_outputs = self.outputs if hasattr(self, "outputs") else dict()
Q
qijun 已提交
205
        op_attrs = self.attrs if hasattr(self, "attrs") else dict()
206
        self.op = create_op(self.scope, self.op_type, op_inputs, op_outputs,
Q
qijun 已提交
207
                            op_attrs)
208 209 210 211 212 213
        if isinstance(place, core.GPUPlace) and not self.op.support_gpu():
            return
        set_input(self.scope, self.op, self.inputs, place)
        ctx = core.DeviceContext.create(place)
        self.op.run(self.scope, ctx)

Q
qijun 已提交
214
        for out_name, out_dup in Operator.get_op_outputs(self.op.type()):
215 216 217
            if out_name not in self.outputs:
                continue

218 219
            if out_dup:
                sub_out = self.outputs[out_name]
Y
Yancey 已提交
220 221 222 223 224
                if not isinstance(sub_out, list):
                    raise AssertionError("sub_out type %s is not list",
                                         type(sub_out))

                for sub_out_name, expect in sub_out:
225 226 227 228
                    actual = np.array(
                        self.scope.find_var(sub_out_name).get_tensor())
                    self.assertTrue(
                        np.allclose(
229 230
                            actual, expect, atol=atol),
                        "output name: " + out_name + " has diff.")
231 232 233
            else:
                actual = np.array(self.scope.find_var(out_name).get_tensor())
                expect = self.outputs[out_name]
234

235 236
                self.assertTrue(
                    np.allclose(
237 238
                        actual, expect, atol=atol),
                    "output name: " + out_name + " has diff.")
239

240
    def check_output(self, atol=1e-5):
Q
qijun 已提交
241
        places = [core.CPUPlace()]
Q
qijun 已提交
242
        if core.is_compile_gpu():
Q
qijun 已提交
243 244
            places.append(core.GPUPlace(0))
        for place in places:
245
            self.check_output_with_place(place, atol)
Q
qijun 已提交
246

247 248 249 250 251 252 253 254 255 256 257 258
    def __assert_is_close(self, numeric_grads, analytic_grads, names,
                          max_relative_error, msg_prefix):

        for a, b, name in itertools.izip(numeric_grads, analytic_grads, names):
            abs_a = np.abs(a)
            abs_a[abs_a < 1e-3] = 1

            diff_mat = np.abs(a - b) / abs_a
            max_diff = np.max(diff_mat)

            def err_msg():
                offset = np.argmax(diff_mat > max_relative_error)
259 260 261 262
                return ("%s Variable %s max gradient diff %f over limit %f, "
                        "the first error element is %d") % (
                            msg_prefix, name, max_diff, max_relative_error,
                            offset)
263 264 265 266 267

            self.assertLessEqual(max_diff, max_relative_error, err_msg())

    def check_grad(self,
                   inputs_to_check,
Y
Yancey 已提交
268
                   output_names,
269 270 271 272
                   no_grad_set=None,
                   in_place=False,
                   max_relative_error=0.005):
        self.scope = core.Scope()
Q
qijun 已提交
273
        op_inputs = self.inputs if hasattr(self, "inputs") else dict()
274
        op_outputs = self.outputs if hasattr(self, "outputs") else dict()
Q
qijun 已提交
275
        op_attrs = self.attrs if hasattr(self, "attrs") else dict()
276
        self.op = create_op(self.scope, self.op_type, op_inputs, op_outputs,
Q
qijun 已提交
277
                            op_attrs)
278 279 280
        if no_grad_set is None:
            no_grad_set = set()

Y
Yancey 已提交
281 282 283
        if not type(output_names) is list:
            output_names = [output_names]

284 285 286 287 288 289
        numeric_grads = [
            get_numeric_gradient(
                self.scope,
                self.op,
                self.inputs,
                input_to_check,
Y
Yancey 已提交
290
                output_names,
291 292 293 294 295 296
                in_place=in_place) for input_to_check in inputs_to_check
        ]
        grad_names = [
            grad_var_name(input_to_check) for input_to_check in inputs_to_check
        ]

Q
qijun 已提交
297 298 299 300 301 302
        cpu_place = core.CPUPlace()
        cpu_analytic_grads = [
            get_gradient(self.scope, self.op, self.inputs, self.outputs,
                         grad_name, cpu_place, no_grad_set)
            for grad_name in grad_names
        ]
303

Q
qijun 已提交
304 305 306 307 308 309 310
        self.__assert_is_close(numeric_grads, cpu_analytic_grads, grad_names,
                               max_relative_error,
                               "Gradient Check On %s" % str(cpu_place))

        if core.is_compile_gpu() and self.op.support_gpu():
            gpu_place = core.GPUPlace(0)
            gpu_analytic_grads = [
311
                get_gradient(self.scope, self.op, self.inputs, self.outputs,
Q
qijun 已提交
312
                             grad_name, gpu_place, no_grad_set)
313 314 315
                for grad_name in grad_names
            ]

Q
qijun 已提交
316 317 318 319 320 321 322
            self.__assert_is_close(numeric_grads, gpu_analytic_grads,
                                   grad_names, max_relative_error,
                                   "Gradient Check On %s" % str(gpu_place))

            for c_grad, g_grad, name in itertools.izip(
                    cpu_analytic_grads, gpu_analytic_grads, grad_names):
                self.assertTrue(
Q
qijun 已提交
323
                    np.allclose(
Q
qijun 已提交
324 325
                        c_grad, g_grad, atol=1e-4),
                    "output name: " + name + " has diff")