-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_acceleration.py
More file actions
448 lines (359 loc) · 15 KB
/
Copy pathtest_acceleration.py
File metadata and controls
448 lines (359 loc) · 15 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
"""
test_acceleration.py — AccelerationStrategy 抽象层测试
来源:熔炉 #33(2026-05-23)
覆盖:策略可用性、加速估算、适配度评分、自动选择、语言检测、安全回退
"""
import pytest
from acceleration import (
AccelerationContext,
AccelerationOrchestrator,
AccelerationResult,
DFlashStrategy,
Hardware,
Language,
MTPStrategy,
NoneStrategy,
PipelineStrategy,
StrategyName,
TaskType,
auto_accelerate,
detect_language,
)
# ─── Fixtures ───────────────────────────────────────────
@pytest.fixture
def dflash() -> DFlashStrategy:
return DFlashStrategy()
@pytest.fixture
def mtp() -> MTPStrategy:
return MTPStrategy()
@pytest.fixture
def pipeline() -> PipelineStrategy:
return PipelineStrategy()
@pytest.fixture
def none_strat() -> NoneStrategy:
return NoneStrategy()
@pytest.fixture
def orch() -> AccelerationOrchestrator:
return AccelerationOrchestrator()
# ─── DFlash Tests ───────────────────────────────────────
class TestDFlash:
def test_available_on_apple_silicon(self, dflash: DFlashStrategy):
ctx = AccelerationContext(hardware=Hardware.APPLE_SILICON)
assert dflash.is_available(ctx) is True
def test_not_available_on_nvidia(self, dflash: DFlashStrategy):
ctx = AccelerationContext(hardware=Hardware.NVIDIA_GPU)
assert dflash.is_available(ctx) is False
def test_not_available_on_unknown(self, dflash: DFlashStrategy):
ctx = AccelerationContext(hardware=Hardware.UNKNOWN)
assert dflash.is_available(ctx) is False
def test_speedup_chinese_penalty(self, dflash: DFlashStrategy):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.CHINESE,
)
assert dflash.estimate_speedup(ctx) == 0.81
def test_speedup_code_peak(self, dflash: DFlashStrategy):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.CODE,
)
assert dflash.estimate_speedup(ctx) == 7.7
def test_speedup_mixed_average(self, dflash: DFlashStrategy):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.MIXED,
)
assert dflash.estimate_speedup(ctx) == 4.08
def test_suitability_chinese_low(self, dflash: DFlashStrategy):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.CHINESE,
)
assert dflash.suitability_score(ctx) == 0.1
def test_suitability_code_high(self, dflash: DFlashStrategy):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.CODE,
)
assert dflash.suitability_score(ctx) == 0.95
def test_suitability_unavailable_zero(self, dflash: DFlashStrategy):
ctx = AccelerationContext(hardware=Hardware.NVIDIA_GPU)
assert dflash.suitability_score(ctx) == 0.0
# ─── MTP Tests ──────────────────────────────────────────
class TestMTP:
def test_available_deepseek_on_nvidia(self, mtp: MTPStrategy):
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
model_name="deepseek-v3-0324",
)
assert mtp.is_available(ctx) is True
def test_available_gemma4_on_nvidia(self, mtp: MTPStrategy):
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
model_name="gemma-4-31b",
)
assert mtp.is_available(ctx) is True
def test_not_available_on_apple_silicon(self, mtp: MTPStrategy):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
model_name="deepseek-v3",
)
assert mtp.is_available(ctx) is False
def test_not_available_unsupported_model(self, mtp: MTPStrategy):
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
model_name="qwen3-4b",
)
assert mtp.is_available(ctx) is False
def test_speedup_moe_batch1_low(self, mtp: MTPStrategy):
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
model_name="deepseek-v3-moe",
batch_size=1,
)
assert mtp.estimate_speedup(ctx) == 1.1
def test_speedup_normal(self, mtp: MTPStrategy):
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
model_name="deepseek-v3",
batch_size=4,
)
assert mtp.estimate_speedup(ctx) == 2.0
def test_suitability_chinese_high(self, mtp: MTPStrategy):
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
model_name="deepseek-v3",
language=Language.CHINESE,
)
assert mtp.suitability_score(ctx) == 0.85
# ─── Pipeline Tests ─────────────────────────────────────
class TestPipeline:
def test_available_on_apple_silicon(self, pipeline: PipelineStrategy):
ctx = AccelerationContext(hardware=Hardware.APPLE_SILICON)
assert pipeline.is_available(ctx) is True
def test_not_available_on_nvidia(self, pipeline: PipelineStrategy):
ctx = AccelerationContext(hardware=Hardware.NVIDIA_GPU)
assert pipeline.is_available(ctx) is False
def test_suitability_small_model_zero(self, pipeline: PipelineStrategy):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
model_name="qwen3-4b",
)
assert pipeline.suitability_score(ctx) == 0.0
def test_suitability_large_model(self, pipeline: PipelineStrategy):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
model_name="qwen3-72b",
)
assert pipeline.suitability_score(ctx) > 0.0
def test_speedup_48x(self, pipeline: PipelineStrategy):
ctx = AccelerationContext(hardware=Hardware.APPLE_SILICON)
assert pipeline.estimate_speedup(ctx) == 48.0
# ─── None Strategy Tests ────────────────────────────────
class TestNoneStrategy:
def test_always_available(self, none_strat: NoneStrategy):
for hw in Hardware:
ctx = AccelerationContext(hardware=hw)
assert none_strat.is_available(ctx) is True
def test_speedup_always_1(self, none_strat: NoneStrategy):
ctx = AccelerationContext()
assert none_strat.estimate_speedup(ctx) == 1.0
def test_suitability_low(self, none_strat: NoneStrategy):
ctx = AccelerationContext()
assert none_strat.suitability_score(ctx) == 0.3
# ─── Orchestrator Tests ────────────────────────────────
class TestOrchestrator:
def test_mac_code_prefers_dflash(self, orch: AccelerationOrchestrator):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.CODE,
task_type=TaskType.CODE_GEN,
model_name="qwen3-4b",
)
result = orch.select(ctx)
assert result.strategy == StrategyName.DFLASH
assert result.estimated_speedup == 7.7
def test_nvidia_deepseek_prefers_mtp(self, orch: AccelerationOrchestrator):
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
language=Language.CHINESE,
model_name="deepseek-v3",
batch_size=4,
)
result = orch.select(ctx)
assert result.strategy == StrategyName.MTP
def test_mac_chinese_avoids_dflash(self, orch: AccelerationOrchestrator):
"""中文场景 DFlash 0.81× → 自动回退到 None。"""
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.CHINESE,
model_name="qwen3-4b",
)
result = orch.select(ctx)
# DFlash score=0.1 but speedup=0.81 → fallback to None
assert result.strategy == StrategyName.NONE
assert result.estimated_speedup == 1.0
def test_mac_large_model_prefers_pipeline(self, orch: AccelerationOrchestrator):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.ENGLISH,
model_name="qwen3-72b",
)
result = orch.select(ctx)
assert result.strategy == StrategyName.PIPELINE
def test_unknown_hardware_fallback_none(self, orch: AccelerationOrchestrator):
ctx = AccelerationContext(
hardware=Hardware.UNKNOWN,
model_name="qwen3-4b",
)
result = orch.select(ctx)
assert result.strategy == StrategyName.NONE
def test_nvidia_unsupported_model_fallback_none(self, orch: AccelerationOrchestrator):
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
model_name="qwen3-4b",
)
result = orch.select(ctx)
assert result.strategy == StrategyName.NONE
def test_override_dflash(self, orch: AccelerationOrchestrator):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
model_name="qwen3-4b",
)
result = orch.select_with_override(ctx, StrategyName.DFLASH)
assert result.strategy == StrategyName.DFLASH
assert "override" in result.reason.lower()
def test_override_unavailable_fallback(self, orch: AccelerationOrchestrator):
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
model_name="qwen3-4b",
)
result = orch.select_with_override(ctx, StrategyName.DFLASH)
assert result.strategy == StrategyName.NONE
assert result.confidence == 0.0
def test_confidence_high_when_clear_winner(self, orch: AccelerationOrchestrator):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.CODE,
model_name="qwen3-4b",
)
result = orch.select(ctx)
assert result.confidence > 0.7
def test_config_hint_dflash(self, orch: AccelerationOrchestrator):
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.CODE,
model_name="qwen3-4b",
)
result = orch.select(ctx)
assert "block_size" in result.config_hint
assert result.config_hint["block_size"] == 16
def test_config_hint_mtp(self, orch: AccelerationOrchestrator):
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
model_name="deepseek-v3",
batch_size=4,
)
result = orch.select(ctx)
assert "method" in result.config_hint
def test_config_hint_none(self):
result = AccelerationResult(
strategy=StrategyName.NONE,
reason="test",
)
assert result.config_hint == {}
# ─── Language Detection Tests ───────────────────────────
class TestLanguageDetection:
def test_chinese(self):
assert detect_language("你好,世界") == Language.CHINESE
def test_english(self):
assert detect_language("Hello world") == Language.ENGLISH
def test_code(self):
assert detect_language("def foo():\n return 42") == Language.CODE
def test_mixed(self):
assert detect_language("Hello 世界") == Language.MIXED
def test_empty(self):
assert detect_language("") == Language.UNKNOWN
def test_code_with_backticks(self):
assert detect_language("```python\nprint('hi')\n```") == Language.CODE
# ─── Auto Accelerate Convenience Tests ──────────────────
class TestAutoAccelerate:
def test_mac_code_auto(self):
result = auto_accelerate(
hardware="apple_silicon",
model_name="qwen3-4b",
text="def hello(): pass",
task_type="code_gen",
)
assert result.strategy == StrategyName.DFLASH
def test_nvidia_deepseek_auto(self):
result = auto_accelerate(
hardware="nvidia_gpu",
model_name="deepseek-v3",
text="你好世界",
task_type="chat",
batch_size=4,
)
assert result.strategy == StrategyName.MTP
def test_chinese_mac_safety_fallback(self):
"""中文+Mac → DFlash减速 → 自动回退None。"""
result = auto_accelerate(
hardware="apple_silicon",
model_name="qwen3-4b",
text="今天天气很好",
task_type="chat",
)
assert result.strategy == StrategyName.NONE
assert result.estimated_speedup == 1.0
# ─── Boundary / Edge Case Tests ────────────────────────
class TestBoundaryConditions:
def test_empty_model_name(self):
result = auto_accelerate(
hardware="apple_silicon",
model_name="",
text="hello",
)
assert result.strategy in (StrategyName.DFLASH, StrategyName.NONE)
def test_long_text(self):
"""超长文本不应崩溃。"""
text = "你好世界" * 5000 # ~20K chars
result = auto_accelerate(
hardware="apple_silicon",
model_name="qwen3-4b",
text=text,
)
assert result.strategy in (StrategyName.DFLASH, StrategyName.NONE, StrategyName.PIPELINE)
def test_special_chars(self):
result = auto_accelerate(
hardware="apple_silicon",
model_name="qwen3-4b",
text="!@#$%^&*()_+-=[]{}|;':\",./<>?",
)
assert result.strategy in (StrategyName.DFLASH, StrategyName.NONE, StrategyName.PIPELINE)
def test_none_model_name(self):
result = auto_accelerate(
hardware="apple_silicon",
model_name="",
text="hello",
)
assert result is not None
def test_override_dflash_chinese_warns_slowdown(self, orch: AccelerationOrchestrator):
"""用户强制 DFlash 在中文场景应返回 speedup<1.0 且带警告。"""
ctx = AccelerationContext(
hardware=Hardware.APPLE_SILICON,
language=Language.CHINESE,
model_name="qwen3-4b",
)
result = orch.select_with_override(ctx, StrategyName.DFLASH)
assert result.strategy == StrategyName.DFLASH
assert result.estimated_speedup == 0.81
assert "WARNING" in result.reason
def test_mtp_model_registration(self):
"""自定义 MTP 模型注册。"""
MTPStrategy.register_mtp_model("my-custom-mtp-model")
mtp = MTPStrategy()
ctx = AccelerationContext(
hardware=Hardware.NVIDIA_GPU,
model_name="my-custom-mtp-model",
)
assert mtp.is_available(ctx) is True