Repository navigation
Expand file tree
/
Copy pathtest_speculation.py
More file actions
1271 lines (1031 loc) · 40.1 KB
/
Copy pathtest_speculation.py
File metadata and controls
1271 lines (1031 loc) · 40.1 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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
from contextlib import closing
import builtins
import ctypes
import numpy as np
import pytest
import llama_cpp
from llama_cpp import llama_cpp as llama_cpp_lib
from llama_cpp import _internals as internals
from llama_cpp.llama_speculative import (
LlamaDFlashDecoding,
LlamaMTPDecoding,
LlamaNGramMapDecoding,
SpecConfig,
SpeculativeType,
create_native_spec_engine,
speculative_output_limits,
)
def test_llama_context_wraps_backend_sampling_and_perf(monkeypatch):
context = object.__new__(internals.LlamaContext)
context.ctx = "native-context"
context._sampler_refs = {}
sampler = type("Sampler", (), {"sampler": "native-sampler"})()
calls = []
monkeypatch.setattr(
internals.llama_cpp,
"llama_set_sampler",
lambda ctx, seq_id, ptr: calls.append(("set", ctx, seq_id, ptr)) or True,
)
assert context.set_sampler(2, sampler)
assert context._sampler_refs[2] is sampler
assert context.set_sampler(2, None)
assert 2 not in context._sampler_refs
pointer_results = {
"llama_get_sampled_probs_ith": object(),
"llama_get_sampled_logits_ith": object(),
"llama_get_sampled_candidates_ith": object(),
}
scalar_results = {
"llama_get_sampled_token_ith": 17,
"llama_get_sampled_probs_count_ith": 3,
"llama_get_sampled_logits_count_ith": 4,
"llama_get_sampled_candidates_count_ith": 5,
}
for name, result in {**pointer_results, **scalar_results}.items():
monkeypatch.setattr(
internals.llama_cpp,
name,
lambda ctx, index, result=result, name=name: (
calls.append((name, ctx, index)) or result
),
)
assert context.get_sampled_token_ith(-1) == 17
assert context.get_sampled_probs_count_ith(-1) == 3
assert context.get_sampled_logits_count_ith(-1) == 4
assert context.get_sampled_candidates_count_ith(-1) == 5
assert (
context.get_sampled_probs_ith(-1)
is pointer_results["llama_get_sampled_probs_ith"]
)
assert (
context.get_sampled_logits_ith(-1)
is pointer_results["llama_get_sampled_logits_ith"]
)
assert (
context.get_sampled_candidates_ith(-1)
is pointer_results["llama_get_sampled_candidates_ith"]
)
perf = llama_cpp_lib.llama_perf_context_data()
perf.n_eval = 9
monkeypatch.setattr(
internals.llama_cpp, "llama_perf_context", lambda ctx: perf
)
monkeypatch.setattr(
internals.llama_cpp,
"llama_perf_context_print",
lambda ctx: calls.append(("perf-print", ctx)),
)
monkeypatch.setattr(
internals.llama_cpp,
"llama_perf_context_reset",
lambda ctx: calls.append(("perf-reset", ctx)),
)
assert context.perf_context().n_eval == 9
context.print_timings()
context.reset_timings()
assert ("perf-print", "native-context") in calls
assert ("perf-reset", "native-context") in calls
context.ctx = None
def test_ngram_map_lifecycle_and_acceptance_feedback():
decoder = LlamaNGramMapDecoding(
ngram_size=3,
num_pred_tokens=3,
spec_type=SpeculativeType.NGRAM_MAP_K,
)
history = [1, 2, 3, 7, 8, 1, 2, 3]
decoder.begin(history)
draft = decoder.draft(history, n_past=len(history), id_last=3, n_max=2)
assert draft.tolist() == [7, 8]
decoder.accept(1)
draft = decoder.draft(history, n_past=len(history), id_last=3, n_max=3)
assert draft.tolist() == [7]
def test_spec_config_requires_draft_model_for_external_architectures():
for spec_type in (
SpeculativeType.DRAFT_EAGLE3,
SpeculativeType.DRAFT_DFLASH,
SpeculativeType.DRAFT_DSPARK,
):
with pytest.raises(ValueError, match="draft_model_path"):
SpecConfig(spec_type=spec_type).validate()
@pytest.mark.parametrize(
"spec_type",
[SpeculativeType.DRAFT_DFLASH, SpeculativeType.DRAFT_DSPARK],
)
def test_native_factory_routes_dflash_family_to_shared_engine(
monkeypatch, spec_type
):
created = []
def fake_engine(config, **kwargs):
created.append((config, kwargs))
return "dflash-engine"
monkeypatch.setattr(
"llama_cpp.llama_speculative.LlamaDFlashDecoding", fake_engine
)
config = SpecConfig(
spec_type=spec_type,
draft_model_path="draft.gguf",
)
result = create_native_spec_engine(
config,
target_model="target-model",
target_context="target-context",
model_params="model-params",
context_params="context-params",
verbose=False,
)
assert result == "dflash-engine"
assert created[0][0] is config
assert created[0][1]["target_model"] == "target-model"
def test_speculative_output_limits_match_llama_cpp():
assert speculative_output_limits(32, 1, 3) == (4, 4)
assert speculative_output_limits(32, 4, 3) == (16, 4)
assert speculative_output_limits(3, 4, 8) == (3, 3)
def test_draft_context_does_not_inherit_target_embedding_mode():
target = llama_cpp_lib.llama_context_default_params()
target.embeddings = True
target.pooling_type = llama_cpp_lib.LLAMA_POOLING_TYPE_MEAN
draft = LlamaMTPDecoding._copy_draft_context_params(
target, llama_cpp_lib.LLAMA_POOLING_TYPE_UNSPECIFIED
)
assert target.embeddings is True
assert target.pooling_type == llama_cpp_lib.LLAMA_POOLING_TYPE_MEAN
assert draft.embeddings is False
assert draft.pooling_type == llama_cpp_lib.LLAMA_POOLING_TYPE_UNSPECIFIED
def test_spec_config_validates_draft_runtime_arguments():
with pytest.raises(ValueError, match="draft_n_threads"):
SpecConfig(
spec_type=SpeculativeType.DRAFT_MTP,
draft_n_threads=0,
).validate()
with pytest.raises(ValueError, match="draft_n_cpu_moe"):
SpecConfig(
spec_type=SpeculativeType.DRAFT_MTP,
draft_n_cpu_moe=-1,
).validate()
class _FakeVocabModel:
def __init__(self, tokens, *, vocab_type=1, add_bos=True, add_eos=False):
self.tokens = tokens
self._vocab_type = vocab_type
self._add_bos = add_bos
self._add_eos = add_eos
def vocab_type(self):
return self._vocab_type
def get_add_bos(self):
return self._add_bos
def get_add_eos(self):
return self._add_eos
def token_bos(self):
return 1
def token_eos(self):
return 2
def n_vocab(self):
return len(self.tokens)
def token_get_text(self, token):
return self.tokens[token]
def test_speculative_vocab_compatibility_rejects_token_mismatch():
target = _FakeVocabModel([str(i) for i in range(8)])
draft = _FakeVocabModel([str(i) for i in range(8)])
draft.tokens[6] = "different"
with pytest.raises(ValueError, match="token 6"):
LlamaMTPDecoding._validate_speculative_vocab_compatibility(
target, draft
)
class _FakeCheckpointContext:
def __init__(self, *, position=7, state_size=8):
self.position = position
self.state_size = state_size
self.removals = []
self.get_flags = []
self.set_flags = []
def memory_seq_pos_max(self, seq_id):
assert seq_id == 0
return self.position
def memory_seq_rm(self, seq_id, p0, p1):
self.removals.append((seq_id, p0, p1))
return True
def get_state_seq_size_ext(self, seq_id, flags):
assert seq_id == 0
self.get_flags.append(flags)
return self.state_size
def get_state_seq_data_ext(self, buffer, size, seq_id, flags):
assert size == self.state_size
assert seq_id == 0
assert flags == self.get_flags[-1]
return size
def set_state_seq_data_ext(self, buffer, size, seq_id, flags):
assert buffer is not None
assert size == self.state_size
assert seq_id == 0
self.set_flags.append(flags)
return size
def _checkpoint_test_engine(*, native):
engine = object.__new__(LlamaMTPDecoding)
engine.draft_context = _FakeCheckpointContext()
engine.n_embd = 2
engine.pending_h = np.asarray([1.0, 2.0], dtype=np.float32)
engine.verify_h = np.empty((0, 2), dtype=np.float32)
engine.verify_tokens = []
engine.verify_positions = []
engine._use_native_draft_rollback = native
engine._pending_verification_checkpoint = None
engine.reset_checkpoint_stats()
return engine
def test_mtp_native_checkpoint_is_reused_for_verification():
engine = _checkpoint_test_engine(native=True)
checkpoint = engine.checkpoint()
engine.pending_h.fill(0.0)
engine.restore(checkpoint)
engine._pending_verification_checkpoint = checkpoint
reused = engine.take_verification_checkpoint()
stats = engine.checkpoint_stats()
assert reused is checkpoint
assert engine.draft_context.removals == [(0, 8, -1)]
np.testing.assert_array_equal(engine.pending_h, [1.0, 2.0])
assert stats["captures"] == 1
assert stats["restores"] == 1
assert stats["verification_reuses"] == 1
assert stats["native_captures"] == 1
assert stats["device_captures"] == 0
def test_mtp_checkpoint_fallback_keeps_state_on_device():
engine = _checkpoint_test_engine(native=False)
checkpoint = engine.checkpoint()
engine.restore(checkpoint)
stats = engine.checkpoint_stats()
expected_flags = (
llama_cpp_lib.LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY
| llama_cpp_lib.LLAMA_STATE_SEQ_FLAGS_ON_DEVICE
)
assert checkpoint["mode"] == "on-device"
assert engine.draft_context.get_flags == [expected_flags]
assert engine.draft_context.set_flags == [expected_flags]
assert stats["device_captures"] == 1
assert stats["device_restores"] == 1
assert stats["buffer_bytes"] == engine.draft_context.state_size
@pytest.mark.parametrize("engine_class", [LlamaMTPDecoding, LlamaDFlashDecoding])
@pytest.mark.parametrize("invalidate", ["capture", "clear", "restore_failure"])
def test_draft_checkpoint_rejects_invalidated_objects(engine_class, invalidate):
if engine_class is LlamaMTPDecoding:
engine = _checkpoint_test_engine(native=True)
else:
engine = object.__new__(engine_class)
engine.draft_context = _FakeCheckpointContext()
engine._use_native_draft_rollback = True
engine.n_embd_enc = 2
engine.verify_positions = []
engine.reset_checkpoint_stats()
engine._closed = False
engine._backend_sampler = None
checkpoint = engine.checkpoint()
if invalidate == "capture":
engine.checkpoint()
elif invalidate == "clear":
engine.draft_context.memory_clear = lambda data: None
engine.clear()
else:
engine.draft_context.memory_seq_rm = lambda *args: False
with pytest.raises(RuntimeError, match="rollback failed"):
engine.restore(checkpoint)
with pytest.raises(RuntimeError, match="stale"):
engine.restore(checkpoint)
def test_mtp_native_verification_rollback_removes_only_rejected_suffix():
engine = _checkpoint_test_engine(native=True)
engine.is_mem_shared = False
engine.verify_tokens = [10, 11, 12]
engine.verify_positions = [8, 9, 10]
engine.verify_h = np.asarray(
[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]], dtype=np.float32
)
engine.rollback_verified(engine.checkpoint(), n_accepted=1)
assert engine.draft_context.removals == [(0, 10, -1)]
np.testing.assert_array_equal(engine.pending_h, [2.0, 2.0])
stats = engine.checkpoint_stats()
assert stats["restores"] == 0
assert stats["native_verification_rollbacks"] == 1
def test_mtp_close_does_not_import_during_interpreter_shutdown():
class _Closable:
def __init__(self, *, context=None):
self.ctx = context
self.closed = False
self.sampler_changes = []
def set_sampler(self, seq_id, sampler):
assert self.ctx == "draft-context"
self.sampler_changes.append((seq_id, sampler))
return True
def close(self):
self.closed = True
engine = object.__new__(LlamaMTPDecoding)
engine._closed = False
engine._llama_cpp_lib = object()
engine._backend_sampler = _Closable()
engine._backend_sampling = True
engine.batch = _Closable()
engine.draft_context = _Closable(context="draft-context")
engine._owns_model = False
engine.draft_model = object()
backend_sampler = engine._backend_sampler
batch = engine.batch
draft_context = engine.draft_context
original_import = builtins.__import__
def fail_import(*args, **kwargs):
raise ImportError("sys.meta_path is None, Python is likely shutting down")
builtins.__import__ = fail_import
try:
engine.close()
finally:
builtins.__import__ = original_import
assert draft_context.sampler_changes == [(0, None)]
assert backend_sampler.closed
assert batch.closed
assert draft_context.closed
assert engine._backend_sampler is None
assert engine.draft_context is None
class _FakeEvalBatch:
def __init__(self):
self.batch = self
self.n_tokens = 0
class _FakeEvalContext:
def __init__(self, statuses, events):
self.statuses = list(statuses)
self.events = events
self.decode_sizes = []
def decode(self, batch):
self.decode_sizes.append(batch.batch.n_tokens)
self.events.append("target-decode")
return self.statuses.pop(0)
def synchronize(self):
self.events.append("target-sync")
class _FakeSpecEngine:
def __init__(self, events):
self.events = events
def process(self, batch, seq_id=0):
assert batch is not None
assert seq_id == 0
self.events.append("spec-process")
def _eval_test_llama(*, statuses, verifying, speculative=True):
events = []
llm = object.__new__(llama_cpp.Llama)
llm._batch = _FakeEvalBatch()
llm._ctx = _FakeEvalContext(statuses, events)
llm._speculative_verifying = verifying
llm.speculative = _FakeSpecEngine(events) if speculative else None
llm._active_speculative_phase_stats = {
"target_decode_seconds": 0.0,
"target_sync_seconds": 0.0,
"process_calls": 0,
"process_seconds": 0.0,
}
llm.n_tokens = 0
llm.verbose = False
return llm, events
def test_speculative_target_sync_precedes_process_and_has_separate_timing():
llm, events = _eval_test_llama(statuses=[0], verifying=True)
assert llm._decode_eval_batch([1, 2, 3], 3) == 3
llm._process_speculative_batch()
assert events == ["target-decode", "target-sync", "spec-process"]
stats = llm._active_speculative_phase_stats
assert stats["target_decode_seconds"] >= 0.0
assert stats["target_sync_seconds"] >= 0.0
assert stats["process_seconds"] >= 0.0
assert stats["process_calls"] == 1
def test_speculative_verification_batch_is_not_dynamically_split():
llm, _ = _eval_test_llama(statuses=[1, 0], verifying=True)
with pytest.raises(RuntimeError, match="verification batch cannot be split"):
llm._decode_eval_batch([1, 2, 3, 4], 4)
assert llm._ctx.decode_sizes == [4]
def test_ordinary_eval_batch_can_still_retry_at_a_smaller_size():
llm, _ = _eval_test_llama(
statuses=[1, 0], verifying=False, speculative=False
)
assert llm._decode_eval_batch([1, 2, 3, 4], 4) == 2
assert llm._ctx.decode_sizes == [4, 2]
def test_speculative_verification_batch_must_fit_n_batch():
llm = object.__new__(llama_cpp.Llama)
llm._speculative_verifying = True
llm.n_batch = 3
with pytest.raises(RuntimeError, match="verification batch exceeds n_batch"):
llm.eval([1, 2, 3, 4], copy_logits=False)
def test_interrupted_hybrid_speculation_uses_native_rollback():
class _Context:
def __init__(self):
self.removals = []
def memory_seq_rm(self, seq_id, p0, p1):
self.removals.append((seq_id, p0, p1))
return True
class _Speculative:
def __init__(self):
self.rollbacks = []
def rollback_verified(self, checkpoint, n_accepted, seq_id=0):
self.rollbacks.append((checkpoint, n_accepted, seq_id))
llm = object.__new__(llama_cpp.Llama)
llm._ctx = _Context()
llm.speculative = _Speculative()
llm.is_hybrid = True
llm.n_tokens = 9
llm._last_eval_output_start = 6
llm._last_eval_output_count = 3
mode = llm._recover_interrupted_speculation(
verification_start=6,
evaluated_tokens=[10, 11, 12],
delivered_accepted=1,
speculative_checkpoint="draft-checkpoint",
use_native_rollback=True,
active_loras=None,
control_vector=None,
)
assert mode == "native"
assert llm._ctx.removals == [(0, 8, -1)]
assert llm.speculative.rollbacks == [("draft-checkpoint", 1, 0)]
assert llm.n_tokens == 8
assert llm._last_eval_output_start == 6
assert llm._last_eval_output_count == 2
def test_interrupted_hybrid_speculation_restores_checkpoint_and_replays():
class _Checkpoint:
pos = 6
class _Cache:
def __init__(self):
self.checkpoint = _Checkpoint()
self.lookups = []
self.restores = []
def find_best_checkpoint(self, tokens, seq_id=0):
self.lookups.append((tokens, seq_id))
return self.checkpoint
def restore_checkpoint(self, checkpoint, seq_id=0):
self.restores.append((checkpoint, seq_id))
return True
class _Speculative:
def __init__(self):
self.restores = []
def restore(self, checkpoint, seq_id=0):
self.restores.append((checkpoint, seq_id))
llm = object.__new__(llama_cpp.Llama)
llm.speculative = _Speculative()
llm._hybrid_cache_mgr = _Cache()
llm.is_hybrid = True
llm.input_ids = np.asarray([1, 2, 3, 4, 5, 6, 10, 11, 12], dtype=np.intc)
llm.n_tokens = 9
replayed = []
def eval_tokens(tokens, **kwargs):
replayed.append((list(tokens), kwargs))
llm.n_tokens += len(tokens)
llm.eval = eval_tokens
active_loras = [{"name": "test", "scale": 0.5}]
control_vector = {"data": [1.0]}
mode = llm._recover_interrupted_speculation(
verification_start=6,
evaluated_tokens=[10, 11, 12],
delivered_accepted=1,
speculative_checkpoint="draft-checkpoint",
use_native_rollback=False,
active_loras=active_loras,
control_vector=control_vector,
)
assert mode == "checkpoint"
assert llm._hybrid_cache_mgr.lookups == [([1, 2, 3, 4, 5, 6], 0)]
assert llm._hybrid_cache_mgr.restores == [
(llm._hybrid_cache_mgr.checkpoint, 0)
]
assert llm.speculative.restores == [("draft-checkpoint", 0)]
assert replayed == [
(
[10, 11],
{
"active_loras": active_loras,
"control_vector": control_vector,
"copy_logits": False,
},
)
]
assert llm.n_tokens == 8
def test_interrupted_non_hybrid_speculation_truncates_both_contexts():
class _Context:
def __init__(self):
self.removals = []
def memory_seq_rm(self, seq_id, p0, p1):
self.removals.append((seq_id, p0, p1))
return True
class _Speculative:
def __init__(self):
self.truncations = []
def truncate(self, position, seq_id=0):
self.truncations.append((position, seq_id))
llm = object.__new__(llama_cpp.Llama)
llm._ctx = _Context()
llm.speculative = _Speculative()
llm.is_hybrid = False
llm.n_tokens = 9
llm._last_eval_output_start = 6
llm._last_eval_output_count = 3
mode = llm._recover_interrupted_speculation(
verification_start=6,
evaluated_tokens=[10, 11, 12],
delivered_accepted=0,
speculative_checkpoint=None,
use_native_rollback=False,
active_loras=None,
control_vector=None,
)
assert mode == "truncate"
assert llm._ctx.removals == [(0, 7, -1)]
assert llm.speculative.truncations == [(7, 0)]
assert llm.n_tokens == 7
assert llm._last_eval_output_count == 1
def test_mtp_truncate_fails_when_native_memory_removal_fails():
class _Model:
def is_recurrent(self):
return False
def is_hybrid(self):
return False
class _Context:
def memory_seq_rm(self, seq_id, p0, p1):
return False
engine = object.__new__(LlamaMTPDecoding)
engine.draft_model = _Model()
engine.draft_context = _Context()
with pytest.raises(RuntimeError, match="draft-context truncation failed"):
engine.truncate(12)
class _FakeDFlashBatch:
def __init__(self):
self.tokens = []
self.positions = []
self.embeddings = None
self.logits = []
self.mrope_positions = None
def reset(self):
self.tokens = []
self.positions = []
self.embeddings = None
self.logits = []
self.mrope_positions = None
def add_sequence(
self, token_array, pos_array, seq_ids, logits_array
):
assert seq_ids == [0]
assert len(token_array) == len(pos_array) == len(logits_array)
self.tokens = list(token_array)
self.positions = list(pos_array)
self.logits = list(logits_array)
def add_embeddings(
self, embeddings, *, pos_array, seq_ids, logits_array=None
):
assert seq_ids == [0]
self.embeddings = np.asarray(embeddings, dtype=np.float32).copy()
self.positions = list(pos_array)
self.logits = list(logits_array or [])
def add_embeddings_mrope(
self, embeddings, *, pos_array, seq_ids, logits_array=None
):
self.add_embeddings(
embeddings,
pos_array=pos_array[0],
seq_ids=seq_ids,
logits_array=logits_array,
)
self.mrope_positions = [list(plane) for plane in pos_array]
class _FakeDFlashDraftContext:
def __init__(self, *, fused=None, confidence=None):
self.fused = fused
self.confidence = confidence
self.decoded = []
self.synchronized = False
self.removals = []
def n_ubatch(self):
return 8
def decode(self, batch):
self.decoded.append(
(list(batch.tokens), list(batch.positions), batch.embeddings)
)
return 0
def get_embeddings_nextn(self):
values = self.confidence if self.confidence is not None else self.fused
return values.ctypes.data_as(
ctypes.POINTER(ctypes.c_float)
)
def synchronize(self):
self.synchronized = True
def memory_seq_rm(self, seq_id, p0, p1):
self.removals.append((seq_id, p0, p1))
return True
def _draft_test_dflash_engine(*, dspark, sample_from_anchor, p_min=0.0):
engine = object.__new__(LlamaDFlashDecoding)
engine.config = SpecConfig(
spec_type=(
SpeculativeType.DRAFT_DSPARK
if dspark
else SpeculativeType.DRAFT_DFLASH
),
draft_model_path="draft.gguf",
draft_n_max=3,
draft_p_min=p_min,
)
engine.is_dspark = dspark
engine.is_dflash2 = False
engine.selector_top_k = 0
engine.sample_from_anchor = sample_from_anchor
engine.draft_limit = 3
engine.draft_min = engine.config.draft_n_min
engine.mask_token_id = 99
engine.n_embd_dec = 2
engine.noise_batch = _FakeDFlashBatch()
engine._pending_verification_checkpoint = None
engine._candidate = lambda output_index: (100 + output_index, 0.9)
engine.checkpoint = lambda seq_id=0: {"position": 5}
engine.restore = lambda checkpoint, seq_id=0: None
return engine
def test_dflash_builds_anchor_plus_mask_block_and_reads_mask_outputs():
engine = _draft_test_dflash_engine(
dspark=False, sample_from_anchor=True, p_min=0.1
)
engine.draft_context = _FakeDFlashDraftContext()
result = engine.draft([], n_past=7, id_last=42, n_max=3)
assert engine.noise_batch.tokens == [42, 99, 99, 99]
assert engine.noise_batch.positions == [7, 8, 9, 10]
assert engine.noise_batch.logits == [True, True, True, True]
assert result.tolist() == [101, 102, 103]
assert engine._pending_verification_checkpoint == {"position": 5}
def test_dspark_anchor_layout_uses_confidence_to_truncate_block():
engine = _draft_test_dflash_engine(
dspark=True, sample_from_anchor=True, p_min=0.5
)
confidence = np.asarray(
[[0.9, 0.9], [0.4, 0.4], [0.8, 0.8]], dtype=np.float32
)
engine.draft_context = _FakeDFlashDraftContext(confidence=confidence)
result = engine.draft([], n_past=7, id_last=42, n_max=3)
assert engine.noise_batch.tokens == [42, 99, 99]
assert result.tolist() == [100]
def test_dflash_uses_block_clamped_minimum_draft_length():
engine = _draft_test_dflash_engine(
dspark=False, sample_from_anchor=True, p_min=0.0
)
engine.config.draft_n_min = 3
engine.draft_limit = 1
engine.draft_min = 1
engine.draft_context = _FakeDFlashDraftContext()
result = engine.draft([], n_past=7, id_last=42, n_max=3)
assert result.tolist() == [101]
def test_dflash2_walks_selector_lattice_without_requesting_logits():
engine = _draft_test_dflash_engine(
dspark=False, sample_from_anchor=True, p_min=0.0
)
engine.is_dflash2 = True
engine.selector_top_k = 2
engine.n_embd_dec = 6
lattice = np.asarray(
[
[0, 0, 0, 0, 0, 0],
[101, 102, 0, 2, 0, 2],
[201, 202, 0, 0, 3, 1],
[301, 302, 0, 4, 0, 0],
],
dtype=np.float32,
)
engine.draft_context = _FakeDFlashDraftContext(confidence=lattice)
result = engine.draft([], n_past=7, id_last=42, n_max=3)
assert engine.noise_batch.tokens == [42, 99, 99, 99]
assert engine.noise_batch.logits == [False, False, False, False]
assert result.tolist() == [102, 201, 302]
assert engine._pending_verification_checkpoint == {"position": 5}
def test_dflash2_selector_probability_truncates_before_low_confidence_token():
engine = _draft_test_dflash_engine(
dspark=False, sample_from_anchor=True, p_min=0.8
)
engine.is_dflash2 = True
engine.selector_top_k = 2
engine.n_embd_dec = 6
lattice = np.asarray(
[
[0, 0, 0, 0, 0, 0],
[101, 102, 0, 3, 0, 3],
[201, 202, 0, 0, 0, 1],
[301, 302, 0, 0, 0, 0],
],
dtype=np.float32,
)
engine.draft_context = _FakeDFlashDraftContext(confidence=lattice)
result = engine.draft([], n_past=7, id_last=42, n_max=3)
assert result.tolist() == [102]
@pytest.mark.parametrize(
("is_dflash2", "expected_masked", "expected_backend_calls"),
[(False, True, 1), (True, False, 0)],
)
def test_dflash2_configures_unmasked_nextn_without_backend_sampling(
is_dflash2, expected_masked, expected_backend_calls
):
engine = object.__new__(LlamaDFlashDecoding)
engine.is_dflash2 = is_dflash2
engine.causal_attn = True
engine.target_layer_ids = [2, 5]
backend_calls = []
engine._enable_backend_sampling = lambda internals: backend_calls.append(
internals
)
class _TargetContext:
def __init__(self):
self.layers = []
def set_embeddings_layer_inp(self, layer_id, enabled):
self.layers.append((layer_id, enabled))
class _DraftContext:
def __init__(self):
self.nextn = []
self.causal = []
def set_embeddings_nextn(self, enabled, masked):
self.nextn.append((enabled, masked))
def set_causal_attn(self, enabled):
self.causal.append(enabled)
engine.target_context = _TargetContext()
engine.draft_context = _DraftContext()
internals = object()
engine._configure_draft_execution(internals)
assert len(backend_calls) == expected_backend_calls
assert engine.target_context.layers == [(2, True), (5, True)]
assert engine.draft_context.nextn == [(True, expected_masked)]
assert engine.draft_context.causal == [True]
def test_dspark_probability_filter_requires_confidence_head():
metadata = {"dflash.has_confidence_head": "false"}
with pytest.raises(ValueError, match="no confidence head"):
LlamaDFlashDecoding._validate_dspark_confidence_head(metadata, 0.1)
LlamaDFlashDecoding._validate_dspark_confidence_head(metadata, 0.0)
def test_dflash_process_interleaves_target_layers_for_fused_injection():
engine = object.__new__(LlamaDFlashDecoding)
engine.target_layer_ids = [1, 3]
engine.n_embd_tgt = 2
engine.n_embd_enc = 4
layer_1 = np.asarray([[1, 2], [3, 4]], dtype=np.float32)
layer_3 = np.asarray([[10, 20], [30, 40]], dtype=np.float32)
class _TargetContext:
def get_embeddings_layer_inp(self, layer_id):
values = {1: layer_1, 3: layer_3}[layer_id]
return values.ctypes.data_as(
ctypes.POINTER(ctypes.c_float)
)
engine.target_context = _TargetContext()
engine.draft_context = _FakeDFlashDraftContext()
engine.inject_batch = _FakeDFlashBatch()
engine.verify_positions = []
engine.is_mrope = False
class _TargetBatch:
n_tokens = 2
token = [11, 12]
embd = None
pos = [4, 5]
n_seq_id = [1, 1]
seq_id = [[0], [0]]
engine.process(_TargetBatch())
np.testing.assert_array_equal(
engine.draft_context.decoded[0][2].reshape(2, 4),
[[1, 2, 10, 20], [3, 4, 30, 40]],
)
assert engine.draft_context.decoded[0][1] == [4, 5]
assert engine.verify_positions == [4, 5]
np.testing.assert_array_equal(
engine.verify_features,
[[1, 2, 10, 20], [3, 4, 30, 40]],
)
assert engine.inject_batch.logits == []
def test_dflash_mrope_process_uses_target_positions_for_fused_injection():
engine = object.__new__(LlamaDFlashDecoding)
engine.target_layer_ids = [1]
engine.n_embd_tgt = 2
engine.n_embd_enc = 2
engine.n_embd_dec = 2
engine.is_mrope = True
target_rows = np.asarray([[1, 2], [3, 4]], dtype=np.float32)
class _TargetContext:
def get_embeddings_layer_inp(self, layer_id):
assert layer_id == 1
return target_rows.ctypes.data_as(
ctypes.POINTER(ctypes.c_float)
)
engine.target_context = _TargetContext()
engine.draft_context = _FakeDFlashDraftContext()
engine.inject_batch = _FakeDFlashBatch()
engine.verify_positions = []
class _TargetBatch:
n_tokens = 2
token = [11, 12]
embd = None
pos = [4, 5]