2023-06-15 03:49:16 +00:00
|
|
|
import pytest
|
2023-06-09 01:41:27 +00:00
|
|
|
import torch
|
2023-06-15 03:49:16 +00:00
|
|
|
import torch.distributed as dist
|
2023-06-08 02:18:17 +00:00
|
|
|
|
2023-06-15 03:49:16 +00:00
|
|
|
import colossalai
|
2023-06-19 03:23:24 +00:00
|
|
|
from colossalai.device.device_mesh import DeviceMesh
|
2023-06-15 03:49:16 +00:00
|
|
|
from colossalai.testing import rerun_if_address_is_in_use, spawn
|
2023-06-19 03:23:24 +00:00
|
|
|
|
2022-08-02 11:23:48 +00:00
|
|
|
|
|
|
|
def test_device_mesh():
|
2023-06-26 07:50:07 +00:00
|
|
|
physical_mesh_id = torch.arange(0, 16)
|
2022-08-02 11:23:48 +00:00
|
|
|
mesh_shape = (4, 4)
|
|
|
|
# [[0, 1, 2, 3],
|
|
|
|
# [4, 5, 6, 7],
|
|
|
|
# [8, 9, 10,11],
|
|
|
|
# [12,13,14,15]]
|
|
|
|
device_mesh = DeviceMesh(physical_mesh_id, mesh_shape)
|
2023-06-26 07:50:07 +00:00
|
|
|
assert device_mesh.global_rank_to_local_rank(5) == [1, 1]
|
|
|
|
assert device_mesh.global_rank_to_local_rank(11) == [2, 3]
|
|
|
|
assert device_mesh.get_ranks_in_process_group(axis=1, global_rank=2) == [0, 1, 2, 3]
|
2022-08-02 11:23:48 +00:00
|
|
|
|
|
|
|
|
2023-06-15 03:49:16 +00:00
|
|
|
def check_1d_device_mesh():
|
|
|
|
# check for 1D device mesh
|
|
|
|
process_group = dist.GroupMember.WORLD
|
|
|
|
device_mesh = DeviceMesh.from_process_group(process_group)
|
|
|
|
|
|
|
|
# checks
|
|
|
|
assert device_mesh.shape == [4]
|
|
|
|
assert len(device_mesh.get_process_group_for_all_axes().keys()) == 1, 'Expected 1 axis for the process group dict'
|
|
|
|
assert device_mesh.get_process_group(axis=0) == process_group, 'Expected world process group'
|
|
|
|
assert device_mesh.is_initialized
|
|
|
|
assert device_mesh.num_devices == 4
|
|
|
|
assert device_mesh.is_initialized
|
|
|
|
assert device_mesh.logical_mesh_id is None
|
|
|
|
assert device_mesh._is_init_from_process_group
|
|
|
|
|
|
|
|
|
|
|
|
def check_2d_device_mesh():
|
|
|
|
# create process group for 2D device mesh
|
|
|
|
first_row_ranks = [0, 1]
|
|
|
|
second_row_ranks = [2, 3]
|
|
|
|
first_col_ranks = [0, 2]
|
|
|
|
second_col_ranks = [1, 3]
|
|
|
|
|
|
|
|
first_row_pg = dist.new_group(first_row_ranks, backend='nccl')
|
|
|
|
second_row_pg = dist.new_group(second_row_ranks, backend='nccl')
|
|
|
|
first_col_pg = dist.new_group(first_col_ranks, backend='nccl')
|
|
|
|
second_col_pg = dist.new_group(second_col_ranks, backend='nccl')
|
|
|
|
|
|
|
|
# check for
|
|
|
|
current_rank = dist.get_rank()
|
|
|
|
|
|
|
|
if current_rank in first_row_ranks:
|
|
|
|
row_pg = first_row_pg
|
|
|
|
else:
|
|
|
|
row_pg = second_row_pg
|
|
|
|
|
|
|
|
if current_rank in first_col_ranks:
|
|
|
|
col_pg = first_col_pg
|
|
|
|
else:
|
|
|
|
col_pg = second_col_pg
|
|
|
|
|
|
|
|
device_mesh = DeviceMesh.from_process_group([col_pg, row_pg])
|
|
|
|
|
|
|
|
# checks
|
|
|
|
assert device_mesh.shape == [2, 2]
|
|
|
|
assert len(device_mesh.get_process_group_for_all_axes().keys()) == 2, 'Expected 2 axes for the process group dict'
|
|
|
|
assert device_mesh.get_process_group(axis=0) == col_pg, 'Expected column process group'
|
|
|
|
assert device_mesh.get_process_group(axis=1) == row_pg, 'Expected row process group'
|
|
|
|
assert device_mesh.num_devices == 4
|
|
|
|
assert device_mesh.is_initialized
|
|
|
|
assert device_mesh.logical_mesh_id is None
|
|
|
|
assert device_mesh._is_init_from_process_group
|
|
|
|
|
|
|
|
|
|
|
|
def check_init_from_process_group(rank, world_size, port):
|
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
def test_device_mesh_from_process_group():
|
|
|
|
spawn(check_init_from_process_group, 4)
|
|
|
|
|
|
|
|
|
2022-08-02 11:23:48 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
test_device_mesh()
|
2023-07-04 08:07:47 +00:00
|
|
|
test_device_mesh_from_process_group()
|