test_batch_norm_op.py 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/env python3

# Copyright (c) 2022 CINN Authors. All Rights Reserved.
#
# 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
18

19
from op_mapper_test import OpMapperTest
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import paddle


class TestBatchNormOp(OpMapperTest):
    def init_input_data(self):
        self.num_channels = 16
        self.feed_data = {
            "x": self.random([2, self.num_channels, 8, 8], "float32"),
            'scale': self.random([self.num_channels], "float32"),
            'bias': self.random([self.num_channels], "float32"),
            'mean': self.random([self.num_channels], "float32"),
            'variance': self.random([self.num_channels], "float32"),
        }

    def set_op_type(self):
        return "batch_norm"

    def set_op_inputs(self):
        x = paddle.static.data(
            name='x',
            shape=self.feed_data['x'].shape,
42 43
            dtype=self.feed_data['x'].dtype,
        )
44 45 46
        scale = paddle.static.data(
            name='scale',
            shape=self.feed_data['scale'].shape,
47 48
            dtype=self.feed_data['scale'].dtype,
        )
49 50 51
        bias = paddle.static.data(
            name='bias',
            shape=self.feed_data['bias'].shape,
52 53
            dtype=self.feed_data['bias'].dtype,
        )
54 55 56
        mean = paddle.static.data(
            name='mean',
            shape=self.feed_data['mean'].shape,
57 58
            dtype=self.feed_data['mean'].dtype,
        )
59 60 61
        variance = paddle.static.data(
            name='variance',
            shape=self.feed_data['variance'].shape,
62 63
            dtype=self.feed_data['variance'].dtype,
        )
64 65 66 67 68
        return {
            'X': [x],
            'Scale': [scale],
            'Bias': [bias],
            'Mean': [mean],
69
            'Variance': [variance],
70 71 72 73 74 75 76 77 78
        }

    def set_op_attrs(self):
        return {
            'epsilon': 1e-5,
            'momentum': 0.9,
            'data_layout': 'NCHW',
            'is_test': False,
            'trainable_statistics': False,
79
            'use_global_stats': False,
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
        }

    def set_op_outputs(self):
        return {
            'Y': [self.feed_data['x'].dtype],
            'SavedMean': [self.feed_data['mean'].dtype],
            'SavedVariance': [self.feed_data['variance'].dtype],
        }

    def set_inplace_outputs(self):
        return {'MeanOut': 'Mean', 'VarianceOut': 'Variance'}

    def skip_check_outputs(self):
        # TODO(thisjiang): remove after Variance compute correct
        return {'SavedVariance', 'VarianceOut'}

    def test_check_results(self):
        self.check_outputs_and_grads()


class TestBatchNormInferOp(TestBatchNormOp):
    def set_op_attrs(self):
        return {
            'epsilon': 1e-5,
            'momentum': 0.9,
            'data_layout': 'NCHW',
            'is_test': True,
            'trainable_statistics': False,
108
            'use_global_stats': False,
109 110 111 112 113 114 115 116 117 118
        }

    def skip_check_outputs(self):
        # 'SavedMean', 'SavedVariance' are None in Paddle
        # TODO(thisjiang): remove after VarianceOut compute correct
        return {'SavedMean', 'SavedVariance', 'VarianceOut'}


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