-
-
Notifications
You must be signed in to change notification settings - Fork 21k
Expand file tree
/
Copy pathtest_moonep_bf16_poc.py
More file actions
354 lines (312 loc) · 10.4 KB
/
Copy pathtest_moonep_bf16_poc.py
File metadata and controls
354 lines (312 loc) · 10.4 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""
Test MoonEP dispatch / prefetch / combine logic (BF16 PoC).
Two paths, both compared against the pure-PyTorch reference MoE:
- MoonEPPrepareAndFinalize + a reference segment-loop expert runner over
MoonEP's expert-grouped ``[NvS, H]`` layout;
- the full modular kernel, MoonEPPrepareAndFinalize + MoonEPExperts through
FusedMoEKernel.
Requires NVSwitch multicast capable GPUs.
"""
import dataclasses
import pytest
import torch
import torch.nn.functional as F
from torch.distributed import ProcessGroup
from tests.kernels.moe.utils import make_test_weights
from tests.kernels.utils import torch_experts
from vllm.config import VllmConfig, set_current_vllm_config
from vllm.model_executor.layers.fused_moe.activation import MoEActivation
from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import (
TopKWeightAndReduceNoOP,
)
from vllm.utils.import_utils import has_moonep
from vllm.utils.torch_utils import set_random_seed
from ...utils import multi_gpu_test
from .parallel_utils import ProcessGroupInfo, parallel_launch
if has_moonep():
from vllm.model_executor.layers.fused_moe.prepare_finalize.moonep import (
MoonEPExpertWeightLayout,
MoonEPPrepareAndFinalize,
make_moonep_weight_layout,
)
requires_moonep = pytest.mark.skipif(
not has_moonep(),
reason="Requires MoonEP",
)
class MulticastNotAvailableError(RuntimeError):
pass
@dataclasses.dataclass
class TestConfig:
topk: int
m: int
k: int
n: int
num_experts: int
router_skew: float
@dataclasses.dataclass
class TestTensors:
rank_tokens: torch.Tensor
topk: torch.Tensor
topk_weights: torch.Tensor
config: TestConfig
@staticmethod
def make(config: TestConfig) -> "TestTensors":
rank_tokens = (
torch.randn((config.m, config.k), device="cuda", dtype=torch.bfloat16) / 10
)
# Skewed router logits so the planner has to duplicate hot experts.
logits = config.router_skew * torch.randn(
config.m, config.num_experts, device="cuda", dtype=torch.float32
)
topk_weights, topk = torch.topk(logits, config.topk, dim=-1)
topk_weights = torch.softmax(topk_weights, dim=-1)
return TestTensors(
rank_tokens=rank_tokens,
topk=topk.to(dtype=torch.int64),
topk_weights=topk_weights,
config=config,
)
def reference_moonep_experts(
hidden_nvsh: torch.Tensor,
route_weights_nvs: torch.Tensor,
cu_seqlens: torch.Tensor,
weight_layout: "MoonEPExpertWeightLayout",
) -> torch.Tensor:
"""Segment loop over MoonEP's expert-grouped layout.
``prefetch_weight`` has already materialized redundant experts' weights
in rows ``[E, E+B)``, so every segment reads its own row. Route weights
are applied here; MoonEP's combine does the K-sum.
"""
output = torch.empty_like(hidden_nvsh)
prev = 0
for row, cur in enumerate(cu_seqlens.tolist()):
if cur == prev:
continue
x = hidden_nvsh[prev:cur]
gate = F.linear(x, weight_layout.full_gate_weight[row])
up = F.linear(x, weight_layout.full_up_weight[row])
y = F.linear(F.silu(gate) * up, weight_layout.full_down_weight[row])
y = y * route_weights_nvs[prev:cur].to(dtype=y.dtype).unsqueeze(-1)
output[prev:cur].copy_(y)
prev = cur
if prev < hidden_nvsh.shape[0]:
output[prev:].zero_()
return output
def make_moonep_prepare_finalize(
pg: ProcessGroup,
pgi: ProcessGroupInfo,
hidden_size: int,
num_experts: int,
topk: int,
max_tokens_per_rank: int,
weight_layout: "MoonEPExpertWeightLayout",
):
from moonep import Buffer
from moonep._C import nvl_multicast_supported
if not nvl_multicast_supported():
raise MulticastNotAvailableError("NVSwitch multicast not available")
buffer = Buffer(
S=max_tokens_per_rank,
H=hidden_size,
K=topk,
E=num_experts,
num_ep_ranks=pgi.world_size,
B=weight_layout.num_prefetch_slots,
group=pg,
explicitly_destroy=True,
)
return buffer, MoonEPPrepareAndFinalize(
buffer=buffer,
max_tokens_per_rank=max_tokens_per_rank,
num_dispatchers=pgi.world_size,
num_global_experts=num_experts,
weight_layout=weight_layout,
)
def moonep_moe_impl(
pg: ProcessGroup,
pgi: ProcessGroupInfo,
test_tensors: TestTensors,
w1: torch.Tensor,
w2: torch.Tensor,
num_prefetch_slots: int,
) -> torch.Tensor:
config = test_tensors.config
hidden_size = test_tensors.rank_tokens.shape[1]
max_tokens_per_rank = 128 * ((config.m + 127) // 128)
weight_layout = make_moonep_weight_layout(w1, w2, num_prefetch_slots)
buffer, pf = make_moonep_prepare_finalize(
pg,
pgi,
hidden_size,
config.num_experts,
config.topk,
max_tokens_per_rank,
weight_layout,
)
try:
hidden_nvsh, _, _, _, route_weights_nvs = pf.prepare(
test_tensors.rank_tokens,
test_tensors.topk_weights,
test_tensors.topk,
num_experts=config.num_experts,
expert_map=None,
apply_router_weight_on_input=False,
quant_config=_no_quant_config(),
)
assert route_weights_nvs is not None
expert_out = reference_moonep_experts(
hidden_nvsh, route_weights_nvs, pf.cu_seqlens, weight_layout
)
output = torch.empty_like(test_tensors.rank_tokens)
pf.finalize(
output,
expert_out,
test_tensors.topk_weights,
test_tensors.topk,
apply_router_weight_on_input=False,
weight_and_reduce_impl=TopKWeightAndReduceNoOP(),
)
torch.accelerator.synchronize()
return output
finally:
buffer.destroy()
def _no_quant_config():
from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig
return FusedMoEQuantConfig.make(quant_dtype=None)
def moonep_modular_kernel_impl(
pg: ProcessGroup,
pgi: ProcessGroupInfo,
test_tensors: TestTensors,
w1: torch.Tensor,
w2: torch.Tensor,
num_prefetch_slots: int,
) -> torch.Tensor:
"""Full modular-kernel path: MoonEPPrepareAndFinalize + MoonEPExperts."""
from tests.kernels.moe.utils import make_dummy_moe_config
from vllm.model_executor.layers.fused_moe.experts.moonep_experts import (
MoonEPExperts,
)
from vllm.model_executor.layers.fused_moe.modular_kernel import FusedMoEKernel
config = test_tensors.config
hidden_size = test_tensors.rank_tokens.shape[1]
max_tokens_per_rank = 128 * ((config.m + 127) // 128)
weight_layout = make_moonep_weight_layout(w1, w2, num_prefetch_slots)
buffer, pf = make_moonep_prepare_finalize(
pg,
pgi,
hidden_size,
config.num_experts,
config.topk,
max_tokens_per_rank,
weight_layout,
)
try:
moe_config = make_dummy_moe_config(
num_experts=config.num_experts,
experts_per_token=config.topk,
hidden_dim=hidden_size,
intermediate_size=config.n,
max_num_tokens=max_tokens_per_rank,
)
experts = MoonEPExperts(moe_config=moe_config, quant_config=_no_quant_config())
experts.set_up_weight(weight_layout.full_up_weight)
kernel = FusedMoEKernel(prepare_finalize=pf, fused_experts=experts)
out = kernel.apply(
hidden_states=test_tensors.rank_tokens,
w1=weight_layout.full_gate_weight,
w2=weight_layout.full_down_weight,
topk_weights=test_tensors.topk_weights,
topk_ids=test_tensors.topk,
activation=MoEActivation.SILU,
global_num_experts=config.num_experts,
expert_map=None,
apply_router_weight_on_input=False,
)
torch.accelerator.synchronize()
return out
finally:
buffer.destroy()
def _moonep_moe(
pgi: ProcessGroupInfo,
config: TestConfig,
w1: torch.Tensor,
w2: torch.Tensor,
num_prefetch_slots: int,
use_modular_kernel: bool,
):
from vllm.v1.worker.workspace import init_workspace_manager
device_idx = torch.accelerator.current_device_index()
init_workspace_manager(torch.device("cuda", device_idx))
w1 = w1.to(device=device_idx)
w2 = w2.to(device=device_idx)
pg = torch.distributed.new_group(list(range(pgi.world_size)))
set_random_seed(7 + pgi.rank)
test_tensors = TestTensors.make(config)
with set_current_vllm_config(VllmConfig()):
torch_combined = torch_experts(
test_tensors.rank_tokens,
w1,
w2,
test_tensors.topk_weights,
test_tensors.topk,
)
impl = moonep_modular_kernel_impl if use_modular_kernel else moonep_moe_impl
moonep_combined = impl(pg, pgi, test_tensors, w1, w2, num_prefetch_slots)
torch.testing.assert_close(
torch_combined,
moonep_combined,
atol=6e-2,
rtol=6e-2,
)
MNKs = [
(1, 256, 512),
(37, 256, 512),
(100, 512, 1024),
(512, 768, 2048),
]
@pytest.mark.parametrize("m,n,k", MNKs)
@pytest.mark.parametrize("num_experts", [32])
@pytest.mark.parametrize("topk", [4])
@pytest.mark.parametrize("router_skew", [1.0, 8.0])
@pytest.mark.parametrize("num_prefetch_slots", [4])
@pytest.mark.parametrize("world_size", [2])
@pytest.mark.parametrize("use_modular_kernel", [False, True])
@multi_gpu_test(num_gpus=2)
@requires_moonep
def test_moonep_bf16_moe(
m: int,
n: int,
k: int,
num_experts: int,
topk: int,
router_skew: float,
num_prefetch_slots: int,
world_size: int,
use_modular_kernel: bool,
):
set_random_seed(7)
config = TestConfig(
topk=topk,
m=m,
k=k,
n=n,
num_experts=num_experts,
router_skew=router_skew,
)
(_, w1, _, _), (_, w2, _, _) = make_test_weights(num_experts, n, k)
try:
parallel_launch(
world_size,
_moonep_moe,
config,
w1,
w2,
num_prefetch_slots,
use_modular_kernel,
)
except Exception as exc:
if "MulticastNotAvailableError" in str(exc):
pytest.skip("NVSwitch multicast not available")
raise