test_sparse_pooling_op.py 3.7 KB
Newer Older
Z
zhangkaihuo 已提交
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2
#
Z
zhangkaihuo 已提交
3 4 5
# 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
6
#
Z
zhangkaihuo 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
Z
zhangkaihuo 已提交
9 10 11 12 13 14 15 16 17 18 19
# 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
import paddle.fluid.core as core
20
from paddle import _C_ops, _legacy_C_ops
Z
zhangkaihuo 已提交
21
from paddle.fluid.framework import _test_eager_guard
Z
zhangkaihuo 已提交
22
import copy
Z
zhangkaihuo 已提交
23 24 25


class TestMaxPool3DFunc(unittest.TestCase):
26

Z
zhangkaihuo 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    def setInput(self):
        paddle.seed(0)
        self.dense_x = paddle.randn((1, 4, 4, 4, 4))

    def setKernelSize(self):
        self.kernel_sizes = [3, 3, 3]

    def setStride(self):
        self.strides = [1, 1, 1]

    def setPadding(self):
        self.paddings = [0, 0, 0]

    def setUp(self):
        self.setInput()
        self.setKernelSize()
        self.setStride()
        self.setPadding()

    def test(self):
        with _test_eager_guard():
            self.setUp()
Z
zhangkaihuo 已提交
49
            self.dense_x.stop_gradient = False
Z
zhangkaihuo 已提交
50
            sparse_x = self.dense_x.to_sparse_coo(4)
51
            sparse_out = paddle.sparse.nn.functional.max_pool3d(
Z
zhangkaihuo 已提交
52 53 54 55
                sparse_x,
                self.kernel_sizes,
                stride=self.strides,
                padding=self.paddings)
Z
zhangkaihuo 已提交
56 57
            out = sparse_out.to_dense()
            out.backward(out)
Z
zhangkaihuo 已提交
58

Z
zhangkaihuo 已提交
59
            dense_x = copy.deepcopy(self.dense_x)
60 61 62 63 64
            dense_out = paddle.nn.functional.max_pool3d(dense_x,
                                                        self.kernel_sizes,
                                                        stride=self.strides,
                                                        padding=self.paddings,
                                                        data_format='NDHWC')
Z
zhangkaihuo 已提交
65 66
            dense_out.backward(dense_out)

Z
zhangkaihuo 已提交
67
            #compare with dense
Z
zhangkaihuo 已提交
68 69
            assert np.allclose(dense_out.numpy(), out.numpy())
            assert np.allclose(dense_x.grad.numpy(), self.dense_x.grad.numpy())
Z
zhangkaihuo 已提交
70 71 72


class TestStride(TestMaxPool3DFunc):
73

Z
zhangkaihuo 已提交
74 75 76 77 78
    def setStride(self):
        self.strides = 1


class TestPadding(TestMaxPool3DFunc):
79

Z
zhangkaihuo 已提交
80 81 82 83 84 85 86 87
    def setPadding(self):
        self.paddings = 1

    def setInput(self):
        self.dense_x = paddle.randn((1, 5, 6, 8, 3))


class TestKernelSize(TestMaxPool3DFunc):
88

Z
zhangkaihuo 已提交
89 90 91 92 93 94 95 96 97
    def setKernelSize(self):
        self.kernel_sizes = [5, 5, 5]

    def setInput(self):
        paddle.seed(0)
        self.dense_x = paddle.randn((1, 6, 9, 6, 3))


class TestInput(TestMaxPool3DFunc):
98

Z
zhangkaihuo 已提交
99 100 101 102 103 104 105 106
    def setInput(self):
        paddle.seed(0)
        self.dense_x = paddle.randn((2, 6, 7, 9, 3))
        dropout = paddle.nn.Dropout(0.8)
        self.dense_x = dropout(self.dense_x)


class TestMaxPool3DAPI(unittest.TestCase):
107

Z
zhangkaihuo 已提交
108 109 110 111
    def test(self):
        with _test_eager_guard():
            dense_x = paddle.randn((2, 3, 6, 6, 3))
            sparse_x = dense_x.to_sparse_coo(4)
112 113
            max_pool3d = paddle.sparse.nn.MaxPool3D(kernel_size=3,
                                                    data_format='NDHWC')
Z
zhangkaihuo 已提交
114 115 116
            out = max_pool3d(sparse_x)
            out = out.to_dense()

117 118 119
            dense_out = paddle.nn.functional.max_pool3d(dense_x,
                                                        3,
                                                        data_format='NDHWC')
Z
zhangkaihuo 已提交
120 121 122 123 124
            assert np.allclose(dense_out.numpy(), out.numpy())


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