check_mask_1d

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

Check if every row of the input matrix mat is in 1D n:m sparse pattern. This function would pad the second dimension of mat by zero to be a multiples of m if necessary.

1D n:m sparse pattern: At least n zeros in every \(1 \times m\) block.

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 row of mat is in 1D 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, 1, 3, 0],
              [1, 0, 0, 1]])
sparsity.check_mask_1d(x, 2, 4) # True

x = np.array([[0, 1, 5, 4],
              [1, 0, 0, 1]])
sparsity.check_mask_1d(x, 2, 4) # False

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