test_asp_utils.py 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2021 NVIDIA Corporation.  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.

16 17
import threading
import time
18
import unittest
19

20 21
import numpy as np

22 23
import paddle

24 25 26 27

class TestASPUtils(unittest.TestCase):
    def test_get_check_method(self):
        self.assertEqual(
28 29
            paddle.incubate.asp.CheckMethod.get_checking_method(
                paddle.incubate.asp.MaskAlgo.MASK_1D
30
            ),
31
            paddle.incubate.asp.CheckMethod.CHECK_1D,
32
        )
33
        self.assertEqual(
34 35
            paddle.incubate.asp.CheckMethod.get_checking_method(
                paddle.incubate.asp.MaskAlgo.MASK_2D_GREEDY
36
            ),
37
            paddle.incubate.asp.CheckMethod.CHECK_2D,
38
        )
39
        self.assertEqual(
40 41
            paddle.incubate.asp.CheckMethod.get_checking_method(
                paddle.incubate.asp.MaskAlgo.MASK_2D_BEST
42
            ),
43
            paddle.incubate.asp.CheckMethod.CHECK_2D,
44
        )
45 46

    def test_density(self):
47 48 49 50 51 52 53 54 55
        x = np.array(
            [
                [1.0, 1.0, 1.0, 0.0, 1.0],
                [1.0, 1.0, 0.0, 0.0, 1.0],
                [1.0, 0.0, 0.0, 0.0, 1.0],
                [1.0, 1.0, 0.0, 0.0, 1.0],
                [0.0, 1.0, 0.0, 0.0, 1.0],
            ]
        )
56 57 58 59 60
        self.assertEqual(paddle.incubate.asp.calculate_density(x), 0.56)
        x[:, 0] = 0.0
        self.assertEqual(paddle.incubate.asp.calculate_density(x), 0.4)

    def test_check_mask_1d(self):
61 62 63 64 65 66 67 68 69
        x = np.array(
            [
                [1.0, 0.0, 0.0, 1.0, 1.0],
                [1.0, 1.0, 0.0, 0.0, 1.0],
                [1.0, 1.0, 0.0, 0.0, 1.0],
                [1.0, 1.0, 0.0, 0.0, 1.0],
                [0.0, 1.0, 0.0, 0.0, 1.0],
            ]
        )
70 71 72 73 74 75
        self.assertTrue(paddle.incubate.asp.check_mask_1d(x, 2, 4))
        self.assertFalse(paddle.incubate.asp.check_mask_1d(x, 3, 4))
        self.assertTrue(paddle.incubate.asp.check_mask_1d(x, 2, 5))
        self.assertFalse(paddle.incubate.asp.check_mask_1d(x, 3, 5))
        self.assertTrue(paddle.incubate.asp.check_mask_1d(x, 3, 6))
        self.assertFalse(paddle.incubate.asp.check_mask_1d(x, 4, 6))
76 77 78 79

    def test_get_mask_1d(self):
        for _ in range(10):
            x = np.random.randint(10, size=(5, 5))
80 81
            x = paddle.incubate.asp.get_mask_1d(x, 2, 4)
            self.assertTrue(paddle.incubate.asp.check_mask_1d(x, 2, 4))
82 83

            x = np.random.randn(5, 4)
84 85
            x = paddle.incubate.asp.get_mask_1d(x, 2, 4)
            self.assertTrue(paddle.incubate.asp.check_mask_1d(x, 2, 4))
86 87

    def test_check_mask_2d(self):
88 89 90 91 92 93 94 95 96
        x = np.array(
            [
                [1.0, 0.0, 0.0, 1.0, 1.0],
                [0.0, 1.0, 0.0, 0.0, 0.0],
                [0.0, 0.0, 1.0, 0.0, 1.0],
                [1.0, 1.0, 0.0, 0.0, 0.0],
                [0.0, 1.0, 0.0, 0.0, 1.0],
            ]
        )
97 98 99 100 101 102
        self.assertTrue(paddle.incubate.asp.check_mask_2d(x, 2, 4))
        self.assertFalse(paddle.incubate.asp.check_mask_2d(x, 3, 4))
        self.assertTrue(paddle.incubate.asp.check_mask_2d(x, 2, 5))
        self.assertFalse(paddle.incubate.asp.check_mask_2d(x, 3, 5))
        self.assertTrue(paddle.incubate.asp.check_mask_2d(x, 3, 6))
        self.assertFalse(paddle.incubate.asp.check_mask_2d(x, 4, 6))
103 104 105 106

    def test_get_mask_2d_greedy(self):
        for _ in range(10):
            x = np.random.randint(10, size=(5, 5))
107 108
            x = paddle.incubate.asp.get_mask_2d_greedy(x, 2, 4)
            self.assertTrue(paddle.incubate.asp.check_mask_2d(x, 2, 4))
109 110

            x = np.random.randn(5, 4)
111 112
            x = paddle.incubate.asp.get_mask_2d_greedy(x, 2, 4)
            self.assertTrue(paddle.incubate.asp.check_mask_2d(x, 2, 4))
113 114 115 116

    def test_get_mask_2d_best(self):
        for _ in range(10):
            x = np.random.randint(10, size=(5, 5))
117 118
            x = paddle.incubate.asp.get_mask_2d_best(x, 2, 4)
            self.assertTrue(paddle.incubate.asp.check_mask_2d(x, 2, 4))
119 120

            x = np.random.randn(5, 4)
121 122
            x = paddle.incubate.asp.get_mask_2d_best(x, 2, 4)
            self.assertTrue(paddle.incubate.asp.check_mask_2d(x, 2, 4))
123 124 125 126 127 128 129 130 131 132 133

    def test_threadsafe_valid_2d_patterns(self):
        def get_reference(m=4, n=2):
            from itertools import permutations

            patterns = np.zeros(m)
            patterns[:n] = 1
            patterns = list(set(permutations(patterns.tolist())))
            patterns = patterns + patterns
            patterns = np.asarray(list(set(permutations(patterns, m))))

134 135 136 137 138
            valid = (
                ((patterns.sum(axis=1) <= n).sum(axis=1) == m)
                .nonzero()[0]
                .reshape(-1)
            )
139 140 141 142 143 144
            valid_patterns = np.empty((valid.shape[0], m, m))
            valid_patterns[:] = patterns[valid[:]]
            return valid_patterns

        for _ in range(4):
            computing_thread = threading.Thread(
145
                target=paddle.incubate.asp.utils._compute_valid_2d_patterns,
146 147
                args=(2, 4),
            )
148 149
            computing_thread.start()
        time.sleep(3)
150
        patterns_map = paddle.incubate.asp.utils._valid_2d_patterns
151 152 153 154 155 156
        reference_patterns = get_reference()
        reference_key = '4_2'

        self.assertTrue(reference_key in patterns_map)
        self.assertTrue(len(patterns_map) == 1)
        self.assertTrue(
157 158
            (reference_patterns == patterns_map[reference_key]).all()
        )
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

    def test_check_sparsity(self):
        for _ in range(10):
            x = np.random.randint(10, size=(5))
            x_2d = x.reshape(1, x.shape[0])
            self.__test_1D_2D_sparsity_checking_methods(x_2d)

            x = np.random.randint(10, size=(5, 5))
            x_2d = x
            self.__test_1D_2D_sparsity_checking_methods(x_2d)

            x = np.random.randint(10, size=(5, 5, 5))
            x_2d = x.reshape(x.shape[0] * x.shape[1], x.shape[2])
            self.__test_1D_2D_sparsity_checking_methods(x_2d)

            x = np.random.randint(10, size=(5, 5, 5, 5))
            x_2d = x.reshape(x.shape[0], x.shape[1] * x.shape[2] * x.shape[3])
            self.__test_1D_2D_sparsity_checking_methods(x_2d)

    def test_create_mask(self):
        for _ in range(10):
            x = np.random.randint(10, size=(5))
            self.__test_1D_2D_sparse_mask_generation_methods(x)

            x = np.random.randint(10, size=(5, 5))
            self.__test_1D_2D_sparse_mask_generation_methods(x)

            x = np.random.randint(10, size=(5, 5, 5))
            self.__test_1D_2D_sparse_mask_generation_methods(x)

            x = np.random.randint(10, size=(5, 5, 5, 5))
            self.__test_1D_2D_sparse_mask_generation_methods(x)

    def __test_1D_2D_sparsity_checking_methods(self, x_2d):
193
        mask = paddle.incubate.asp.get_mask_1d(x_2d, 2, 4)
194
        self.assertEqual(
195
            paddle.incubate.asp.check_sparsity(
196
                mask,
197
                func_name=paddle.incubate.asp.CheckMethod.CHECK_1D,
198
                n=2,
199 200
                m=4,
            ),
201
            paddle.incubate.asp.check_mask_1d(mask, 2, 4),
202
        )
203
        mask = paddle.incubate.asp.get_mask_2d_best(x_2d, 2, 4)
204
        self.assertEqual(
205
            paddle.incubate.asp.check_sparsity(
206
                mask,
207
                func_name=paddle.incubate.asp.CheckMethod.CHECK_2D,
208
                n=2,
209 210
                m=4,
            ),
211
            paddle.incubate.asp.check_mask_2d(mask, 2, 4),
212
        )
213 214

    def __test_1D_2D_sparse_mask_generation_methods(self, x):
215
        mask = paddle.incubate.asp.create_mask(
216
            x,
217
            func_name=paddle.incubate.asp.MaskAlgo.MASK_1D,
218
            n=2,
219 220
            m=4,
        )
221
        self.assertTrue(
222
            paddle.incubate.asp.check_sparsity(
223
                mask,
224
                func_name=paddle.incubate.asp.CheckMethod.CHECK_1D,
225
                n=2,
226 227 228
                m=4,
            )
        )
229
        mask = paddle.incubate.asp.create_mask(
230
            x,
231
            func_name=paddle.incubate.asp.MaskAlgo.MASK_2D_GREEDY,
232
            n=2,
233 234
            m=4,
        )
235
        self.assertTrue(
236
            paddle.incubate.asp.check_sparsity(
237
                mask,
238
                func_name=paddle.incubate.asp.CheckMethod.CHECK_2D,
239
                n=2,
240 241 242
                m=4,
            )
        )
243
        mask = paddle.incubate.asp.create_mask(
244
            x,
245
            func_name=paddle.incubate.asp.MaskAlgo.MASK_2D_BEST,
246
            n=2,
247 248
            m=4,
        )
249
        self.assertTrue(
250
            paddle.incubate.asp.check_sparsity(
251
                mask,
252
                func_name=paddle.incubate.asp.CheckMethod.CHECK_2D,
253
                n=2,
254 255 256
                m=4,
            )
        )
257 258 259 260


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