-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhidden_decoding.patch
More file actions
1176 lines (1126 loc) · 51.2 KB
/
Copy pathhidden_decoding.patch
File metadata and controls
1176 lines (1126 loc) · 51.2 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
diff --git a/python/sglang/srt/layers/attention/flashattention_backend.py b/python/sglang/srt/layers/attention/flashattention_backend.py
index f7ca5e203..0360fd1e8 100644
--- a/python/sglang/srt/layers/attention/flashattention_backend.py
+++ b/python/sglang/srt/layers/attention/flashattention_backend.py
@@ -352,6 +352,11 @@ class FlashAttentionBackend(AttentionBackend):
self.sliding_window_size is not None and self.sliding_window_size > -1
)
+ self.scale_seq_factor = getattr(
+ model_runner.model_config.hf_config, "scale_seq_times", 0
+ ) + 1
+ self.scale_seq_metadata = {}
+
def init_forward_metadata(self, forward_batch: ForwardBatch):
"""Initialize forward metadata hence all layers in the forward pass can reuse it."""
metadata = FlashAttentionMetadata()
@@ -447,7 +452,7 @@ class FlashAttentionBackend(AttentionBackend):
]
# TODO: we need to test this part for llama 4 eagle case
self._init_local_attn_metadata(forward_batch, metadata, device)
- elif forward_batch.forward_mode.is_target_verify():
+ elif forward_batch.forward_mode.is_target_verify() and forward_batch.spec_info is not None:
if self.topk <= 1:
metadata.cache_seqlens_int32 = (
forward_batch.seq_lens + self.speculative_num_draft_tokens
@@ -574,7 +579,13 @@ class FlashAttentionBackend(AttentionBackend):
metadata, metadata_expand
)
- elif forward_batch.forward_mode.is_extend_or_draft_extend_or_mixed():
+ elif (
+ forward_batch.forward_mode.is_extend_or_draft_extend_or_mixed()
+ or (
+ forward_batch.forward_mode.is_target_verify()
+ and forward_batch.spec_info is None
+ )
+ ):
metadata.cache_seqlens_int32 = seqlens_in_batch.to(torch.int32)
metadata.max_seq_len_k = forward_batch.seq_lens_cpu.max().item()
metadata.cu_seqlens_k = torch.nn.functional.pad(
@@ -587,6 +598,7 @@ class FlashAttentionBackend(AttentionBackend):
if (
any(forward_batch.extend_prefix_lens_cpu)
or forward_batch.forward_mode == ForwardMode.DRAFT_EXTEND
+ or forward_batch.forward_mode == ForwardMode.TARGET_VERIFY
):
extend_seq_lens = forward_batch.extend_seq_lens
metadata.max_seq_len_q = max(forward_batch.extend_seq_lens_cpu)
@@ -1430,6 +1442,33 @@ class FlashAttentionBackend(AttentionBackend):
),
}
+ if self.scale_seq_factor > 1:
+ scale = self.scale_seq_factor
+ self.scale_seq_metadata = {
+ "cache_seqlens": torch.zeros(
+ max_bs, dtype=torch.int32, device=self.device
+ ),
+ "cu_seqlens_q": torch.arange(
+ 0,
+ max_bs * scale + 1,
+ step=scale,
+ dtype=torch.int32,
+ device=self.device,
+ ),
+ "cu_seqlens_k": torch.zeros(
+ max_bs + 1, dtype=torch.int32, device=self.device
+ ),
+ "page_table": torch.zeros(
+ max_bs,
+ max_num_pages,
+ dtype=torch.int32,
+ device=self.device,
+ ),
+ "strided_indices": torch.arange(
+ 0, self.max_context_len, self.page_size, device=self.device
+ ),
+ }
+
self.encoder_metadata = {
"encoder_page_table": torch.zeros(
max_bs,
@@ -1551,7 +1590,44 @@ class FlashAttentionBackend(AttentionBackend):
self._update_local_attn_metadata_for_capture(metadata, batch_size)
elif forward_mode.is_target_verify():
- if self.topk <= 1:
+ if spec_info is None and self.scale_seq_factor > 1:
+ # scale_seq decode: seq_lens already includes the scale increment
+ scale = self.scale_seq_factor
+ metadata.cache_seqlens_int32 = self.scale_seq_metadata[
+ "cache_seqlens"
+ ][:bs]
+ metadata.cache_seqlens_int32.copy_(seq_lens.to(torch.int32))
+
+ metadata.max_seq_len_q = scale
+ metadata.max_seq_len_k = seq_lens.max().item()
+
+ metadata.cu_seqlens_q = self.scale_seq_metadata["cu_seqlens_q"][
+ : bs + 1
+ ]
+
+ metadata.cu_seqlens_k = self.scale_seq_metadata["cu_seqlens_k"][
+ : (bs + 1)
+ ]
+
+ max_seq_pages = (
+ metadata.max_seq_len_k + self.page_size - 1
+ ) // self.page_size
+ metadata.page_table = self.scale_seq_metadata["page_table"][:bs, :]
+ normal_decode_set_metadata(
+ metadata.cache_seqlens_int32,
+ metadata.cu_seqlens_k,
+ metadata.page_table,
+ self.req_to_token,
+ req_pool_indices,
+ self.scale_seq_metadata["strided_indices"],
+ max_seq_pages,
+ seq_lens,
+ 0,
+ self.page_size,
+ )
+
+ self.scale_seq_metadata[bs] = metadata
+ elif self.topk <= 1:
metadata.cache_seqlens_int32 = self.target_verify_metadata[
"cache_seqlens"
][:bs]
@@ -1780,7 +1856,28 @@ class FlashAttentionBackend(AttentionBackend):
bs,
)
elif forward_mode.is_target_verify():
- if self.topk <= 1:
+ if spec_info is None and self.scale_seq_factor > 1:
+ # scale_seq decode replay
+ metadata = self.scale_seq_metadata[bs]
+ metadata.cache_seqlens_int32.copy_(seq_lens.to(torch.int32))
+
+ max_len = seq_lens_cpu.max().item()
+ metadata.max_seq_len_k = max_len
+ max_seq_pages = (max_len + self.page_size - 1) // self.page_size
+
+ normal_decode_set_metadata(
+ metadata.cache_seqlens_int32,
+ metadata.cu_seqlens_k,
+ metadata.page_table,
+ self.req_to_token,
+ req_pool_indices,
+ self.scale_seq_metadata["strided_indices"],
+ max_seq_pages,
+ seq_lens,
+ 0,
+ self.page_size,
+ )
+ elif self.topk <= 1:
metadata = self.target_verify_metadata[bs]
metadata.cache_seqlens_int32.copy_(
(seq_lens + self.speculative_num_draft_tokens)
diff --git a/python/sglang/srt/managers/schedule_batch.py b/python/sglang/srt/managers/schedule_batch.py
index a35ba0253..6e7adaea7 100644
--- a/python/sglang/srt/managers/schedule_batch.py
+++ b/python/sglang/srt/managers/schedule_batch.py
@@ -1133,14 +1133,20 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
len(self.out_cache_loc) == self.extend_num_tokens
), f"Expected {len(self.out_cache_loc)}, got {self.extend_num_tokens}"
+ def _get_scale_seq_factor(self):
+ """Return (scale_seq_times + 1). Returns 1 when scale_seq is disabled."""
+ return getattr(self.model_config.hf_config, "scale_seq_times", 0) + 1
+
def prepare_for_extend(self):
self.forward_mode = ForwardMode.EXTEND
+ scale = self._get_scale_seq_factor()
+
# Allocate req slots
bs = len(self.reqs)
req_pool_indices = self.alloc_req_slots(bs)
- # Init tensors
+ # Init tensors (logical, un-expanded values)
reqs = self.reqs
input_ids = [r.fill_ids[len(r.prefix_indices) :] for r in reqs]
extend_num_tokens = sum(len(ids) for ids in input_ids)
@@ -1149,6 +1155,12 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
prefix_lens = [len(r.prefix_indices) for r in reqs]
extend_lens = [r.extend_input_len for r in reqs]
+ # Expanded values for KV cache / attention (multiply by scale)
+ expanded_seq_lens = [s * scale for s in seq_lens]
+ expanded_prefix_lens = [p * scale for p in prefix_lens]
+ expanded_extend_lens = [e * scale for e in extend_lens]
+ expanded_extend_num_tokens = extend_num_tokens * scale
+
token_type_ids = [
r.token_type_ids for r in reqs if r.token_type_ids is not None
]
@@ -1159,14 +1171,16 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
input_ids_tensor = torch.tensor(
list(chain.from_iterable(input_ids)), dtype=torch.int64
).to(self.device, non_blocking=True)
- seq_lens_tensor = torch.tensor(seq_lens, dtype=torch.int64).to(
+
+ # Use EXPANDED seq_lens/prefix_lens for the tensor (used by attention / KV cache)
+ seq_lens_tensor = torch.tensor(expanded_seq_lens, dtype=torch.int64).to(
self.device, non_blocking=True
)
orig_seq_lens_tensor = torch.tensor(orig_seq_lens, dtype=torch.int32).to(
self.device, non_blocking=True
)
prefix_lens_tensor = torch.tensor(
- prefix_lens, dtype=torch.int64, device=self.device
+ expanded_prefix_lens, dtype=torch.int64, device=self.device
)
token_type_ids_tensor = None
@@ -1187,6 +1201,17 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
assert seq_len - pre_len == req.extend_input_len
if pre_len > 0:
+ # NOTE: prefix caching with scale_seq is not yet supported.
+ # Prefix indices are in logical (un-expanded) space.
+ # For now, we expand them by repeating each slot `scale` times
+ # to fill the expanded prefix region in req_to_token_pool.
+ if scale > 1:
+ # When scale_seq is active, prefix caching is currently unsupported.
+ # Write placeholder zeros for the expanded prefix region.
+ logger.warning_once(
+ "Prefix caching with scale_seq is not fully supported. "
+ "Results may be incorrect if prefix caching is active."
+ )
self.req_to_token_pool.write(
(req.req_pool_idx, slice(0, pre_len)), req.prefix_indices
)
@@ -1202,6 +1227,7 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
multimodal_inputs.append(req.multimodal_inputs)
+ # Keep request bookkeeping in LOGICAL (un-expanded) space
req.cached_tokens += pre_len - req.already_computed
req.already_computed = seq_len
req.is_retracted = False
@@ -1261,9 +1287,9 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
else:
extend_input_logprob_token_ids = None
- # Allocate memory
+ # Allocate memory — use EXPANDED token count
if self.token_to_kv_pool_allocator.page_size == 1:
- out_cache_loc = self.alloc_token_slots(extend_num_tokens)
+ out_cache_loc = self.alloc_token_slots(expanded_extend_num_tokens)
else:
last_loc = get_last_loc(
self.req_to_token_pool.req_to_token,
@@ -1271,15 +1297,15 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
prefix_lens_tensor,
)
out_cache_loc = self.alloc_paged_token_slots_extend(
- prefix_lens_tensor, seq_lens_tensor, last_loc, extend_num_tokens
+ prefix_lens_tensor, seq_lens_tensor, last_loc, expanded_extend_num_tokens
)
- # Set fields
+ # Set fields — tensor fields use EXPANDED values
self.input_ids = input_ids_tensor
self.req_pool_indices = req_pool_indices_tensor
- self.seq_lens = seq_lens_tensor
+ self.seq_lens = seq_lens_tensor # expanded
self.orig_seq_lens = orig_seq_lens_tensor
- self.out_cache_loc = out_cache_loc
+ self.out_cache_loc = out_cache_loc # expanded count
self.input_embeds = (
torch.tensor(input_embeds).to(self.device, non_blocking=True)
if input_embeds
@@ -1294,22 +1320,21 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
mm_item.feature = pixel_values.to(self.device, non_blocking=True)
self.multimodal_inputs = multimodal_inputs
self.token_type_ids = token_type_ids_tensor
- self.seq_lens_sum = sum(seq_lens)
+ self.seq_lens_sum = sum(expanded_seq_lens)
if self.return_logprob:
self.top_logprobs_nums = [r.top_logprobs_num for r in reqs]
self.token_ids_logprobs = [r.token_ids_logprob for r in reqs]
self.extend_logprob_start_lens = [r.extend_logprob_start_len for r in reqs]
- self.extend_num_tokens = extend_num_tokens
- self.prefix_lens = prefix_lens
- self.extend_lens = extend_lens
+ self.extend_num_tokens = expanded_extend_num_tokens
+ self.prefix_lens = expanded_prefix_lens
+ self.extend_lens = expanded_extend_lens
self.extend_input_logprob_token_ids = extend_input_logprob_token_ids
- # Write to req_to_token_pool
+ # Write to req_to_token_pool — use EXPANDED lens/seq_lens
if support_triton(global_server_args_dict.get("attention_backend")):
# TODO: some tensors can be reused for ForwardBatchInfo (e.g., extend_lens, cumsum_start)
-
write_req_to_token_pool_triton[(bs,)](
self.req_to_token_pool.req_to_token,
req_pool_indices_tensor,
@@ -1323,10 +1348,10 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
pt = 0
for i in range(bs):
self.req_to_token_pool.write(
- (req_pool_indices[i], slice(prefix_lens[i], seq_lens[i])),
- out_cache_loc[pt : pt + extend_lens[i]],
+ (req_pool_indices[i], slice(expanded_prefix_lens[i], expanded_seq_lens[i])),
+ out_cache_loc[pt : pt + expanded_extend_lens[i]],
)
- pt += extend_lens[i]
+ pt += expanded_extend_lens[i]
if self.model_config.is_encoder_decoder:
self.prepare_encoder_info_extend(input_ids, seq_lens)
@@ -1345,6 +1370,7 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
def mix_with_running(self, running_batch: "ScheduleBatch"):
self.forward_mode = ForwardMode.MIXED
running_bs = running_batch.batch_size()
+ scale = self._get_scale_seq_factor()
for req in running_batch.reqs:
req.fill_ids = req.origin_input_ids + req.output_ids
@@ -1361,32 +1387,50 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
delta = 0 if self.enable_overlap else -1
# NOTE: prefix_indices is what has been cached, but we don't cache each decode step
+ # With scale_seq, KV entries are expanded, so prefix_lens and extend_lens
+ # must be in expanded (KV) space.
self.prefix_lens.extend(
[
- len(r.origin_input_ids) + len(r.output_ids) + delta
+ (len(r.origin_input_ids) + len(r.output_ids) + delta) * scale
for r in running_batch.reqs
]
)
- self.extend_lens.extend([1] * running_bs)
- self.extend_num_tokens += running_bs
+ self.extend_lens.extend([scale] * running_bs)
+ self.extend_num_tokens += running_bs * scale
# TODO (lianmin): Revisit this. It should be seq_len - 1
self.extend_logprob_start_lens.extend([0] * running_bs)
- def new_page_count_next_decode(self):
+ def new_page_count_next_decode(self, selected_indices: Optional[List[int]] = None):
page_size = self.token_to_kv_pool_allocator.page_size
+ scale = self._get_scale_seq_factor()
+ requests = (
+ self.reqs
+ if selected_indices is None
+ else [self.reqs[i] for i in selected_indices]
+ )
if page_size == 1:
- return len(self.reqs)
- # In the decoding phase, the length of a request's KV cache should be
- # the total length of the request minus 1
+ return len(requests) * scale
+
+ # Count how many new pages are needed when adding `scale` KV entries
+ # per request. For each request compute:
+ # ceil((cur + scale) / page_size) - ceil(cur / page_size)
+ def _new_pages(cur_len):
+ return (
+ (cur_len + scale + page_size - 1) // page_size
+ - (cur_len + page_size - 1) // page_size
+ )
+
return (
- sum(1 for req in self.reqs if req.seqlen % page_size == 0)
+ sum(_new_pages(req.seqlen) for req in requests)
if self.enable_overlap
- else sum(1 for req in self.reqs if (req.seqlen - 1) % page_size == 0)
+ else sum(_new_pages(req.seqlen - 1) for req in requests)
)
- def check_decode_mem(self, buf_multiplier=1):
+ def check_decode_mem(
+ self, buf_multiplier=1, selected_indices: Optional[List[int]] = None
+ ):
num_tokens = (
- self.new_page_count_next_decode()
+ self.new_page_count_next_decode(selected_indices)
* buf_multiplier
* self.token_to_kv_pool_allocator.page_size
)
@@ -1412,34 +1456,11 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
reverse=True,
)
- def get_required_tokens(num_reqs: int):
- headroom_for_spec_decode = 0
- if server_args.speculative_algorithm:
- headroom_for_spec_decode += (
- num_reqs
- * server_args.speculative_eagle_topk
- * server_args.speculative_num_steps
- + num_reqs * server_args.speculative_num_draft_tokens
- )
- return (
- num_reqs * global_config.retract_decode_steps + headroom_for_spec_decode
- )
-
- def _get_available_size():
- if self.is_hybrid:
- return min(
- self.token_to_kv_pool_allocator.full_available_size(),
- self.token_to_kv_pool_allocator.swa_available_size(),
- )
- else:
- return self.token_to_kv_pool_allocator.available_size()
-
retracted_reqs = []
seq_lens_cpu = self.seq_lens.cpu().numpy()
first_iter = True
- while (
- _get_available_size() < get_required_tokens(len(sorted_indices))
- or first_iter
+ while first_iter or (
+ not self.check_decode_mem(selected_indices=sorted_indices)
):
if len(sorted_indices) == 1:
# Corner case: only one request left
@@ -1493,10 +1514,6 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
else:
self.tree_cache.dec_lock_ref(req.last_node)
- # NOTE(lsyin): we should use the newly evictable memory instantly.
- num_tokens = len(sorted_indices) * global_config.retract_decode_steps
- self._evict_tree_cache_if_needed(num_tokens)
-
req.reset_for_retract()
if len(retracted_reqs) == 0:
@@ -1537,7 +1554,13 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
)
def prepare_for_decode(self):
+ scale = self._get_scale_seq_factor()
+
+ # Always keep DECODE for scheduler routing.
+ # When scale > 1, the ForwardBatch will override to EXTEND for the
+ # attention backend (see ForwardBatch.init_new).
self.forward_mode = ForwardMode.DECODE
+
bs = len(self.reqs)
if self.spec_algorithm.is_eagle():
@@ -1576,17 +1599,16 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
locs = self.encoder_lens + self.seq_lens
self.prepare_encoder_info_decode()
else:
- locs = self.seq_lens.clone()
+ locs = self.seq_lens.clone() # expanded positions before update
+ # Update seq_lens by `scale` (each logical token produces `scale` KV entries)
if self.enable_overlap:
- # Do not use in-place operations in the overlap mode
- self.seq_lens = self.seq_lens + 1
+ self.seq_lens = self.seq_lens + scale
self.orig_seq_lens = self.orig_seq_lens + 1
else:
- # A faster in-place version
- self.seq_lens.add_(1)
+ self.seq_lens.add_(scale)
self.orig_seq_lens.add_(1)
- self.seq_lens_sum += bs
+ self.seq_lens_sum += bs * scale
# free memory
if isinstance(self.tree_cache, SWAChunkCache):
@@ -1595,9 +1617,12 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
req, req.seqlen - 1, self.model_config.attention_chunk_size
)
- # Allocate memory
- if self.token_to_kv_pool_allocator.page_size == 1:
- self.out_cache_loc = self.alloc_token_slots(bs)
+ # Allocate memory — `scale` KV slots per request
+ alloc_tokens = bs * scale
+ if self.token_to_kv_pool_allocator.page_size == 1 or scale > 1:
+ # alloc_paged_token_slots_decode only supports 1 token per request,
+ # so fall back to generic alloc_token_slots when scale > 1.
+ self.out_cache_loc = self.alloc_token_slots(alloc_tokens)
else:
last_loc = self.req_to_token_pool.req_to_token[
self.req_pool_indices, self.seq_lens - 2
@@ -1606,9 +1631,27 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
self.seq_lens, last_loc
)
- self.req_to_token_pool.write(
- (self.req_pool_indices, locs), self.out_cache_loc.to(torch.int32)
- )
+ # Write `scale` entries per request to req_to_token_pool
+ if scale > 1:
+ expanded_req_pool_indices = self.req_pool_indices.repeat_interleave(scale)
+ offsets = torch.arange(scale, device=self.device, dtype=locs.dtype)
+ expanded_locs = (locs.unsqueeze(1) + offsets).reshape(-1)
+ self.req_to_token_pool.write(
+ (expanded_req_pool_indices, expanded_locs),
+ self.out_cache_loc.to(torch.int32),
+ )
+ else:
+ self.req_to_token_pool.write(
+ (self.req_pool_indices, locs), self.out_cache_loc.to(torch.int32)
+ )
+
+ # When using EXTEND mode for decode, set up extend metadata
+ if scale > 1:
+ self.extend_num_tokens = alloc_tokens
+ self.extend_lens = [scale] * bs
+ self.prefix_lens = locs.tolist() # expanded prefix = seq_lens before update
+ self.extend_logprob_start_lens = [0] * bs
+ self.extend_input_logprob_token_ids = None
def filter_batch(
self,
@@ -1711,7 +1754,10 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
def get_model_worker_batch(
self, seq_lens_cpu_cache: Optional[torch.Tensor] = None
) -> ModelWorkerBatch:
- if self.forward_mode.is_decode_or_idle():
+ # For scale_seq decode, extend metadata is populated even in DECODE mode
+ # so that ForwardBatch can override to EXTEND for the attention backend.
+ scale = self._get_scale_seq_factor()
+ if self.forward_mode.is_decode_or_idle() and scale <= 1:
extend_seq_lens = extend_prefix_lens = extend_logprob_start_lens = None
else:
extend_seq_lens = self.extend_lens
diff --git a/python/sglang/srt/managers/scheduler.py b/python/sglang/srt/managers/scheduler.py
index 91901ca8b..e39ce1d16 100644
--- a/python/sglang/srt/managers/scheduler.py
+++ b/python/sglang/srt/managers/scheduler.py
@@ -664,6 +664,10 @@ class Scheduler(
enable_kv_cache_events=self.enable_kv_cache_events,
)
+ # Propagate scale_seq_factor to tree_cache for correct KV cache freeing
+ scale_seq_times = getattr(self.model_config.hf_config, "scale_seq_times", 0)
+ self.tree_cache.scale_seq_factor = scale_seq_times + 1
+
self.decode_mem_cache_buf_multiplier = (
1
if self.spec_algorithm.is_none()
diff --git a/python/sglang/srt/managers/scheduler_output_processor_mixin.py b/python/sglang/srt/managers/scheduler_output_processor_mixin.py
index c6205a094..d508d3197 100644
--- a/python/sglang/srt/managers/scheduler_output_processor_mixin.py
+++ b/python/sglang/srt/managers/scheduler_output_processor_mixin.py
@@ -76,9 +76,18 @@ class SchedulerOutputProcessorMixin:
continue
if self.is_mixed_chunk and self.enable_overlap and req.finished():
- # Free the one delayed token for the mixed decode batch
- j = len(batch.out_cache_loc) - len(batch.reqs) + i
- self.token_to_kv_pool_allocator.free(batch.out_cache_loc[j : j + 1])
+ # Free the delayed KV entries for the mixed decode batch.
+ # With scale_seq each decode request occupies `scale` entries.
+ scale = batch._get_scale_seq_factor()
+ running_bs = (
+ len(batch.decoding_reqs) if batch.decoding_reqs else 0
+ )
+ decode_start = len(batch.out_cache_loc) - running_bs * scale
+ k = i - (len(batch.reqs) - running_bs)
+ j_start = decode_start + k * scale
+ self.token_to_kv_pool_allocator.free(
+ batch.out_cache_loc[j_start : j_start + scale]
+ )
continue
if req.is_chunked <= 0:
@@ -227,17 +236,25 @@ class SchedulerOutputProcessorMixin:
continue
if self.enable_overlap and req.finished():
- # Free the one extra delayed token
+ # Free the extra delayed KV entries written in this decode step.
+ # With scale_seq, each logical token produces `scale` KV entries.
+ scale = batch._get_scale_seq_factor()
+ start = i * scale
+ end = start + scale
if self.page_size == 1:
- self.token_to_kv_pool_allocator.free(batch.out_cache_loc[i : i + 1])
+ self.token_to_kv_pool_allocator.free(
+ batch.out_cache_loc[start:end]
+ )
else:
- # Only free when the extra token is in a new page
- if (
+ # Only free pages that are newly allocated
+ logical_len = (
len(req.origin_input_ids) + len(req.output_ids) - 1
- ) % self.page_size == 0:
- self.token_to_kv_pool_allocator.free(
- batch.out_cache_loc[i : i + 1]
- )
+ )
+ for s in range(scale):
+ if (logical_len * scale + s) % self.page_size == 0:
+ self.token_to_kv_pool_allocator.free(
+ batch.out_cache_loc[start + s : start + s + 1]
+ )
continue
if batch.spec_algorithm.is_none():
diff --git a/python/sglang/srt/mem_cache/chunk_cache.py b/python/sglang/srt/mem_cache/chunk_cache.py
index 1a576bfa2..2612b486e 100644
--- a/python/sglang/srt/mem_cache/chunk_cache.py
+++ b/python/sglang/srt/mem_cache/chunk_cache.py
@@ -39,10 +39,11 @@ class ChunkCache(BasePrefixCache):
)
def cache_finished_req(self, req: Req):
+ scale = getattr(self, "scale_seq_factor", 1)
+ logical_len = len(req.origin_input_ids) + max(len(req.output_ids) - 1, 0)
+ kv_len = logical_len * scale
kv_indices = self.req_to_token_pool.req_to_token[
- req.req_pool_idx,
- # For decode server: if req.output_ids is empty, we want to free all req.origin_input_ids
- : len(req.origin_input_ids) + max(len(req.output_ids) - 1, 0),
+ req.req_pool_idx, :kv_len
]
self.req_to_token_pool.free(req.req_pool_idx)
self.token_to_kv_pool_allocator.free(kv_indices)
diff --git a/python/sglang/srt/mem_cache/radix_cache.py b/python/sglang/srt/mem_cache/radix_cache.py
index b0cf0bb9c..f52a07801 100644
--- a/python/sglang/srt/mem_cache/radix_cache.py
+++ b/python/sglang/srt/mem_cache/radix_cache.py
@@ -204,9 +204,16 @@ class RadixCache(BasePrefixCache):
def cache_finished_req(self, req: Req):
"""Cache request when it finishes."""
- if self.disable:
+ scale = getattr(self, "scale_seq_factor", 1)
+ logical_len = len(req.origin_input_ids) + len(req.output_ids) - 1
+ kv_len = logical_len * scale
+
+ if self.disable or scale > 1:
+ # When scale_seq is active, radix tree prefix caching is
+ # incompatible (1-token maps to N KV entries), so we skip
+ # tree insertion and free all KV entries directly.
kv_indices = self.req_to_token_pool.req_to_token[
- req.req_pool_idx, : len(req.origin_input_ids) + len(req.output_ids) - 1
+ req.req_pool_idx, :kv_len
]
self.token_to_kv_pool_allocator.free(kv_indices)
self.req_to_token_pool.free(req.req_pool_idx)
@@ -243,6 +250,10 @@ class RadixCache(BasePrefixCache):
"""Cache request when it is unfinished."""
if self.disable:
return
+ scale = getattr(self, "scale_seq_factor", 1)
+ if scale > 1:
+ # scale_seq: radix tree prefix caching is incompatible, skip
+ return
token_ids = req.fill_ids
kv_indices = self.req_to_token_pool.req_to_token[
diff --git a/python/sglang/srt/mem_cache/swa_radix_cache.py b/python/sglang/srt/mem_cache/swa_radix_cache.py
index 0624e84e1..c4e8f7e0a 100644
--- a/python/sglang/srt/mem_cache/swa_radix_cache.py
+++ b/python/sglang/srt/mem_cache/swa_radix_cache.py
@@ -429,10 +429,13 @@ class SWARadixCache(BasePrefixCache):
def cache_finished_req(self, req: Req) -> None:
"""Cache request when it finishes."""
- if self.disable:
+ scale = getattr(self, "scale_seq_factor", 1)
+ logical_len = len(req.origin_input_ids) + max(len(req.output_ids) - 1, 0)
+ kv_len = logical_len * scale
+
+ if self.disable or scale > 1:
kv_indices = self.req_to_token_pool.req_to_token[
- req.req_pool_idx,
- : len(req.origin_input_ids) + max(len(req.output_ids) - 1, 0),
+ req.req_pool_idx, :kv_len
]
self.token_to_kv_pool_allocator.free(kv_indices)
self.req_to_token_pool.free(req.req_pool_idx)
diff --git a/python/sglang/srt/model_executor/cuda_graph_runner.py b/python/sglang/srt/model_executor/cuda_graph_runner.py
index 2effec9c0..2b32eadf8 100644
--- a/python/sglang/srt/model_executor/cuda_graph_runner.py
+++ b/python/sglang/srt/model_executor/cuda_graph_runner.py
@@ -280,6 +280,13 @@ class CudaGraphRunner:
self.model_runner.server_args.speculative_num_draft_tokens
)
+ self.scale_seq_factor = getattr(
+ model_runner.model_config.hf_config, "scale_seq_times", 0
+ ) + 1
+ if self.scale_seq_factor > 1 and self.num_tokens_per_bs == 1:
+ self.num_tokens_per_bs = self.scale_seq_factor
+ self.capture_forward_mode = ForwardMode.TARGET_VERIFY
+
# If returning hidden states is enabled, set initial capture hidden mode to full to avoid double-capture on startup
if model_runner.server_args.enable_return_hidden_states:
self.capture_hidden_mode = CaptureHiddenMode.FULL
@@ -317,7 +324,9 @@ class CudaGraphRunner:
(self.max_num_token,), dtype=self._cache_loc_dtype()
)
self.positions = torch.zeros((self.max_num_token,), dtype=torch.int64)
- self.mrope_positions = torch.zeros((3, self.max_bs), dtype=torch.int64)
+ self.mrope_positions = torch.zeros(
+ (3, self.max_num_token), dtype=torch.int64
+ )
self.num_token_non_padded = torch.zeros((1,), dtype=torch.int32)
self.tbo_plugin = TboCudaGraphRunnerPlugin()
@@ -523,7 +532,11 @@ class CudaGraphRunner:
num_tokens = bs * self.num_tokens_per_bs
# Graph inputs
- input_ids = self.input_ids[:num_tokens]
+ # For scale_seq: model receives bs input_ids (internally expanded to
+ # bs*scale hidden_states), but positions/out_cache_loc need bs*scale.
+ is_scale_seq = self.scale_seq_factor > 1
+ model_input_len = bs if is_scale_seq else num_tokens
+ input_ids = self.input_ids[:model_input_len]
req_pool_indices = self.req_pool_indices[:bs]
seq_lens = self.seq_lens[:bs]
out_cache_loc = self.out_cache_loc[:num_tokens]
@@ -532,8 +545,8 @@ class CudaGraphRunner:
encoder_lens = self.encoder_lens[:bs]
else:
encoder_lens = None
- mrope_positions = self.mrope_positions[:, :bs]
- next_token_logits_buffer = self.next_token_logits_buffer[:num_tokens]
+ mrope_positions = self.mrope_positions[:, :num_tokens]
+ next_token_logits_buffer = self.next_token_logits_buffer[:model_input_len]
self.num_token_non_padded[...] = num_tokens
# pipeline parallelism
@@ -730,7 +743,11 @@ class CudaGraphRunner:
self.out_cache_loc.zero_()
# Common inputs
- self.input_ids[:raw_num_token].copy_(forward_batch.input_ids)
+ # For scale_seq: input_ids and n_gram_ids have raw_bs elements (model
+ # internally expands to raw_num_token hidden states).
+ is_scale_seq = self.scale_seq_factor > 1
+ raw_model_input = raw_bs if is_scale_seq else raw_num_token
+ self.input_ids[:raw_model_input].copy_(forward_batch.input_ids)
self.req_pool_indices[:raw_bs].copy_(forward_batch.req_pool_indices)
self.seq_lens[:raw_bs].copy_(forward_batch.seq_lens)
self.out_cache_loc[:raw_num_token].copy_(forward_batch.out_cache_loc)
@@ -751,7 +768,7 @@ class CudaGraphRunner:
if self.is_encoder_decoder:
self.encoder_lens[:raw_bs].copy_(forward_batch.encoder_lens)
if forward_batch.mrope_positions is not None:
- self.mrope_positions[:, :raw_bs].copy_(forward_batch.mrope_positions)
+ self.mrope_positions[:, :raw_num_token].copy_(forward_batch.mrope_positions)
if self.require_gathered_buffer:
self.global_num_tokens_gpu.fill_(bs * self.num_tokens_per_bs)
self.global_num_tokens_for_logprob_gpu.fill_(bs * self.num_tokens_per_bs)
@@ -809,12 +826,15 @@ class CudaGraphRunner:
# Replay
self.graphs[self.bs].replay()
+ # For scale_seq: model internally contracts bs*scale hidden states back
+ # to bs logits, so output slice uses raw_bs instead of raw_num_token.
+ output_len = self.raw_bs if self.scale_seq_factor > 1 else self.raw_num_token
output = self.output_buffers[self.bs]
if isinstance(output, LogitsProcessorOutput):
return LogitsProcessorOutput(
- next_token_logits=output.next_token_logits[: self.raw_num_token],
+ next_token_logits=output.next_token_logits[:output_len],
hidden_states=(
- output.hidden_states[: self.raw_num_token]
+ output.hidden_states[:output_len]
if output.hidden_states is not None
else None
),
diff --git a/python/sglang/srt/model_executor/forward_batch_info.py b/python/sglang/srt/model_executor/forward_batch_info.py
index 8904e89f1..1153f6543 100644
--- a/python/sglang/srt/model_executor/forward_batch_info.py
+++ b/python/sglang/srt/model_executor/forward_batch_info.py
@@ -132,6 +132,9 @@ class ForwardMode(IntEnum):
or self == ForwardMode.IDLE
)
+ def is_cpu_graph(self):
+ return self == ForwardMode.DECODE
+
def is_dummy_first(self):
return self == ForwardMode.DUMMY_FIRST
@@ -416,6 +419,17 @@ class ForwardBatch:
):
ret.positions = ret.spec_info.positions
+ # scale_seq decode: ScheduleBatch keeps DECODE for scheduler routing,
+ # but the attention backend and position computation need extend-like
+ # mode because each decode step produces multiple KV entries.
+ # Use TARGET_VERIFY so is_cuda_graph() returns True while is_extend()
+ # also returns True for the attention / position computation paths.
+ if (
+ ret.forward_mode.is_decode()
+ and batch.extend_seq_lens is not None
+ ):
+ ret.forward_mode = ForwardMode.TARGET_VERIFY
+
# Init position information
if ret.forward_mode.is_decode():
if ret.positions is None:
diff --git a/python/sglang/srt/models/qwen3_scale_seq.py b/python/sglang/srt/models/qwen3_scale_seq.py
new file mode 100644
index 000000000..7cf5150e8
--- /dev/null
+++ b/python/sglang/srt/models/qwen3_scale_seq.py
@@ -0,0 +1,366 @@
+# Adapted from qwen3.py
+# Adds scale_seq (sequence length scaling via embedding replication) support for Qwen3.
+"""Inference-only Qwen3 model with scale_seq support, compatible with HuggingFace weights."""
+import logging
+from typing import Iterable, Optional, Tuple, Union
+
+import torch
+from torch import nn
+
+from sglang.srt.layers.dp_attention import is_dp_attention_enabled
+from sglang.srt.layers.quantization.base_config import QuantizationConfig
+from sglang.srt.layers.utils import get_layer_id
+from sglang.srt.layers.vocab_parallel_embedding import VocabParallelEmbedding
+from sglang.srt.model_executor.forward_batch_info import ForwardBatch, PPProxyTensors
+from sglang.srt.model_loader.weight_utils import (
+ default_weight_loader,
+ maybe_remap_kv_scale_name,
+)
+from sglang.srt.models.qwen3 import Qwen3ForCausalLM, Qwen3Model
+from sglang.srt.utils import add_prefix
+
+Qwen3ScaleSeqConfig = None
+
+logger = logging.getLogger(__name__)
+
+
+# ---------------------------------------------------------------------------
+# Model (transformer body) — inherits Qwen3Model, adds scale_seq embeddings
+# ---------------------------------------------------------------------------
+class Qwen3ScaleSeqModel(Qwen3Model):
+ """Qwen3Model extended with multi-stream embedding for sequence scaling.
+
+ When scale_seq_times > 0, each logical token is expanded into
+ (scale_seq_times + 1) KV cache entries via interleaved embeddings:
+ [main_emb_i, scale_seq_1_emb_i, ..., scale_seq_N_emb_i]
+ """
+
+ def __init__(
+ self,
+ config,
+ quant_config: Optional[QuantizationConfig] = None,
+ prefix: str = "",
+ ) -> None:
+ self.scale_seq_times = getattr(config, "scale_seq_times", 0)
+ super().__init__(config=config, quant_config=quant_config, prefix=prefix)
+
+ # Create additional embedding tables for scale_seq
+ if self.scale_seq_times > 0 and self.pp_group.is_first_rank:
+ self.scale_seq_embed_tokens_list = nn.ModuleList(
+ [
+ VocabParallelEmbedding(
+ config.vocab_size,
+ config.hidden_size,
+ enable_tp=not is_dp_attention_enabled(),
+ )
+ for _ in range(self.scale_seq_times)
+ ]
+ )
+
+ def _expand_scale_seq(
+ self, input_ids: torch.Tensor, hidden_states: torch.Tensor
+ ) -> torch.Tensor:
+ """Expand hidden_states from (T, D) to (T * scale, D) by interleaving
+ main embedding with scale_seq embeddings.
+
+ Layout per original token i:
+ [main_emb_i, scale_seq_1_emb_i, ..., scale_seq_N_emb_i]
+ """
+ scale = self.scale_seq_times + 1
+ T, D = hidden_states.shape
+
+ # (T, D) -> (T, 1, D)
+ parts = [hidden_states.unsqueeze(1)]
+ for s in range(self.scale_seq_times):
+ hs_s = self.scale_seq_embed_tokens_list[s](input_ids) # (T, D)
+ parts.append(hs_s.unsqueeze(1)) # (T, 1, D)
+
+ # (T, scale, D) -> (T * scale, D)
+ return torch.cat(parts, dim=1).reshape(T * scale, D).contiguous()
+
+ def forward(
+ self,
+ input_ids: torch.Tensor,
+ positions: torch.Tensor,
+ forward_batch: ForwardBatch,
+ input_embeds: torch.Tensor = None,
+ pp_proxy_tensors: Optional[PPProxyTensors] = None,
+ ) -> Union[torch.Tensor, PPProxyTensors]:
+ if self.pp_group.is_first_rank:
+ if input_embeds is None:
+ hidden_states = self.embed_tokens(input_ids)
+ else:
+ hidden_states = input_embeds
+
+ # Scale sequence length: expand (T, D) -> (T * scale, D)
+ if self.scale_seq_times > 0:
+ hidden_states = self._expand_scale_seq(input_ids, hidden_states)
+
+ residual = None
+ else:
+ assert pp_proxy_tensors is not None
+ hidden_states = pp_proxy_tensors["hidden_states"]
+ residual = pp_proxy_tensors["residual"]
+
+ aux_hidden_states = []
+ for i in range(self.start_layer, self.end_layer):
+ if i in self.layers_to_capture:
+ aux_hidden_states.append(
+ hidden_states + residual
+ if residual is not None
+ else hidden_states
+ )
+ layer = self.layers[i]
+ hidden_states, residual = layer(
+ positions, hidden_states, forward_batch, residual
+ )
+
+ if not self.pp_group.is_last_rank:
+ return PPProxyTensors(
+ {"hidden_states": hidden_states, "residual": residual}
+ )
+ else:
+ if hidden_states.shape[0] != 0:
+ if residual is None:
+ hidden_states = self.norm(hidden_states)
+ else:
+ hidden_states, _ = self.norm(hidden_states, residual)
+
+ if len(aux_hidden_states) == 0:
+ return hidden_states
+ return hidden_states, aux_hidden_states
+
+
+# ---------------------------------------------------------------------------
+# CausalLM wrapper — inherits Qwen3ForCausalLM, adds contract + load logic
+# ---------------------------------------------------------------------------
+class Qwen3ScaleSeqForCausalLM(Qwen3ForCausalLM):
+ """Qwen3ForCausalLM with scale_seq support.
+
+ After the transformer body produces (T*scale, D) hidden states,
+ contracts back to (T, D) by selecting the last stream per token group
+ before applying lm_head.
+ """
+
+ def __init__(
+ self,
+ config,
+ quant_config: Optional[QuantizationConfig] = None,
+ prefix: str = "",
+ ) -> None:
+ # Expand max_position_embeddings BEFORE parent builds decoder layers
+ # so that RoPE caches are sized correctly.
+ scale_seq_times = getattr(config, "scale_seq_times", 0)
+ if scale_seq_times > 0:
+ config.max_position_embeddings = getattr(
+ config, "max_position_embeddings", 32768
+ ) * (scale_seq_times + 1)
+
+ super().__init__(config, quant_config, prefix)
+
+ # Replace Qwen3Model with Qwen3ScaleSeqModel (adds scale_seq embeddings).
+ # Config is already expanded so Qwen3ScaleSeqModel won't double-expand.
+ self.model = Qwen3ScaleSeqModel(
+ config, quant_config=quant_config, prefix=add_prefix("model", prefix)
+ )
+
+ # ----- scale_seq contraction -----
+
+ def _contract_scale_seq(
+ self, hidden_states: torch.Tensor, forward_batch: ForwardBatch
+ ) -> torch.Tensor:
+ """Contract expanded hidden_states back to logical token space.
+
+ Selects every scale-th element (last of each group), matching the
+ training-side contraction semantic. Also restores forward_batch
+ metadata so LogitsProcessor sees un-expanded lengths.
+ """
+ scale = self.model.scale_seq_times + 1
+ indices = torch.arange(
+ scale - 1,
+ hidden_states.shape[0],
+ scale,
+ device=hidden_states.device,
+ )
+ hidden_states = hidden_states[indices]
+
+ if forward_batch.extend_seq_lens is not None:
+ forward_batch.extend_seq_lens = forward_batch.extend_seq_lens // scale
+ if forward_batch.extend_seq_lens_cpu is not None:
+ forward_batch.extend_seq_lens_cpu = [