get_mask_2d_greedy¶
- paddle.fluid.contrib.sparsity.utils. get_mask_2d_greedy ( mat, n, m ) [source]
-
Greedily generate 2D n:m sparse pattern mask of the input matrix
mat
. This function would pad each dimension ofmat
by zero to be a multiples ofm
before mask generation.2D n:m sparse pattern: At least \(n \times n\) zeros in every \(m \times m\) block under the constraint of at least
n
zeros for each row and column. Greedily generating: For each \(m \times m\) block, selecting values to keep in descent order.- Parameters
-
mat (nparray) – The input matrix.
n (int) – n of n:m sparse pattern.
m (int) – m of n:m sparse pattern.
- Returns
-
The 2D n:m sparse mask of
mat
. - Return type
-
nparray
Examples
import numpy as np import paddle.fluid.contrib.sparsity as sparsity mat = np.array([[9, 8, 3, 7], [9, 2, 1, 10], [5, 1, 3, 6], [2, 4, 6, 1]]) mask = sparsity.get_mask_2d_greedy(mat, 2, 4) # nparray([[1. 1. 0. 0.] # [1. 0. 0. 1.] # [0. 0. 1. 1.] # [0. 1. 1. 0.]]) sparsity.check_mask_2d(mask, 2, 4) # True