-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_sparse_layer.py
320 lines (282 loc) · 10.1 KB
/
test_sparse_layer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import torch
from torch import nn
import torch.quantization as quant
import pytest
from layer.sparse_linear import SparseLinear
# torch.set_num_threads(1)
def replace_zeros_with_random(tensor):
# Create a mask for zero elements
zero_mask = tensor == 0
# import pdb; pdb.set_trace()
# Generate random numbers with the same shape as the input tensor
random_tensor = torch.randn_like(tensor)
# Replace zeros with random numbers
tensor[zero_mask] = random_tensor[zero_mask]
# import pdb; pdb.set_trace()
return tensor
def set_weights_to_zero(layer, percentage):
# Get the weight tensor
weights = layer.weight.data
# Calculate the number of weights to set to 0
num_weights = weights.numel()
num_zeros = int(num_weights * percentage / 100.0)
# Create a mask tensor
mask = torch.ones_like(weights)
# Randomly select indices to set to 0
zero_indices = torch.randperm(num_weights)[:num_zeros]
# Set the selected indices in the mask to 0
mask.view(-1)[zero_indices] = 0
# Apply the mask to the weights
layer.weight.data *= mask
def perform_zero_input_test(batch_size, out_rows, inner_dim, out_cols):
one_layer_net_torch_stock = nn.Linear(in_features=inner_dim, out_features=out_cols, bias=False, dtype=torch.bfloat16)
# Make sure all weights are representable in bfloat16 since that's what AMX will operate on.
one_layer_net_torch_extcpp = SparseLinear.from_linear(one_layer_net_torch_stock)
x = torch.zeros(batch_size, out_rows, inner_dim, dtype=torch.bfloat16)
with torch.no_grad():
o_torch_stock = one_layer_net_torch_stock(x)
o_torch_extcpp = one_layer_net_torch_extcpp(x)
assert torch.equal(o_torch_stock, o_torch_extcpp)
def perform_random_input_test(batch_size, out_rows, inner_dim, out_cols):
one_layer_net_torch_stock = nn.Linear(in_features=inner_dim, out_features=out_cols, bias=False, dtype=torch.bfloat16)
one_layer_net_torch_stock.weight.data = replace_zeros_with_random(one_layer_net_torch_stock.weight.data)
# Make sure all weights are representable in bfloat16 since that's what AMX will operate on.
one_layer_net_torch_extcpp = SparseLinear.from_linear(one_layer_net_torch_stock)
x = torch.randn(batch_size, out_rows, inner_dim, dtype=torch.bfloat16)
with torch.no_grad():
o_torch_stock = one_layer_net_torch_stock(x)
o_torch_extcpp = one_layer_net_torch_extcpp(x)
torch.testing.assert_close(o_torch_extcpp, o_torch_stock)
# assert torch.equal(o_torch_stock, o_torch_extcpp)
def perform_random_input_test_with_sparsity(batch_size, out_rows, inner_dim, out_cols, sparsity_percentage):
one_layer_net_torch_stock = nn.Linear(in_features=inner_dim, out_features=out_cols, bias=False, dtype=torch.bfloat16)
# Make sure all weights are representable in bfloat16 since that's what AMX will operate on.
set_weights_to_zero(one_layer_net_torch_stock, sparsity_percentage)
one_layer_net_torch_extcpp = SparseLinear.from_linear(one_layer_net_torch_stock)
x = torch.randn(batch_size, out_rows, inner_dim, dtype=torch.bfloat16)
with torch.no_grad():
o_torch_stock = one_layer_net_torch_stock(x)
o_torch_extcpp = one_layer_net_torch_extcpp(x)
torch.testing.assert_close(o_torch_extcpp, o_torch_stock)
# assert torch.equal(o_torch_stock, o_torch_extcpp)
test_data = [
(1, 1, 32, 16),
(1, 1, 256, 16),
(1, 2, 32, 16),
(1, 16, 32, 16),
(1, 32, 32, 16),
(1, 512, 512, 16),
(1, 1024, 1024, 16),
# (1, 1, 2, 16),
# (1, 4, 2, 16),
# (1, 5, 2, 16),
# (1, 1, 4, 16),
# (1, 2, 4, 16),
# (1, 4, 4, 16),
# (1, 6, 4, 16),
# (1, 6, 18, 16),
]
@pytest.mark.parametrize("batch_size,out_rows,inner_dim,out_cols", test_data)
def test_zero_inputs_even_d(batch_size, out_rows,inner_dim,out_cols):
perform_zero_input_test(batch_size, out_rows,inner_dim,out_cols)
test_data = [
(1, 1, 1, 1),
]
@pytest.mark.parametrize("batch_size,out_rows,inner_dim,out_cols", test_data)
@pytest.mark.skip(reason="Odd Inner Dimension is not yet supported because each 2 elements are put in the same row.")
def test_zero_inputs_odd_d(batch_size,out_rows,inner_dim,out_cols):
perform_zero_input_test(batch_size,out_rows,inner_dim,out_cols)
test_data = [
(1, 1, 32, 32),
]
@pytest.mark.parametrize("batch_size,out_rows,inner_dim,out_cols", test_data)
def test_zero_inputs_more_than_a_tile(batch_size,out_rows,inner_dim,out_cols):
perform_zero_input_test(batch_size,out_rows,inner_dim,out_cols)
test_data = [
# (1, 1, 2, 16),
# (1, 1, 8, 16),
# (1, 1, 2, 16),
# (1, 3, 2, 16),
# (1, 4, 2, 16),
# (1, 5, 2, 16),
# (1, 1, 4, 16),
# (1, 2, 4, 16),
# (1, 4, 4, 16),
# (1, 6, 4, 16),
# (1, 8, 4, 16),
# (1, 1, 8, 16),
# (1, 2, 8, 16),
# (1, 4, 8, 16),
# (1, 6, 8, 16),
# (1, 8, 8, 16),
# (1, 1, 16, 16),
# (1, 2, 16, 16),
# (1, 4, 16, 16),
# (1, 6, 16, 16),
# (1, 8, 16, 16),
# (1, 1, 32, 16),
# (1, 2, 32, 16),
# (1, 4, 32, 16),
# (1, 6, 32, 16),
(1, 32, 32, 32),
# (1, 16, 32, 16),
]
@pytest.mark.parametrize("batch_size,out_rows,inner_dim,out_cols", test_data)
def test_random_inputs_even_d(batch_size,out_rows,inner_dim,out_cols):
perform_random_input_test(batch_size,out_rows,inner_dim,out_cols)
test_data = [
(1, 32, 32, 16),
(1, 32, 32, 16),
(1, 32, 32, 16),
(1, 32, 32, 16),
(1, 64, 32, 16),
(1, 128, 32, 16),
(1, 512, 32, 16),
# (1, 512, 18, 16), # Non-standard tile size.
(1, 1, 32, 32),
(1, 2, 32, 32),
(1, 4, 32, 32),
(1, 32, 32, 32),
(1, 512, 32, 32),
(1, 1, 32, 64),
(1, 1, 32, 512),
(1, 128, 32, 128),
(1, 1, 32, 32),
(1, 32, 32, 32),
(1, 128, 32, 128),
(1, 64, 32, 64),
(1, 1, 64, 16),
(1, 2, 64, 16),
(1, 2, 64, 16),
(1, 16, 64, 16),
(1, 32, 64, 32),
(1, 128, 128, 128),
(1, 1024, 1024, 1024),
# (1, 1, 4096 * 4, 4096),
# (1, 1, 4096 * 4, 4096), # Added 3 times because this case before using int64 type would randomly fail.
# (1, 1, 4096 * 4, 4096),
]
@pytest.mark.parametrize("batch_size,out_rows,inner_dim,out_cols", test_data)
# @pytest.mark.skip(reason="More than 1 tile is not supported yet.")
def test_random_inputs_more_than_a_tile_all_full(batch_size,out_rows,inner_dim,out_cols):
perform_random_input_test(batch_size,out_rows,inner_dim,out_cols)
test_data = [
(1, 18, 2, 16),
]
@pytest.mark.parametrize("batch_size,out_rows,inner_dim,out_cols", test_data)
@pytest.mark.skip(reason="Partial tiles are not supported yet.")
def test_random_inputs_more_than_a_tile_partial_tiles(batch_size,out_rows,inner_dim,out_cols):
perform_random_input_test(batch_size,out_rows,inner_dim,out_cols)
test_data = [
# (1, 1, 2, 16),
(1, 16, 32, 16),
(1, 1, 64, 16),
(1, 1, 128, 16),
(1, 1, 256, 16),
(1, 16, 64, 16),
(1, 16, 256, 16),
# (1, 32, 2, 16),
# (1, 64, 2, 16),
# (1, 128, 2, 16),
# (1, 256, 2, 16),
(1, 256, 256, 16),
(1, 1, 32, 32),
(1, 1, 32, 64),
(1, 1, 32, 128),
(1, 1, 32, 256),
# (1, 1, 4, 32),
(1, 32, 32, 16),
# (1, 512, 18, 16), # Non-standard tile size.
(1, 32, 32, 16),
# (1, 32, 4, 16),
# (1, 32, 4, 16),
(1, 32, 32, 16),
(1, 32, 32, 16),
(1, 64, 32, 16),
(1, 128, 32, 16),
(1, 512, 32, 16),
(1, 1, 32, 32),
(1, 2, 32, 32),
(1, 4, 32, 32),
(1, 32, 32, 32),
(1, 512, 32, 32),
(1, 1, 32, 64),
(1, 1, 32, 512),
(1, 128, 32, 128),
(1, 1, 32, 32),
(1, 32, 32, 32),
(1, 128, 32, 128),
(1, 64, 32, 64),
(1, 1, 64, 16),
(1, 2, 64, 16),
(1, 2, 64, 16),
(1, 16, 64, 16),
(1, 32, 64, 32),
(1, 128, 128, 128),
(1, 1024, 1024, 1024),
(1, 1, 4096 * 4, 4096),
# (1, 1024, 1024*4, 1024*4),
(16, 1, 32, 16),
(16, 16, 32, 16),
(16, 1, 64, 16),
(16, 1, 128, 16),
(16, 1, 256, 16),
(16, 16, 64, 16),
(16, 16, 256, 16),
(16, 32, 32, 16),
(16, 64, 32, 16),
(16, 128, 32, 16),
(16, 256, 32, 16),
(16, 256, 256, 16),
(16, 1, 32, 32),
(16, 1, 32, 64),
(16, 1, 32, 128),
(16, 1, 32, 256),
(16, 1, 32, 32),
(16, 32, 32, 16),
(16, 32, 32, 16),
(16, 32, 32, 16),
(16, 32, 32, 16),
(16, 32, 32, 16),
(16, 32, 32, 16),
(16, 64, 32, 16),
(16, 128, 32, 16),
(16, 512, 32, 16),
(16, 1, 32, 32),
(16, 2, 32, 32),
(16, 4, 32, 32),
(16, 32, 32, 32),
(16, 512, 32, 32),
(16, 1, 32, 64),
(16, 1, 32, 512),
(16, 128, 32, 128),
(16, 1, 32, 32),
(16, 32, 32, 32),
(16, 128, 32, 128),
(16, 64, 32, 64),
(16, 1, 64, 16),
(16, 2, 64, 16),
(16, 2, 64, 16),
(16, 16, 64, 16),
(16, 32, 64, 32),
(16, 128, 128, 128),
(16, 1024, 1024, 1024),
(16, 1, 4096 * 4, 4096),
# (16, 1024, 1024*4, 1024*4),
]
@pytest.mark.parametrize("batch_size,out_rows,inner_dim,out_cols", test_data)
# @pytest.mark.skip(reason="More than 1 tile is not supported yet.")
def test_random_inputs_more_than_a_tile_with_20p_sparsity(batch_size,out_rows,inner_dim,out_cols):
perform_random_input_test_with_sparsity(batch_size,out_rows,inner_dim,out_cols,20)
@pytest.mark.parametrize("batch_size,out_rows,inner_dim,out_cols", test_data)
# @pytest.mark.skip(reason="More than 1 tile is not supported yet.")
def test_random_inputs_more_than_a_tile_with_50p_sparsity(batch_size,out_rows,inner_dim,out_cols):
perform_random_input_test_with_sparsity(batch_size,out_rows,inner_dim,out_cols,50)
@pytest.mark.parametrize("batch_size,out_rows,inner_dim,out_cols", test_data)
# @pytest.mark.skip(reason="More than 1 tile is not supported yet.")
def test_random_inputs_more_than_a_tile_with_80p_sparsity(batch_size,out_rows,inner_dim,out_cols):
perform_random_input_test_with_sparsity(batch_size,out_rows,inner_dim,out_cols,80)
@pytest.mark.parametrize("batch_size,out_rows,inner_dim,out_cols", test_data)
# @pytest.mark.skip(reason="More than 1 tile is not supported yet.")
def test_random_inputs_more_than_a_tile_with_99p_sparsity(batch_size,out_rows,inner_dim,out_cols):
perform_random_input_test_with_sparsity(batch_size,out_rows,inner_dim,out_cols,99)