forked from tile-ai/tilelang
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_remote_copy.py
More file actions
85 lines (65 loc) · 2.29 KB
/
Copy pathtest_remote_copy.py
File metadata and controls
85 lines (65 loc) · 2.29 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
"""Small distributed remote-copy correctness test."""
from __future__ import annotations
import os
import torch
import torch.distributed as dist
import tilelang
import tilelang.language as T
import tilelang.testing
from testing.python.distributed._utils import distributed_test
os.environ.setdefault("NCCL_DEBUG", "WARN")
_M = 1024
_BLOCK_M = 128
_THREADS = 128
def _kernel_remote_copy(M: int, block_M: int, threads: int):
@T.prim_func
def main(dst: T.Tensor((M,), "float32"), src: T.Tensor((M,), "float32")):
with T.Kernel(T.ceildiv(M, block_M), threads=threads) as bx:
rank = T.alloc_local((1,), "uint64")
rank[0] = T.get_rank()
T.put_block(
src=T.address_of(src[bx * block_M]),
dst=T.address_of(dst[bx * block_M]),
size=block_M,
dst_pe=rank[0] ^ 1,
)
return main
@tilelang.testing.requires_cuda_compute_version_ge(9, 0)
@distributed_test(nprocs=4)
def test_remote_copy(local_rank: int, num_ranks: int):
from tilelang.distributed.host import init_dist
rank, num_ranks, group = init_dist(local_rank, num_ranks)
allocator = tilelang.get_allocator(
size=2**20,
device="cuda",
is_distributed=True,
local_rank=local_rank,
num_local_ranks=num_ranks,
group=group,
)
kernel = tilelang.compile(
_kernel_remote_copy(_M, _BLOCK_M, _THREADS),
compile_once=True,
compile_group=group,
)
if rank == 0:
source = kernel.get_kernel_source()
assert "tl::get_remote_base_ptr" in source
assert "tl::get_uintptr_t" in source
kernel.initialize(allocator=allocator)
src = tilelang.tensor((_M,), torch.float32, allocator=allocator).normal_()
dst = tilelang.tensor((_M,), torch.float32, allocator=allocator).zero_()
torch.cuda.synchronize()
dist.barrier(group)
kernel(dst, src)
torch.cuda.synchronize()
dist.barrier(group)
src_refs = [torch.empty_like(src) for _ in range(num_ranks)]
dist.all_gather(src_refs, src, group)
expected = src_refs[rank ^ 1]
assert torch.allclose(expected, dst, atol=1e-6, rtol=1e-6)
allocator.close()
dist.destroy_process_group()
if __name__ == "__main__":
import tilelang.testing
tilelang.testing.main()