test_layer_norm_op.py 8.5 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
import numpy as np

C
chengduoZH 已提交
18
from operator import mul
C
chengduoZH 已提交
19
from op_test import OpTest
C
chengduoZH 已提交
20 21 22
import paddle.v2.fluid.core as core
from paddle.v2.fluid.op import Operator
from paddle.v2.fluid.framework import grad_var_name
C
chengduoZH 已提交
23 24


C
chengduoZH 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
def get_backward_op(scope, op, no_grad_set):
    backward_op = core.Operator.backward(op, no_grad_set)
    for input in backward_op.input_vars():
        var = scope.var(input)
        var.get_tensor()
    for output in backward_op.output_vars():
        var = scope.var(output)
        var.get_tensor()
    return backward_op


def _reference_layer_norm_naive(x, scale, beta, epsilon):
    old_shape = x.shape
    N = x.shape[0]
    D = reduce(mul, old_shape, 1) / N
    x.shape = [N, D]
    mean = np.mean(x, axis=1)
    var = np.var(x, axis=1) + epsilon
    output = scale * np.divide((x - mean.reshape([N, 1])),
                               (np.sqrt(var)).reshape([N, 1])) + beta
    output.shape = old_shape
C
chengduoZH 已提交
46 47 48
    return output, mean, var


C
chengduoZH 已提交
49 50 51 52 53 54 55 56
def _reference_layer_norm_grad(x, grad_y, scale, mean, var, epsilon):
    x_shape = x.shape
    N = x_shape[0]
    D = reduce(mul, x_shape, 1) / N
    grad_y.shape = [N, D]
    x.shape = [N, D]
    mean.shape = [N, 1]
    var.shape = [N, 1]
C
chengduoZH 已提交
57 58 59

    d_scale = np.sum(grad_y).reshape([1, ])
    d_bias = np.sum(((x - mean) * np.sqrt(1 / var)) * grad_y).reshape([1, ])
C
chengduoZH 已提交
60 61 62 63

    dx_end = np.sqrt(1.0 / var) * grad_y

    d_mean_0 = np.sum(-np.sqrt(1.0 / var) * grad_y, axis=1).reshape([N, 1])
C
chengduoZH 已提交
64 65 66 67
    # d_mean_1 = np.sum(-1.0 / var * (x - mean) * grad_y, axis=1).reshape(
    #     [N, 1]) * (-1.0 / D * np.sqrt(1.0 / var) *
    #                np.sum(x - mean, axis=1).reshape([N, 1])).reshape([N, 1])
    d_mean = 1.0 / D * (d_mean_0)
C
chengduoZH 已提交
68 69 70 71 72 73 74 75 76

    d_std = np.sum(-1.0 / var * (x - mean) * grad_y, axis=1).reshape([N, 1]) * (
        1.0 / D * np.sqrt(1.0 / var).reshape([N, 1]) * (x - mean))

    grad_x = scale * (dx_end + d_mean + d_std)

    grad_y.shape = x_shape
    x.shape = x_shape

C
chengduoZH 已提交
77
    return grad_x, d_bias, d_scale
C
chengduoZH 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110


def create_or_get_tensor(scope, var_name, var, place):
    tensor = scope.var(var_name).get_tensor()
    if var is not None:
        assert isinstance(var, np.ndarray)
        tensor.set_lod([[]])
        tensor.set_dims(var.shape)
        tensor.set(var, place)
    return tensor


def set_output_grad(scope, outputs, place, feed_dict=None):
    def __set_tensor__(name, data=None):
        out_tensor = scope.find_var(name).get_tensor()
        grad_tensor = scope.var(grad_var_name(name)).get_tensor()
        out_dtype = out_tensor.dtype()
        if data is None:
            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)

    for output in outputs:
        data = None
        if output in feed_dict:
            data = feed_dict[output]
        __set_tensor__(output, data)


C
chengduoZH 已提交
111
class TestLayerNormdOp(OpTest):
C
chengduoZH 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    def __assert_close(self, tensor, np_array, msg, atol=1e-4):
        self.assertTrue(
            np.allclose(
                np.array(tensor).reshape(np_array.shape), np_array, atol=atol),
            msg)

    def __assert_grad_close(self,
                            tensor,
                            np_array,
                            name,
                            place,
                            max_relative_error=0.02):
        a = np.array(tensor).reshape(np_array.shape)
        b = np_array
        abs_a = np.abs(a)
        abs_a[abs_a < 1e-5] = 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)
            return ("%s Variable %s max gradient diff %f over limit %f, "
                    "the first error element is %d, %f, %f") % (
                        "Gradient Check On %s" % str(place), name, max_diff,
                        max_relative_error, offset, a.flatten()[offset],
                        b.flatten()[offset])

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

    def test_forward_backward(self):
        def test_with_place(place, shape):
            # attr
            epsilon = 0.00001
            x_shape = shape
            scale_shape = [1]
C
chengduoZH 已提交
148
            np.random.random(123)
C
chengduoZH 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            x_val = np.random.random_sample(x_shape).astype(np.float32)
            scale_val = np.random.random_sample(scale_shape).astype(np.float32)
            bias_val = np.random.random_sample(scale_shape).astype(np.float32)

            # run forward
            y_out, saved_mean, var_ref = _reference_layer_norm_naive(
                x_val, scale_val, bias_val, epsilon)

            #  for gradient test
            y_grad = np.random.random_sample(x_shape).astype(np.float32)

            x_grad_ref, scale_grad_ref, bias_grad_ref = _reference_layer_norm_grad(
                x_val, y_grad, scale_val, saved_mean, var_ref, epsilon)

            scope = core.Scope()

            # create input
            x_tensor = create_or_get_tensor(scope, "X", x_val, place)
            scale_tensor = create_or_get_tensor(scope, "Scale", scale_val,
                                                place)
            bias_tensor = create_or_get_tensor(scope, "Bias", bias_val, place)

            # create output
            y_tensor = create_or_get_tensor(scope, "Y", None, place)
            mean_tensor = create_or_get_tensor(scope, "Mean", None, place)
            variance_tensor = create_or_get_tensor(scope, "Variance", None,
                                                   place)

            layer_norm_op = Operator(
                "layer_norm",
                # inputs
                X="X",
                Scale="Scale",
                Bias="Bias",
                # outputs
                Y="Y",
                Mean="Mean",
                Variance="Variance",
                # attrs
                epsilon=epsilon)

            layer_norm_op.run(scope, place)

            # check forward result
            if isinstance(place, core.CUDAPlace):
                atol = 5e-2
            else:
                atol = 1e-4
            self.__assert_close(y_tensor, y_out, "Y", atol)
            self.__assert_close(mean_tensor, saved_mean, "Mean", atol)
            self.__assert_close(variance_tensor, var_ref, "Variance", atol)

            # run backward
            layer_norm_op_grad = get_backward_op(scope, layer_norm_op, set())
            set_output_grad(
                scope, ["Y", "Mean", "Variance"],
                place,
                feed_dict={"Y": y_grad})
            layer_norm_op_grad.run(scope, place)

            x_grad_tensor = create_or_get_tensor(scope,
                                                 grad_var_name("X"), None,
                                                 place)
            scale_grad_tensor = create_or_get_tensor(scope,
                                                     grad_var_name("Scale"),
                                                     None, place)
            bias_grad_tensor = create_or_get_tensor(scope,
                                                    grad_var_name("Bias"), None,
                                                    place)

            # check gradient output
            self.__assert_grad_close(x_grad_tensor, x_grad_ref, "x_grad", place)
            self.__assert_grad_close(scale_grad_tensor, scale_grad_ref,
                                     "scale_grad", place)
            self.__assert_grad_close(bias_grad_tensor, bias_grad_ref,
                                     "bias_grad", place)

        places = [core.CPUPlace()]
        if core.is_compile_gpu() and core.op_support_gpu("layer_norm"):
            places.append(core.CUDAPlace(0))

        for place in places:
            test_with_place(place, [2, 3, 4, 5])
C
chengduoZH 已提交
232 233 234 235


if __name__ == '__main__':
    unittest.main()