test_sparse_unary_op.py 4.5 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
# 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
from typing import Union, Callable
import numpy as np
import paddle
from paddle.fluid.framework import _test_eager_guard
from paddle import _C_ops


class TestSparseUnary(unittest.TestCase):
    def assert_raises_on_dense_tensor(self, sparse_func):
        with _test_eager_guard():
            dense_x = paddle.ones((2, 3))
            with self.assertRaises(ValueError):
                sparse_func(dense_x)

    def compare_with_dense(
            self,
            x,
            to_sparse: Callable[[paddle.Tensor], paddle.Tensor],
            dense_func: Callable[[paddle.Tensor], paddle.Tensor],
            sparse_func: Callable[[paddle.Tensor], paddle.Tensor],
            test_gradient: bool, ):
        def tensor_allclose(dense_tensor: paddle.Tensor,
                            sparse_tensor: paddle.Tensor):
            dense_numpy = dense_tensor.numpy()
            mask = ~np.isnan(dense_numpy)
            return np.allclose(dense_numpy[mask],
                               sparse_tensor.to_dense().numpy()[mask])

        with _test_eager_guard():
            dense_x = paddle.to_tensor(
                x, dtype="float32", stop_gradient=not test_gradient)

            sparse_x = to_sparse(dense_x)
            sparse_out = sparse_func(sparse_x)

            dense_x = paddle.to_tensor(
                x, dtype="float32", stop_gradient=not test_gradient)
            dense_out = dense_func(dense_x)

            assert tensor_allclose(dense_out, sparse_out)

            if test_gradient:
                dense_out.backward(dense_out)
                sparse_out.backward(sparse_out)
                assert tensor_allclose(dense_x.grad, sparse_x.grad)

    def test_sparse_relu(self):
        x = [[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]]
        sparse_dim = 2
        self.compare_with_dense(
            x,
            lambda x: x.to_sparse_coo(sparse_dim),
            paddle.nn.ReLU(),
70
            paddle.incubate.sparse.nn.ReLU(),
71 72 73 74 75
            True, )
        self.compare_with_dense(
            x,
            lambda x: x.to_sparse_csr(),
            paddle.nn.ReLU(),
76
            paddle.incubate.sparse.nn.ReLU(),
77
            False, )
78
        self.assert_raises_on_dense_tensor(paddle.incubate.sparse.nn.ReLU())
79 80 81 82 83 84 85 86

    def test_sparse_sqrt(self):
        x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, 4, 2, 0]]
        sparse_dim = 2
        self.compare_with_dense(
            x,
            lambda x: x.to_sparse_coo(sparse_dim),
            paddle.sqrt,
87
            paddle.incubate.sparse.sqrt,
88 89 90 91 92
            True, )
        self.compare_with_dense(
            x,
            lambda x: x.to_sparse_csr(),
            paddle.sqrt,
93
            paddle.incubate.sparse.sqrt,
94
            False, )
95
        self.assert_raises_on_dense_tensor(paddle.incubate.sparse.sqrt)
96 97 98 99 100 101 102 103

    def test_sparse_sin(self):
        x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, 4, 2, 0]]
        sparse_dim = 2
        self.compare_with_dense(
            x,
            lambda x: x.to_sparse_coo(sparse_dim),
            paddle.sin,
104
            paddle.incubate.sparse.sin,
105 106 107 108 109
            True, )
        self.compare_with_dense(
            x,
            lambda x: x.to_sparse_csr(),
            paddle.sin,
110
            paddle.incubate.sparse.sin,
111
            False, )
112
        self.assert_raises_on_dense_tensor(paddle.incubate.sparse.sin)
113 114 115 116 117 118 119 120

    def test_sparse_tanh(self):
        x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, -4, 2, 0]]
        sparse_dim = 2
        self.compare_with_dense(
            x,
            lambda x: x.to_sparse_coo(sparse_dim),
            paddle.tanh,
121
            paddle.incubate.sparse.tanh,
122 123 124 125 126
            True, )
        self.compare_with_dense(
            x,
            lambda x: x.to_sparse_csr(),
            paddle.tanh,
127
            paddle.incubate.sparse.tanh,
128
            False, )
129
        self.assert_raises_on_dense_tensor(paddle.incubate.sparse.tanh)
130 131 132 133


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