test_sparse_conv_op.py 5.1 KB
Newer Older
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
# Copyright (c) 2022 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
from paddle import _C_ops
from paddle.fluid import core
from paddle.fluid.framework import _test_eager_guard


class TestSparseConv(unittest.TestCase):
    def test_conv3d(self):
        with _test_eager_guard():
            kernel = [[[[[1], [1], [1]], [[1], [1], [1]], [[1], [1], [1]]]]]
            dense_kernel = paddle.to_tensor(
                kernel, dtype='float32', stop_gradient=False)
            dense_kernel = paddle.reshape(dense_kernel, [1, 3, 3, 1, 1])
            paddings = [0, 0, 0]
            strides = [1, 1, 1]
            dilations = [1, 1, 1]
34
            bias = [1]
35 36 37 38 39 40

            indices = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 2], [1, 3, 2, 3]]
            values = [1, 2, 3, 4]
            indices = paddle.to_tensor(indices, dtype='int32')
            values = paddle.to_tensor(values, dtype='float32')
            dense_shape = [1, 1, 3, 4, 1]
41
            correct_out_values = [[5], [11]]
42 43
            sparse_input = core.eager.sparse_coo_tensor(indices, values,
                                                        dense_shape, False)
44
            out = paddle.incubate.sparse.nn.functional.conv3d(
45 46
                sparse_input,
                dense_kernel,
47 48
                bias=paddle.to_tensor(
                    bias, dtype='float32'),
49 50 51 52 53
                stride=strides,
                padding=paddings,
                dilation=dilations,
                groups=1,
                data_format="NDHWC")
54
            out.backward(out)
55
            assert np.array_equal(correct_out_values, out.values().numpy())
56

57 58 59 60 61 62 63
    def test_subm_conv3d(self):
        with _test_eager_guard():
            indices = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 2], [1, 3, 2, 3]]
            values = [[1], [2], [3], [4]]
            indices = paddle.to_tensor(indices, dtype='int32')
            values = paddle.to_tensor(values, dtype='float32')
            dense_shape = [1, 1, 3, 4, 1]
64
            sparse_x = paddle.incubate.sparse.sparse_coo_tensor(
65 66
                indices, values, dense_shape, stop_gradient=True)
            weight = paddle.randn((1, 3, 3, 1, 1), dtype='float32')
67 68
            y = paddle.incubate.sparse.nn.functional.subm_conv3d(sparse_x,
                                                                 weight)
69 70 71 72 73 74 75 76 77 78 79 80 81
            assert np.array_equal(sparse_x.indices().numpy(),
                                  y.indices().numpy())

    def test_Conv3D(self):
        with _test_eager_guard():
            #(4, non_zero_num), 4-D:(N, D, H, W)
            indices = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 2], [1, 3, 2, 3]]
            #(non_zero_num, C)
            values = [[1], [2], [3], [4]]
            indices = paddle.to_tensor(indices, dtype='int32')
            values = paddle.to_tensor(values, dtype='float32')
            dense_shape = [1, 1, 3, 4, 1]
            correct_out_values = [[4], [10]]
82 83
            sparse_input = paddle.incubate.sparse.sparse_coo_tensor(
                indices, values, dense_shape, False)
84

85
            sparse_conv3d = paddle.incubate.sparse.nn.Conv3D(
86 87 88 89 90
                1, 1, (1, 3, 3), data_format='NDHWC')
            sparse_out = sparse_conv3d(sparse_input)
            #test errors
            with self.assertRaises(ValueError):
                #Currently, only support data_format='NDHWC'
91
                conv3d = paddle.incubate.sparse.nn.SubmConv3D(
92 93 94 95 96 97 98 99 100 101
                    1, 1, (1, 3, 3), data_format='NCDHW')

    def test_SubmConv3D(self):
        with _test_eager_guard():
            indices = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 2], [1, 3, 2, 3]]
            values = [[1], [2], [3], [4]]
            indices = paddle.to_tensor(indices, dtype='int32')
            values = paddle.to_tensor(values, dtype='float32')
            dense_shape = [1, 1, 3, 4, 1]
            correct_out_values = [[4], [10]]
102 103
            sparse_input = paddle.incubate.sparse.sparse_coo_tensor(
                indices, values, dense_shape, False)
104

105
            subm_conv3d = paddle.incubate.sparse.nn.SubmConv3D(
106 107 108 109 110 111 112
                1, 1, (1, 3, 3), data_format='NDHWC')
            # test extra_repr
            print(subm_conv3d.extra_repr())

            sparse_out = subm_conv3d(sparse_input)
            # the output shape of subm_conv is same as input shape
            assert np.array_equal(indices, sparse_out.indices().numpy())
113

114 115 116
            #test errors
            with self.assertRaises(ValueError):
                #Currently, only support data_format='NDHWC'
117
                conv3d = paddle.incubate.sparse.nn.SubmConv3D(
118
                    1, 1, (1, 3, 3), data_format='NCDHW')