“6287df1848fcd8fff06abb1c3f3f89559f509388”上不存在“tests/pytest/insert/basic.py”
test_asp_utils.py 9.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
# 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.

from __future__ import print_function

import unittest
import threading, time
import paddle
import numpy as np


class TestASPUtils(unittest.TestCase):
    def test_get_check_method(self):
        self.assertEqual(
27 28 29
            paddle.fluid.contrib.sparsity.CheckMethod.get_checking_method(
                paddle.fluid.contrib.sparsity.MaskAlgo.MASK_1D),
            paddle.fluid.contrib.sparsity.CheckMethod.CHECK_1D)
30
        self.assertEqual(
31 32 33
            paddle.fluid.contrib.sparsity.CheckMethod.get_checking_method(
                paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_GREEDY),
            paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D)
34
        self.assertEqual(
35 36 37
            paddle.fluid.contrib.sparsity.CheckMethod.get_checking_method(
                paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_BEST),
            paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D)
38 39 40 41 42

    def test_density(self):
        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]])
43
        self.assertEqual(paddle.incubate.asp.calculate_density(x), 0.56)
44
        x[:, 0] = 0.0
45
        self.assertEqual(paddle.incubate.asp.calculate_density(x), 0.4)
46 47 48 49 50

    def test_check_mask_1d(self):
        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]])
51 52 53 54 55 56
        self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(x, 2, 4))
        self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_1d(x, 3, 4))
        self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(x, 2, 5))
        self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_1d(x, 3, 5))
        self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_1d(x, 3, 6))
        self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_1d(x, 4, 6))
57 58 59 60

    def test_get_mask_1d(self):
        for _ in range(10):
            x = np.random.randint(10, size=(5, 5))
61 62 63
            x = paddle.fluid.contrib.sparsity.get_mask_1d(x, 2, 4)
            self.assertTrue(
                paddle.fluid.contrib.sparsity.check_mask_1d(x, 2, 4))
64 65

            x = np.random.randn(5, 4)
66 67 68
            x = paddle.fluid.contrib.sparsity.get_mask_1d(x, 2, 4)
            self.assertTrue(
                paddle.fluid.contrib.sparsity.check_mask_1d(x, 2, 4))
69 70 71 72 73

    def test_check_mask_2d(self):
        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]])
74 75 76 77 78 79
        self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(x, 2, 4))
        self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_2d(x, 3, 4))
        self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(x, 2, 5))
        self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_2d(x, 3, 5))
        self.assertTrue(paddle.fluid.contrib.sparsity.check_mask_2d(x, 3, 6))
        self.assertFalse(paddle.fluid.contrib.sparsity.check_mask_2d(x, 4, 6))
80 81 82 83

    def test_get_mask_2d_greedy(self):
        for _ in range(10):
            x = np.random.randint(10, size=(5, 5))
84 85 86
            x = paddle.fluid.contrib.sparsity.get_mask_2d_greedy(x, 2, 4)
            self.assertTrue(
                paddle.fluid.contrib.sparsity.check_mask_2d(x, 2, 4))
87 88

            x = np.random.randn(5, 4)
89 90 91
            x = paddle.fluid.contrib.sparsity.get_mask_2d_greedy(x, 2, 4)
            self.assertTrue(
                paddle.fluid.contrib.sparsity.check_mask_2d(x, 2, 4))
92 93 94 95

    def test_get_mask_2d_best(self):
        for _ in range(10):
            x = np.random.randint(10, size=(5, 5))
96 97 98
            x = paddle.fluid.contrib.sparsity.get_mask_2d_best(x, 2, 4)
            self.assertTrue(
                paddle.fluid.contrib.sparsity.check_mask_2d(x, 2, 4))
99 100

            x = np.random.randn(5, 4)
101 102 103
            x = paddle.fluid.contrib.sparsity.get_mask_2d_best(x, 2, 4)
            self.assertTrue(
                paddle.fluid.contrib.sparsity.check_mask_2d(x, 2, 4))
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

    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))))

            valid = ((patterns.sum(axis=1) <= n).sum(axis=1) == m
                     ).nonzero()[0].reshape(-1)
            valid_patterns = np.empty((valid.shape[0], m, m))
            valid_patterns[:] = patterns[valid[:]]
            return valid_patterns

        for _ in range(4):
            computing_thread = threading.Thread(
                target=paddle.fluid.contrib.sparsity.utils.
124
                _compute_valid_2d_patterns,
125 126 127
                args=(2, 4))
            computing_thread.start()
        time.sleep(3)
128
        patterns_map = paddle.fluid.contrib.sparsity.utils._valid_2d_patterns
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        reference_patterns = get_reference()
        reference_key = '4_2'

        self.assertTrue(reference_key in patterns_map)
        self.assertTrue(len(patterns_map) == 1)
        self.assertTrue((reference_patterns == patterns_map[reference_key]).all(
        ))

    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):
170
        mask = paddle.fluid.contrib.sparsity.get_mask_1d(x_2d, 2, 4)
171
        self.assertEqual(
172 173 174 175 176 177 178
            paddle.fluid.contrib.sparsity.check_sparsity(
                mask,
                func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_1D,
                n=2,
                m=4),
            paddle.fluid.contrib.sparsity.check_mask_1d(mask, 2, 4))
        mask = paddle.fluid.contrib.sparsity.get_mask_2d_best(x_2d, 2, 4)
179
        self.assertEqual(
180 181 182 183 184 185
            paddle.fluid.contrib.sparsity.check_sparsity(
                mask,
                func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D,
                n=2,
                m=4),
            paddle.fluid.contrib.sparsity.check_mask_2d(mask, 2, 4))
186 187

    def __test_1D_2D_sparse_mask_generation_methods(self, x):
188 189 190 191 192
        mask = paddle.fluid.contrib.sparsity.create_mask(
            x,
            func_name=paddle.fluid.contrib.sparsity.MaskAlgo.MASK_1D,
            n=2,
            m=4)
193
        self.assertTrue(
194 195 196 197 198 199 200 201 202 203
            paddle.fluid.contrib.sparsity.check_sparsity(
                mask,
                func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_1D,
                n=2,
                m=4))
        mask = paddle.fluid.contrib.sparsity.create_mask(
            x,
            func_name=paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_GREEDY,
            n=2,
            m=4)
204
        self.assertTrue(
205 206 207 208 209 210 211 212 213 214
            paddle.fluid.contrib.sparsity.check_sparsity(
                mask,
                func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D,
                n=2,
                m=4))
        mask = paddle.fluid.contrib.sparsity.create_mask(
            x,
            func_name=paddle.fluid.contrib.sparsity.MaskAlgo.MASK_2D_BEST,
            n=2,
            m=4)
215
        self.assertTrue(
216 217 218 219 220
            paddle.fluid.contrib.sparsity.check_sparsity(
                mask,
                func_name=paddle.fluid.contrib.sparsity.CheckMethod.CHECK_2D,
                n=2,
                m=4))
221 222 223 224


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