test_elementwise_mul_mkldnn_op.py 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#  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
18
from paddle.fluid.tests.unittests.op_test import OpTest
19 20
import paddle.fluid.core as core
from paddle.fluid.op import Operator
21
from paddle.fluid.tests.unittests.test_elementwise_mul_op import *
22

23

24
class TestElementwiseMulMKLDNNOp_BroadcastNCHW16c(ElementwiseMulOp):
25 26 27 28 29 30 31 32
    def init_input_output(self):
        x = np.random.rand(1, 16, 2, 2).astype(self.dtype)
        self.x = x.transpose(0, 2, 3, 1).reshape(1, 16, 2, 2)
        self.y = np.random.rand(1, 16).astype(self.dtype)

        self.out = x * self.y.reshape(1, 16, 1, 1)
        self.out = self.out.transpose(0, 2, 3, 1).reshape(1, 16, 2, 2)

33 34 35 36
    def setUp(self):
        super(TestElementwiseMulMKLDNNOp_BroadcastNCHW16c, self).setUp()
        self.attrs["x_data_format"] = "nchw16c"
        self.attrs["y_data_format"] = "nc"
37
        self._cpu_only = True
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    def init_kernel_type(self):
        self.use_mkldnn = True

    def init_axis(self):
        self.axis = 0

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_x(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

54 55 56

@unittest.skip(
    "Not implemented yet.")  # TODO(mgallus): enable when implemented.
M
Michal Gallus 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69
class TestElementwiseMulMKLDNNOp_BroadcastNCHW8c(ElementwiseMulOp):
    def init_input_output(self):
        x = np.random.rand(1, 8, 2, 2).astype(self.dtype)
        self.x = x.transpose(0, 2, 3, 1).reshape(1, 8, 2, 2)
        self.y = np.random.rand(1, 8).astype(self.dtype)

        self.out = x * self.y.reshape(1, 8, 1, 1)
        self.out = self.out.transpose(0, 2, 3, 1).reshape(1, 8, 2, 2)

    def setUp(self):
        super(TestElementwiseMulMKLDNNOp_BroadcastNCHW8c, self).setUp()
        self.attrs["x_data_format"] = "nchw8c"
        self.attrs["y_data_format"] = "nc"
70
        self._cpu_only = True
M
Michal Gallus 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    def init_kernel_type(self):
        self.use_mkldnn = True

    def init_axis(self):
        self.axis = 0

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_x(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

87

M
Michal Gallus 已提交
88
class TestElementwiseMulMKLDNNOp_FallbackNCHW(ElementwiseMulOp):
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    def init_input_output(self):
        self.x = np.random.rand(1, 16, 2, 2).astype(self.dtype)
        self.y = np.random.rand(1, 16).astype(self.dtype)

        self.out = self.x * self.y.reshape(1, 16, 1, 1)

    def init_kernel_type(self):
        self.use_mkldnn = True

    def init_axis(self):
        self.axis = 0

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_x(self):
        pass

    def test_check_grad_ingore_y(self):
        pass
109

110

M
Michal Gallus 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123
class TestElementwiseMulMKLDNNOp_FallbackNCHW16C(ElementwiseMulOp):
    def init_input_output(self):
        x = np.random.rand(1, 16, 2, 2).astype(self.dtype)
        self.x = x.transpose(0, 2, 3, 1).reshape(1, 16, 2, 2)
        y = np.random.rand(1, 16, 2, 2).astype(self.dtype)
        self.y = y.transpose(0, 2, 3, 1).reshape(1, 16, 2, 2)

        self.out = self.x * self.y

    def setUp(self):
        super(TestElementwiseMulMKLDNNOp_FallbackNCHW16C, self).setUp()
        self.attrs["x_data_format"] = "nchw16c"
        self.attrs["y_data_format"] = "nchw16c"
124
        self._cpu_only = True
M
Michal Gallus 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

    def init_kernel_type(self):
        self.use_mkldnn = True

    def init_axis(self):
        self.axis = 0

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_x(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

141

M
Michal Gallus 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154
class TestElementwiseMulMKLDNNOp_FallbackNoReorders(ElementwiseMulOp):
    def init_input_output(self):
        x = np.random.rand(1, 16, 2, 2).astype(self.dtype)
        self.x = x.transpose(0, 2, 3, 1).reshape(1, 16, 2, 2)
        y = np.random.rand(1, 16, 2, 2).astype(self.dtype)
        self.y = y.transpose(0, 2, 3, 1).reshape(1, 16, 2, 2)

        self.out = self.x * self.y

    def setUp(self):
        super(TestElementwiseMulMKLDNNOp_FallbackNoReorders, self).setUp()
        self.attrs["x_data_format"] = "nchw16c"
        self.attrs["y_data_format"] = "nchw16c"
155
        self._cpu_only = True
M
Michal Gallus 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

    def init_kernel_type(self):
        self.use_mkldnn = True

    def init_axis(self):
        self.axis = 0

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_x(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

172

173
class TestElementwiseMulMKLDNNOp_FallbackWithReorder1(ElementwiseMulOp):
M
Michal Gallus 已提交
174 175 176 177 178 179 180 181
    def init_input_output(self):
        self.x = np.random.rand(1, 16, 2, 2).astype(self.dtype)
        y = np.random.rand(1, 16, 2, 2).astype(self.dtype)
        self.y = y.transpose(0, 2, 3, 1).reshape(1, 16, 2, 2)

        self.out = self.x * y

    def setUp(self):
182
        super(TestElementwiseMulMKLDNNOp_FallbackWithReorder1, self).setUp()
M
Michal Gallus 已提交
183 184
        self.attrs["x_data_format"] = "nchw"
        self.attrs["y_data_format"] = "nchw16c"
185
        self._cpu_only = True
M
Michal Gallus 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

    def init_kernel_type(self):
        self.use_mkldnn = True

    def init_axis(self):
        self.axis = 0

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_x(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

202

203 204 205 206 207 208 209 210 211 212 213 214
class TestElementwiseMulMKLDNNOp_FallbackWithReorder2(ElementwiseMulOp):
    def init_input_output(self):
        self.y = np.random.rand(1, 16, 2, 2).astype(self.dtype)
        x = np.random.rand(1, 16, 2, 2).astype(self.dtype)
        self.x = x.transpose(0, 2, 3, 1).reshape(1, 16, 2, 2)

        self.out = x * self.y

    def setUp(self):
        super(TestElementwiseMulMKLDNNOp_FallbackWithReorder2, self).setUp()
        self.attrs["x_data_format"] = "nchw16c"
        self.attrs["y_data_format"] = "nchw"
215
        self._cpu_only = True
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

    def init_kernel_type(self):
        self.use_mkldnn = True

    def init_axis(self):
        self.axis = 0

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_x(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

232

233 234 235 236 237 238 239 240 241 242 243
class TestElementwiseMulMKLDNNOp_FallbackNoReorders2(ElementwiseMulOp):
    def init_input_output(self):
        self.x = np.random.rand(1, 16).astype(self.dtype)
        self.y = np.random.rand(1, 16).astype(self.dtype)

        self.out = self.x * self.y

    def setUp(self):
        super(TestElementwiseMulMKLDNNOp_FallbackNoReorders2, self).setUp()
        self.attrs["x_data_format"] = "nc"
        self.attrs["y_data_format"] = "nc"
244
        self._cpu_only = True
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

    def init_kernel_type(self):
        self.use_mkldnn = True

    def init_axis(self):
        self.axis = 0

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_x(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

261

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