Repository navigation
Expand file tree
/
Copy pathtest_forward_context.py
More file actions
805 lines (714 loc) · 26.4 KB
/
Copy pathtest_forward_context.py
File metadata and controls
805 lines (714 loc) · 26.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the AFD plugin project
from __future__ import annotations
import ast
import sys
from pathlib import Path
from types import ModuleType, SimpleNamespace
from typing import Any
import pytest
torch = pytest.importorskip("torch")
pytest.importorskip("vllm")
from vllm.forward_context import ( # noqa: E402
get_forward_context as get_current_forward_context,
)
from afd_plugin.model_executor.models import ( # noqa: E402
get_afd_metadata_from_forward_context,
)
from afd_plugin.model_executor.models.npu.async_cam_layout import ( # noqa: E402
ASYNC_MOE_UBATCH_METADATA_KEY,
AsyncMoeUbatchMetadata,
get_async_moe_ubatch_metadata_from_forward_context,
)
from afd_plugin.model_executor.npu.async_cam_ubatching import ( # noqa: E402
AsyncMoeStage,
)
def test_get_afd_metadata_from_additional_kwargs():
forward_context = SimpleNamespace(
additional_kwargs={"afd_metadata": {"stage": 0}},
afd_metadata={"stage": 1},
)
assert get_afd_metadata_from_forward_context(forward_context) == {"stage": 0}
def test_get_afd_metadata_ignores_forward_context_attribute():
forward_context = SimpleNamespace(
additional_kwargs={},
afd_metadata={"stage": 0},
)
assert get_afd_metadata_from_forward_context(forward_context) is None
def test_get_async_moe_ubatch_metadata_from_additional_kwargs():
sidecar = {"ubatch_slices": ["stage0", "stage1"]}
forward_context = SimpleNamespace(
additional_kwargs={ASYNC_MOE_UBATCH_METADATA_KEY: sidecar},
)
assert (
get_async_moe_ubatch_metadata_from_forward_context(forward_context) is sidecar
)
@pytest.mark.parametrize(
("is_first_rank", "is_last_rank"),
[(True, False), (False, True)],
)
def test_async_model_forward_preserves_pp_boundaries(
monkeypatch,
is_first_rank,
is_last_rank,
):
from afd_plugin.model_executor.models.npu import (
deepseek_v2_async_cam_forward as async_forward,
)
class FakeIntermediateTensors(dict):
pass
monkeypatch.setattr(async_forward, "IntermediateTensors", FakeIntermediateTensors)
monkeypatch.setattr(
async_forward,
"get_pp_group",
lambda: SimpleNamespace(
is_first_rank=is_first_rank,
is_last_rank=is_last_rank,
),
)
forward_context = SimpleNamespace()
afd_metadata = object()
monkeypatch.setattr(async_forward, "get_forward_context", lambda: forward_context)
monkeypatch.setattr(
async_forward,
"get_afd_metadata_from_forward_context",
lambda context: afd_metadata if context is forward_context else None,
)
monkeypatch.setattr(
async_forward,
"get_async_moe_ubatch_metadata_from_forward_context",
lambda context: None,
)
schedule_calls: list[tuple[Any, ...]] = []
def run_schedule(
model,
hidden_states,
residual,
positions,
received_metadata,
llama_4_scaling,
):
schedule_calls.append(
(
model,
hidden_states,
residual,
positions,
received_metadata,
llama_4_scaling,
)
)
next_residual = (
torch.zeros_like(hidden_states) if residual is None else residual + 2
)
return hidden_states + 1, next_residual
monkeypatch.setattr(
async_forward,
"run_attention_gate_afd_forward",
run_schedule,
)
norm_calls = []
def run_norm(hidden_states, residual):
norm_calls.append((hidden_states, residual))
return hidden_states + residual, None
model = SimpleNamespace(
aux_hidden_state_layers=(),
embed_input_ids=lambda input_ids: input_ids.to(torch.float32).unsqueeze(-1),
_get_llama_4_scaling=lambda positions: None,
norm=run_norm,
)
positions = torch.arange(2)
if is_first_rank:
input_ids = torch.tensor([3, 4])
intermediate_tensors = None
expected_hidden_states = model.embed_input_ids(input_ids)
expected_residual = None
else:
input_ids = None
expected_hidden_states = torch.full((2, 1), 5.0)
expected_residual = torch.full((2, 1), 7.0)
intermediate_tensors = FakeIntermediateTensors(
{
"hidden_states": expected_hidden_states,
"residual": expected_residual,
}
)
output = async_forward.run_model_forward(
model,
input_ids,
positions,
intermediate_tensors,
)
assert len(schedule_calls) == 1
assert torch.equal(schedule_calls[0][1], expected_hidden_states)
assert schedule_calls[0][2] is expected_residual
assert schedule_calls[0][4] is afd_metadata
scheduled_hidden_states = expected_hidden_states + 1
scheduled_residual = (
torch.zeros_like(expected_hidden_states)
if expected_residual is None
else expected_residual + 2
)
if is_last_rank:
assert len(norm_calls) == 1
assert torch.equal(output, scheduled_hidden_states + scheduled_residual)
else:
assert isinstance(output, FakeIntermediateTensors)
assert torch.equal(output["hidden_states"], scheduled_hidden_states)
assert torch.equal(output["residual"], scheduled_residual)
def test_async_cam_profile_forward_runs_matched_connector_io(monkeypatch):
from afd_plugin.model_executor.models.npu import (
deepseek_v2_async_cam_forward as async_forward,
)
forward_context = SimpleNamespace(
in_profile_run=True,
ubatch_idx=0,
flash_comm_v1_enabled=True,
)
monkeypatch.setattr(async_forward, "get_forward_context", lambda: forward_context)
monkeypatch.setattr(
async_forward,
"maybe_apply_dbo_yield",
lambda hidden_states, **_kwargs: hidden_states,
)
connector_calls: list[str] = []
dispatch_layouts: list[object] = []
restored_layouts: list[object] = []
def prepare_dispatch_payload(
hidden_states,
topk_weights,
topk_ids,
router_logits,
*,
use_sequence_parallel,
):
assert use_sequence_parallel is True
layout = object()
dispatch_layouts.append(layout)
return SimpleNamespace(
hidden_states=hidden_states,
topk_weights=topk_weights,
topk_ids=topk_ids,
router_logits=router_logits,
layout=layout,
)
def restore_dispatch_output(local_output, layout):
restored_layouts.append(layout)
return local_output
monkeypatch.setattr(
async_forward,
"prepare_cam_dispatch_payload",
prepare_dispatch_payload,
)
monkeypatch.setattr(
async_forward,
"restore_cam_dispatch_output",
restore_dispatch_output,
)
def send_attn_output(*args, **kwargs):
connector_calls.append("send")
def recv_ffn_output(ref_tensor, ubatch_idx):
connector_calls.append("recv")
return ref_tensor
connector = SimpleNamespace(
send_attn_output=send_attn_output,
recv_ffn_output=recv_ffn_output,
)
afd_metadata = SimpleNamespace(connector=connector, stage_idx=0)
class _ProfileMoELayer:
is_moe_layer = True
layer_idx = 0
def compute_attn_output(
self,
positions,
hidden_states,
residual,
llama_4_scaling,
):
return (
hidden_states + 1,
residual,
torch.ones((hidden_states.shape[0], 1)),
torch.zeros((hidden_states.shape[0], 1), dtype=torch.int32),
torch.ones((hidden_states.shape[0], 1)),
)
model = SimpleNamespace(
layers=[_ProfileMoELayer(), _ProfileMoELayer()],
start_layer=0,
end_layer=2,
)
hidden_states = torch.zeros((2, 4))
output, residual = async_forward.run_attention_gate_afd_forward(
model,
hidden_states,
None,
torch.arange(2),
afd_metadata,
)
assert torch.equal(output, hidden_states + 2)
assert residual is None
assert connector_calls == ["send", "recv", "send", "recv"]
assert restored_layouts == dispatch_layouts
def test_deepseek_afd_wrapper_keeps_full_model_compile_enabled():
source = Path("afd_plugin/model_executor/models/deepseek_v2.py").read_text()
assert "@native.support_torch_compile\nclass AFDDeepseekV2Model" in source
assert "from __future__ import annotations" not in source
assert "self.do_not_compile = True" not in source
def test_deepseek_afd_wrapper_treats_index_topk_as_optional():
source = Path("afd_plugin/model_executor/models/deepseek_v2.py").read_text()
assert 'self.is_v32 = hasattr(config, "index_topk")' in source
assert "self.is_v32 = config.index_topk is not None" not in source
assert "topk_tokens = config.index_topk" in source
def test_deepseek_afd_wrapper_treats_llama_4_scaling_as_optional():
source = Path("afd_plugin/model_executor/models/deepseek_v2.py").read_text()
assert 'getattr(self.config, "llama_4_scaling", None)' in source
assert "self.config.llama_4_scaling" not in source
def test_deepseek_afd_attention_path_can_compute_gate_before_send():
source = Path("afd_plugin/model_executor/models/deepseek_v2.py").read_text()
executor_source = Path(
"afd_plugin/model_executor/models/npu/deepseek_v2_async_cam_forward.py",
).read_text()
module_imports = source.split("logger = init_logger(__name__)", 1)[0]
model_source = source.split("class AFDDeepseekV2Model", 1)[1].split(
"class AFDDeepseekV2ForCausalLM",
1,
)[0]
model_forward = model_source.split(" def forward(", 1)[1].split(
" def compute_ffn_output(",
1,
)[0]
gate_proxy = source.split("class GateOnlyRemoteMoE", 1)[1].split(
"class AFDDeepseekV2RemoteExpertsMoE",
1,
)[0]
attention_gate_forward = executor_source.split(
"def run_attention_gate_afd_forward(",
1,
)[1].split("def run_async_moe_ubatch_afd_forward(", 1)[0]
assert 'if afd_role == "attention":' in source
assert "afd_plugin.model_executor.models.npu" not in module_imports
assert "def _forward_attention(" not in source
assert "return super().forward(" in model_forward
assert "deepseek_v2_async_cam_forward.run_model_forward(" in model_forward
assert "compute_gate_topk(" in gate_proxy
assert "topk_weights=topk_weights" in gate_proxy
assert "topk_ids=topk_ids" in gate_proxy
assert "router_logits=router_logits" in gate_proxy
assert "layer.compute_attn_output(" in attention_gate_forward
assert "pending_ffn_recv" in attention_gate_forward
assert "topk_weights" in attention_gate_forward
assert "topk_ids" in attention_gate_forward
def test_deepseek_afd_attention_gate_can_force_balanced_topk_ids():
source = Path("afd_plugin/model_executor/models/deepseek_v2.py").read_text()
gate_source = Path(
"afd_plugin/model_executor/models/npu/deepseek_v2_attention_gate.py",
).read_text()
module_imports = source.split("logger = init_logger(__name__)", 1)[0]
compute_attn_output = source.split(" def compute_attn_output(", 1)[1].split(
" def compute_ffn_output(",
1,
)[0]
assert "compute_attention_gate_topk(" in compute_attn_output
assert "afd_plugin.model_executor.models.npu" not in module_imports
assert "from afd_plugin.model_executor.models.npu import (" in compute_attn_output
assert "deepseek_v2_attention_gate," in compute_attn_output
assert "force_balanced_topk_ids_enabled" in gate_source
assert "def _force_balanced_topk_ids(" in gate_source
assert "topk_ids.copy_(balanced_topk_ids)" in gate_source
assert "topk_weights, topk_ids = afd_connector.select_experts(" in (gate_source)
assert "if force_balanced_topk_ids_enabled():" in gate_source
assert (
gate_source.index(
"topk_weights, topk_ids = afd_connector.select_experts(",
)
< gate_source.index("if force_balanced_topk_ids_enabled():")
< gate_source.index("topk_weights = topk_weights.to(torch.float32)")
)
def test_deepseek_afd_gate_on_attention_keeps_dense_layers_local():
source = Path("afd_plugin/model_executor/models/deepseek_v2.py").read_text()
executor_source = Path(
"afd_plugin/model_executor/models/npu/deepseek_v2_async_cam_forward.py",
).read_text()
assert "self.is_moe_layer = is_moe_layer" in source
assert "self.compute_gate_on_attention and not self.is_moe_layer" in source
assert "if not layer.is_moe_layer:" in executor_source
assert (
"return _ATTENTION_ROLE if compute_gate_on_attention else _FFN_ROLE" in source
)
def test_deepseek_compute_gate_on_attention_selects_backend_boundary():
source = Path("afd_plugin/model_executor/models/deepseek_v2.py").read_text()
assert 'device_type not in ("cuda", "npu")' in source
assert "self.mlp = AFDDeepseekV2RemoteExpertsMoE(" in source
assert "self.mlp = GateOnlyRemoteMoE(" in source
assert 'prefix=f"{prefix}.mlp"' in source
assert (
"# NPU-only: Attention-side gate/topk is implemented in the NPU helper."
in source
)
assert (
"# NPU-only: gated MoE FFN compute consumes Attention-side topk payloads."
in source
)
def test_async_moe_pipeline_preserves_stage_order(monkeypatch):
from afd_plugin.model_executor.models.npu import deepseek_v2_async_cam_forward
events: list[tuple[Any, ...]] = []
forward_context = SimpleNamespace(
attn_metadata={"layer": "full"},
additional_kwargs={},
ubatch_idx=0,
num_ubatches=1,
num_tokens=4,
pad_size=0,
flash_comm_v1_enabled=True,
)
def send_attn_output(_hidden_states, context, **_kwargs):
events.append(("send", context.metadata.stage_idx))
def recv_ffn_output(ref_tensor, ubatch_idx):
events.append(("recv", ubatch_idx))
return ref_tensor
connector = SimpleNamespace(
send_attn_output=send_attn_output,
recv_ffn_output=recv_ffn_output,
)
parent_metadata = SimpleNamespace(
stage_idx=0,
connector=connector,
)
forward_context.additional_kwargs["afd_metadata"] = parent_metadata
execution_plan = AsyncMoeUbatchMetadata(
attn_metadata=[{"layer": "stage-0"}, {"layer": "stage-1"}],
stages=[
AsyncMoeStage(
slice(0, 1),
slice(0, 2),
input_tokens=2,
),
AsyncMoeStage(
slice(1, 2),
slice(2, 4),
input_tokens=4,
),
],
parent_input_tokens=4,
use_sequence_parallel=True,
)
stage_hidden_states = [torch.zeros((1, 8)), torch.ones((2, 8))]
def compute_attn_output(
_positions,
hidden_states,
residual,
_llama_4_scaling,
):
stage_context = get_current_forward_context()
events.append(
(
"compute",
stage_context.ubatch_idx,
stage_context.attn_metadata,
stage_context.num_tokens,
stage_context.pad_size,
),
)
topk = hidden_states[:, :1]
return hidden_states, residual, topk, topk.to(torch.int32), None
monkeypatch.setattr(
deepseek_v2_async_cam_forward,
"get_forward_context",
lambda: forward_context,
)
monkeypatch.setattr(
deepseek_v2_async_cam_forward,
"get_tensor_model_parallel_world_size",
lambda: 2,
)
monkeypatch.setattr(
deepseek_v2_async_cam_forward,
"build_async_moe_stage_inputs",
lambda *_args, **_kwargs: SimpleNamespace(
hidden_states=stage_hidden_states,
residuals=[None, None],
positions=["positions-0", "positions-1"],
llama_4_scaling=["scaling-0", "scaling-1"],
),
)
monkeypatch.setattr(
deepseek_v2_async_cam_forward,
"restore_async_moe_stage_outputs",
lambda outputs, _metadata: tuple(outputs),
)
monkeypatch.setattr(
deepseek_v2_async_cam_forward,
"prepare_cam_dispatch_payload",
lambda hidden_states, topk_weights, topk_ids, router_logits, **_kwargs: (
SimpleNamespace(
hidden_states=hidden_states,
topk_weights=topk_weights,
topk_ids=topk_ids,
router_logits=router_logits,
layout=object(),
)
),
)
monkeypatch.setattr(
deepseek_v2_async_cam_forward,
"restore_cam_dispatch_output",
lambda output, _layout: output,
)
output, residual = deepseek_v2_async_cam_forward.run_async_moe_ubatch_afd_forward(
model=SimpleNamespace(
start_layer=0,
end_layer=2,
layers=[
SimpleNamespace(
is_moe_layer=True,
layer_idx=layer_idx,
compute_attn_output=compute_attn_output,
)
for layer_idx in range(2)
],
),
hidden_states=torch.zeros((4, 8)),
residual=None,
positions="full-positions",
afd_metadata=parent_metadata,
async_moe_ubatch_metadata=execution_plan,
llama_4_scaling="full-scaling",
)
assert [event[:2] for event in events] == [
("compute", 0),
("send", 0),
("compute", 1),
("recv", 0),
("send", 1),
("compute", 0),
("recv", 1),
("send", 0),
("compute", 1),
("recv", 0),
("send", 1),
("recv", 1),
]
for event in (event for event in events if event[0] == "compute"):
stage_idx = event[1]
assert event[2] == {"layer": f"stage-{stage_idx}"}
assert event[3] == 2
assert event[4] == (0, 2)[stage_idx]
assert all(
restored is expected
for restored, expected in zip(output, stage_hidden_states, strict=True)
)
assert residual is None
assert forward_context.attn_metadata == {"layer": "full"}
assert forward_context.num_tokens == 4
assert forward_context.pad_size == 0
def test_deepseek_afd_ffn_path_reuses_ascend_moe_mlp_after_attention_gate():
source = Path("afd_plugin/model_executor/models/deepseek_v2.py").read_text()
gate_source = Path(
"afd_plugin/model_executor/models/npu/deepseek_v2_attention_gate.py",
).read_text()
compute_ffn_output = source.split(
" def compute_ffn_output(",
1,
)[1].split("\n\n@native.support_torch_compile", 1)[0]
compute_moe = gate_source.split(
"def compute_attention_gate_moe_ffn(",
1,
)[1].split("\ndef _dequantize_int8_activation(", 1)[0]
assert "compute_attention_gate_moe_ffn(" in compute_ffn_output
assert "from afd_plugin.model_executor.models.npu import (" in compute_ffn_output
assert "deepseek_v2_attention_gate," in compute_ffn_output
assert "AFDF2ATransferPayload(" in compute_moe
assert "MoEMlpComputeInput(" in compute_moe
assert "unified_apply_mlp(" in compute_moe
assert "routed_output, _ = unified_apply_mlp(" in compute_moe
assert "quant_type == QuantType.W8A8" in compute_moe
assert 'experts.get_eplb_parameter("w13_weight")' in compute_moe
assert 'experts.get_eplb_parameter("w2_weight")' in compute_moe
assert "experts.w13_weight" not in compute_moe
assert "experts.w2_weight" not in compute_moe
assert "w13_weight_scale_fp32" in compute_moe
assert "w13_weight_scale_fp32_list" in compute_moe
assert "w2_weight_scale_list" in compute_moe
compute_moe_function = next(
node
for node in ast.parse(gate_source).body
if isinstance(node, ast.FunctionDef)
and node.name == "compute_attention_gate_moe_ffn"
)
quant_params_calls = [
node
for node in ast.walk(compute_moe_function)
if isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id == "MoEQuantParams"
]
assert len(quant_params_calls) == 1
# Check the contract without pinning formatting or optional quant fields.
assert any(
keyword.arg == "quant_type"
and isinstance(keyword.value, ast.Name)
and keyword.value.id == "quant_type"
for keyword in quant_params_calls[0].keywords
)
assert "_gmmswigluquant_fusion_enabled()" in compute_moe
assert "fusion=use_gmmswigluquant_fusion" in compute_moe
assert "_compute_w8a8_shared_experts_from_int8(" in compute_moe
assert "shared_input.dtype == torch.int8" in compute_moe
assert 'getattr(layer.mlp, "swiglu_limit", None)' in compute_moe
assert "fusion=False" not in compute_moe
assert "output_dtype=torch.int32" in gate_source
assert "npu_dequant_swiglu_quant(" in gate_source
assert "activation_scale=pertoken_scale" in gate_source
@pytest.mark.parametrize(
("num_routed_tokens", "num_shared_tokens"),
[(2, 2), (2, 0), (0, 2), (0, 0)],
)
@pytest.mark.parametrize(
("routed_scale_applied_in_topk", "expected_routed_value"),
[(False, 2.0), (True, 1.0)],
)
def test_deepseek_afd_ffn_skips_empty_rank_local_moe_work(
monkeypatch,
num_routed_tokens,
num_shared_tokens,
routed_scale_applied_in_topk,
expected_routed_value,
):
from afd_plugin.model_executor.models.npu import deepseek_v2_attention_gate
class FakeQuantType:
NONE = "none"
W8A8 = "w8a8"
W4A8 = "w4a8"
class KeywordArguments:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
routed_calls = []
def fake_unified_apply_mlp(*, mlp_compute_input):
assert mlp_compute_input.quant.quant_type == FakeQuantType.W8A8
assert mlp_compute_input.quant.is_per_channel_weight is False
routed_calls.append(mlp_compute_input.hidden_states)
return (
torch.ones_like(
mlp_compute_input.hidden_states,
dtype=torch.bfloat16,
),
None,
)
fake_moe_mlp: Any = ModuleType("vllm_ascend.ops.fused_moe.moe_mlp")
fake_moe_mlp.unified_apply_mlp = fake_unified_apply_mlp
fake_stage_contracts: Any = ModuleType(
"vllm_ascend.ops.fused_moe.moe_stage_contracts",
)
fake_stage_contracts.MoEMlpComputeInput = KeywordArguments
fake_stage_contracts.MoEWeights = KeywordArguments
fake_stage_params: Any = ModuleType(
"vllm_ascend.ops.fused_moe.moe_stage_params",
)
fake_stage_params.MoEQuantParams = KeywordArguments
fake_quant_type: Any = ModuleType("vllm_ascend.quantization.quant_type")
fake_quant_type.QuantType = FakeQuantType
monkeypatch.setitem(
sys.modules,
"vllm_ascend.ops.fused_moe.moe_mlp",
fake_moe_mlp,
)
monkeypatch.setitem(
sys.modules,
"vllm_ascend.ops.fused_moe.moe_stage_contracts",
fake_stage_contracts,
)
monkeypatch.setitem(
sys.modules,
"vllm_ascend.ops.fused_moe.moe_stage_params",
fake_stage_params,
)
monkeypatch.setitem(
sys.modules,
"vllm_ascend.quantization.quant_type",
fake_quant_type,
)
shared_calls = []
def fake_compute_shared(
shared_experts,
hidden_states,
dynamic_scales,
*,
swiglu_limit,
output_dtype,
):
shared_calls.append(
(shared_experts, hidden_states, dynamic_scales, swiglu_limit),
)
return torch.zeros_like(hidden_states, dtype=output_dtype)
monkeypatch.setattr(
deepseek_v2_attention_gate,
"_compute_w8a8_shared_experts_from_int8",
fake_compute_shared,
)
monkeypatch.setattr(
deepseek_v2_attention_gate,
"_gmmswigluquant_fusion_enabled",
lambda: False,
)
shared_experts = object()
experts = SimpleNamespace(
quant_type=FakeQuantType.W8A8,
dynamic_eplb=False,
get_eplb_parameter=lambda name: name,
activation="silu",
_shared_experts=shared_experts,
)
layer = SimpleNamespace(
mlp=SimpleNamespace(
experts=experts,
routed_scaling_factor=2.0,
),
)
hidden_states = torch.zeros((num_routed_tokens, 4), dtype=torch.int8)
expand_x_shared = torch.zeros((num_shared_tokens, 4), dtype=torch.int8)
output = deepseek_v2_attention_gate.compute_attention_gate_moe_ffn(
layer,
hidden_states=hidden_states,
group_list=torch.zeros(2, dtype=torch.int64),
dynamic_scales=torch.ones(num_routed_tokens),
expand_x_shared=expand_x_shared,
dynamic_scales_shared=torch.ones(num_shared_tokens),
topk_scales=None,
group_list_type=1,
routed_scale_applied_in_topk=routed_scale_applied_in_topk,
)
assert len(routed_calls) == int(num_routed_tokens > 0)
assert output.routed_output.shape == hidden_states.shape
assert output.routed_output.dtype == torch.bfloat16
if num_routed_tokens > 0:
assert torch.equal(
output.routed_output,
torch.full_like(output.routed_output, expected_routed_value),
)
assert len(shared_calls) == int(num_shared_tokens > 0)
if num_shared_tokens > 0:
assert output.shared_output is not None
assert output.shared_output.shape == expand_x_shared.shape
assert shared_calls[0][3] is None
else:
assert output.shared_output is None
def test_deepseek_afd_ffn_compute_omits_stub_io_diagnostics():
source = Path("afd_plugin/model_executor/models/deepseek_v2.py").read_text()
gate_source = Path(
"afd_plugin/model_executor/models/npu/deepseek_v2_attention_gate.py",
).read_text()
compute_ffn_output = source.split(
" def compute_ffn_output(",
1,
)[1].split("\n\n@native.support_torch_compile", 1)[0]
compute_moe = gate_source.split(
"def compute_attention_gate_moe_ffn(",
1,
)[1].split("\ndef _dequantize_int8_activation(", 1)[0]
assert "camp2p_stub_io_enabled()" not in source
assert "_log_ffn_compute_step(" not in compute_ffn_output
assert '"dense_mlp_begin"' not in compute_ffn_output
assert '"dense_scaling_begin"' not in compute_ffn_output
assert "_log_ffn_compute_step(" not in compute_moe
assert '"routed_scaling_begin"' not in compute_moe
assert '"shared_scaling_begin"' not in compute_moe