2022-03-25 05:02:39 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
2023-11-02 02:21:24 +00:00
|
|
|
import torch.distributed as dist
|
2023-04-06 06:51:35 +00:00
|
|
|
|
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 rerun_if_address_is_in_use, spawn
|
2022-03-25 05:02:39 +00:00
|
|
|
|
2023-11-02 02:21:24 +00:00
|
|
|
BATCH_SIZE = 4
|
2022-03-25 05:02:39 +00:00
|
|
|
NUM_EXPERTS = 4
|
|
|
|
|
|
|
|
|
|
|
|
def check_equal(tensor_a, tensor_b, atol=1e-06):
|
|
|
|
assert torch.allclose(tensor_a, tensor_b, rtol=0, atol=atol) is True
|
|
|
|
|
|
|
|
|
2023-11-02 02:21:24 +00:00
|
|
|
def run_routing(rank, world_size, port, rs=2, hidden_size=128, data_type=torch.float32, topk=1):
|
2022-03-25 05:02:39 +00:00
|
|
|
# Here we do not need TF32, since it brings absolute error on results
|
|
|
|
torch.backends.cuda.matmul.allow_tf32 = False
|
|
|
|
|
2023-11-08 15:07:03 +00:00
|
|
|
colossalai.launch(config=dict(), rank=rank, world_size=world_size, host="localhost", port=port, backend="nccl")
|
2023-11-02 02:21:24 +00:00
|
|
|
local_rank = dist.get_rank()
|
2022-03-25 05:02:39 +00:00
|
|
|
|
2023-11-08 15:07:03 +00:00
|
|
|
MOE_MANAGER.setup(parallel="EP") # MOE environment initialization
|
2023-11-02 02:21:24 +00:00
|
|
|
MOE_MANAGER.reset_loss()
|
2023-11-08 15:07:03 +00:00
|
|
|
torch.manual_seed(rs + local_rank) # set each process has different random seed
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
# get randomized data
|
2024-01-09 02:20:05 +00:00
|
|
|
tokens = torch.randn(
|
|
|
|
BATCH_SIZE, hidden_size, dtype=data_type, device=get_accelerator().get_current_device(), requires_grad=True
|
|
|
|
)
|
2022-03-25 05:02:39 +00:00
|
|
|
|
2023-11-08 15:07:03 +00:00
|
|
|
layer = SparseMLP(
|
|
|
|
hidden_size=hidden_size,
|
|
|
|
intermediate_size=hidden_size * 2,
|
|
|
|
num_experts=NUM_EXPERTS,
|
|
|
|
router_top_k=topk,
|
|
|
|
router_capacity_factor_train=1.0,
|
|
|
|
)
|
2024-01-09 02:20:05 +00:00
|
|
|
layer = layer.to(get_accelerator().get_current_device())
|
2022-03-25 05:02:39 +00:00
|
|
|
if data_type == torch.float16:
|
|
|
|
layer = layer.half()
|
|
|
|
|
2023-05-11 08:30:58 +00:00
|
|
|
# use matrix multiplication instead of COL_MOE_KERNEL in MOE dispatch and combine
|
2023-11-02 02:21:24 +00:00
|
|
|
layer.enable_kernel = False
|
|
|
|
old_out = layer(tokens)
|
2022-03-25 05:02:39 +00:00
|
|
|
ech = old_out.shape
|
2024-01-09 02:20:05 +00:00
|
|
|
grad = torch.randn(ech, device=get_accelerator().get_current_device())
|
2023-09-19 06:20:26 +00:00
|
|
|
old_out.backward(grad) # get gradient
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
# save all results
|
|
|
|
o_tk_grad = tokens.grad.data.clone()
|
2022-09-22 05:56:30 +00:00
|
|
|
o_gt_grad = layer.gate_weight.grad.data.clone()
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
# reset all gradients
|
|
|
|
tokens.grad.zero_()
|
2022-09-22 05:56:30 +00:00
|
|
|
layer.gate_weight.grad.zero_()
|
2022-03-25 05:02:39 +00:00
|
|
|
|
2023-11-02 02:21:24 +00:00
|
|
|
layer.enable_kernel = True
|
2023-11-08 15:07:03 +00:00
|
|
|
new_out = layer(tokens) # get outputs through colossal kernel
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
if data_type == torch.float32:
|
|
|
|
check_equal(old_out, new_out)
|
|
|
|
else:
|
|
|
|
check_equal(old_out, new_out, 1e-2)
|
|
|
|
# forward function passed
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
new_out.backward(grad) # get new type gradient
|
2022-03-25 05:02:39 +00:00
|
|
|
n_tk_grad = tokens.grad.data.clone()
|
2022-09-22 05:56:30 +00:00
|
|
|
n_gt_grad = layer.gate_weight.grad.data.clone()
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
if data_type == torch.float32:
|
|
|
|
check_equal(o_tk_grad, n_tk_grad)
|
|
|
|
else:
|
|
|
|
check_equal(o_tk_grad, o_tk_grad, 1e-2)
|
|
|
|
# tokens gradient is correct
|
|
|
|
|
|
|
|
if data_type == torch.float32:
|
|
|
|
check_equal(o_gt_grad, n_gt_grad, 5e-05)
|
|
|
|
else:
|
|
|
|
check_equal(o_gt_grad, n_gt_grad, 2e-01)
|
|
|
|
# bias gradient is correct
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
@pytest.mark.parametrize("rs", [131])
|
|
|
|
@pytest.mark.parametrize("hidden_size", [32, 144])
|
|
|
|
@pytest.mark.parametrize("data_type", [torch.float32, torch.float16])
|
2023-11-02 02:21:24 +00:00
|
|
|
@pytest.mark.parametrize("topk", [1, 2])
|
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_kernel(rs, hidden_size, data_type, topk):
|
|
|
|
spawn(run_routing, 4, rs=rs, hidden_size=hidden_size, data_type=data_type, topk=topk)
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
|
2023-11-08 15:07:03 +00:00
|
|
|
if __name__ == "__main__":
|
2023-11-02 02:21:24 +00:00
|
|
|
test_moe_kernel(2, 256, torch.float16, 2)
|