test_conv2d_fusion_op.py 11.0 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
#   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


26
def create_test_padding_SAME_class(parent):
27
    class TestPaddingSAMECase(parent):
28 29 30 31 32
        def init_paddings(self):
            self.pad = [0, 0]
            self.padding_algorithm = "SAME"

    cls_name = "{0}_{1}".format(parent.__name__, "PaddingSAMEOp")
33 34
    TestPaddingSAMECase.__name__ = cls_name
    globals()[cls_name] = TestPaddingSAMECase
35 36 37 38 39 40 41 42 43 44 45 46 47


def create_test_padding_VALID_class(parent):
    class TestPaddingVALIDCase(parent):
        def init_paddings(self):
            self.pad = [1, 1]
            self.padding_algorithm = "VALID"

    cls_name = "{0}_{1}".format(parent.__name__, "PaddingVALIDOp")
    TestPaddingVALIDCase.__name__ = cls_name
    globals()[cls_name] = TestPaddingVALIDCase


Q
qingqing01 已提交
48 49 50 51
class TestConv2dFusionOp(OpTest):
    def setUp(self):
        self.op_type = "conv2d_fusion"
        self.exhaustive_search = False
52
        self.data_format = "NCHW"
Q
qingqing01 已提交
53 54 55
        self.dtype = np.float32
        self.activation = 'relu'
        self.add_residual_data = True
56
        self.split_channels = None
Q
qingqing01 已提交
57
        self.outputs = None
58
        self.padding_algorithm = "EXIPLICIT"
Q
qingqing01 已提交
59 60 61 62

        self.init_group()
        self.init_dilation()
        self.init_test_case()
63
        self.init_residual()
Q
qingqing01 已提交
64
        self.init_activation()
65
        self.init_paddings()
Q
qingqing01 已提交
66 67 68 69 70 71 72 73 74 75
        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)
76
        bias = np.random.random(self.filter_size[0]).astype(self.dtype)
Q
qingqing01 已提交
77

78
        self.output, _, _, _, _ = conv2d_forward_naive(
79 80 81
            input, filter, self.groups, conv2d_param, self.padding_algorithm,
            self.data_format)

82
        self.output = self.output.astype(self.dtype)
Q
qingqing01 已提交
83 84 85

        self.inputs = {
            'Input': OpTest.np_dtype_to_fluid_dtype(input),
86 87
            'Filter': OpTest.np_dtype_to_fluid_dtype(filter),
            'Bias': OpTest.np_dtype_to_fluid_dtype(bias)
Q
qingqing01 已提交
88 89 90
        }

        if self.add_residual_data:
Q
qingqing01 已提交
91 92
            residual_data = np.random.random(self.output.shape).astype(
                self.dtype)
Q
qingqing01 已提交
93 94
            self.inputs['ResidualData'] = OpTest.np_dtype_to_fluid_dtype(
                residual_data)
Q
qingqing01 已提交
95
            self.output += residual_data
Q
qingqing01 已提交
96

97 98
        # Add bias
        self.output = self.output + bias.reshape((1, bias.size, 1, 1))
Q
qingqing01 已提交
99 100 101

        assert self.activation in ['relu', 'identity']
        if self.activation == 'relu':
Q
qingqing01 已提交
102
            self.output = np.maximum(self.output, 0)
Q
qingqing01 已提交
103 104 105 106 107 108 109 110

        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 已提交
111
            'activation': self.activation,
112
            'padding_algorithm': self.padding_algorithm
Q
qingqing01 已提交
113
        }
114 115 116
        if self.split_channels is not None:
            self.attrs['split_channels'] = self.split_channels

Q
qingqing01 已提交
117 118 119
        self.outputs = {'Output': self.output}

        self.set_outputs()
Q
qingqing01 已提交
120

121
    def has_cuda(self):
Q
qingqing01 已提交
122 123 124
        return core.is_compiled_with_cuda()

    def test_check_output(self):
125
        if self.has_cuda():
Q
qingqing01 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)

    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

143
    def init_residual(self):
Q
qingqing01 已提交
144 145 146 147 148 149 150 151
        self.add_residual_data = True

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

    def set_search_method(self):
        self.exhaustive_search = False

Q
qingqing01 已提交
152 153 154
    def set_outputs(self):
        pass

155 156 157 158
    def init_paddings(self):
        self.pad = [0, 0]
        self.padding_algorithm = "EXPLICIT"

Q
qingqing01 已提交
159 160

class TestWithoutResidual(TestConv2dFusionOp):
161
    def init_residual(self):
Q
qingqing01 已提交
162 163 164 165 166 167 168 169
        self.add_residual_data = False


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


170 171 172 173 174 175
class TestIdentityActivation(TestConv2dFusionOp):
    def init_activation(self):
        self.activation = 'identity'
        self.add_residual_data = False


Q
qingqing01 已提交
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
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 已提交
202 203 204 205 206 207 208 209
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]
210
        self.split_channels = [84, 42]
Q
qingqing01 已提交
211 212 213 214 215 216 217

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


218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
class TestAsyPadding(TestConv2dFusionOp):
    def init_paddings(self):
        self.pad = [0, 0, 1, 2]
        self.padding_algorithm = "EXPLICIT"


class TestWithPad_AsyPadding(TestConv2dFusionOp):
    def init_test_case(self):
        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_paddings(self):
        self.pad = [2, 1, 3, 2]
        self.padding_algorithm = "EXPLICIT"


class TestWithStride_AsyPadding(TestConv2dFusionOp):
    def init_test_case(self):
        self.stride = [2, 2]
        self.input_size = [2, 3, 6, 6]  # 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_paddings(self):
        self.pad = [2, 1, 3, 2]
        self.padding_algorithm = "EXPLICIT"


class TestWith1x1_AsyPadding(TestConv2dFusionOp):
    def init_test_case(self):
        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, 1, 1]

    def init_group(self):
        self.groups = 3

    def init_paddings(self):
        self.pad = [2, 2, 4, 0]
        self.padding_algorithm = "EXPLICIT"


class TestWithGroup_AsyPadding(TestConv2dFusionOp):
    def init_group(self):
        self.groups = 3


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

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

    def init_group(self):
        self.groups = 4

    def init_paddings(self):
        self.pad = [1, 3, 2, 1]
        self.padding_algorithm = "EXPLICIT"


class TestWithDepthWise5x5_AsyPadding(TestConv2dFusionOp):
    def init_test_case(self):
        self.stride = [1, 1]
        self.input_size = [2, 4, 10, 10]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [8, f_c, 5, 5]

    def init_group(self):
        self.groups = 4

    def init_paddings(self):
        self.pad = [0, 1, 1, 0]
        self.padding_algorithm = "EXPLICIT"


class TestWithDepthWise7x7_AsyPadding(TestConv2dFusionOp):
    def init_test_case(self):
        self.stride = [2, 2]
        self.input_size = [2, 8, 10, 10]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [16, f_c, 7, 7]

    def init_group(self):
        self.groups = 8

    def init_paddings(self):
        self.pad = [1, 3, 4, 1]
        self.padding_algorithm = "EXPLICIT"


class TestWithDilation_AsyPadding(TestConv2dFusionOp):
    def init_test_case(self):
        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

    def init_paddings(self):
        self.pad = [0, 1, 3, 0]
        self.padding_algorithm = "EXPLICIT"


class TestWithInput1x1Filter1x1_AsyPadding(TestConv2dFusionOp):
    def init_test_case(self):
        self.stride = [1, 1]
        self.input_size = [2, 3, 1, 1]  # 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, 1, 1]

    def init_group(self):
        self.groups = 3

    def init_paddings(self):
        self.pad = [0, 3, 4, 0]
        self.padding_algorithm = "EXPLICIT"


create_test_padding_SAME_class(TestAsyPadding)
create_test_padding_SAME_class(TestWithPad_AsyPadding)
create_test_padding_SAME_class(TestWithStride_AsyPadding)
create_test_padding_SAME_class(TestWithGroup_AsyPadding)
create_test_padding_SAME_class(TestWithInput1x1Filter1x1_AsyPadding)

create_test_padding_VALID_class(TestAsyPadding)
create_test_padding_VALID_class(TestWithPad_AsyPadding)
create_test_padding_VALID_class(TestWithStride_AsyPadding)
create_test_padding_VALID_class(TestWithGroup_AsyPadding)
create_test_padding_VALID_class(TestWithInput1x1Filter1x1_AsyPadding)

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