forked from DefTruth/CUDA-Learn-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftmax.py
201 lines (184 loc) · 8.22 KB
/
softmax.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
import torch
import time
from torch.utils.cpp_extension import load
from functools import partial
from typing import Optional
torch.set_grad_enabled(False)
# Load the CUDA kernel as a python module
lib = load(name='softmax_lib',
sources=['softmax.cu'],
extra_cuda_cflags=[
"-O3",
"-U__CUDA_NO_HALF_OPERATORS__",
"-U__CUDA_NO_HALF_CONVERSIONS__",
"-U__CUDA_NO_HALF2_OPERATORS__",
"-U__CUDA_NO_BFLOAT16_CONVERSIONS__",
"--expt-relaxed-constexpr",
"--expt-extended-lambda",
"--use_fast_math"
],
extra_cflags=['-std=c++17'])
def run_benchmark(perf_func: callable, x: torch.Tensor,
tag: str, out: Optional[torch.Tensor] = None,
warmup: int = 10, iters: int = 1000,
show_all: bool = False):
if out is not None:
out.fill_(0)
if out is not None:
for i in range(warmup):
perf_func(x, out)
else:
for i in range(warmup):
_ = perf_func(x)
torch.cuda.synchronize()
start = time.time()
# iters
if out is not None:
for i in range(iters):
perf_func(x, out)
else:
for i in range(iters):
out = perf_func(x)
torch.cuda.synchronize()
end = time.time()
total_time = (end - start) * 1000 # ms
mean_time = total_time / iters
out_info = f"out_{tag}"
out_val = out.flatten().detach().cpu().numpy().tolist()[:3]
out_val = [round(v, 8) for v in out_val]
out_val = [f"{v:<12}" for v in out_val]
print(f"{out_info:>24}: {out_val}, time:{mean_time:.8f}ms")
if show_all: print(out)
return out, mean_time
# grid memory fence
print("-" * 100)
N = 128 * 128
print(" " * 45 + f"N={N}")
print("-" * 100)
x = torch.randn((N)).cuda().float()
out = torch.zeros_like(x).cuda().float().contiguous()
run_benchmark(lib.softmax_f32, x, "f32(fence)", out)
run_benchmark(lib.softmax_f32x4, x, "f32x4(fence)", out)
run_benchmark(partial(torch.softmax, dim=0, out=out), x, "f32_th")
# per token softmax
print("-" * 100)
S, H = 4096, 256
print(" " * 45 + f"S={S}, H={H}")
print("-" * 100)
x = torch.randn((S, H)).cuda().float().contiguous()
out = torch.zeros_like(x).cuda().float().contiguous()
run_benchmark(lib.softmax_f32_per_token, x, "f32(per)", out)
run_benchmark(lib.softmax_f32x4_per_token, x, "f32x4(per)", out)
run_benchmark(lib.safe_softmax_f32_per_token, x, "f32(safe)", out)
run_benchmark(lib.online_safe_softmax_f32_per_token, x, "f32(safe+online)", out)
run_benchmark(lib.online_safe_softmax_f32x4_pack_per_token, x, "f32x4(safe+online)", out)
run_benchmark(lib.safe_softmax_f32x4_per_token, x, "f32x4(safe)", out)
run_benchmark(partial(torch.softmax, dim=1, out=out), x, "f32_th(per)")
print("-" * 100)
x_f16 = x.half().contiguous()
out_f16 = out.half().contiguous()
run_benchmark(lib.safe_softmax_f16_f32_per_token, x_f16, "f16f32(safe)", out_f16)
run_benchmark(lib.safe_softmax_f16x2_f32_per_token, x_f16, "f16x2f32(safe)", out_f16)
run_benchmark(lib.safe_softmax_f16x8_pack_f32_per_token, x_f16, "f16x8packf32(safe)", out_f16)
run_benchmark(partial(torch.softmax, dim=1, out=out_f16), x_f16, "f16_th(per)")
print("-" * 100)
# per token softmax
print("-" * 100)
S, H = 4096, 512
print(" " * 45 + f"S={S}, H={H}")
print("-" * 100)
x = torch.randn((S, H)).cuda().float().contiguous()
out = torch.zeros_like(x).cuda().float().contiguous()
run_benchmark(lib.softmax_f32_per_token, x, "f32(per)", out)
run_benchmark(lib.softmax_f32x4_per_token, x, "f32x4(per)", out)
run_benchmark(lib.safe_softmax_f32_per_token, x, "f32(safe)", out)
run_benchmark(lib.online_safe_softmax_f32_per_token, x, "f32(safe+online)", out)
run_benchmark(lib.online_safe_softmax_f32x4_pack_per_token, x, "f32x4(safe+online)", out)
run_benchmark(lib.safe_softmax_f32x4_per_token, x, "f32x4(safe)", out)
run_benchmark(partial(torch.softmax, dim=1, out=out), x, "f32_th(per)")
print("-" * 100)
x_f16 = x.half().contiguous()
out_f16 = out.half().contiguous()
run_benchmark(lib.safe_softmax_f16_f32_per_token, x_f16, "f16f32(safe)", out_f16)
run_benchmark(lib.safe_softmax_f16x2_f32_per_token, x_f16, "f16x2f32(safe)", out_f16)
run_benchmark(lib.safe_softmax_f16x8_pack_f32_per_token, x_f16, "f16x8packf32(safe)", out_f16)
run_benchmark(partial(torch.softmax, dim=1, out=out_f16), x_f16, "f16_th(per)")
print("-" * 100)
# per token softmax
print("-" * 100)
S, H = 4096, 1024
print(" " * 45 + f"S={S}, H={H}")
print("-" * 100)
x = torch.randn((S, H)).cuda().float().contiguous()
out = torch.zeros_like(x).cuda().float().contiguous()
run_benchmark(lib.softmax_f32_per_token, x, "f32(per)", out)
run_benchmark(lib.softmax_f32x4_per_token, x, "f32x4(per)", out)
run_benchmark(lib.safe_softmax_f32_per_token, x, "f32(safe)", out)
run_benchmark(lib.online_safe_softmax_f32_per_token, x, "f32(safe+online)", out)
run_benchmark(lib.online_safe_softmax_f32x4_pack_per_token, x, "f32x4(safe+online)", out)
run_benchmark(lib.safe_softmax_f32x4_per_token, x, "f32x4(safe)", out)
run_benchmark(partial(torch.softmax, dim=1, out=out), x, "f32_th(per)")
print("-" * 100)
x_f16 = x.half().contiguous()
out_f16 = out.half().contiguous()
run_benchmark(lib.safe_softmax_f16_f32_per_token, x_f16, "f16f32(safe)", out_f16)
run_benchmark(lib.safe_softmax_f16x2_f32_per_token, x_f16, "f16x2f32(safe)", out_f16)
run_benchmark(lib.safe_softmax_f16x8_pack_f32_per_token, x_f16, "f16x8packf32(safe)", out_f16)
run_benchmark(partial(torch.softmax, dim=1, out=out_f16), x_f16, "f16_th(per)")
print("-" * 100)
# per token softmax
print("-" * 100)
S, H = 4096, 2048
print(" " * 45 + f"S={S}, H={H}")
print("-" * 100)
x = torch.randn((S, H)).cuda().float().contiguous()
out = torch.zeros_like(x).cuda().float().contiguous()
run_benchmark(lib.softmax_f32x4_per_token, x, "f32x4(per)", out)
run_benchmark(lib.safe_softmax_f32x4_per_token, x, "f32x4(safe)", out)
run_benchmark(partial(torch.softmax, dim=1, out=out), x, "f32_th(per)")
print("-" * 100)
x_f16 = x.half().contiguous()
out_f16 = out.half().contiguous()
run_benchmark(lib.safe_softmax_f16x2_f32_per_token, x_f16, "f16x2f32(safe)", out_f16)
run_benchmark(lib.safe_softmax_f16x8_pack_f32_per_token, x_f16, "f16x8packf32(safe)", out_f16)
run_benchmark(partial(torch.softmax, dim=1, out=out_f16), x_f16, "f16_th(per)")
print("-" * 100)
# per token softmax
print("-" * 100)
S, H = 4096, 4096
print(" " * 45 + f"S={S}, H={H}")
print("-" * 100)
x = torch.randn((S, H)).cuda().float().contiguous()
out = torch.zeros_like(x).cuda().float().contiguous()
run_benchmark(lib.softmax_f32x4_per_token, x, "f32x4(per)", out)
run_benchmark(lib.safe_softmax_f32x4_per_token, x, "f32x4(safe)", out)
run_benchmark(partial(torch.softmax, dim=1, out=out), x, "f32_th(per)")
print("-" * 100)
x_f16 = x.half().contiguous()
out_f16 = out.half().contiguous()
run_benchmark(lib.safe_softmax_f16x8_pack_f32_per_token, x_f16, "f16x8packf32(safe)", out_f16)
run_benchmark(partial(torch.softmax, dim=1, out=out_f16), x_f16, "f16_th(per)")
print("-" * 100)
# per token softmax
print("-" * 100)
S, H = 4096, 8192
print(" " * 45 + f"S={S}, H={H}")
print("-" * 100)
x = torch.randn((S, H)).cuda().float().contiguous()
out = torch.zeros_like(x).cuda().float().contiguous()
x_f16 = x.half().contiguous()
out_f16 = out.half().contiguous()
run_benchmark(lib.safe_softmax_f16x8_pack_f32_per_token, x_f16, "f16x8packf32(safe)", out_f16)
run_benchmark(partial(torch.softmax, dim=1, out=out_f16), x_f16, "f16_th(per)")
# per token softmax
print("-" * 100)
S, H = 8192, 8192
print(" " * 45 + f"S={S}, H={H}")
print("-" * 100)
x = torch.randn((S, H)).cuda().float().contiguous()
out = torch.zeros_like(x).cuda().float().contiguous()
x_f16 = x.half().contiguous()
out_f16 = out.half().contiguous()
run_benchmark(lib.safe_softmax_f16x8_pack_f32_per_token, x_f16, "f16x8packf32(safe)", out_f16)
run_benchmark(partial(torch.softmax, dim=1, out=out_f16), x_f16, "f16_th(per)")
print("-" * 100)