test_conv2d_fusion_op.py 5.5 KB
Newer Older
Q
qingqing01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#   Copyright (c) 2018 PaddlePaddle 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.

from __future__ import print_function

import unittest
import numpy as np

import paddle.fluid.core as core
from op_test import OpTest

from test_conv2d_op import conv2d_forward_naive


class TestConv2dFusionOp(OpTest):
    def setUp(self):
        self.op_type = "conv2d_fusion"
        self.exhaustive_search = False
        self.data_format = "AnyLayout"
        self.dtype = np.float32
        self.activation = 'relu'
        self.add_bias = True
        self.add_residual_data = True
Q
qingqing01 已提交
35 36
        self.channels = None
        self.outputs = None
Q
qingqing01 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

        self.init_group()
        self.init_dilation()
        self.init_test_case()
        self.init_bias_residual()
        self.init_activation()
        self.set_search_method()

        conv2d_param = {
            'stride': self.stride,
            'pad': self.pad,
            'dilation': self.dilations
        }

        input = np.random.random(self.input_size).astype(self.dtype)
        filter = np.random.random(self.filter_size).astype(self.dtype)

Q
qingqing01 已提交
54 55
        self.output = conv2d_forward_naive(input, filter, self.groups,
                                           conv2d_param).astype(self.dtype)
Q
qingqing01 已提交
56 57 58 59 60 61 62

        self.inputs = {
            'Input': OpTest.np_dtype_to_fluid_dtype(input),
            'Filter': OpTest.np_dtype_to_fluid_dtype(filter)
        }

        if self.add_residual_data:
Q
qingqing01 已提交
63 64
            residual_data = np.random.random(self.output.shape).astype(
                self.dtype)
Q
qingqing01 已提交
65 66
            self.inputs['ResidualData'] = OpTest.np_dtype_to_fluid_dtype(
                residual_data)
Q
qingqing01 已提交
67
            self.output += residual_data
Q
qingqing01 已提交
68 69 70 71

        if self.add_bias:
            bias = np.random.random(self.filter_size[0]).astype(self.dtype)
            self.inputs['Bias'] = OpTest.np_dtype_to_fluid_dtype(bias)
Q
qingqing01 已提交
72
            self.output = self.output + bias.reshape((1, bias.size, 1, 1))
Q
qingqing01 已提交
73 74 75

        assert self.activation in ['relu', 'identity']
        if self.activation == 'relu':
Q
qingqing01 已提交
76
            self.output = np.maximum(self.output, 0)
Q
qingqing01 已提交
77 78 79 80 81 82 83 84

        self.attrs = {
            'strides': self.stride,
            'paddings': self.pad,
            'groups': self.groups,
            'dilations': self.dilations,
            'data_format': self.data_format,
            'exhaustive_search': self.exhaustive_search,
Q
qingqing01 已提交
85 86
            'activation': self.activation,
            'split_channels': self.channels
Q
qingqing01 已提交
87
        }
Q
qingqing01 已提交
88 89 90
        self.outputs = {'Output': self.output}

        self.set_outputs()
Q
qingqing01 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    def testcuda(self):
        return core.is_compiled_with_cuda()

    def test_check_output(self):
        if self.testcuda():
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)
        else:
            pass

    def init_test_case(self):
        self.pad = [0, 0]
        self.stride = [1, 1]
        self.input_size = [2, 3, 5, 5]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 3, 3]

    def init_dilation(self):
        self.dilations = [1, 1]

    def init_group(self):
        self.groups = 1

    def init_bias_residual(self):
        self.add_bias = True
        self.add_residual_data = True

    def init_activation(self):
        self.activation = 'relu'

    def set_search_method(self):
        self.exhaustive_search = False

Q
qingqing01 已提交
126 127 128
    def set_outputs(self):
        pass

Q
qingqing01 已提交
129 130 131 132 133 134 135 136 137 138 139

class TestWithoutResidual(TestConv2dFusionOp):
    def init_bias_residual(self):
        self.add_residual_data = False


class TestIdentityActivation(TestConv2dFusionOp):
    def init_activation(self):
        self.activation = 'identity'


140 141 142 143 144 145
class TestIdentityActivation(TestConv2dFusionOp):
    def init_activation(self):
        self.activation = 'identity'
        self.add_residual_data = False


Q
qingqing01 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
class TestWithGroup(TestConv2dFusionOp):
    def init_group(self):
        self.groups = 3


class TestWithDilation(TestConv2dFusionOp):
    def init_test_case(self):
        self.pad = [0, 0]
        self.stride = [1, 1]
        self.input_size = [2, 3, 10, 10]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 3, 3]

    def init_dilation(self):
        self.dilations = [2, 2]

    def init_group(self):
        self.groups = 3


class TestCUDNNExhaustiveSearch(TestConv2dFusionOp):
    def set_search_method(self):
        self.exhaustive_search = True


Q
qingqing01 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
class TestMultipleOutputs(TestConv2dFusionOp):
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
        self.input_size = [1, 32, 17, 17]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [126, f_c, 3, 3]
        self.channels = [84, 42]

    def set_outputs(self):
        out1 = self.output[:, 0:84, :, :]
        out2 = self.output[:, 84:126, :, :]
        self.outputs['Outputs'] = [('out1', out1), ('out2', out2)]


Q
qingqing01 已提交
188 189
if __name__ == '__main__':
    unittest.main()