Skip to content

Commit 7901ff9

Browse files
authored
Revert "speedup sparse block (PaddlePaddle#1022)" (PaddlePaddle#1058)
1 parent f01fc4f commit 7901ff9

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

paddleslim/prune/unstructured_pruner_utils.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,11 @@
88

99
def cal_mxn_avg_matrix(mat, m=1, n=1):
1010
if m == 1 and n == 1: return copy.deepcopy(mat)
11-
12-
ori_row, ori_col = mat.shape[0], mat.shape[1]
13-
if len(mat.shape) == 4:
14-
assert mat.shape[2:] == (1, 1), "Only support for (n, n, 1, 1) for now."
15-
mat = mat.reshape(ori_row, ori_col)
16-
17-
res_col = n - len(mat[0]) % n
18-
res_row = m - len(mat) % m
19-
20-
mat = np.pad(mat, ((0, res_col), (0, res_col)), 'reflect')
2111
avg_mat = np.zeros_like(mat)
22-
new_shape = [len(mat) // m, len(mat[0]) // n, m, n]
23-
strides = mat.itemsize * np.array([len(mat) * m, n, len(mat), 1])
24-
mat = np.lib.stride_tricks.as_strided(mat, shape=new_shape, strides=strides)
25-
mat = mat.mean((2, 3), keepdims=True)
26-
mat = np.tile(mat, (1, 1, m, n))
27-
for i in range(len(mat)):
28-
avg_mat[i * m:i * m + m] = np.concatenate(list(mat[i]), axis=1)
29-
avg_mat = avg_mat[:ori_row, :ori_col]
30-
if len(mat.shape) == 4:
31-
avg_mat = avg_mat.reshape(ori_row, ori_col, 1, 1)
12+
rows = len(mat) // m + 1
13+
cols = len(mat[0]) // n + 1
14+
for row in range(rows):
15+
for col in range(cols):
16+
avg_mat[m * row:m * row + m, n * col:n * col + n] = np.mean(mat[
17+
m * row:m * row + m, n * col:n * col + n])
3218
return avg_mat

0 commit comments

Comments
 (0)