test_sparse_mv_op.py 4.2 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
# 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.

import paddle
from paddle.fluid.framework import _test_eager_guard

import numpy as np
import unittest
import os
import re

paddle.seed(100)


def get_cuda_version():
    result = os.popen("nvcc --version").read()
    regex = r'release (\S+),'
    match = re.search(regex, result)
    if match:
        num = str(match.group(1))
        integer, decimal = num.split('.')
        return int(integer) * 1000 + int(float(decimal) * 10)
    else:
        return -1


@unittest.skipIf(
    not paddle.is_compiled_with_cuda() or get_cuda_version() < 11000,
    "paddle is not compiled with CUDA and cuda version need to >= 11.0")
class TestCsrMv(unittest.TestCase):
    # x: csr-matrix, y: dense-vec, out: dense-vec
    def test_mv(self):
        with _test_eager_guard():
            paddle.set_default_dtype('float64')
            origin_x = paddle.rand([64, 32])
            mask = paddle.randint(0, 2, [64, 32])
            origin_x = origin_x * mask
            origin_vec = paddle.rand([32])

            dense_x = origin_x.detach()
            dense_x.stop_gradient = False
            dense_vec = origin_vec.detach()
            dense_vec.stop_gradient = False
            dense_out = paddle.mv(dense_x, dense_vec)
            dense_out.backward()

            sp_x = origin_x.detach().to_sparse_csr()
            sp_x.stop_gradient = False
            sp_vec = origin_vec.detach()
            sp_vec.stop_gradient = False
62
            sp_out = paddle.sparse.mv(sp_x, sp_vec)
63 64
            sp_out.backward()

65 66 67 68 69 70 71 72 73
            np.testing.assert_allclose(sp_out.numpy(),
                                       dense_out.numpy(),
                                       rtol=1e-05)
            np.testing.assert_allclose(sp_x.grad.to_dense().numpy(),
                                       (dense_x.grad * mask).numpy(),
                                       rtol=1e-05)
            np.testing.assert_allclose(sp_vec.grad.numpy(),
                                       dense_vec.grad.numpy(),
                                       rtol=1e-05)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99


@unittest.skipIf(
    not paddle.is_compiled_with_cuda() or get_cuda_version() < 11000,
    "paddle is not compiled with CUDA and cuda version need to >= 11.0")
class TestCooMv(unittest.TestCase):
    # x: csr-matrix, y: dense-vec, out: dense-vec
    def test_mv(self):
        with _test_eager_guard():
            paddle.set_default_dtype('float64')
            origin_x = paddle.rand([64, 32])
            mask = paddle.randint(0, 2, [64, 32])
            origin_x = origin_x * mask
            origin_vec = paddle.rand([32])

            dense_x = origin_x.detach()
            dense_x.stop_gradient = False
            dense_vec = origin_vec.detach()
            dense_vec.stop_gradient = False
            dense_out = paddle.mv(dense_x, dense_vec)
            dense_out.backward()

            sp_x = origin_x.detach().to_sparse_coo(sparse_dim=2)
            sp_x.stop_gradient = False
            sp_vec = origin_vec.detach()
            sp_vec.stop_gradient = False
100
            sp_out = paddle.sparse.mv(sp_x, sp_vec)
101 102
            sp_out.backward()

103 104 105 106 107 108 109 110 111
            np.testing.assert_allclose(sp_out.numpy(),
                                       dense_out.numpy(),
                                       rtol=1e-05)
            np.testing.assert_allclose(sp_x.grad.to_dense().numpy(),
                                       (dense_x.grad * mask).numpy(),
                                       rtol=1e-05)
            np.testing.assert_allclose(sp_vec.grad.numpy(),
                                       dense_vec.grad.numpy(),
                                       rtol=1e-05)
112 113 114 115


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