check_sparsity

paddle.fluid.contrib.sparsity.utils. check_sparsity ( tensor, func_name=CheckMethod.CHECK_1D, n=2, m=4 ) [source]

Check if input tensor is in n:m sparse pattern via function given by func_name. Currently only support tensor with dimension less than or equal to 4.

Parameters
  • tensor (nparray) – The input tensor.

  • func_name (CheckMethod, optional) – The function name to generate spase mask. Default is CheckMethod.CHECK_1D. All options please refer to CheckMethod.

  • n (int, optional) – n of n:m sparse pattern. Default is 2.

  • m (int, optional) – m of n:m sparse pattern. Default is 4.

Returns

True if tensor pass checking of function given by func_name, else False.

Return type

bool

Examples

import numpy as np
import paddle.fluid.contrib.sparsity as sparsity

tensor = np.array([[2, 8, 9, 9],
                   [9, 1, 3, 9],
                   [5, 6, 3, 9],
                   [2, 4, 6, 9]])
mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D)
# nparray([[0 0 1 1],
#          [1 0 0 1],
#          [0 1 0 1],
#          [0 0 1 1]])
sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_1D) # True
sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_2D) # False