-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
50 lines (36 loc) · 1.27 KB
/
Copy pathtest.py
File metadata and controls
50 lines (36 loc) · 1.27 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
import torch
import triton
import triton.language as tl
from vectorized_copy import copy, vectorized_copy
@triton.jit
def copy_kernel(x_ptr, out_ptr, n, BLOCK_SIZE: tl.constexpr):
pid = tl.program_id(axis=0)
block_start = pid * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n
x = tl.load(x_ptr + offsets, mask=mask)
out = x
tl.store(out_ptr + offsets, out, mask=mask)
def copy_triton(x: torch.Tensor, out: torch.Tensor):
assert x.device == out.device
n_elements = out.numel()
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),)
copy_kernel[grid](x, out, n_elements, BLOCK_SIZE=512)
if __name__ == "__main__":
numel = 2**16 + 123
x = torch.randn(numel, dtype=torch.float16, device="cuda")
out = torch.zeros_like(x)
copy(x, out)
print("input:\n", x)
print("output:\n", out)
assert torch.allclose(x, out, atol=0, rtol=0)
out = torch.zeros_like(x)
vectorized_copy(x, out)
print("output (vectorized):\n", out)
assert torch.allclose(x, out, atol=0, rtol=0)
out = torch.zeros_like(x)
torch.cuda.profiler.start()
copy_triton(x, out)
torch.cuda.profiler.stop()
print("output (triton):\n", out)
assert torch.allclose(x, out, atol=0, rtol=0)