-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_4k.py
212 lines (171 loc) · 6.15 KB
/
test_4k.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
import tempfile
import numpy as np
import tvm
from tvm import meta_schedule as ms
from tvm.script import tir as T
from tvm.tir import TensorIntrin, IntImm, Cast
from tvm import te, tir
from tvm.tir.tensor_intrin.cuda import (
WMMA_LOAD_16x16x16_F16_A_INTRIN,
WMMA_LOAD_16x16x16_F16_B_INTRIN,
WMMA_SYNC_16x16x16_f16f16f32_INTRIN,
WMMA_FILL_16x16x16_F32_INTRIN,
WMMA_STORE_16x16x16_F32_GLOBAL_INTRIN,
)
def get_schedule_fun(tune):
def schedule(sch):
block = sch.get_block("compute")
i, j, k = sch.get_loops(block)
i, i_inner = sch.split(i, factors=[None, 16])
j, j_inner = sch.split(j, factors=[None, 16])
k, k_inner = sch.split(k, factors=[None, 16])
sch.reorder(
i,
j,
k,
i_inner,
j_inner,
k_inner,
)
block_outer = sch.blockize(i_inner)
block_inner = block
if tune:
i_factors = sch.sample_perfect_tile(i, n=5)
j_factors = sch.sample_perfect_tile(j, n=5)
k_factors = sch.sample_perfect_tile(k, n=3)
num_ty = sch.get(i_factors[2]) * sch.get(j_factors[2])
else:
i_factors, j_factors, k_factors = (
[64, 1, 4, 1, 1],
[1, 64, 1, 2, 2],
[128, 2, 1],
)
num_ty = i_factors[2] * j_factors[2]
i0, i1, i2, i3, i4 = sch.split(i, factors=i_factors)
j0, j1, j2, j3, j4 = sch.split(j, factors=j_factors)
k0, k1, k2 = sch.split(k, k_factors)
sch.reorder(
i0,
j0,
i1,
j1,
j2,
i2,
k0,
k1,
i3,
j3,
k2,
i4,
j4,
)
block_idx = sch.fuse(i0, j0)
block_idy = sch.fuse(i1, j1)
thread_idy = sch.fuse(j2, i2)
sch.bind(block_idx, "blockIdx.x")
sch.bind(block_idy, "blockIdx.y")
sch.bind(thread_idy, "threadIdx.y")
def fetch_to_shared(block, idx, ndim):
block_read = sch.cache_read(block, idx, "shared")
sch.compute_at(block_read, k0)
vector_size = 4
warp_size = 32
fused = sch.fuse(*sch.get_loops(block_read)[-ndim:])
_, f_1, f_2, f_3 = sch.split(
fused, factors=[None, num_ty, warp_size, vector_size]
)
sch.bind(f_2, "threadIdx.x")
sch.bind(f_1, "threadIdx.y")
sch.vectorize(f_3)
sch.storage_align(block_read, 0, axis=-2, factor=32, offset=8)
return block_read
fetch_to_shared(block_outer, 0, 2)
fetch_to_shared(block_outer, 1, 2)
loop = sch.get_loops(block_outer)[-1]
A_mat = sch.cache_read(block_outer, 0, "wmma.matrix_a")
B_mat = sch.cache_read(block_outer, 1, "wmma.matrix_b")
sch.compute_at(A_mat, k1)
sch.compute_at(B_mat, k1)
store = sch.cache_write(block_outer, 0, "wmma.accumulator")
sch.reverse_compute_at(store, thread_idy)
ii, jj = sch.get_loops(store)[-2:]
io, ii = sch.split(ii, factors=[None, 16])
jo, ji = sch.split(jj, factors=[None, 16])
sch.reorder(io, jo, ii, ji)
loop = sch.get_loops(block_outer)[3]
block_init_c = sch.decompose_reduction(block_outer, loop)
block_init_c_inner = sch.get_child_blocks(block_init_c)[0]
i, j = sch.get_loops(A_mat)[-2:]
i0, i1 = sch.split(i, factors=[None, 16])
j0, j1 = sch.split(j, factors=[None, 16])
sch.reorder(i0, j0, i1, j1)
sch.unroll(i0)
sch.unroll(j0)
sch.tensorize(i1, WMMA_LOAD_16x16x16_F16_A_INTRIN)
i, j = sch.get_loops(B_mat)[-2:]
i0, i1 = sch.split(i, factors=[None, 16])
j0, j1 = sch.split(j, factors=[None, 16])
sch.reorder(i0, j0, i1, j1)
sch.unroll(i0)
sch.unroll(j0)
sch.tensorize(i1, WMMA_LOAD_16x16x16_F16_B_INTRIN)
sch.tensorize(sch.get_loops(block_init_c_inner)[-2], WMMA_FILL_16x16x16_F32_INTRIN)
sch.tensorize(sch.get_loops(store)[-2], WMMA_STORE_16x16x16_F32_GLOBAL_INTRIN)
sch.tensorize(sch.get_loops(block_inner)[-3], WMMA_SYNC_16x16x16_f16f16f32_INTRIN)
return schedule
def get_matmul(m, n, k, out_dtype="float32"):
X = te.placeholder((m, k), name="X", dtype="float16")
W = te.placeholder((k, n), name="W", dtype="float16")
ak = te.reduce_axis((0, k), name="k")
if out_dtype == "float32":
matmul = te.compute(
(m, n),
lambda i, j: te.sum(
X[i, ak].astype("float32") * W[ak, j].astype("float32"),
axis=ak,
),
name="compute",
)
else:
matmul = te.compute(
(m, n),
lambda i, j: te.sum(X[i, ak] * W[ak, j], axis=ak),
name="compute",
)
return te.create_prim_func([X, W, matmul])
out_dtype = "float32"
tune = False
M, N, K = 4096, 4096, 4096
target = "vulkan -from_device=0"
# target = "cuda"
workload = get_matmul(M, N, K, out_dtype)
if tune:
with tempfile.TemporaryDirectory() as work_dir:
db = ms.tune_tir(
mod=workload,
target=tvm.target.Target(target),
max_trials_global=128,
work_dir=work_dir,
space=ms.space_generator.ScheduleFn(get_schedule_fun(tune)),
)
sch = ms.tir_integration.compile_tir(db, workload, target)
print(sch.trace)
else:
sch = tir.Schedule(workload)
get_schedule_fun(tune)(sch)
# print(sch.mod)
f = tvm.build(sch.mod, target=target)
dev = tvm.device(target, 0)
A = tvm.nd.array(np.random.randn(M, K).astype("float16"), dev)
B = tvm.nd.array(np.random.randn(K, N).astype("float16"), dev)
C = tvm.nd.array(np.random.randn(M, N).astype(out_dtype), dev)
f(A, B, C)
evaluator = f.time_evaluator(f.entry_name, dev, number=100)
gflops = (N * M * K) * 2 / 1e9
time_ms = evaluator(A, B, C).mean * 1e3
print("%f GFLOPS" % (gflops / (time_ms / 1e3)))
out = C.numpy()
A_np = A.numpy()
B_np = B.numpy()
ref = np.dot(A_np.astype("float32"), B_np.astype("float32"))
print(np.max(np.abs(out - ref)), np.mean(np.abs(out - ref)))