Repository navigation
Expand file tree
/
Copy pathengine.patch
More file actions
1715 lines (1657 loc) · 72.9 KB
/
Copy pathengine.patch
File metadata and controls
1715 lines (1657 loc) · 72.9 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/kernels/ops/lora/moe/fused_moe_lora_kernel.py b/python/sglang/kernels/ops/lora/moe/fused_moe_lora_kernel.py
index b0c85481e..dbe22e049 100644
--- a/python/sglang/kernels/ops/lora/moe/fused_moe_lora_kernel.py
+++ b/python/sglang/kernels/ops/lora/moe/fused_moe_lora_kernel.py
@@ -339,6 +339,7 @@ def _fused_moe_lora_expand(
split_k: int,
mul_routed_weight: bool = False,
offset: int = 0,
+ interleaved_output: bool = False,
) -> None:
b_ptr = _get_ptr(lora_b_stacked, device)
@@ -404,7 +405,11 @@ def _fused_moe_lora_expand(
**expand_config,
)
for i in range(num_slices):
- output[:, :, i * N + offset : (i + 1) * N + offset] += b_intermediate_cache1[i]
+ if interleaved_output:
+ assert offset == 0 and output.shape[-1] == num_slices * N
+ output[:, :, i::num_slices] += b_intermediate_cache1[i]
+ else:
+ output[:, :, i * N + offset : (i + 1) * N + offset] += b_intermediate_cache1[i]
@torch.inference_mode()
@@ -442,6 +447,7 @@ def _fused_moe_lora(
mul_routed_weight: bool = False,
fully_sharded: bool = False,
offset: int = 0,
+ interleaved_output: bool = False,
) -> None:
assert len(lora_a_stacked) == len(lora_b_stacked) > 0
assert (
@@ -562,6 +568,7 @@ def _fused_moe_lora(
expand_split_k,
mul_routed_weight,
offset,
+ interleaved_output,
)
@@ -595,6 +602,7 @@ def _fused_moe_lora_fake(
mul_routed_weight: bool = False,
fully_sharded: bool = False,
offset: int = 0,
+ interleaved_output: bool = False,
) -> None:
return
@@ -661,6 +669,7 @@ def _fused_moe_lora_expand_fake(
split_k: int,
mul_routed_weight: bool = False,
offset: int = 0,
+ interleaved_output: bool = False,
) -> None:
return
diff --git a/python/sglang/kernels/ops/lora/moe/virtual_experts.py b/python/sglang/kernels/ops/lora/moe/virtual_experts.py
index e8b35e446..d6df22b7d 100644
--- a/python/sglang/kernels/ops/lora/moe/virtual_experts.py
+++ b/python/sglang/kernels/ops/lora/moe/virtual_experts.py
@@ -273,11 +273,17 @@ def _invoke_moe_lora_shrink_splitk(
SPLIT_K = min(max_split_k, max(1, 128 // base_grid)) if base_grid < 128 else 1
grid = (SPLIT_K * base_grid,)
+ # Split-K partial sums must not be rounded to BF16 before each atomic add.
+ # Small decode batches use many splits, amplifying that rounding error.
+ # Keep the parallel reduction in FP32, then round once to the LoRA dtype.
+ reduction_output = (
+ torch.zeros_like(output, dtype=torch.float32) if SPLIT_K > 1 else output
+ )
_moe_lora_shrink_splitk_kernel[grid](
hidden_states,
weight,
- output,
+ reduction_output,
sorted_token_ids,
expert_ids,
num_tokens_post_padded,
@@ -289,8 +295,8 @@ def _invoke_moe_lora_shrink_splitk(
weight.stride(0),
weight.stride(1),
weight.stride(2),
- output.stride(0),
- output.stride(1),
+ reduction_output.stride(0),
+ reduction_output.stride(1),
top_k=top_k,
BLOCK_SIZE_M=BLOCK_SIZE_M,
BLOCK_SIZE_N=BLOCK_SIZE_N,
@@ -300,6 +306,8 @@ def _invoke_moe_lora_shrink_splitk(
num_warps=config.get("num_warps", 4),
num_stages=config.get("num_stages", 4),
)
+ if SPLIT_K > 1:
+ output.copy_(reduction_output)
def _align_block_size_jit(
diff --git a/python/sglang/srt/arg_groups/fields/lora.py b/python/sglang/srt/arg_groups/fields/lora.py
index c7ea9ea8f..b8118932c 100644
--- a/python/sglang/srt/arg_groups/fields/lora.py
+++ b/python/sglang/srt/arg_groups/fields/lora.py
@@ -110,3 +110,30 @@ class Lora(msgspec.Struct):
float,
"When any LoRA adapter request waits longer than this threshold (in seconds), the scheduler will selectively drain one running adapter to make room. This mitigates extreme tail latency under high or skewed workloads by preventing a small set of adapters from monopolizing batch slots. Set to 0 to disable draining (default).",
] = 0.0
+
+ # -------------------------------------------------------------------------
+ # Soft prompts (input-only learned embeddings, one per tenant)
+ # -------------------------------------------------------------------------
+ enable_soft_prompt: A[
+ bool,
+ "Enable multi-tenant soft prompts. Requests name a registered prompt via `soft_prompt`; placeholder positions receive the prompt's embeddings and the prefix cache is namespaced per prompt.",
+ ] = False
+ max_soft_prompts: A[
+ int,
+ "Maximum number of soft prompts resident in the GPU pool at once.",
+ ] = 64
+ max_soft_prompt_len: A[
+ int,
+ "Maximum soft prompt length in tokens (pool row width).",
+ ] = 1024
+ soft_prompt_placeholder_id: A[
+ Optional[int],
+ "Token id used as the soft-prompt placeholder. Must be a vocabulary id the model never needs to see as real text (e.g. an unused padded-vocab row). Required when --enable-soft-prompt is set.",
+ ] = None
+ soft_prompt_paths: A[
+ Optional[List[str]],
+ Arg(
+ help="Soft prompts to load at startup, as NAME=PATH (safetensors with one [length, hidden_size] tensor).",
+ nargs="*",
+ ),
+ ] = None
diff --git a/python/sglang/srt/entrypoints/http_server.py b/python/sglang/srt/entrypoints/http_server.py
index 7165a5ca8..4b47463e3 100644
--- a/python/sglang/srt/entrypoints/http_server.py
+++ b/python/sglang/srt/entrypoints/http_server.py
@@ -139,6 +139,7 @@ from sglang.srt.managers.io_struct import (
InitWeightsUpdateGroupReqInput,
LoadLoRAAdapterFromTensorsReqInput,
LoadLoRAAdapterReqInput,
+ LoadSoftPromptReqInput,
OpenSessionReqInput,
ParseFunctionCallReq,
PauseGenerationReqInput,
@@ -151,6 +152,7 @@ from sglang.srt.managers.io_struct import (
SetInternalStateReq,
SlowDownReqInput,
UnloadLoRAAdapterReqInput,
+ UnloadSoftPromptReqInput,
UpdateWeightFromDiskReqInput,
UpdateWeightsFromDistributedReqInput,
UpdateWeightsFromIPCReqInput,
@@ -1649,6 +1651,35 @@ async def unload_lora_adapter(
return ORJSONResponse(msgspec_to_builtins(result), status_code=status_code)
+@app.api_route("/load_soft_prompt", methods=["POST"])
+@auth_level(AuthLevel.ADMIN_OPTIONAL)
+async def load_soft_prompt(
+ obj: Annotated[LoadSoftPromptReqInput, Body()], request: Request
+):
+ """Load (or reload) a tenant soft prompt into the GPU pool."""
+ result = await _global_state.tokenizer_manager.load_soft_prompt(obj, request)
+ status_code = HTTPStatus.OK if result.success else HTTPStatus.BAD_REQUEST
+ return ORJSONResponse(msgspec_to_builtins(result), status_code=status_code)
+
+
+@app.api_route("/unload_soft_prompt", methods=["POST"])
+@auth_level(AuthLevel.ADMIN_OPTIONAL)
+async def unload_soft_prompt(
+ obj: Annotated[UnloadSoftPromptReqInput, Body()], request: Request
+):
+ """Remove a tenant soft prompt from the GPU pool."""
+ result = await _global_state.tokenizer_manager.unload_soft_prompt(obj, request)
+ status_code = HTTPStatus.OK if result.success else HTTPStatus.BAD_REQUEST
+ return ORJSONResponse(msgspec_to_builtins(result), status_code=status_code)
+
+
+@app.api_route("/list_soft_prompts", methods=["GET"])
+async def list_soft_prompts():
+ """Registered soft prompts: name -> {length, version}."""
+ registry = _global_state.tokenizer_manager.soft_prompt_registry
+ return {name: {"length": l, "version": v} for name, (l, v) in registry.items()}
+
+
@app.api_route("/open_session", methods=["GET", "POST"])
async def open_session(obj: Annotated[OpenSessionReqInput, Body()], request: Request):
"""Open a session, and return its unique session id."""
diff --git a/python/sglang/srt/lora/layers.py b/python/sglang/srt/lora/layers.py
index 9e47c6c4b..40eb2c173 100644
--- a/python/sglang/srt/lora/layers.py
+++ b/python/sglang/srt/lora/layers.py
@@ -1149,6 +1149,7 @@ class FusedMoEWithLoRA(BaseLayerWithLoRA):
experts_shared_outer_loras=self.experts_shared_outer_loras,
cg_buffers=cg_buffers,
lora_use_virtual_experts=self.lora_use_virtual_experts,
+ gate_up_interleaved=self._uses_interleaved_gate_up,
)
def forward(self, hidden_states: torch.Tensor, topk_output: TopKOutput, **kwargs):
diff --git a/python/sglang/srt/lora/lora.py b/python/sglang/srt/lora/lora.py
index c154e1c55..edb41e6c9 100644
--- a/python/sglang/srt/lora/lora.py
+++ b/python/sglang/srt/lora/lora.py
@@ -19,6 +19,7 @@
# https://github.com/vllm-project/vllm/blob/4abf6336ec65c270343eb895e7b18786e9274176/vllm/lora/layers.py
import logging
+import os
import re
from typing import Dict, List, Optional
@@ -142,6 +143,17 @@ class LoRAAdapter(nn.Module):
def initialize_weights(self):
model_path = self.config.path
+ from sglang.srt.unique_adapter_cpu_store import weights as shared_weights
+ tensors = shared_weights(model_path)
+ if tensors is not None:
+ # Already normalized once by the CPU owner, bitwise-checked against
+ # these normalization methods. Keep views; no per-TP-rank clone.
+ for name, tensor in tensors.items():
+ layer_id = get_layer_id(name)
+ if layer_id is None:
+ raise ValueError(f"Unexpected shared CPU tensor: {name}")
+ self.layers[layer_id].weights[name] = tensor
+ return
loader = DefaultModelLoader(self.load_config)
revision = getattr(self.config.hf_config, "revision", None)
@@ -164,6 +176,9 @@ class LoRAAdapter(nn.Module):
def _process_weight(self, name: str, loaded_weight: torch.Tensor):
from sglang.srt.lora.utils import get_normalized_target_modules
+ if os.environ.get("UNIQUE_ADAPTER_AUDIT"):
+ loaded_weight = loaded_weight.cpu().clone()
+
normalized_target_modules = get_normalized_target_modules(
self.config.target_modules
)
diff --git a/python/sglang/srt/lora/lora_manager.py b/python/sglang/srt/lora/lora_manager.py
index 9327307b3..346e92b5d 100644
--- a/python/sglang/srt/lora/lora_manager.py
+++ b/python/sglang/srt/lora/lora_manager.py
@@ -16,6 +16,8 @@
# and "Punica: Multi-Tenant LoRA Serving"
import logging
+import os
+import time
import re
from typing import Dict, Iterable, List, Optional, Sequence
@@ -314,6 +316,14 @@ class LoRAManager:
# keep metadata for displayed messages
self.lora_refs[lora_ref.lora_id] = lora_ref
self.num_pinned_loras += int(lora_ref.pinned)
+ if os.environ.get("UNIQUE_ADAPTER_RESIDENT") == "1":
+ # Explicit finite all-resident diagnostic, never enabled in E2E.
+ self.fetch_new_loras(set(self.loras))
+ torch.cuda.synchronize()
+ if os.environ.get("UNIQUE_ADAPTER_AUDIT"):
+ # The HTTP response comes from TP0. Finish CPU staging (and,
+ # for the diagnostic, GPU copies) on every rank before it replies.
+ get_parallel().tp_group.barrier()
except Exception as e:
return self.create_lora_update_result(
success=False,
@@ -453,6 +463,7 @@ class LoRAManager:
new_uids = {
uid for uid in cur_uids if uid not in self.memory_pool.uid_to_buffer_id
}
+ audit_start = time.monotonic_ns()
self.memory_pool.prepare_lora_batch(
cur_uids=cur_uids,
lora_adapters=self.loras,
@@ -462,6 +473,8 @@ class LoRAManager:
lora_lm_head_module=self.lm_head_module, # merge into embedding or lora module
)
if new_uids:
+ from sglang.srt.unique_adapter_audit import loading
+ loading("lora_cpu_to_gpu_batch", [self.lora_refs[u].lora_name for u in new_uids if u is not None], audit_start)
changed_slots = {self.memory_pool.uid_to_buffer_id[uid] for uid in new_uids}
self._notify_lora_slots_updated(changed_slots)
diff --git a/python/sglang/srt/lora/lora_moe_runners.py b/python/sglang/srt/lora/lora_moe_runners.py
index adddaf10d..10c94f5d5 100644
--- a/python/sglang/srt/lora/lora_moe_runners.py
+++ b/python/sglang/srt/lora/lora_moe_runners.py
@@ -185,6 +185,7 @@ class LoRAInfo:
cg_buffers: dict | None = None
lora_use_virtual_experts: bool = False
+ gate_up_interleaved: bool = False
@dataclass
@@ -336,28 +337,41 @@ def _add_lora_gate_up_delta(
if is_gated:
inter_size = gate_up_b.shape[2] // 2
lora_a_stacked = [gate_up_a[:, :, :r, :], gate_up_a[:, :, r : 2 * r, :]]
- lora_b_stacked = [
- gate_up_b[:, :, :inter_size, :],
- gate_up_b[:, :, inter_size:, :],
- ]
+ if lora_info.gate_up_interleaved:
+ lora_b_stacked = [gate_up_b[:, :, 0::2, :], gate_up_b[:, :, 1::2, :]]
+ else:
+ lora_b_stacked = [
+ gate_up_b[:, :, :inter_size, :],
+ gate_up_b[:, :, inter_size:, :],
+ ]
else:
lora_a_stacked = [gate_up_a]
lora_b_stacked = [gate_up_b]
if lora_info.lora_use_virtual_experts:
- merged_experts_fused_moe_lora_add(
- output=intermediate_cache,
- hidden_states=hidden_states,
- lora_a=gate_up_a,
- lora_b=gate_up_b,
- topk_ids=topk_ids,
- topk_weights=topk_weights,
- token_lora_mapping=token_lora_mapping,
- mul_routed_weight=False,
- experts_shared_outer_loras_a=lora_info.experts_shared_outer_loras,
- experts_shared_outer_loras_b=False,
- routing_cache=routing_cache,
- )
+ # Independent gate/up A factors cannot be fed to a single rank-r B
+ # expansion: that would silently use only the gate A factor for both.
+ # The fused expansion honors output strides, including even/odd views.
+ for i, (lora_a, lora_b) in enumerate(zip(lora_a_stacked, lora_b_stacked)):
+ if is_gated and lora_info.gate_up_interleaved:
+ output = intermediate_cache[:, :, i::2]
+ elif is_gated:
+ output = intermediate_cache[:, :, i * inter_size : (i + 1) * inter_size]
+ else:
+ output = intermediate_cache
+ merged_experts_fused_moe_lora_add(
+ output=output,
+ hidden_states=hidden_states,
+ lora_a=lora_a,
+ lora_b=lora_b,
+ topk_ids=topk_ids,
+ topk_weights=topk_weights,
+ token_lora_mapping=token_lora_mapping,
+ mul_routed_weight=False,
+ experts_shared_outer_loras_a=lora_info.experts_shared_outer_loras,
+ experts_shared_outer_loras_b=False,
+ routing_cache=routing_cache,
+ )
else:
blk = _get_moe_lora_block_config(r)
fused_moe_lora(
@@ -387,6 +401,7 @@ def _add_lora_gate_up_delta(
expand_num_warps=4,
expand_num_stages=2,
expand_split_k=1,
+ interleaved_output=is_gated and lora_info.gate_up_interleaved,
)
diff --git a/python/sglang/srt/managers/io_struct.py b/python/sglang/srt/managers/io_struct.py
index 45d6228da..100d0a35e 100644
--- a/python/sglang/srt/managers/io_struct.py
+++ b/python/sglang/srt/managers/io_struct.py
@@ -323,6 +323,10 @@ class GenerateReqInput:
priority: Optional[int] = None
# Extra cache key for caller-defined request classification.
extra_key: Optional[Union[List[str], str]] = None
+ # Name of a registered soft prompt (see /load_soft_prompt). The prompt text must contain
+ # the soft-prompt marker (or input_ids a placeholder run); those positions receive the
+ # tenant's learned embeddings and the prefix cache is namespaced by the soft prompt.
+ soft_prompt: Optional[Union[List[Optional[str]], str]] = None
# Whether to disallow logging for this request (e.g. due to ZDR)
no_logs: bool = False
@@ -571,7 +575,7 @@ class GenerateReqInput:
self.token_ids_logprob = None
if self.return_sampling_mask is None:
self.return_sampling_mask = False
- for field_name in ("extra_key", "cache_salt"):
+ for field_name in ("extra_key", "cache_salt", "soft_prompt"):
value = getattr(self, field_name)
if value is not None and not isinstance(value, str):
raise ValueError(
@@ -608,6 +612,7 @@ class GenerateReqInput:
self._normalize_return_hidden_states(num)
self._normalize_custom_logit_processor(num)
self._normalize_extra_key(num)
+ self._normalize_soft_prompt(num)
self._normalize_cache_salt(num)
self._normalize_kv_hints(num)
self._normalize_bootstrap_params(num)
@@ -865,6 +870,24 @@ class GenerateReqInput:
else:
raise ValueError("extra_key should be a list or a string.")
+ def _normalize_soft_prompt(self, num):
+ """Normalize soft_prompt for batch processing (same shape rules as extra_key)."""
+ if self.soft_prompt is None:
+ return
+ if isinstance(self.soft_prompt, str):
+ self.soft_prompt = [self.soft_prompt or None] * num
+ elif isinstance(self.soft_prompt, list):
+ if len(self.soft_prompt) != self.batch_size:
+ raise ValueError(
+ "The length of soft_prompt should be equal to the batch size."
+ )
+ if any(v is not None and not isinstance(v, str) for v in self.soft_prompt):
+ raise ValueError("Every soft_prompt should be a string or None.")
+ self.soft_prompt = [v or None for v in self.soft_prompt]
+ self.soft_prompt = self.soft_prompt * self.parallel_sample_num
+ else:
+ raise ValueError("soft_prompt should be a list or a string.")
+
def _normalize_cache_salt(self, num):
"""Normalize cache_salt for batch processing."""
if self.cache_salt is None:
@@ -1036,6 +1059,7 @@ class GenerateReqInput:
max_thinking_tokens=self.max_thinking_tokens,
priority=self.priority,
extra_key=self.extra_key[i] if self.extra_key is not None else None,
+ soft_prompt=self.soft_prompt[i] if self.soft_prompt is not None else None,
cache_salt=(self.cache_salt[i] if self.cache_salt is not None else None),
kv_hints=(self.kv_hints[i] if self.kv_hints is not None else None),
no_logs=self.no_logs,
@@ -1133,6 +1157,11 @@ class TokenizedGenerateReqInput(BaseReq, kw_only=True):
# Extra cache key for caller-defined request classification.
extra_key: Optional[str] = None
+ # Resolved soft prompt: registered name, its version, and the absolute position of the
+ # first placeholder token in input_ids (the run length is the registered prompt length).
+ soft_prompt: Optional[str] = None
+ soft_prompt_version: Optional[int] = None
+ soft_prompt_start: Optional[int] = None
# Whether to disallow logging for this request (e.g. due to ZDR)
no_logs: bool = False
@@ -2564,6 +2593,32 @@ LoadLoRAAdapterReqOutput = UnloadLoRAAdapterReqOutput = (
) = LoRAUpdateOutput
+class LoadSoftPromptReqInput(BaseReq, kw_only=True):
+ # Name used by requests (`soft_prompt=<name>`).
+ name: str
+ # Path to a safetensors file holding exactly one tensor of shape [length, hidden_size].
+ path: str
+ # Experiment-only CPU staging; no GPU copy or request registration.
+ cpu_only: bool = False
+ # Assigned by the TokenizerManager; bumps on every (re)load so cached prefixes of an
+ # older prompt with the same name are never reused.
+ version: Optional[int] = None
+
+
+class UnloadSoftPromptReqInput(BaseReq, kw_only=True):
+ name: str
+
+
+class SoftPromptUpdateOutput(BaseReq, kw_only=True):
+ success: bool
+ error_message: Optional[str] = None
+ # name -> prompt length (number of placeholder tokens) for all loaded prompts.
+ loaded: Optional[Dict[str, int]] = None
+
+
+LoadSoftPromptReqOutput = UnloadSoftPromptReqOutput = SoftPromptUpdateOutput
+
+
class BlockReqType(Enum):
BLOCK = 1
UNBLOCK = 2
diff --git a/python/sglang/srt/managers/schedule_batch.py b/python/sglang/srt/managers/schedule_batch.py
index 45ff08e36..8aa9390ff 100755
--- a/python/sglang/srt/managers/schedule_batch.py
+++ b/python/sglang/srt/managers/schedule_batch.py
@@ -5,6 +5,7 @@ from sglang.srt.model_executor.forward_batch_info import ForwardBatch
from sglang.srt.runtime_context import (
get_disagg,
get_exec,
+ get_lora,
get_parallel,
get_schedule,
get_serving,
@@ -141,6 +142,10 @@ from sglang.srt.observability.req_time_stats import (
from sglang.srt.sampling.sampling_batch_info import SamplingBatchInfo
from sglang.srt.sampling.sampling_mask import SamplingMaskRows
from sglang.srt.sampling.sampling_params import SamplingParams
+from sglang.srt.soft_prompt.scheduling import (
+ map_soft_prompt_ids_for_model,
+ soft_prompt_extend_overrides,
+)
from sglang.srt.utils import flatten_nested_list
from sglang.srt.utils.token_sequence_matcher import TokenSequenceMatcher
@@ -1137,6 +1142,8 @@ class Req(ReqDllmMixin):
self.extra_key = extra_key
self.cache_salt = cache_salt or None
+ # Set by soft_prompt.scheduling.attach_soft_prompt for soft-prompt requests.
+ self.soft_prompt = None
self.lora_id = lora_id
self.routing_key = routing_key
@@ -2773,6 +2780,18 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
# Init tensors
reqs = self.reqs
input_ids = [r.get_fill_ids()[r.prefix_len :] for r in reqs]
+ if any(r.soft_prompt is not None for r in reqs):
+ # Soft-prompt spans carry per-prompt ids (prefix-cache keys); the model gets the
+ # in-vocab placeholder id there and the prompt's embeddings via replace_embeds.
+ placeholder_id = get_lora().soft_prompt_placeholder_id
+ input_ids = [
+ (
+ map_soft_prompt_ids_for_model(ids, placeholder_id)
+ if r.soft_prompt is not None
+ else ids
+ )
+ for r, ids in zip(reqs, input_ids)
+ ]
extend_num_tokens = sum(len(ids) for ids in input_ids)
seq_lens = [r.extend_end for r in reqs]
orig_seq_lens = [max(r.extend_end, len(r.origin_input_ids)) for r in reqs]
@@ -2875,6 +2894,17 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
req.positional_embed_overrides.embeds[list(indices)]
)
all_replace_positions.extend(positions)
+ if req.soft_prompt is not None:
+ # Placeholder positions inside this extend chunk get the tenant's
+ # embeddings; positions already in the cached prefix need nothing.
+ sp_overrides = soft_prompt_extend_overrides(
+ req, pre_len, req.extend_len, input_id_pointer
+ )
+ if sp_overrides is not None:
+ has_replace_embeds = True
+ sp_embeds, sp_positions = sp_overrides
+ all_replace_embeds.append(sp_embeds)
+ all_replace_positions.extend(sp_positions)
input_id_pointer += input_id_lens[i]
multimodal_inputs.append(req.multimodal_inputs)
@@ -2968,8 +2998,10 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
extend_input_logprob_token_ids = None
if has_replace_embeds:
- replace_embeds_tensor = torch.cat(all_replace_embeds, dim=0).to(
- self.device, non_blocking=True
+ # Soft-prompt rows already live on the device; other overrides may be on CPU.
+ replace_embeds_tensor = torch.cat(
+ [e.to(self.device, non_blocking=True) for e in all_replace_embeds],
+ dim=0,
)
replace_positions_tensor = torch.tensor(
all_replace_positions, dtype=torch.long, device=self.device
@@ -3605,6 +3637,10 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
# Decode embeds the last output token via embed_tokens; clear the stale
# prefill-time tensor so it doesn't leak into ForwardBatch.
self.input_embeds = None
+ # Embedding overrides apply to extend tokens only. Leaving them set would make every
+ # later decode step of this batch skip the decode CUDA graph (it refuses overrides).
+ self.replace_embeds = None
+ self.replace_positions = None
self.mamba_cow_src_indices = None
self.mamba_cow_dst_indices = None
diff --git a/python/sglang/srt/managers/scheduler.py b/python/sglang/srt/managers/scheduler.py
index 0d0ec41b6..b6498a4df 100644
--- a/python/sglang/srt/managers/scheduler.py
+++ b/python/sglang/srt/managers/scheduler.py
@@ -159,6 +159,7 @@ from sglang.srt.managers.io_struct import (
LoadLoRAAdapterFromTensorsReqOutput,
LoadLoRAAdapterReqInput,
LoadLoRAAdapterReqOutput,
+ LoadSoftPromptReqInput,
MMInputsProcessError,
OpenSessionReqInput,
PauseGenerationReqInput,
@@ -178,10 +179,12 @@ from sglang.srt.managers.io_struct import (
ShutdownReq,
SlowDownReqInput,
SlowDownReqOutput,
+ SoftPromptUpdateOutput,
TokenizedEmbeddingReqInput,
TokenizedGenerateReqInput,
UnloadLoRAAdapterReqInput,
UnloadLoRAAdapterReqOutput,
+ UnloadSoftPromptReqInput,
UpdateWeightFromDiskReqInput,
UpdateWeightsFromDistributedReqInput,
UpdateWeightsFromIPCReqInput,
@@ -327,6 +330,8 @@ from sglang.srt.rust_server.server import RustServer
from sglang.srt.sampling.sampling_batch_info import SamplingBatchInfo
from sglang.srt.server_args import PortArgs, ServerArgs
from sglang.srt.session.session_controller import SessionController
+from sglang.srt.soft_prompt.pool import get_soft_prompt_pool, read_soft_prompt_tensor
+from sglang.srt.soft_prompt.scheduling import attach_soft_prompt
from sglang.srt.speculative.base_spec_worker import BaseSpecWorker
from sglang.srt.speculative.dflash_utils import validate_dflash_request
from sglang.srt.speculative.eagle_utils import (
@@ -1851,6 +1856,8 @@ class Scheduler(
self.load_lora_adapter_from_tensors,
),
(UnloadLoRAAdapterReqInput, self.unload_lora_adapter),
+ (LoadSoftPromptReqInput, self.load_soft_prompt),
+ (UnloadSoftPromptReqInput, self.unload_soft_prompt),
(PauseGenerationReqInput, self.pause_generation),
(ContinueGenerationReqInput, self.continue_generation),
(ConfigureLoggingReq, self.configure_logging),
@@ -2943,6 +2950,8 @@ class Scheduler(
token_indices_to_pool=recv_req.token_indices_to_pool,
)
req.tokenizer = self.tokenizer
+ if recv_req.soft_prompt is not None:
+ attach_soft_prompt(req, recv_req)
if radix_native_session:
req.session_generation = self.tree_cache.ensure_session_generation(
@@ -5898,6 +5907,48 @@ class Scheduler(
)
return result
+ def load_soft_prompt(
+ self, recv_req: LoadSoftPromptReqInput
+ ) -> SoftPromptUpdateOutput:
+ """Load (or reload in place) a tenant soft prompt into the GPU pool."""
+ try:
+ pool = get_soft_prompt_pool()
+ if pool is None:
+ raise ValueError("soft prompt pool is not initialized")
+ import time
+ from sglang.srt.unique_adapter_audit import loading
+ started = time.monotonic_ns()
+ if not hasattr(pool, "cpu_staged"):
+ pool.cpu_staged = {}
+ if recv_req.cpu_only:
+ # Clone materializes the mmap-backed safetensor in CPU RAM.
+ pool.cpu_staged[recv_req.name] = read_soft_prompt_tensor(recv_req.path).clone()
+ get_parallel().tp_group.barrier()
+ return SoftPromptUpdateOutput(rid=recv_req.rid, success=True, loaded={recv_req.name: pool.cpu_staged[recv_req.name].shape[0]})
+ tensor = pool.cpu_staged.pop(recv_req.name, None)
+ if tensor is None:
+ tensor = read_soft_prompt_tensor(recv_req.path)
+ pool.load(recv_req.name, tensor, recv_req.version or 0)
+ loading("sp_cpu_to_gpu", recv_req.name, started)
+ if os.environ.get("UNIQUE_ADAPTER_RESIDENT") == "1":
+ torch.cuda.synchronize()
+ get_parallel().tp_group.barrier()
+ return SoftPromptUpdateOutput(rid=recv_req.rid, success=True, loaded=pool.loaded())
+ except Exception as e: # report to the caller instead of killing the scheduler
+ return SoftPromptUpdateOutput(rid=recv_req.rid, success=False, error_message=str(e))
+
+ def unload_soft_prompt(
+ self, recv_req: UnloadSoftPromptReqInput
+ ) -> SoftPromptUpdateOutput:
+ try:
+ pool = get_soft_prompt_pool()
+ if pool is None:
+ raise ValueError("soft prompt pool is not initialized")
+ pool.unload(recv_req.name)
+ return SoftPromptUpdateOutput(rid=recv_req.rid, success=True, loaded=pool.loaded())
+ except Exception as e:
+ return SoftPromptUpdateOutput(rid=recv_req.rid, success=False, error_message=str(e))
+
def load_lora_adapter_from_tensors(
self, recv_req: LoadLoRAAdapterFromTensorsReqInput
) -> LoadLoRAAdapterFromTensorsReqOutput:
diff --git a/python/sglang/srt/managers/scheduler_components/batch_result_processor.py b/python/sglang/srt/managers/scheduler_components/batch_result_processor.py
index 928e3cb41..123d1f042 100644
--- a/python/sglang/srt/managers/scheduler_components/batch_result_processor.py
+++ b/python/sglang/srt/managers/scheduler_components/batch_result_processor.py
@@ -14,6 +14,7 @@ from typing import (
import numpy as np
import torch
+from sglang.srt.unique_adapter_audit import token as audit_unique_token
from sglang.srt.disaggregation.utils import DisaggregationMode
from sglang.srt.environ import envs
@@ -363,6 +364,7 @@ class SchedulerBatchResultProcessor:
else:
# req output_ids are set here
req.output_ids.append(next_token_id)
+ audit_unique_token(req)
self._maybe_update_reasoning_tokens(req, next_token_id)
@@ -1034,6 +1036,7 @@ class SchedulerBatchResultProcessor:
new_accept_len = 0
else:
req.output_ids.extend(next_token_id)
+ audit_unique_token(req)
new_accept_len = len(next_token_id)
self._maybe_update_reasoning_tokens(req, next_token_id)
req.update_finish_state(new_accept_len)
diff --git a/python/sglang/srt/managers/tokenizer_control_mixin.py b/python/sglang/srt/managers/tokenizer_control_mixin.py
index f62afbae8..4e7e1ec51 100644
--- a/python/sglang/srt/managers/tokenizer_control_mixin.py
+++ b/python/sglang/srt/managers/tokenizer_control_mixin.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import asyncio
import hashlib
import logging
+import os
import time
import uuid
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple
@@ -51,6 +52,7 @@ from sglang.srt.managers.io_struct import (
LoadLoRAAdapterFromTensorsReqOutput,
LoadLoRAAdapterReqInput,
LoadLoRAAdapterReqOutput,
+ LoadSoftPromptReqInput,
LoRAUpdateOutput,
OpenSessionReqInput,
PdRoleSwitchReqInput,
@@ -71,8 +73,10 @@ from sglang.srt.managers.io_struct import (
SetInternalStateReqOutput,
SlowDownReqInput,
SlowDownReqOutput,
+ SoftPromptUpdateOutput,
UnloadLoRAAdapterReqInput,
UnloadLoRAAdapterReqOutput,
+ UnloadSoftPromptReqInput,
UpdateWeightsFromDistributedReqInput,
UpdateWeightsFromDistributedReqOutput,
UpdateWeightsFromIPCReqInput,
@@ -138,6 +142,7 @@ _COMMUNICATOR_SPECS = [
("begin_weight_update", BeginWeightUpdateReqOutput),
("end_weight_update", EndWeightUpdateReqOutput),
("update_lora_adapter", LoRAUpdateOutput),
+ ("update_soft_prompt", SoftPromptUpdateOutput),
("dumper_control", DumperControlReqOutput),
("scale_elastic_ep", ScaleElasticEPReqOutput),
]
@@ -184,7 +189,11 @@ class TokenizerControlMixin:
for spec in _COMMUNICATOR_SPECS:
name, resp_type = spec[0], spec[1]
mode = spec[2] if len(spec) > 2 else "queueing"
- comm = FanOutCommunicator(
+ communicator = FanOutCommunicator
+ if name == "update_soft_prompt" and os.environ.get("UNIQUE_ADAPTER_SP_CONCURRENT_UPDATES") == "1":
+ from sglang.srt.soft_prompt.control import PromptUpdateCommunicator
+ communicator = PromptUpdateCommunicator
+ comm = communicator(
self._dispatch_to_scheduler,
get_parallel().num_dp_ranks,
mode,
@@ -824,6 +833,84 @@ class TokenizerControlMixin:
error_message=str(e),
)
+ def _soft_prompt_update_lock_for(self, name):
+ if os.environ.get("UNIQUE_ADAPTER_SP_CONCURRENT_UPDATES") != "1":
+ return self.soft_prompt_update_lock
+ from sglang.srt.soft_prompt.control import PromptNameLocks
+ if not hasattr(self, "_soft_prompt_name_locks"):
+ self._soft_prompt_name_locks = PromptNameLocks()
+ return self._soft_prompt_name_locks.get(name)
+
+ async def load_soft_prompt(
+ self: TokenizerManager,
+ obj: LoadSoftPromptReqInput,
+ _: Optional[fastapi.Request] = None,
+ ) -> SoftPromptUpdateOutput:
+ self.auto_create_handle_loop()
+ try:
+ if not get_lora().enable_soft_prompt:
+ raise ValueError(
+ "Soft prompts are not enabled. Please set `--enable-soft-prompt`."
+ )
+ if get_parallel().dp_size != 1:
+ raise ValueError(
+ "data-parallel replicas are not supported for soft prompts"
+ )
+ from sglang.srt.soft_prompt.control import audit_update
+ update_started = time.monotonic_ns()
+ async with self._soft_prompt_update_lock_for(obj.name):
+ update_acquired = time.monotonic_ns()
+ obj.version = self.soft_prompt_next_version
+ self.soft_prompt_next_version += 1
+ results = await self.update_soft_prompt_communicator(obj)
+ audit_update(obj.name, "load" if isinstance(obj, LoadSoftPromptReqInput) else "unload",
+ update_started, update_acquired)
+ failed = [r for r in results if not r.success]
+ if failed:
+ return failed[0]
+ if obj.cpu_only and os.environ.get("UNIQUE_ADAPTER_REGISTERED") != "1":
+ return results[0]
+ length = results[0].loaded[obj.name]
+ self.soft_prompt_registry[obj.name] = (length, obj.version)
+ logger.info(
+ "Loaded soft prompt %s (length=%d, version=%d) from %s",
+ obj.name,
+ length,
+ obj.version,
+ obj.path,
+ )
+ return results[0]
+ except ValueError as e:
+ return SoftPromptUpdateOutput(success=False, error_message=str(e))
+
+ async def unload_soft_prompt(
+ self: TokenizerManager,
+ obj: UnloadSoftPromptReqInput,
+ _: Optional[fastapi.Request] = None,
+ ) -> SoftPromptUpdateOutput:
+ self.auto_create_handle_loop()
+ try:
+ if not get_lora().enable_soft_prompt:
+ raise ValueError(
+ "Soft prompts are not enabled. Please set `--enable-soft-prompt`."
+ )
+ from sglang.srt.soft_prompt.control import audit_update
+ update_started = time.monotonic_ns()
+ async with self._soft_prompt_update_lock_for(obj.name):
+ update_acquired = time.monotonic_ns()
+ if obj.name not in self.soft_prompt_registry:
+ raise ValueError(f"soft prompt {obj.name!r} is not loaded")
+ # Stop admitting new requests first. Known limitation: a request already
+ # tokenized against this prompt but not yet prefilled fails at prefill.
+ del self.soft_prompt_registry[obj.name]
+ results = await self.update_soft_prompt_communicator(obj)
+ audit_update(obj.name, "load" if isinstance(obj, LoadSoftPromptReqInput) else "unload",
+ update_started, update_acquired)
+ failed = [r for r in results if not r.success]
+ return failed[0] if failed else results[0]
+ except ValueError as e:
+ return SoftPromptUpdateOutput(success=False, error_message=str(e))
+
async def unload_lora_adapter(
self: TokenizerManager,
obj: UnloadLoRAAdapterReqInput,
diff --git a/python/sglang/srt/managers/tokenizer_manager.py b/python/sglang/srt/managers/tokenizer_manager.py
index e8e4b450f..f99f32194 100644
--- a/python/sglang/srt/managers/tokenizer_manager.py
+++ b/python/sglang/srt/managers/tokenizer_manager.py
@@ -175,6 +175,13 @@ from sglang.srt.server_args import (
PortArgs,
ServerArgs,
)
+from sglang.srt.soft_prompt.utils import (
+ SOFT_PROMPT_LOGIT_BIAS,
+ SOFT_PROMPT_MARKER,
+ parse_soft_prompt_paths,
+ soft_prompt_length,
+ soft_prompt_token_ids,
+)
from sglang.srt.utils import (
configure_gc_warning,
freeze_gc,
@@ -786,6 +793,15 @@ class TokenizerManager(TokenizerControlMixin, TokenizerManagerScoreMixin):
# Please note that, unlike `model_update_lock`, this does not block inference, allowing
# LoRA updates and inference to overlap.
self.lora_update_lock = asyncio.Lock()
+ # Soft prompts: name -> (length, version). Every (re)load gets a unique version
+ # (startup prompts 1..k in --soft-prompt-paths order, matching the pool), which
+ # selects the prompt's per-prompt token ids and hence its prefix-cache keys.
+ self.soft_prompt_registry: Dict[str, Tuple[int, int]] = {}
+ self.soft_prompt_update_lock = asyncio.Lock()
+ startup_prompts = parse_soft_prompt_paths(get_lora().soft_prompt_paths)
+ for version, (name, path) in enumerate(startup_prompts, start=1):
+ self.soft_prompt_registry[name] = (soft_prompt_length(path), version)
+ self.soft_prompt_next_version = len(startup_prompts) + 1
# A cache for mapping the lora_name for LoRA adapters that have been loaded at any
# point to their latest LoRARef objects, so that they can be
# dynamically loaded if needed for inference
@@ -1282,10 +1298,89 @@ class TokenizerManager(TokenizerControlMixin, TokenizerManagerScoreMixin):
else:
mm_inputs = None
+ soft_prompt_meta = None
+ if isinstance(obj, GenerateReqInput) and obj.soft_prompt is not None:
+ input_ids, soft_prompt_meta = self._resolve_soft_prompt(
+ obj, input_text, input_ids, input_embeds, mm_inputs
+ )
+
self._validate_one_request(obj, input_ids)
- return self._create_tokenized_object(
+ if soft_prompt_meta is not None:
+ # After validation (which requires in-vocab ids): give the soft-prompt run its
+ # per-prompt ids so the prefix cache keys it by prompt and version.
+ _, sp_version, sp_start = soft_prompt_meta
+ sp_length = self.soft_prompt_registry[obj.soft_prompt][0]
+ input_ids = (
+ list(input_ids[:sp_start])
+ + soft_prompt_token_ids(
+ sp_version, sp_length, get_lora().max_soft_prompt_len
+ )
+ + list(input_ids[sp_start + sp_length :])
+ )
+ tokenized_obj = self._create_tokenized_object(
obj, input_text, input_ids, input_embeds, mm_inputs, token_type_ids
)
+ if soft_prompt_meta is not None:
+ (
+ tokenized_obj.soft_prompt,
+ tokenized_obj.soft_prompt_version,
+ tokenized_obj.soft_prompt_start,
+ ) = soft_prompt_meta
+ return tokenized_obj
+
+ def _resolve_soft_prompt(
+ self: TokenizerManager, obj, input_text, input_ids, input_embeds, mm_inputs
+ ):
+ """Place the placeholder run for ``obj.soft_prompt`` and return (input_ids, meta).
+
+ Text prompts mark the position with SOFT_PROMPT_MARKER (exactly once); token-id
+ prompts must already contain one contiguous run of the placeholder id whose length
+ equals the registered prompt length.
+ """
+ lora_args = get_lora()
+ if not lora_args.enable_soft_prompt:
+ raise ValueError(
+ "soft_prompt was given but soft prompts are disabled; launch with --enable-soft-prompt."
+ )
+ if input_embeds is not None or mm_inputs is not None:
+ raise ValueError(
+ "soft_prompt cannot be combined with input_embeds or multimodal inputs."
+ )
+ name = obj.soft_prompt
+ if name not in self.soft_prompt_registry:
+ raise ValueError(f"soft prompt {name!r} is not loaded.")
+ length, version = self.soft_prompt_registry[name]
+ placeholder = lora_args.soft_prompt_placeholder_id
+
+ if obj.input_ids is None and input_text is not None:
+ if input_text.count(SOFT_PROMPT_MARKER) != 1:
+ raise ValueError(
+ f"a text prompt with soft_prompt must contain {SOFT_PROMPT_MARKER!r} exactly once."
+ )
+ pre, post = input_text.split(SOFT_PROMPT_MARKER)
+ input_ids = (
+ (self.tokenizer.encode(pre) if pre else [])
+ + [placeholder] * length
+ + self.tokenizer.encode(post, add_special_tokens=False)
+ )
+ positions = [i for i, t in enumerate(input_ids) if t == placeholder]
+ if not positions:
+ raise ValueError(
+ f"input_ids for soft prompt {name!r} contain no placeholder id {placeholder}."
+ )
+ start = positions[0]
+ if positions != list(range(start, start + length)):
+ raise ValueError(
+ f"soft prompt {name!r} needs exactly one contiguous run of {length} placeholder ids."
+ )
+
+ # The placeholder id is input-only: never let it be sampled.
+ sampling_params = dict(obj.sampling_params or {})
+ logit_bias = dict(sampling_params.get("logit_bias") or {})
+ logit_bias[str(placeholder)] = SOFT_PROMPT_LOGIT_BIAS
+ sampling_params["logit_bias"] = logit_bias
+ obj.sampling_params = sampling_params
+ return input_ids, (name, version, start)
@staticmethod
def _normalize_mm_content_hashes(obj: GenerateReqInput) -> None:
diff --git a/python/sglang/srt/model_executor/forward_batch_info.py b/python/sglang/srt/model_executor/forward_batch_info.py
index 0f10d5f2a..6bb20f5d5 100644
--- a/python/sglang/srt/model_executor/forward_batch_info.py
+++ b/python/sglang/srt/model_executor/forward_batch_info.py
@@ -1050,8 +1050,16 @@ class ForwardBatch(ForwardBatchDeepSeekMHAMixin):
encoder_lens=batch.encoder_lens,
encoder_out_cache_loc=batch.encoder_out_cache_loc,
input_embeds=batch.input_embeds,
- replace_embeds=batch.replace_embeds,
- replace_positions=batch.replace_positions,
+ # Embedding overrides index the target's extend tokens. Draft models see a
+ # different token layout (EAGLE rotates ids left by one; DFlash feeds its own
+ # input_embeds), so they do not inherit them; EAGLE re-adds them rotated in
+ # _draft_extend_for_prefill.
+ replace_embeds=(
+ None if model_runner.is_draft_worker else batch.replace_embeds
+ ),
+ replace_positions=(
+ None if model_runner.is_draft_worker else batch.replace_positions
+ ),
# Scalar config / flags
return_logprob=batch.return_logprob,
is_extend_in_batch=batch.is_extend_in_batch,
diff --git a/python/sglang/srt/model_executor/model_runner.py b/python/sglang/srt/model_executor/model_runner.py
index e1c12af64..ce380fbe9 100644
--- a/python/sglang/srt/model_executor/model_runner.py
+++ b/python/sglang/srt/model_executor/model_runner.py
@@ -724,6 +724,7 @@ class ModelRunner:
)
self.maybe_apply_post_load_model_transforms()
self.maybe_init_lora_manager()
+ self.maybe_init_soft_prompt_pool()
self.maybe_enable_batch_invariant_mode()
self.configure_kv_cache_dtype()
@@ -814,6 +815,39 @@ class ModelRunner:
if get_parallel().tp_size > 1 and supports_torch_tp:
self.apply_torch_tp()