test_conv_nn_grad.py 19.6 KB
Newer Older
L
lvmengsi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2019 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.

import unittest
16 17

import gradient_checker
L
lvmengsi 已提交
18
import numpy as np
19
from decorator_helper import prog_scope
L
lvmengsi 已提交
20

21
import paddle
L
lvmengsi 已提交
22 23 24 25 26 27 28
import paddle.fluid as fluid
import paddle.fluid.core as core


class TestConvDoubleGradCheck(unittest.TestCase):
    @prog_scope()
    def func(self, place):
L
liym27 已提交
29
        shape = [2, 4, 3, 3]
L
lvmengsi 已提交
30
        eps = 0.005
R
ronnywang 已提交
31
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
32
        x = paddle.static.data('x', shape, dtype)
33
        y = paddle.static.nn.conv2d(x, 2, 1, groups=1, bias_attr=False)
L
liym27 已提交
34 35 36 37 38 39
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
40 41 42
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
43 44 45 46 47 48 49 50 51 52

    def test_grad(self):
        places = [fluid.CPUPlace()]

        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


Z
zhangchunle 已提交
53
class TestConvDoubleGradCheckTest0(unittest.TestCase):
L
liym27 已提交
54 55 56 57
    @prog_scope()
    def func(self, place):
        shape = [2, 4, 3, 3]
        eps = 0.005
R
ronnywang 已提交
58
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
59
        x = paddle.static.data('x', shape, dtype)
60
        y = paddle.static.nn.conv2d(x, 2, 1, bias_attr=False)
L
lvmengsi 已提交
61 62 63 64 65 66
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
67 68 69
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
lvmengsi 已提交
70 71 72 73 74 75 76 77 78 79 80 81

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConvDoubleGradCheckTest1(unittest.TestCase):
    @prog_scope()
    def func(self, place):
L
liym27 已提交
82
        shape = [2, 3, 3, 3]
L
lvmengsi 已提交
83
        eps = 0.005
R
ronnywang 已提交
84
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
85
        x = paddle.static.data('x', shape, dtype)
86
        y = paddle.static.nn.conv2d(x, 2, 1, padding=1, bias_attr=False)
L
lvmengsi 已提交
87 88 89 90 91 92
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
93 94 95
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
lvmengsi 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv3DDoubleGradCheck(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 4, 3, 4, 2]
        eps = 0.005
R
ronnywang 已提交
110
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
111
        x = paddle.static.data('x', shape, dtype)
112
        y = paddle.static.nn.conv3d(x, 2, 1, bias_attr=False)
L
lvmengsi 已提交
113 114 115 116 117 118
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
119 120 121
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
lvmengsi 已提交
122 123

    def test_grad(self):
124
        # places = [fluid.CPUPlace()]
H
hong 已提交
125
        places = []
L
lvmengsi 已提交
126 127 128 129 130 131 132 133 134 135 136
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv3DDoubleGradCheckTest1(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 4, 5, 3, 2]
        eps = 0.005
R
ronnywang 已提交
137
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
138
        x = paddle.static.data('x', shape, dtype)
139
        y = paddle.static.nn.conv3d(x, 2, 1, padding=1, bias_attr=False)
L
liym27 已提交
140 141 142 143 144 145
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
146 147 148
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv2DoubleGradCheck_AsyPadding(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 2, 3, 3]
        eps = 0.005
R
ronnywang 已提交
163
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
164
        x = paddle.static.data('x', shape, dtype)
165
        y = paddle.static.nn.conv2d(
166 167 168 169 170 171 172
            input=x,
            num_filters=2,
            filter_size=1,
            padding=[1, 0, 0, 1],
            bias_attr=False,
            use_cudnn=True,
        )
L
liym27 已提交
173 174 175 176 177 178
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
179 180 181
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv2DoubleGradCheck_PaddingSAME(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 2, 3, 3]
        eps = 0.005
R
ronnywang 已提交
196
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
197
        x = paddle.static.data('x', shape, dtype)
198
        y = paddle.static.nn.conv2d(
199 200 201 202 203 204 205
            input=x,
            num_filters=2,
            filter_size=1,
            padding="SAME",
            bias_attr=False,
            use_cudnn=True,
        )
L
liym27 已提交
206 207 208 209 210 211
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
212 213 214
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv2DoubleGradCheck_PaddingVALID(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 2, 3, 3]
        eps = 0.005
R
ronnywang 已提交
229
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
230
        x = paddle.static.data('x', shape, dtype)
231
        y = paddle.static.nn.conv2d(
232 233 234 235 236 237 238
            input=x,
            num_filters=2,
            filter_size=1,
            padding="VALID",
            bias_attr=False,
            use_cudnn=True,
        )
L
liym27 已提交
239 240 241 242 243 244
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
245 246 247
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv2DoubleGradCheck_ChannelLast(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 2, 3, 3]
        eps = 0.005
R
ronnywang 已提交
262
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
263
        x = paddle.static.data('x', shape, dtype)
264
        y = paddle.static.nn.conv2d(
265 266 267 268 269 270 271 272 273
            input=x,
            num_filters=2,
            filter_size=1,
            padding=[1, 1],
            bias_attr=False,
            use_cudnn=True,
            groups=1,
            data_format="NHWC",
        )
L
liym27 已提交
274 275 276 277 278 279
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
280 281 282
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv2DoubleGradCheck_ChannelLast_AsyPadding(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 2, 3, 3]
        eps = 0.005
R
ronnywang 已提交
297
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
298
        x = paddle.static.data('x', shape, dtype)
299
        y = paddle.static.nn.conv2d(
300 301 302 303 304 305 306 307 308
            input=x,
            num_filters=2,
            filter_size=1,
            padding=[1, 0, 1, 0],
            bias_attr=False,
            use_cudnn=True,
            groups=1,
            data_format="NHWC",
        )
L
liym27 已提交
309 310 311 312 313 314
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
315 316 317
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv3DDoubleGradCheck_AsyPadding(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 2, 2, 2, 2]
        eps = 0.005
R
ronnywang 已提交
332
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
333
        x = paddle.static.data('x', shape, dtype)
334
        y = paddle.static.nn.conv3d(
335 336 337 338 339 340 341
            input=x,
            num_filters=2,
            filter_size=1,
            padding=[1, 0, 0, 1, 1, 2],
            bias_attr=False,
            use_cudnn=True,
        )
L
liym27 已提交
342 343 344 345 346 347
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
348 349 350
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv3DoubleGradCheck_PaddingSAME(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 2, 2, 2, 2]
        eps = 0.005
R
ronnywang 已提交
365
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
366
        x = paddle.static.data('x', shape, dtype)
367
        y = paddle.static.nn.conv3d(
368 369 370 371 372 373 374 375
            input=x,
            num_filters=2,
            filter_size=1,
            padding="SAME",
            groups=1,
            bias_attr=False,
            use_cudnn=True,
        )
L
liym27 已提交
376 377 378 379 380 381
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
382 383 384
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv3DoubleGradCheck_PaddingVALID(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 2, 3, 3, 2]
        eps = 0.005
R
ronnywang 已提交
399
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
400
        x = paddle.static.data('x', shape, dtype)
401
        y = paddle.static.nn.conv3d(
402 403 404 405 406 407 408
            input=x,
            num_filters=2,
            filter_size=1,
            padding="VALID",
            bias_attr=False,
            use_cudnn=True,
        )
L
liym27 已提交
409 410 411 412 413 414
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
415 416 417
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv3DDoubleGradCheck_ChannelLast(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 2, 2, 2, 3]
        eps = 0.005
R
ronnywang 已提交
432
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
433
        x = paddle.static.data('x', shape, dtype)
434
        y = paddle.static.nn.conv3d(
435 436 437 438 439 440 441 442 443
            input=x,
            num_filters=2,
            filter_size=1,
            padding=[1, 1, 1],
            bias_attr=False,
            use_cudnn=True,
            groups=1,
            data_format="NDHWC",
        )
L
liym27 已提交
444 445 446 447 448 449
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
450 451 452
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
liym27 已提交
453 454 455 456 457 458 459 460 461

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


C
cnn 已提交
462
class TestConv3DDoubleGradCheck_ChannelLast_AsyPadding(unittest.TestCase):
L
liym27 已提交
463 464 465 466
    @prog_scope()
    def func(self, place):
        shape = [2, 2, 2, 2, 3]
        eps = 0.005
R
ronnywang 已提交
467
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
468
        x = paddle.static.data('x', shape, dtype)
469
        y = paddle.static.nn.conv3d(
470 471 472 473 474 475 476 477 478
            input=x,
            num_filters=2,
            filter_size=1,
            padding=[1, 0, 1, 0, 1, 0],
            bias_attr=False,
            use_cudnn=True,
            groups=1,
            data_format="NDHWC",
        )
L
lvmengsi 已提交
479 480 481 482 483 484
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
485 486 487
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
L
lvmengsi 已提交
488 489 490 491 492 493 494 495 496

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


497 498 499 500 501
class TestDepthWiseConvDoubleGradCheck(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 4, 3, 3]
        eps = 0.005
R
ronnywang 已提交
502
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
503
        x = paddle.static.data('x', shape, dtype)
504

505
        # condition of depthwise conv:
506 507 508
        # use_cudnn == False
        # groups == filters
        # num_filters % num_channels == 0
509
        y = paddle.static.nn.conv2d(
510 511
            x, shape[1], 1, groups=shape[1], bias_attr=False, use_cudnn=False
        )
512 513 514 515 516 517
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        w = fluid.default_main_program().global_block().all_parameters()
        w_arr = []
        for p in w:
            w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype))
518 519 520
        gradient_checker.double_grad_check(
            [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps
        )
521 522 523 524 525 526 527 528 529

    def test_grad(self):
        places = []
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


530 531 532 533 534 535 536 537 538 539
class TestDepthWiseConvDoubleGradCheckCase1(unittest.TestCase):
    def depthwise_conv2d_wrapper(self, x):
        return paddle.nn.functional.conv2d(x[0], x[1], groups=4)

    @prog_scope()
    def func(self, place):
        x_shape = [2, 4, 3, 3]
        w_shape = [4, 1, 3, 3]
        eps = 0.005
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
540 541
        x = paddle.static.data('x', x_shape, dtype)
        w = paddle.static.data('w', w_shape, dtype)
542

543
        # condition of depthwise conv:
544 545 546 547 548 549 550 551
        # use_cudnn == False
        # groups == filters
        # num_filters % num_channels == 0

        y = paddle.nn.functional.conv2d(x, w, groups=4)
        x_arr = np.random.uniform(-1, 1, x_shape).astype(dtype)
        w_arr = np.random.uniform(-1, 1, w_shape).astype(dtype)

552 553 554
        gradient_checker.double_grad_check(
            [x, w], y, x_init=[x_arr, w_arr], place=place, eps=eps
        )
555
        gradient_checker.double_grad_check_for_dygraph(
556 557
            self.depthwise_conv2d_wrapper,
            [x, w],
558 559
            y,
            x_init=[x_arr, w_arr],
560 561
            place=place,
        )
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580

    def test_grad(self):
        places = []
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestConv3DDoubleGradCheck_NN(unittest.TestCase):
    def conv3d_wrapper(self, x):
        return paddle.nn.functional.conv3d(x[0], x[1])

    @prog_scope()
    def func(self, place):
        x_shape = [2, 3, 8, 8, 8]
        w_shape = [6, 3, 3, 3, 3]
        eps = 0.005
        dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64
G
GGBond8488 已提交
581 582
        x = paddle.static.data('x', x_shape, dtype)
        w = paddle.static.data('w', w_shape, dtype)
583 584 585 586 587 588
        x.persistable = True
        w.persistable = True
        y = paddle.nn.functional.conv3d(x, w)
        x_arr = np.random.uniform(-1, 1, x_shape).astype(dtype)
        w_arr = np.random.uniform(-1, 1, w_shape).astype(dtype)

589 590 591 592 593 594
        gradient_checker.double_grad_check(
            [x, w], y, x_init=[x_arr, w_arr], place=place, eps=eps
        )
        gradient_checker.double_grad_check_for_dygraph(
            self.conv3d_wrapper, [x, w], y, x_init=[x_arr, w_arr], place=place
        )
595 596 597 598 599 600 601 602 603

    def test_grad(self):
        places = []
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


L
lvmengsi 已提交
604
if __name__ == "__main__":
H
hong 已提交
605
    paddle.enable_static()
L
lvmengsi 已提交
606
    unittest.main()