2022-03-25 05:02:39 +00:00
|
|
|
import pytest
|
|
|
|
import torch.distributed as dist
|
2023-04-06 06:51:35 +00:00
|
|
|
import torch.nn as nn
|
|
|
|
|
2022-03-25 05:02:39 +00:00
|
|
|
import colossalai
|
2024-01-09 02:20:05 +00:00
|
|
|
from colossalai.accelerator import get_accelerator
|
2023-11-02 02:21:24 +00:00
|
|
|
from colossalai.moe.experts import MLPExperts
|
|
|
|
from colossalai.moe.manager import MOE_MANAGER
|
|
|
|
from colossalai.moe.utils import sync_moe_model_param
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.testing import assert_equal_in_group, rerun_if_address_is_in_use, spawn
|
2022-03-25 05:02:39 +00:00
|
|
|
|
2023-11-02 02:21:24 +00:00
|
|
|
HIDDEN_SIZE = 4
|
|
|
|
INTERMEDIATE_SIZE = 8
|
|
|
|
|
|
|
|
|
|
|
|
def run_moe_init(expert_parallel):
|
|
|
|
MOE_MANAGER.__init__()
|
2023-11-08 15:07:03 +00:00
|
|
|
MOE_MANAGER.setup(parallel=expert_parallel)
|
2023-11-02 02:21:24 +00:00
|
|
|
expert_args = dict(
|
|
|
|
hidden_size=HIDDEN_SIZE,
|
|
|
|
intermediate_size=INTERMEDIATE_SIZE,
|
|
|
|
expert_parallel=expert_parallel,
|
|
|
|
)
|
|
|
|
exp0 = MLPExperts(1, **expert_args)
|
|
|
|
exp1 = MLPExperts(2, **expert_args)
|
|
|
|
exp2 = MLPExperts(4, **expert_args)
|
|
|
|
|
|
|
|
if expert_parallel == "EP":
|
|
|
|
assert exp0.num_local_experts == 1
|
|
|
|
assert exp1.num_local_experts == 1
|
|
|
|
assert exp2.num_local_experts == 2
|
|
|
|
else:
|
|
|
|
assert exp0.num_local_experts == 1
|
|
|
|
assert exp1.num_local_experts == 2
|
|
|
|
assert exp2.num_local_experts == 4
|
|
|
|
|
|
|
|
parallel_info_dict = MOE_MANAGER.parallel_info_dict
|
2022-03-25 05:02:39 +00:00
|
|
|
rank = dist.get_rank()
|
|
|
|
|
2023-11-02 02:21:24 +00:00
|
|
|
# group creation assert
|
|
|
|
assert len(parallel_info_dict) == 2
|
2022-03-25 05:02:39 +00:00
|
|
|
assert dist.get_rank(parallel_info_dict[2].ep_group) == rank % 2
|
|
|
|
assert dist.get_rank(parallel_info_dict[1].ep_group) == 0
|
|
|
|
|
|
|
|
assert dist.get_rank(parallel_info_dict[2].dp_group) == rank // 2
|
|
|
|
assert dist.get_rank(parallel_info_dict[1].dp_group) == rank
|
|
|
|
|
2023-11-02 02:21:24 +00:00
|
|
|
model = nn.ModuleList([exp0, exp1, exp2])
|
2024-01-09 02:20:05 +00:00
|
|
|
model = model.to(get_accelerator().get_current_device())
|
2022-03-25 05:02:39 +00:00
|
|
|
sync_moe_model_param(model)
|
|
|
|
|
|
|
|
# MOE experts layout success when ep_size = 1
|
2023-11-02 02:21:24 +00:00
|
|
|
assert_equal_in_group(exp0.wi.data, parallel_info_dict[1].dp_group)
|
|
|
|
assert_equal_in_group(exp0.wo.data, parallel_info_dict[1].dp_group)
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
# MOE experts layout success when ep_size = 2
|
2023-11-02 02:21:24 +00:00
|
|
|
assert_equal_in_group(exp1.wi.data, parallel_info_dict[2].dp_group)
|
|
|
|
assert_equal_in_group(exp1.wo.data, parallel_info_dict[2].dp_group)
|
|
|
|
|
|
|
|
|
|
|
|
def _run_test(rank, world_size, port, expert_parallel):
|
|
|
|
colossalai.launch(
|
|
|
|
config=dict(),
|
|
|
|
rank=rank,
|
|
|
|
world_size=world_size,
|
|
|
|
host="localhost",
|
|
|
|
port=port,
|
|
|
|
backend="nccl",
|
|
|
|
)
|
|
|
|
run_moe_init(expert_parallel)
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
2023-11-02 02:21:24 +00:00
|
|
|
@pytest.mark.parametrize("expert_parallel", ["EP", "TP"])
|
2022-04-14 16:33:04 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
2023-11-02 02:21:24 +00:00
|
|
|
def test_moe_initialization(expert_parallel):
|
|
|
|
spawn(_run_test, 2, expert_parallel=expert_parallel)
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
if __name__ == "__main__":
|
2023-11-02 02:21:24 +00:00
|
|
|
test_moe_initialization("EP")
|
|
|
|
test_moe_initialization("TP")
|