get_mask_2d_best¶
- paddle.fluid.contrib.sparsity.utils. get_mask_2d_best ( mat, n, m ) [source]
-
Generate 2D n:m sparse pattern mask of the input matrix
mat
to form sparse matrix with maximun L1 norm .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.Note: L1 norm of sparse matrix from Best API is greater than or equal to the one from Greedy.
- Parameters
-
mat (nparray) – The input matrix.
n (int) – n of n:m sparse pattern.
m (int) – m of n:m sparse pattern.
- Returns
-
The 1D n:m sparse mask of
mat
. - Return type
-
nparray
Examples
import numpy as np import paddle.fluid.contrib.sparsity as sparsity mat = np.array([[2, 8, 9, 9], [9, 1, 3, 9], [5, 6, 3, 9], [2, 4, 6, 9]]) mask_greedy = sparsity.get_mask_2d_greedy(mat, 2, 4) mask_best = sparsity.get_mask_2d_best(mat, 2, 4) print("L1 norm of `greedy` sparse matrix", np.multiply(mat, mask_greedy).sum()) # 56 print("L1 norm of `best` sparse matrix", np.multiply(mat, mask_best).sum()) # 61