-
Notifications
You must be signed in to change notification settings - Fork 43
/
bench_matvec_kernel.py
248 lines (183 loc) · 7.86 KB
/
bench_matvec_kernel.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
import argparse
import os
import time
import numpy as np
import torch
from scipy.stats import gmean
from spqr_quant import QuantizedLinear
from spqr_quant.inference import FeatureFlags
from spqr_quant.inference_kernels.kernel_selector import get_spqr_mul_timer, get_torch_mul_timer
def spqr_mul_timer(spqr_device: QuantizedLinear, x, feature_flag: FeatureFlags, num_runs):
runs = torch.empty(num_runs).cpu().float()
y = torch.zeros(spqr_device.m, dtype=x.dtype, device=x.device)
for i in range(num_runs):
y = torch.zeros_like(y)
get_spqr_mul_timer()(
spqr_device.m,
spqr_device.n,
spqr_device.bits,
spqr_device.beta1,
spqr_device.beta2,
spqr_device.dense_weights,
spqr_device.row_offsets,
spqr_device.col_vals,
spqr_device.nnz,
x,
y,
runs[i],
feature_flag,
)
return y, runs
def torch_mul_timer_runs(deq_w, x, num_runs):
if len(deq_w.shape) == 1:
n = x.shape[0]
m = deq_w.shape[0] // n
else:
m, n = deq_w.shape
assert n == x.shape[0]
runs = torch.empty(num_runs).cpu().float()
y = torch.zeros(m, dtype=x.dtype, device=x.device)
for i in range(num_runs):
y = torch.zeros_like(y)
get_torch_mul_timer()(deq_w, x, y, runs[i])
return y, runs
if __name__ == "__main__":
torch_runs = {}
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument(
"--tensor_path",
type=str,
required=True,
help="Path to folder containing the tensors of the form"
"model_path/"
" 0/"
" tensor0"
" tensor1",
)
parser.add_argument(
"--ptcsr_path",
type=str,
required=False,
help="Path to folder containing the tensors of the form"
"model_path/"
" 0/"
" tensor0"
" tensor1",
)
parser.add_argument(
"--output_path",
type=str,
help="Path to results *.csv file.",
)
args = parser.parse_args()
with open(args.output_path, "w") as f:
run_ptcsr = args.ptcsr_path is not None
base_path = args.tensor_path
base_path_modified_csr = args.ptcsr_path
seed = 1
np.random.seed(seed)
torch.random.manual_seed(seed)
NUM_RUNS = 2000
WARMUP = 10
device = torch.device("cuda")
for m in [4096, 11008]:
for n in [4096, 11008]:
d = torch.zeros((m, n), dtype=torch.float16, device=device)
x = torch.zeros(n, dtype=torch.float16, device=device)
y, dense_runs = torch_mul_timer_runs(d, x, NUM_RUNS)
this_algorithm = dense_runs[WARMUP:].min()
torch_runs[(m, n)] = this_algorithm
torch.cuda.empty_cache()
time.sleep(2)
csr_folders = os.listdir(base_path)
if run_ptcsr:
folders_modified_csr = os.listdir(base_path_modified_csr)
else:
folders_modified_csr = os.listdir(base_path)
csr_folders.sort()
folders_modified_csr.sort()
methods = [
FeatureFlags.SPARSE_FUSED_FP32,
]
f.write("Layer;Tensor Name;M;N;Sparsity (%)")
for method in [FeatureFlags.TORCH_FP16] + methods:
f.write(f";{method.pretty()} (ms)")
f.write(f";{method.pretty()} Modified CSR (ms)")
f.write("\n")
benchmark_results_ms = []
benchmark_speed_up = []
for layer_id in csr_folders:
folder = os.path.join(base_path, layer_id)
folder_ptcsr = os.path.join(base_path_modified_csr, layer_id)
if run_ptcsr:
folders_modified_csr = os.path.join(base_path_modified_csr, layer_id)
else:
folders_modified_csr = os.path.join(base_path, layer_id)
if not os.path.isdir(folder):
continue
for p, p_modified_csr in zip(os.listdir(folder), os.listdir(folder_ptcsr)):
tensor_path = os.path.join(folder, p)
tensor_path_modified_csr = os.path.join(folder_ptcsr, p_modified_csr)
spqr_module_modified_csr = torch.load(tensor_path_modified_csr)
deq_w_modified_csr = spqr_module_modified_csr.dequantize()
spqr_module_modified_csr.to(device=device)
spqr_module_device_modified_csr = spqr_module_modified_csr
spqr_module = torch.load(tensor_path)
m = spqr_module.m
n = spqr_module.n
print(f"Running {m} x {n}")
deq_w = spqr_module.dequantize()
spqr_module.to(device=device)
spqr_module_device = spqr_module
def generate_x_fp32(n, upper_bound=3):
x_fp32 = ((torch.rand(n) - 0.5) * 4 * upper_bound).int()
return x_fp32.float()
x_fp32 = generate_x_fp32(n)
x_fp16_device = x_fp32.cuda(device=device).half()
deq_w_device = deq_w.to(device).half().flatten()
dense_speed_up = 0
baseline_speed_up = 0
sparsity_perc = spqr_module_device.sparsity * 100
torch_run = torch_runs[(spqr_module_device.m, spqr_module_device.n)]
f.write(f"{layer_id};{p};{m};{n};{sparsity_perc:.3f};{torch_run:.4f}")
for flag in methods:
print(f"Running {repr(flag)} on {layer_id}.{p}")
y_csr, spqr_runs = spqr_mul_timer(spqr_module_device, x_fp16_device, flag, NUM_RUNS)
spqr_runs = spqr_runs[WARMUP:]
this_algorithm = spqr_runs.min()
torch.cuda.empty_cache()
time.sleep(1)
y_ptcsr, spqr_runs_modified_csr = spqr_mul_timer(
spqr_module_device_modified_csr, x_fp16_device, flag, NUM_RUNS
)
# assert torch.allclose(y_csr, y_ptcsr)
spqr_runs_modified_csr = spqr_runs_modified_csr[WARMUP:]
speed_up = torch_run / this_algorithm
print(
f"\t{repr(flag)} running {this_algorithm} ms {speed_up:.2f}X speed-up vs torch {torch_run} ms"
)
if run_ptcsr:
this_algorithm_modified_csr = spqr_runs_modified_csr.min()
speed_up_modified_csr = torch_run / this_algorithm_modified_csr
print(
f"\t{repr(flag)} modified csr running {this_algorithm_modified_csr} ms {speed_up_modified_csr:.2f}X speed-up vs torch {torch_run} ms"
)
if run_ptcsr:
f.write(f";{this_algorithm:.4f};{this_algorithm_modified_csr:.4f}")
baseline_speed_up = max(speed_up, speed_up_modified_csr)
else:
baseline_speed_up = speed_up
f.write(f";{this_algorithm:.4f}")
if run_ptcsr:
benchmark_results_ms.append(min(this_algorithm, this_algorithm_modified_csr))
else:
benchmark_results_ms.append(this_algorithm)
benchmark_speed_up.append(baseline_speed_up)
f.write("\n")
f.flush()
print("\n\n")
print(f"Total benchmark geomean = {gmean(benchmark_results_ms)}")
print(f"Total benchmark speed-up geomean = {gmean(benchmark_speed_up)}")
print(f"Total benchmark mean = {np.array(benchmark_results_ms).mean()}")
print(f"Total benchmark speed-up mean= {np.array(benchmark_speed_up).mean()}")
print("\n\n")