|
| 1 | +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import print_function |
| 16 | +import unittest |
| 17 | + |
| 18 | +import paddle |
| 19 | +from paddle.incubate.sparse.binary import is_same_shape |
| 20 | + |
| 21 | + |
| 22 | +class TestSparseIsSameShapeAPI(unittest.TestCase): |
| 23 | + """ |
| 24 | + test paddle.incubate.sparse.is_same_shape |
| 25 | + """ |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + self.shapes = [[2, 5, 8], [3, 4]] |
| 29 | + self.tensors = [ |
| 30 | + paddle.rand(self.shapes[0]), |
| 31 | + paddle.rand(self.shapes[0]), |
| 32 | + paddle.rand(self.shapes[1]) |
| 33 | + ] |
| 34 | + self.sparse_dim = 2 |
| 35 | + |
| 36 | + def test_dense_dense(self): |
| 37 | + self.assertTrue(is_same_shape(self.tensors[0], self.tensors[1])) |
| 38 | + self.assertFalse(is_same_shape(self.tensors[0], self.tensors[2])) |
| 39 | + self.assertFalse(is_same_shape(self.tensors[1], self.tensors[2])) |
| 40 | + |
| 41 | + def test_dense_csr(self): |
| 42 | + self.assertTrue( |
| 43 | + is_same_shape(self.tensors[0], self.tensors[1].to_sparse_csr())) |
| 44 | + self.assertFalse( |
| 45 | + is_same_shape(self.tensors[0], self.tensors[2].to_sparse_csr())) |
| 46 | + self.assertFalse( |
| 47 | + is_same_shape(self.tensors[1], self.tensors[2].to_sparse_csr())) |
| 48 | + |
| 49 | + def test_dense_coo(self): |
| 50 | + self.assertTrue( |
| 51 | + is_same_shape(self.tensors[0], |
| 52 | + self.tensors[1].to_sparse_coo(self.sparse_dim))) |
| 53 | + self.assertFalse( |
| 54 | + is_same_shape(self.tensors[0], |
| 55 | + self.tensors[2].to_sparse_coo(self.sparse_dim))) |
| 56 | + self.assertFalse( |
| 57 | + is_same_shape(self.tensors[1], |
| 58 | + self.tensors[2].to_sparse_coo(self.sparse_dim))) |
| 59 | + |
| 60 | + def test_csr_dense(self): |
| 61 | + self.assertTrue( |
| 62 | + is_same_shape(self.tensors[0].to_sparse_csr(), self.tensors[1])) |
| 63 | + self.assertFalse( |
| 64 | + is_same_shape(self.tensors[0].to_sparse_csr(), self.tensors[2])) |
| 65 | + self.assertFalse( |
| 66 | + is_same_shape(self.tensors[1].to_sparse_csr(), self.tensors[2])) |
| 67 | + |
| 68 | + def test_csr_csr(self): |
| 69 | + self.assertTrue( |
| 70 | + is_same_shape(self.tensors[0].to_sparse_csr(), |
| 71 | + self.tensors[1].to_sparse_csr())) |
| 72 | + self.assertFalse( |
| 73 | + is_same_shape(self.tensors[0].to_sparse_csr(), |
| 74 | + self.tensors[2].to_sparse_csr())) |
| 75 | + self.assertFalse( |
| 76 | + is_same_shape(self.tensors[1].to_sparse_csr(), |
| 77 | + self.tensors[2].to_sparse_csr())) |
| 78 | + |
| 79 | + def test_csr_coo(self): |
| 80 | + self.assertTrue( |
| 81 | + is_same_shape(self.tensors[0].to_sparse_csr(), |
| 82 | + self.tensors[1].to_sparse_coo(self.sparse_dim))) |
| 83 | + self.assertFalse( |
| 84 | + is_same_shape(self.tensors[0].to_sparse_csr(), |
| 85 | + self.tensors[2].to_sparse_coo(self.sparse_dim))) |
| 86 | + self.assertFalse( |
| 87 | + is_same_shape(self.tensors[1].to_sparse_csr(), |
| 88 | + self.tensors[2].to_sparse_coo(self.sparse_dim))) |
| 89 | + |
| 90 | + def test_coo_dense(self): |
| 91 | + self.assertTrue( |
| 92 | + is_same_shape(self.tensors[0].to_sparse_coo(self.sparse_dim), |
| 93 | + self.tensors[1])) |
| 94 | + self.assertFalse( |
| 95 | + is_same_shape(self.tensors[0].to_sparse_coo(self.sparse_dim), |
| 96 | + self.tensors[2])) |
| 97 | + self.assertFalse( |
| 98 | + is_same_shape(self.tensors[1].to_sparse_coo(self.sparse_dim), |
| 99 | + self.tensors[2])) |
| 100 | + |
| 101 | + def test_coo_csr(self): |
| 102 | + self.assertTrue( |
| 103 | + is_same_shape(self.tensors[0].to_sparse_coo(self.sparse_dim), |
| 104 | + self.tensors[1].to_sparse_csr())) |
| 105 | + self.assertFalse( |
| 106 | + is_same_shape(self.tensors[0].to_sparse_coo(self.sparse_dim), |
| 107 | + self.tensors[2].to_sparse_csr())) |
| 108 | + self.assertFalse( |
| 109 | + is_same_shape(self.tensors[1].to_sparse_coo(self.sparse_dim), |
| 110 | + self.tensors[2].to_sparse_csr())) |
| 111 | + |
| 112 | + def test_coo_coo(self): |
| 113 | + self.assertTrue( |
| 114 | + is_same_shape(self.tensors[0].to_sparse_coo(self.sparse_dim), |
| 115 | + self.tensors[1].to_sparse_coo(self.sparse_dim))) |
| 116 | + self.assertFalse( |
| 117 | + is_same_shape(self.tensors[0].to_sparse_coo(self.sparse_dim), |
| 118 | + self.tensors[2].to_sparse_coo(self.sparse_dim))) |
| 119 | + self.assertFalse( |
| 120 | + is_same_shape(self.tensors[1].to_sparse_coo(self.sparse_dim), |
| 121 | + self.tensors[2].to_sparse_coo(self.sparse_dim))) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + unittest.main() |
0 commit comments