-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathtest_mm.py
More file actions
159 lines (138 loc) · 4.24 KB
/
Copy pathtest_mm.py
File metadata and controls
159 lines (138 loc) · 4.24 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
import torch
import triton
import pytest
import triton.language as tl
import benchmark
@triton.jit
def prev_multiple_of(a, b):
# the largest x<a that x%b ==0
return tl.cdiv(a, b) * b - b
@triton.jit
def mm_kernel(
A,
B,
C,
M,
N,
K,
stride_am,
stride_ak,
stride_bk,
stride_bn,
stride_cm,
stride_cn,
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr,
BLOCK_K: tl.constexpr,
GROUP_M: tl.constexpr,
):
# matrix multiplication
pid = tl.program_id(0)
grid_m = tl.cdiv(M, BLOCK_M)
grid_n = tl.cdiv(N, BLOCK_N)
# re-order program ID for better L2 performance
width = GROUP_M * grid_n
group_id = pid // width
group_size = min(grid_m - group_id * GROUP_M, GROUP_M)
pid_m = group_id * GROUP_M + (pid % group_size)
pid_n = (pid % width) // (group_size)
# do matrix multiplication
rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)
rn = pid_n * BLOCK_N + tl.arange(0, BLOCK_N)
ram = tl.max_contiguous(tl.multiple_of(rm % M, BLOCK_M), BLOCK_M)
rbn = tl.max_contiguous(tl.multiple_of(rn % N, BLOCK_N), BLOCK_N)
prev_multiple = prev_multiple_of(K, BLOCK_K)
acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32)
for start_k in range(0, prev_multiple, BLOCK_K):
rk = start_k + tl.arange(0, BLOCK_K)
a = tl.load(A + (ram[:, None] * stride_am + rk[None, :] * stride_ak))
b = tl.load(B + (rk[:, None] * stride_bk + rbn[None, :] * stride_bn))
if a.dtype != b.dtype:
a = a.to(C.dtype.element_ty)
b = b.to(C.dtype.element_ty)
acc += tl.dot(a, b, out_dtype=tl.float32, allow_tf32=False)
# loop peeling
rk = prev_multiple + tl.arange(0, BLOCK_K)
mask_k = rk < K
a = tl.load(
A + (ram[:, None] * stride_am + rk[None, :] * stride_ak),
mask=mask_k[None, :],
)
b = tl.load(
B + (rk[:, None] * stride_bk + rbn[None, :] * stride_bn),
mask=mask_k[:, None],
)
if a.dtype != b.dtype:
a = a.to(C.dtype.element_ty)
b = b.to(C.dtype.element_ty)
acc += tl.dot(a, b, out_dtype=tl.float32, allow_tf32=False)
acc = acc.to(C.dtype.element_ty)
# rematerialize rm and rn to save registers
rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)
rn = pid_n * BLOCK_N + tl.arange(0, BLOCK_N)
C = C + (rm[:, None] * stride_cm + rn[None, :] * stride_cn)
mask = (rm < M)[:, None] & (rn < N)[None, :]
# handles write-back with reduction-splitting
tl.store(C, acc, mask=mask)
_ordered_datatypes = [torch.float16, torch.bfloat16, torch.float32]
def get_higher_dtype(a, b):
if a is b:
return a
assert a in _ordered_datatypes
assert b in _ordered_datatypes
for d in _ordered_datatypes:
if a is d:
return b
if b is d:
return a
def mm(a, b):
device = a.device
# handle non-contiguous inputs if necessary
if a.stride(0) > 1 and a.stride(1) > 1:
a = a.contiguous()
if b.stride(0) > 1 and b.stride(1) > 1:
b = b.contiguous()
# checks constraints
assert a.shape[1] == b.shape[0], "incompatible dimensions"
M, K = a.shape
_, N = b.shape
# allocates output
c_dtype = get_higher_dtype(a.dtype, b.dtype)
c = torch.empty((M, N), device=device, dtype=c_dtype)
# launch kernel
grid = lambda META: (
triton.cdiv(M, META["BLOCK_M"]) * triton.cdiv(N, META["BLOCK_N"]),
)
mm_kernel[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),
32,
32,
32,
GROUP_M=8,
)
return c
@pytest.mark.interpreter
@pytest.mark.parametrize("M, N, K", [(1, 1, 32), (15, 160, 1024), (495, 5333, 71)])
@pytest.mark.parametrize("dtype", [torch.float32])
def test_accuracy_mm(M, N, K, dtype):
device = 'cpu'
a = torch.randn((M, K), dtype=dtype, device=device)
b = torch.randn((K, N), dtype=dtype, device=device)
ref_out = torch.mm(a, b)
res_out = mm(a, b)
torch.testing.assert_close(res_out, ref_out, atol=1e-2, rtol=0)
if __name__ == "__main__":
benchmark.select_cpu_backend()
M, N, K = (495, 5333, 71)
test_accuracy_mm(M, N, K, torch.float32)