|
| 1 | +import os |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch.distributed as dist |
| 5 | +import triton |
| 6 | +import triton.experimental.tle.language as tle |
| 7 | +import triton.language as tl |
| 8 | + |
| 9 | +LOCAL_WORLD_SIZE = int(os.environ["LOCAL_WORLD_SIZE"]) |
| 10 | +WORLD_SIZE = int(os.environ["WORLD_SIZE"]) |
| 11 | +if WORLD_SIZE % LOCAL_WORLD_SIZE != 0: |
| 12 | + raise ValueError("WORLD_SIZE must be divisible by LOCAL_WORLD_SIZE") |
| 13 | + |
| 14 | +DEVICE_MESH = tle.device_mesh(tle.MeshConfig(node=WORLD_SIZE // LOCAL_WORLD_SIZE, device=LOCAL_WORLD_SIZE)) |
| 15 | + |
| 16 | + |
| 17 | +@triton.jit |
| 18 | +def _tle_node_rank_kernel(out_ptr, device_dptr: tl.constexpr, mesh: tl.constexpr): |
| 19 | + pid = tl.program_id(0) |
| 20 | + node_rank = tle.shard_id(mesh, "node", device_dptr=device_dptr) |
| 21 | + tl.store(out_ptr + pid, node_rank) |
| 22 | + |
| 23 | + |
| 24 | +def test_tle_get_node_rank(): |
| 25 | + grid = 2 |
| 26 | + with torch.cuda.use_mem_pool(tle.get_mem_pool()): |
| 27 | + source = torch.empty((1, ), dtype=torch.float32, device="cuda") |
| 28 | + device_dptr = tle.create_dist_tensor(source) |
| 29 | + node_rank_out = torch.empty((grid, ), dtype=torch.int32, device="cuda") |
| 30 | + |
| 31 | + compiled = _tle_node_rank_kernel.warmup( |
| 32 | + out_ptr=node_rank_out, |
| 33 | + device_dptr=device_dptr, |
| 34 | + mesh=DEVICE_MESH, |
| 35 | + grid=(grid, ), |
| 36 | + num_ctas=1, |
| 37 | + num_warps=4, |
| 38 | + ) |
| 39 | + assert "get_world_rank" in compiled.asm["ttgir"] |
| 40 | + assert "get_num_pes" in compiled.asm["ttgir"] |
| 41 | + assert "flagcxDevCommGetRank" in compiled.asm["ptx"] |
| 42 | + assert "flagcxDevCommGetIntraSize" in compiled.asm["ptx"] |
| 43 | + |
| 44 | + _tle_node_rank_kernel[(grid, )]( |
| 45 | + out_ptr=node_rank_out, |
| 46 | + device_dptr=device_dptr, |
| 47 | + mesh=DEVICE_MESH, |
| 48 | + ) |
| 49 | + torch.cuda.synchronize() |
| 50 | + |
| 51 | + rank = dist.get_rank() |
| 52 | + expected_node_rank = rank // LOCAL_WORLD_SIZE |
| 53 | + actual_node_ranks = node_rank_out.cpu().tolist() |
| 54 | + try: |
| 55 | + torch.testing.assert_close( |
| 56 | + node_rank_out, |
| 57 | + torch.full_like(node_rank_out, expected_node_rank), |
| 58 | + ) |
| 59 | + except AssertionError: |
| 60 | + print( |
| 61 | + f"[Rank {rank}] FAILED: node ranks={actual_node_ranks}, " |
| 62 | + f"expected={expected_node_rank}", |
| 63 | + flush=True, |
| 64 | + ) |
| 65 | + raise |
| 66 | + else: |
| 67 | + print( |
| 68 | + f"[Rank {rank}] PASSED: node ranks={actual_node_ranks}, " |
| 69 | + f"expected={expected_node_rank}", |
| 70 | + flush=True, |
| 71 | + ) |
| 72 | + finally: |
| 73 | + tle.cleanup_communicator() |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + test_tle_get_node_rank() |
0 commit comments