-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathtest_moe.py
More file actions
152 lines (137 loc) · 5.38 KB
/
test_moe.py
File metadata and controls
152 lines (137 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import torch
from xtuner.v1.model.moe.moe import MoEConfig, MoE, SequenceContext
from xtuner.v1.module.router import NoAuxRouterConfig
from xtuner.v1.module.attention import MHAConfig
from torch.distributed.device_mesh import init_device_mesh
import os
from copy import deepcopy
from xtuner.v1.loss.ce_loss import CELossContext, CELossConfig
from xtuner._testing import DeterministicDDPTestCase
from xtuner.v1.utils.compile import maybe_compile
import parametrize
class TestMoE:
@parametrize.parametrize("dtype,device", [(torch.bfloat16, "cuda")])
def test_moe_config(self, dtype, device):
router_config = NoAuxRouterConfig(
scoring_func="sigmoid",
router_scaling_factor=1.0,
n_group=8,
topk_group=4,
norm_topk_prob=True,
)
attention_config = MHAConfig(
num_attention_heads=32,
num_key_value_heads=32,
head_dim=16,
)
config = MoEConfig(
vocab_size=10240,
max_position_embeddings=2048,
pad_token_id=0,
eos_token_id=0,
num_hidden_layers=6,
hidden_size=512,
intermediate_size=2048,
rms_norm_eps=1e-6,
rope_theta=1e6,
hidden_act="silu",
attention=attention_config,
tie_word_embeddings=False,
n_routed_experts=32,
n_shared_experts=1,
num_experts_per_tok=2,
first_k_dense_replace=1,
hidden_factor=1.0,
moe_intermediate_size=512, # TODO: Restriction of triton grouped gemm, should be optimizer
router=router_config,
compile_cfg=False,
)
model = MoE(config=config).to(dtype).to(device)
model.cuda()
loss_cfg = CELossConfig()
input_ids = torch.randint(
0, config.vocab_size, (1, 128), dtype=torch.int64, device="cuda"
)
shift_input_ids = input_ids[:, :-1]
shifted_labels = input_ids[:, 1:]
seq_ctx = SequenceContext.from_input_ids(input_ids=(shift_input_ids.to('cuda'),))
seq_ctx_list = [seq_ctx]
LossContext = loss_cfg.loss_ctx_cls
loss_ctx = loss_cfg.build(data={"shifted_labels": shifted_labels}, sp_mesh=None)
loss_ctx_list = [loss_ctx]
loss_ctx_list = LossContext.build_batches(loss_ctx_list)
loss_ctx = loss_ctx_list[0]
seq_ctx = seq_ctx_list[0]
model(seq_ctx=seq_ctx, loss_ctx={"lm": loss_ctx})
class TestDistributedMoE(DeterministicDDPTestCase):
@parametrize.parametrize(
"dtype,device,dispatcher,n_shared_experts,first_k_dense_replace",
[
# (torch.bfloat16, "cuda", "deepep", 1, 2),
(torch.bfloat16, "cuda", "all2all", 1, 2),
(torch.bfloat16, "cuda", "all2all", 0, 0),
],
)
def test_parallel_accuracy(self, dtype, device, dispatcher, n_shared_experts, first_k_dense_replace):
self.create_pg(device)
router_config = NoAuxRouterConfig(
scoring_func="sigmoid",
router_scaling_factor=1.0,
n_group=8,
topk_group=4,
norm_topk_prob=True,
)
attention_config = MHAConfig(
num_attention_heads=32,
num_key_value_heads=32,
head_dim=16,
)
config = MoEConfig(
vocab_size=10240,
max_position_embeddings=2048,
pad_token_id=0,
eos_token_id=0,
num_hidden_layers=6,
hidden_size=512,
intermediate_size=2048,
rms_norm_eps=1e-6,
rope_theta=1e6,
hidden_act="silu",
attention=attention_config,
tie_word_embeddings=False,
n_routed_experts=32,
n_shared_experts=n_shared_experts,
num_experts_per_tok=2,
first_k_dense_replace=first_k_dense_replace,
hidden_factor=1.0,
moe_intermediate_size=512, # TODO: Restriction of triton grouped gemm, should be optimizer
router=router_config,
)
loss_cfg = CELossConfig()
model = MoE(config=config).to(dtype).to(device)
parallel_config = deepcopy(config)
parallel_config.dispatcher = dispatcher
ep_mesh = init_device_mesh(
device_type="cuda",
mesh_shape=(8,)
)
parallel_model = MoE(config=parallel_config).to(dtype).to(device)
input_ids = torch.randint(
0, config.vocab_size, (1, 128), dtype=torch.int64, device="cuda"
)
shift_input_ids = input_ids[:, :-1]
shifted_labels = input_ids[:, 1:]
seq_ctx = SequenceContext.from_input_ids(input_ids=(shift_input_ids.to('cuda'),))
seq_ctx_list = [seq_ctx]
LossContext = loss_cfg.loss_ctx_cls
loss_ctx = loss_cfg.build(data={"shifted_labels": shifted_labels}, sp_mesh=None)
loss_ctx_list = [loss_ctx]
loss_ctx_list = LossContext.build_batches(loss_ctx_list)
loss_ctx = loss_ctx_list[0]
seq_ctx = seq_ctx_list[0]
loss_parallel = parallel_model(seq_ctx=seq_ctx, loss_ctx={"lm": loss_ctx})["loss"]
loss_expected = model(seq_ctx=seq_ctx, loss_ctx={"lm": loss_ctx})["loss"]
torch.allclose(loss_expected, loss_parallel, atol=1e-6, rtol=1e-4)
@property
def world_size(self) -> int:
return int(os.getenv("XTUNER_TEST_WORLD_SIZE", "8"))