-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathgemm_preop_exp_benchmark.py
More file actions
297 lines (264 loc) · 13 KB
/
Copy pathgemm_preop_exp_benchmark.py
File metadata and controls
297 lines (264 loc) · 13 KB
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
"""
Gemm + PreOp (exp) benchmark
============================
This benchmark is modified from gemm_benchmark.py to include an exponential
operation on the input matrix A.
"""
import torch
import triton
import triton.language as tl
import triton_kernels_benchmark as benchmark_suite
@triton.autotune(
configs=[
triton.Config(
{'BLOCK_SIZE_M': 256, 'BLOCK_SIZE_N': 256, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 4, 'grf_mode': '256'},
num_stages=2, num_warps=32),
triton.Config(
{'BLOCK_SIZE_M': 256, 'BLOCK_SIZE_N': 256, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 4, 'grf_mode': '256'},
num_stages=3, num_warps=32),
triton.Config(
{'BLOCK_SIZE_M': 256, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 4, 'grf_mode': '256'},
num_stages=2, num_warps=32),
triton.Config(
{'BLOCK_SIZE_M': 64, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 4, 'grf_mode': '256'},
num_stages=2, num_warps=32),
triton.Config(
{'BLOCK_SIZE_M': 8, 'BLOCK_SIZE_N': 512, 'BLOCK_SIZE_K': 64, 'GROUP_SIZE_M': 1, 'grf_mode': '256'},
num_stages=2, num_warps=32),
],
key=['M', 'N', 'K'],
restore_value=['c_ptr'],
)
@triton.jit
def matmul_kernel_with_tensor_descriptors(
# Pointers to matrices
a_ptr, b_ptr, c_ptr,
# Matrix dimensions
M: tl.constexpr, N: tl.constexpr, K: tl.constexpr,
# Stride variables
stride_am: tl.constexpr, stride_ak: tl.constexpr, #
stride_bk: tl.constexpr, stride_bn: tl.constexpr, #
stride_cm: tl.constexpr, stride_cn: tl.constexpr,
# Meta-parameters
BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr, GROUP_SIZE_M: tl.constexpr):
pid = tl.program_id(axis=0)
num_pid_m = tl.cdiv(M, BLOCK_SIZE_M)
num_pid_n = tl.cdiv(N, BLOCK_SIZE_N)
num_pid_in_group = GROUP_SIZE_M * num_pid_n
group_id = pid // num_pid_in_group
first_pid_m = group_id * GROUP_SIZE_M
group_size_m = min(num_pid_m - first_pid_m, GROUP_SIZE_M)
pid_m = first_pid_m + ((pid % num_pid_in_group) % group_size_m)
pid_n = (pid % num_pid_in_group) // group_size_m
a_desc = tl.make_tensor_descriptor(base=a_ptr, shape=(M, K), strides=(stride_am, stride_ak),
block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_K))
b_desc = tl.make_tensor_descriptor(base=b_ptr, shape=(K, N), strides=(stride_bk, stride_bn),
block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_N))
accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
off_k = 0
for _ in range(0, K, BLOCK_SIZE_K):
a = a_desc.load([pid_m * BLOCK_SIZE_M, off_k])
a = a.to(tl.float32)
a = tl.math.exp(a)
a = a.to(tl.bfloat16)
b = b_desc.load([off_k, pid_n * BLOCK_SIZE_N])
accumulator = tl.dot(a, b, accumulator)
off_k += BLOCK_SIZE_K
c = accumulator.to(tl.float32)
c_desc = tl.make_tensor_descriptor(base=c_ptr, shape=(M, N), strides=(stride_cm, stride_cn),
block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_N))
c_desc.store([pid_m * BLOCK_SIZE_M, pid_n * BLOCK_SIZE_N], c)
# pylint: disable=unused-argument
@triton.autotune(
configs=[
triton.Config(
{'BLOCK_SIZE_M': 256, 'BLOCK_SIZE_N': 256, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 4, 'grf_mode': '256'},
num_stages=2, num_warps=32),
triton.Config(
{'BLOCK_SIZE_M': 256, 'BLOCK_SIZE_N': 256, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 4, 'grf_mode': '256'},
num_stages=3, num_warps=32),
triton.Config(
{'BLOCK_SIZE_M': 256, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 4, 'grf_mode': '256'},
num_stages=2, num_warps=32),
triton.Config(
{'BLOCK_SIZE_M': 64, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 4, 'grf_mode': '256'},
num_stages=2, num_warps=32),
triton.Config(
{'BLOCK_SIZE_M': 8, 'BLOCK_SIZE_N': 512, 'BLOCK_SIZE_K': 64, 'GROUP_SIZE_M': 1, 'grf_mode': '256'},
num_stages=2, num_warps=32),
triton.Config(
{'BLOCK_SIZE_M': 8, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 64, 'GROUP_SIZE_M': 1, 'grf_mode': '256'},
num_stages=2, num_warps=4),
],
key=['M', 'N', 'K'],
restore_value=['c_ptr'],
)
@triton.jit
def matmul_kernel_with_tensor_descriptors_batched(
# Pointers to matrices
a_ptr, b_ptr, c_ptr,
# Matrix dimensions
B: tl.constexpr, M: tl.constexpr, N: tl.constexpr, K: tl.constexpr,
# Stride variables
stride_az: tl.constexpr, stride_am: tl.constexpr, stride_ak: tl.constexpr, #
stride_bz: tl.constexpr, stride_bk: tl.constexpr, stride_bn: tl.constexpr, #
stride_cz: tl.constexpr, stride_cm: tl.constexpr, stride_cn: tl.constexpr,
# Meta-parameters
BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr, GROUP_SIZE_M: tl.constexpr):
bid = tl.program_id(axis=1)
pid = tl.program_id(axis=0)
num_pid_m = tl.cdiv(M, BLOCK_SIZE_M)
num_pid_n = tl.cdiv(N, BLOCK_SIZE_N)
num_pid_in_group = GROUP_SIZE_M * num_pid_n
group_id = pid // num_pid_in_group
first_pid_m = group_id * GROUP_SIZE_M
group_size_m = min(num_pid_m - first_pid_m, GROUP_SIZE_M)
pid_m = first_pid_m + ((pid % num_pid_in_group) % group_size_m)
pid_n = (pid % num_pid_in_group) // group_size_m
offset_a = bid.to(tl.int64) * stride_az
offset_b = bid.to(tl.int64) * stride_bz
a_desc = tl.make_tensor_descriptor(base=a_ptr + offset_a, shape=(M, K), strides=(stride_am, stride_ak),
block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_K))
b_desc = tl.make_tensor_descriptor(base=b_ptr + offset_b, shape=(K, N), strides=(stride_bk, stride_bn),
block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_N))
accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
off_k = 0
for _ in range(0, K, BLOCK_SIZE_K):
a = a_desc.load([pid_m * BLOCK_SIZE_M, off_k])
a = a.to(tl.float32)
a = tl.math.exp(a)
a = a.to(tl.bfloat16)
b = b_desc.load([off_k, pid_n * BLOCK_SIZE_N])
accumulator = tl.dot(a, b, accumulator)
off_k += BLOCK_SIZE_K
c = accumulator.to(tl.float32)
offset_c = bid.to(tl.int64) * stride_cz
c_desc = tl.make_tensor_descriptor(base=c_ptr + offset_c, shape=(M, N), strides=(stride_cm, stride_cn),
block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_N))
c_desc.store([pid_m * BLOCK_SIZE_M, pid_n * BLOCK_SIZE_N], c)
# We can now create a convenience wrapper function that only takes two input tensors,
# and (1) checks any shape constraint; (2) launches the above kernel.
def matmul(a, b, c):
# Check constraints.
if len(a.shape) == 3 and len(b.shape) == 3:
assert a.shape[0] == b.shape[0], 'Incompatible Batch dimension'
assert a.shape[2] == b.shape[1], 'Incompatible dimensions'
assert a.is_contiguous(), 'Matrix A must be contiguous'
assert b.is_contiguous(), 'Matrix B must be contiguous'
B, M, K = a.shape
B, K, N = b.shape
# 1D launch kernel where each block gets its own program.
grid = lambda META: (
triton.cdiv(M, META['BLOCK_SIZE_M']) * triton.cdiv(N, META['BLOCK_SIZE_N']),
B,
)
matmul_kernel_with_tensor_descriptors_batched[grid](
a, b, c, #
B, M, N, K, #
a.stride(0), a.stride(1), a.stride(2), #
b.stride(0), b.stride(1), b.stride(2), #
c.stride(0), c.stride(1), c.stride(2))
elif len(a.shape) == 2 and len(b.shape) == 2:
assert a.shape[1] == b.shape[0], 'Incompatible dimensions'
assert a.is_contiguous(), 'Matrix A must be contiguous'
assert b.is_contiguous(), 'Matrix B must be contiguous'
M, K = a.shape
K, N = b.shape
grid = lambda META: (triton.cdiv(M, META['BLOCK_SIZE_M']) * triton.cdiv(N, META['BLOCK_SIZE_N']), )
matmul_kernel_with_tensor_descriptors[grid](
a, b, c, #
M, N, K, #
a.stride(0), a.stride(1), #
b.stride(0), b.stride(1), #
c.stride(0), c.stride(1))
else:
assert False, 'Input matrixs dimensions mismatch'
return c
X_VALS = [[1, 1024 * i, 1024 * i, 1024 * i]
for i in [1, 2, 4, 8]] + [[1, 1, 5120, 13824], #
[1, 4, 4096, 12288], #
[1, 512, 8192, 8192], #
[1, 512, 8192, 32768], #
[1, 512, 32768, 8192], #
[1, 1024, 16384, 8192], #
[1, 1024, 28672, 8192], #
[1, 3072, 4096, 3072], # FIXME: Remove this case when gemm_streamk_benchmark works
[1, 4096, 16384, 8192], #
[1, 8192, 16384, 1024], #
[1, 8192, 16384, 4096], #
[1, 16384, 1024, 8192], #
[1, 16384, 4096, 8192], #
[1, 16384, 8192, 1024], #
[1, 16384, 8192, 4096], #
[4, 32768, 128, 4096], #
[4, 32768, 4096, 128], #
[32, 4096, 4096, 128], #
[4096, 8, 128, 16384], #
[4096, 8, 16384, 128]]
DEVICE_NAME = torch.xpu.get_device_name()
DEVICE_TOTAL_MEMORY = torch.xpu.get_device_properties().total_memory
def is_enough_memory(x_val):
# x_val: (B, M, K, N)
B, M, K, N = x_val
# a: (B, M, K) bfloat16
# b: (B, K, N) bfloat16
# c: (B, M, N) float32
# pytorch reference: (B, M, N) float32
required_memory = B * M * K * 2 + B * K * N * 2 + 2 * B * M * N * 4
enough_memory = required_memory < DEVICE_TOTAL_MEMORY
if not enough_memory:
print(f"'{x_val}' combination skipped for '{DEVICE_NAME}'; {required_memory=} but {DEVICE_TOTAL_MEMORY=}")
return enough_memory
X_VALS = [x_val for x_val in X_VALS if is_enough_memory(x_val)]
# Benchmark Performance
@benchmark_suite.perf_report(
benchmark_suite.Benchmark(
# argument names to use as an x-axis for the plot
x_names=['B', 'M', 'K', 'N'],
# different possible values for `x_name`
x_vals=X_VALS,
line_arg='provider',
# argument name whose value corresponds to a different line in the plot
# possible values for `line_arg``
line_vals=['triton'],
# label name for the lines
line_names=['Triton'],
# line styles
styles=[('green', '-'), ('green', '--'), ('blue', '-'), ('blue', '--')],
ylabel=['GB/s', 'TFlops'], # label name for the y-axis
plot_name='matmul-performance-preop-exp',
# name for the plot. Used also as a file name for saving the plot.
args={},
))
def benchmark(B, M, N, K, provider):
# Some configs increase performance with warmup as a step function, but some slowly decrease with saturation.
# Performance is best at 200-400ms range, but we want stable, not just best.
# This warmup improves performance on BMG
do_bench = benchmark_suite.get_do_bench(n_warmup=800, n_repeat=10, quantiles=[0.5, 0.0, 1.0])
if B == 1:
a = torch.rand((M, K), device='xpu', dtype=torch.bfloat16)
b = torch.rand((K, N), device='xpu', dtype=torch.bfloat16)
else:
a = torch.rand((B, M, K), device='xpu', dtype=torch.bfloat16)
b = torch.rand((B, K, N), device='xpu', dtype=torch.bfloat16)
if provider == 'triton':
assert len(a.shape) == len(b.shape), 'Incompatible sizes'
if len(a.shape) == 3:
c = torch.empty((B, M, N), device='xpu', dtype=torch.float32)
else:
assert len(a.shape) == 2, 'Expecting shape of length 2'
c = torch.empty((M, N), device='xpu', dtype=torch.float32)
triton_fn = lambda: matmul(a, b, c)
torch_fn = lambda: torch.matmul(torch.exp(a), b).to(torch.float32)
rtol = 1e-2 if a.dtype == torch.bfloat16 else 1e-3
benchmark_suite.assert_close(triton_fn, torch_fn, atol=1e-4, rtol=rtol, err_msg='triton to torch')
_, min_ms, max_ms, mean_ms, cv = do_bench(triton_fn)
else:
raise NotImplementedError(f'Unsupported provider {provider}')
tflops = lambda ms: 2 * B * M * N * K * (1e-12) / (ms * 1e-3)
gbps = lambda ms: B * (2 * (M * K + K * N) + 4.0 * (M * N)) * (1e-9) / (ms * 1e-3)
return (gbps(mean_ms), gbps(max_ms), gbps(min_ms)), (tflops(mean_ms), tflops(max_ms), tflops(min_ms)), cv
def get_benchmark(providers_filter=None):
return benchmark
if __name__ == '__main__':
benchmark.run(show_plots=False, print_data=True)