From 7901ff90b1c054be0d27f2504c349df439178149 Mon Sep 17 00:00:00 2001 From: Chang Xu Date: Wed, 20 Apr 2022 14:53:05 +0800 Subject: [PATCH] Revert "speedup sparse block (#1022)" (#1058) --- paddleslim/prune/unstructured_pruner_utils.py | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/paddleslim/prune/unstructured_pruner_utils.py b/paddleslim/prune/unstructured_pruner_utils.py index 7d9318a7..724db8cd 100644 --- a/paddleslim/prune/unstructured_pruner_utils.py +++ b/paddleslim/prune/unstructured_pruner_utils.py @@ -8,25 +8,11 @@ BLOCK_SPARSE_ACCURATE_THRESHOLD = 0.05 def cal_mxn_avg_matrix(mat, m=1, n=1): if m == 1 and n == 1: return copy.deepcopy(mat) - - ori_row, ori_col = mat.shape[0], mat.shape[1] - if len(mat.shape) == 4: - assert mat.shape[2:] == (1, 1), "Only support for (n, n, 1, 1) for now." - mat = mat.reshape(ori_row, ori_col) - - res_col = n - len(mat[0]) % n - res_row = m - len(mat) % m - - mat = np.pad(mat, ((0, res_col), (0, res_col)), 'reflect') avg_mat = np.zeros_like(mat) - new_shape = [len(mat) // m, len(mat[0]) // n, m, n] - strides = mat.itemsize * np.array([len(mat) * m, n, len(mat), 1]) - mat = np.lib.stride_tricks.as_strided(mat, shape=new_shape, strides=strides) - mat = mat.mean((2, 3), keepdims=True) - mat = np.tile(mat, (1, 1, m, n)) - for i in range(len(mat)): - avg_mat[i * m:i * m + m] = np.concatenate(list(mat[i]), axis=1) - avg_mat = avg_mat[:ori_row, :ori_col] - if len(mat.shape) == 4: - avg_mat = avg_mat.reshape(ori_row, ori_col, 1, 1) + rows = len(mat) // m + 1 + cols = len(mat[0]) // n + 1 + for row in range(rows): + for col in range(cols): + avg_mat[m * row:m * row + m, n * col:n * col + n] = np.mean(mat[ + m * row:m * row + m, n * col:n * col + n]) return avg_mat -- GitLab