2022-03-25 05:02:39 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
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 import SparseMLP
|
|
|
|
from colossalai.moe.manager import MOE_MANAGER
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.testing import assert_equal_in_group, rerun_if_address_is_in_use, spawn
|
2023-11-08 15:07:03 +00:00
|
|
|
from tests.test_moe.moe_utils import MoeGradientHandler
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
BATCH_SIZE = 4
|
|
|
|
DIM = 16
|
|
|
|
|
|
|
|
|
|
|
|
def run_test(rank, world_size, port):
|
2023-11-02 02:21:24 +00:00
|
|
|
colossalai.launch(
|
|
|
|
config=dict(),
|
|
|
|
rank=rank,
|
|
|
|
world_size=world_size,
|
|
|
|
host="localhost",
|
|
|
|
port=port,
|
|
|
|
backend="nccl",
|
|
|
|
)
|
|
|
|
|
2023-11-08 15:07:03 +00:00
|
|
|
MOE_MANAGER.setup(parallel="EP") # MOE initialization
|
2022-03-25 05:02:39 +00:00
|
|
|
num_experts_list = [1, 2, 4]
|
|
|
|
layer_list = []
|
|
|
|
for num_experts in num_experts_list:
|
2023-11-02 02:21:24 +00:00
|
|
|
moe_layer = SparseMLP(
|
|
|
|
hidden_size=DIM,
|
|
|
|
intermediate_size=DIM * 4,
|
|
|
|
num_experts=num_experts,
|
|
|
|
router_top_k=1,
|
|
|
|
router_noisy_policy="Jitter",
|
|
|
|
)
|
2022-03-25 05:02:39 +00:00
|
|
|
layer_list.append(moe_layer)
|
|
|
|
|
2022-09-23 07:33:57 +00:00
|
|
|
model = nn.ModuleList(layer_list)
|
2024-01-09 02:20:05 +00:00
|
|
|
model = model.to(get_accelerator().get_current_device())
|
2023-11-02 02:21:24 +00:00
|
|
|
dist_dict = MOE_MANAGER.parallel_info_dict
|
|
|
|
assert_equal_in_group(layer_list[0].experts.wi.data, dist_dict[1].dp_group)
|
|
|
|
assert_equal_in_group(layer_list[0].experts.wo.data, dist_dict[1].dp_group)
|
|
|
|
assert_equal_in_group(layer_list[1].experts.wi.data, dist_dict[2].dp_group)
|
|
|
|
assert_equal_in_group(layer_list[1].experts.wo.data, dist_dict[2].dp_group)
|
|
|
|
assert_equal_in_group(layer_list[2].experts.wi.data, dist_dict[4].dp_group)
|
|
|
|
assert_equal_in_group(layer_list[2].experts.wo.data, dist_dict[4].dp_group)
|
2022-03-25 05:02:39 +00:00
|
|
|
# MoE model synchronization passed
|
|
|
|
|
|
|
|
grad_handler = MoeGradientHandler(model, 0)
|
|
|
|
|
|
|
|
rank = dist.get_rank()
|
|
|
|
torch.cuda.manual_seed(78 + rank)
|
2024-01-09 02:20:05 +00:00
|
|
|
data = torch.randn(BATCH_SIZE, DIM, device=get_accelerator().get_current_device())
|
2022-03-25 05:02:39 +00:00
|
|
|
grad = torch.randn_like(data)
|
|
|
|
|
2023-11-02 02:21:24 +00:00
|
|
|
MOE_MANAGER.reset_loss()
|
2022-09-23 07:33:57 +00:00
|
|
|
for layer in layer_list:
|
2023-11-02 02:21:24 +00:00
|
|
|
data = layer(data)
|
2022-09-23 07:33:57 +00:00
|
|
|
data.backward(grad)
|
2022-03-25 05:02:39 +00:00
|
|
|
grad_handler.handle_gradient()
|
|
|
|
|
2023-11-02 02:21:24 +00:00
|
|
|
assert_equal_in_group(layer_list[0].experts.wi.grad, dist_dict[1].dp_group)
|
|
|
|
assert_equal_in_group(layer_list[0].experts.wo.grad, dist_dict[1].dp_group)
|
|
|
|
assert_equal_in_group(layer_list[1].experts.wi.grad, dist_dict[2].dp_group)
|
|
|
|
assert_equal_in_group(layer_list[1].experts.wo.grad, dist_dict[2].dp_group)
|
|
|
|
assert_equal_in_group(layer_list[2].experts.wi.grad, dist_dict[4].dp_group)
|
|
|
|
assert_equal_in_group(layer_list[2].experts.wo.grad, dist_dict[4].dp_group)
|
2022-03-25 05:02:39 +00:00
|
|
|
# MoE grad handler test passed
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
2022-04-14 16:33:04 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
2022-03-25 05:02:39 +00:00
|
|
|
def test_grad_handler():
|
2023-04-06 06:51:35 +00:00
|
|
|
spawn(run_test, 4)
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
if __name__ == "__main__":
|
2022-03-25 05:02:39 +00:00
|
|
|
test_grad_handler()
|