test_sparse_utils_op.py 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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
19
import paddle.fluid.core as core
20 21 22
from paddle.fluid.framework import _test_eager_guard


23 24
class TestSparseCreate(unittest.TestCase):
    def test_create_coo_by_tensor(self):
25
        with _test_eager_guard():
26 27
            indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
            values = [1, 2, 3, 4, 5]
28
            dense_shape = [3, 4]
29 30
            dense_indices = paddle.to_tensor(indices)
            dense_elements = paddle.to_tensor(values, dtype='float32')
31 32
            coo = paddle.sparse.sparse_coo_tensor(
                dense_indices, dense_elements, dense_shape, stop_gradient=False)
33 34
            assert np.array_equal(indices, coo.indices().numpy())
            assert np.array_equal(values, coo.values().numpy())
35

36 37 38 39 40 41
    def test_create_coo_by_np(self):
        with _test_eager_guard():
            indices = [[0, 1, 2], [1, 2, 0]]
            values = [1.0, 2.0, 3.0]
            dense_shape = [2, 3]
            coo = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape)
42 43
            assert np.array_equal(indices, coo.indices().numpy())
            assert np.array_equal(values, coo.values().numpy())
44

45
    def test_create_csr_by_tensor(self):
46
        with _test_eager_guard():
47 48 49
            crows = [0, 2, 3, 5]
            cols = [1, 3, 2, 0, 1]
            values = [1, 2, 3, 4, 5]
50
            dense_shape = [3, 4]
51 52 53
            dense_crows = paddle.to_tensor(crows)
            dense_cols = paddle.to_tensor(cols)
            dense_elements = paddle.to_tensor(values, dtype='float32')
54
            stop_gradient = False
55 56 57 58 59 60
            csr = paddle.sparse.sparse_csr_tensor(
                dense_crows,
                dense_cols,
                dense_elements,
                dense_shape,
                stop_gradient=stop_gradient)
61

62 63 64 65 66 67 68 69
    def test_create_csr_by_np(self):
        with _test_eager_guard():
            crows = [0, 2, 3, 5]
            cols = [1, 3, 2, 0, 1]
            values = [1, 2, 3, 4, 5]
            dense_shape = [3, 4]
            csr = paddle.sparse.sparse_csr_tensor(crows, cols, values,
                                                  dense_shape)
70 71 72
            assert np.array_equal(crows, csr.crows().numpy())
            assert np.array_equal(cols, csr.cols().numpy())
            assert np.array_equal(values, csr.values().numpy())
73 74 75 76 77 78 79 80 81 82

    def test_place(self):
        with _test_eager_guard():
            place = core.CPUPlace()
            indices = [[0, 1], [0, 1]]
            values = [1.0, 2.0]
            dense_shape = [2, 2]
            coo = paddle.sparse.sparse_coo_tensor(
                indices, values, dense_shape, place=place)
            assert coo.place.is_cpu_place()
83 84
            assert coo.values().place.is_cpu_place()
            assert coo.indices().place.is_cpu_place()
85 86 87 88 89 90 91

            crows = [0, 2, 3, 5]
            cols = [1, 3, 2, 0, 1]
            values = [1.0, 2.0, 3.0, 4.0, 5.0]
            csr = paddle.sparse.sparse_csr_tensor(
                crows, cols, values, [3, 5], place=place)
            assert csr.place.is_cpu_place()
92 93 94
            assert csr.crows().place.is_cpu_place()
            assert csr.cols().place.is_cpu_place()
            assert csr.values().place.is_cpu_place()
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    def test_dtype(self):
        with _test_eager_guard():
            indices = [[0, 1], [0, 1]]
            values = [1.0, 2.0]
            dense_shape = [2, 2]
            indices = paddle.to_tensor(indices, dtype='int32')
            values = paddle.to_tensor(values, dtype='float32')
            coo = paddle.sparse.sparse_coo_tensor(
                indices, values, dense_shape, dtype='float64')
            assert coo.dtype == paddle.float64

            crows = [0, 2, 3, 5]
            cols = [1, 3, 2, 0, 1]
            values = [1.0, 2.0, 3.0, 4.0, 5.0]
            csr = paddle.sparse.sparse_csr_tensor(
                crows, cols, values, [3, 5], dtype='float16')
            assert csr.dtype == paddle.float16

    def test_create_coo_no_shape(self):
        with _test_eager_guard():
            indices = [[0, 1], [0, 1]]
            values = [1.0, 2.0]
            indices = paddle.to_tensor(indices, dtype='int32')
            values = paddle.to_tensor(values, dtype='float32')
            coo = paddle.sparse.sparse_coo_tensor(indices, values)
            assert [2, 2] == coo.shape


class TestSparseConvert(unittest.TestCase):
125 126 127
    def test_to_sparse_coo(self):
        with _test_eager_guard():
            x = [[0, 1, 0, 2], [0, 0, 3, 0], [4, 5, 0, 0]]
128 129 130
            indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
            values = [1.0, 2.0, 3.0, 4.0, 5.0]
            dense_x = paddle.to_tensor(x, dtype='float32', stop_gradient=False)
131
            out = dense_x.to_sparse_coo(2)
132 133 134 135 136
            assert np.array_equal(out.indices().numpy(), indices)
            assert np.array_equal(out.values().numpy(), values)
            #test to_sparse_coo_grad backward
            out_grad_indices = [[0, 1], [0, 1]]
            out_grad_values = [2.0, 3.0]
137
            out_grad = paddle.sparse.sparse_coo_tensor(
138
                paddle.to_tensor(out_grad_indices),
139 140 141
                paddle.to_tensor(out_grad_values),
                shape=out.shape,
                stop_gradient=True)
142 143 144 145 146 147 148 149
            out.backward(out_grad)
            assert np.array_equal(dense_x.grad.numpy(),
                                  out_grad.to_dense().numpy())

    def test_coo_to_dense(self):
        with _test_eager_guard():
            indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
            values = [1.0, 2.0, 3.0, 4.0, 5.0]
150
            sparse_x = paddle.sparse.sparse_coo_tensor(
151
                paddle.to_tensor(indices),
152 153 154
                paddle.to_tensor(values),
                shape=[3, 4],
                stop_gradient=False)
155 156 157 158 159 160 161 162 163
            dense_tensor = sparse_x.to_dense()
            #test to_dense_grad backward
            out_grad = [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0],
                        [9.0, 10.0, 11.0, 12.0]]
            dense_tensor.backward(paddle.to_tensor(out_grad))
            #mask the out_grad by sparse_x.indices() 
            correct_x_grad = [2.0, 4.0, 7.0, 9.0, 10.0]
            assert np.array_equal(correct_x_grad,
                                  sparse_x.grad.values().numpy())
164

165 166 167 168 169 170 171 172 173 174 175
            paddle.device.set_device("cpu")
            sparse_x_cpu = paddle.sparse.sparse_coo_tensor(
                paddle.to_tensor(indices),
                paddle.to_tensor(values),
                shape=[3, 4],
                stop_gradient=False)
            dense_tensor_cpu = sparse_x_cpu.to_dense()
            dense_tensor_cpu.backward(paddle.to_tensor(out_grad))
            assert np.array_equal(correct_x_grad,
                                  sparse_x_cpu.grad.values().numpy())

176 177 178
    def test_to_sparse_csr(self):
        with _test_eager_guard():
            x = [[0, 1, 0, 2], [0, 0, 3, 0], [4, 5, 0, 0]]
179 180 181
            crows = [0, 2, 3, 5]
            cols = [1, 3, 2, 0, 1]
            values = [1, 2, 3, 4, 5]
182
            dense_x = paddle.to_tensor(x)
183
            out = dense_x.to_sparse_csr()
184 185 186
            assert np.array_equal(out.crows().numpy(), crows)
            assert np.array_equal(out.cols().numpy(), cols)
            assert np.array_equal(out.values().numpy(), values)
187

188
            dense_tensor = out.to_dense()
189 190
            assert np.array_equal(dense_tensor.numpy(), x)

191 192 193 194
    def test_coo_values_grad(self):
        with _test_eager_guard():
            indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
            values = [1.0, 2.0, 3.0, 4.0, 5.0]
195
            sparse_x = paddle.sparse.sparse_coo_tensor(
196
                paddle.to_tensor(indices),
197 198 199
                paddle.to_tensor(values),
                shape=[3, 4],
                stop_gradient=False)
200 201 202 203 204 205
            values_tensor = sparse_x.values()
            out_grad = [2.0, 3.0, 5.0, 8.0, 9.0]
            # test coo_values_grad
            values_tensor.backward(paddle.to_tensor(out_grad))
            assert np.array_equal(out_grad, sparse_x.grad.values().numpy())

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    def test_sparse_coo_tensor_grad(self):
        with _test_eager_guard():
            indices = [[0, 1], [0, 1]]
            values = [1, 2]
            indices = paddle.to_tensor(indices, dtype='int32')
            values = paddle.to_tensor(
                values, dtype='float32', stop_gradient=False)
            sparse_x = paddle.sparse.sparse_coo_tensor(
                indices, values, shape=[2, 2], stop_gradient=False)
            grad_indices = [[0, 1], [1, 1]]
            grad_values = [2, 3]
            grad_indices = paddle.to_tensor(grad_indices, dtype='int32')
            grad_values = paddle.to_tensor(grad_values, dtype='float32')
            sparse_out_grad = paddle.sparse.sparse_coo_tensor(
                grad_indices, grad_values, shape=[2, 2])
            sparse_x.backward(sparse_out_grad)
            correct_values_grad = [0, 3]
            assert np.array_equal(correct_values_grad, values.grad.numpy())

            place = core.CPUPlace()
            indices_cpu = paddle.to_tensor(indices, dtype='int32', place=place)
            values_cpu = paddle.to_tensor(
                values, dtype='float32', place=place, stop_gradient=False)
            sparse_x_cpu = paddle.sparse.sparse_coo_tensor(
                indices_cpu,
                values_cpu,
                shape=[2, 2],
                place=place,
                stop_gradient=False)

            sparse_out_grad_cpu = paddle.sparse.sparse_coo_tensor(
                grad_indices, grad_values, shape=[2, 2], place=place)
            sparse_x_cpu.backward(sparse_out_grad_cpu)
            assert np.array_equal(correct_values_grad, values_cpu.grad.numpy())

241 242 243

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