check_mask_2d

paddle.fluid.contrib.sparsity.utils. check_mask_2d ( mat, n, m ) [source]

Check if every \(m \times m\) block of the input matrix mat is in 2D n:m sparse pattern. This function would pad each dimension of mat by zero to be a multiples of m if necessary.

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.

Parameters
  • mat (nparray) – The input matrix.

  • n (int) – n of n:m sparse pattern.

  • m (int) – m of n:m sparse pattern.

Returns

True if every \(m \times m\) block of the input matrix mat is in 2D n:m sparse pattern, else False.

Return type

bool

Examples

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

x = np.array([[0, 8, 9, 0],
              [9, 0, 0, 10],
              [5, 0, 0, 6],
              [0, 4, 6, 0]])
sparsity.check_mask_2d(x, 2, 4) # True

x = np.array([[0, 8, 0, 9],
              [9, 0, 0, 10],
              [0, 5, 0, 6],
              [0, 4, 6, 0]])
sparsity.check_mask_2d(x, 2, 4) # False

# x would be padded to shape (8, 8)
x = np.array([[0, 8, 0, 9],
              [9, 0, 7, 0],
              [0, 5, 0, 6],
              [3, 0, 6, 0],
              [1, 1, 0, 1]])
sparsity.check_mask_2d(x, 2, 4) # True