-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelementwise.py
More file actions
76 lines (59 loc) · 2.1 KB
/
Copy pathelementwise.py
File metadata and controls
76 lines (59 loc) · 2.1 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
"""Elementwise operations with fused math.
Multiple math operations compile into a single fused GPU kernel
with no intermediate buffers — each thread loads once, computes
everything, and stores once.
"""
import alloy as al
import numpy as np
@al.kernel
def gelu(x_ptr, out_ptr: al.output, N: al.constexpr) -> None:
pid = al.program_id(0)
offs = pid * 1024 + al.arange(0, 1024)
mask = offs < N
x = al.load(x_ptr + offs, mask=mask)
result = x * 0.5 * (1.0 + al.tanh(0.7978845608 * (x + 0.044715 * x * x * x)))
al.store(out_ptr + offs, result, mask=mask)
@al.kernel
def sigmoid(x_ptr, out_ptr: al.output, N: al.constexpr) -> None:
pid = al.program_id(0)
offs = pid * 1024 + al.arange(0, 1024)
mask = offs < N
x = al.load(x_ptr + offs, mask=mask)
al.store(out_ptr + offs, 1.0 / (1.0 + al.exp(-x)), mask=mask)
@al.kernel
def silu(x_ptr, out_ptr: al.output, N: al.constexpr) -> None:
pid = al.program_id(0)
offs = pid * 1024 + al.arange(0, 1024)
mask = offs < N
x = al.load(x_ptr + offs, mask=mask)
al.store(out_ptr + offs, x * al.sigmoid(x), mask=mask)
def main() -> None:
np.random.seed(42)
N = 4096
x = np.random.randn(N).astype(np.float32)
out = np.zeros(N, dtype=np.float32)
grid = (N + 1023) // 1024
print("=== GELU kernel MSL ===")
al.inspect(gelu, N=N)
print()
r = gelu[grid](x, out, N=N)
x64 = x.astype(np.float64)
expected = (x64 * 0.5 * (1.0 + np.tanh(0.7978845608 * (x64 + 0.044715 * x64**3)))).astype(
np.float32
)
err = np.max(np.abs(np.array(r) - expected))
print(f"GELU N={N}: error={err:.2e}")
assert err < 1e-5
out = np.zeros(N, dtype=np.float32)
r = sigmoid[grid](x, out, N=N)
err = np.max(np.abs(np.array(r) - 1.0 / (1.0 + np.exp(-x))))
print(f"Sigmoid N={N}: error={err:.2e}")
assert err < 1e-6
out = np.zeros(N, dtype=np.float32)
r = silu[grid](x, out, N=N)
err = np.max(np.abs(np.array(r) - x * (1.0 / (1.0 + np.exp(-x)))))
print(f"SiLU N={N}: error={err:.2e}")
assert err < 1e-6
print("All PASSED")
if __name__ == "__main__":
main()