Opt-in strip of thinking tokens from radix cache (#23315)

Co-authored-by: ianliuy <ianl@alumni.usc.edu>
Co-authored-by: Wen-xuan-Xu <lilmeep727@gmail.com>
This commit is contained in:
Liangsheng Yin
2026-04-20 22:59:50 -07:00
committed by GitHub
co-authored by ianliuy Wen-xuan-Xu
parent a8e3a534a4
commit a490632416
4 changed files with 72 additions and 4 deletions
+9 -2
View File
@@ -905,13 +905,20 @@ class Req(ReqDllmMixin):
return self.output_ids[: self.finished_len]
return self.output_ids
def _cache_commit_len(self) -> int:
# Report only the prompt prefix so thinking + answer fall into the
# overallocated range and are reclaimed by release_kv_cache. #22373.
if get_global_server_args().strip_thinking_cache and self.reasoning_tokens > 0:
return min(self.kv_committed_len, len(self.origin_input_ids))
return self.kv_committed_len
def pop_committed_kv_cache(self) -> int:
"""Return the length of committed KV cache and mark them as freed."""
assert (
not self.kv_committed_freed
), f"Committed KV cache already freed ({self.kv_committed_len=})"
self.kv_committed_freed = True
return self.kv_committed_len
return self._cache_commit_len()
def pop_overallocated_kv_cache(self) -> Tuple[int, int]:
"""Return the range of over-allocated KV cache and mark them as freed."""
@@ -923,7 +930,7 @@ class Req(ReqDllmMixin):
not self.kv_overallocated_freed
), f"Overallocated KV cache already freed, {self.kv_committed_len=}, {self.kv_allocated_len=}"
self.kv_overallocated_freed = True
return self.kv_committed_len, self.kv_allocated_len
return self._cache_commit_len(), self.kv_allocated_len
def update_spec_acceptance_histogram(self, accepted_draft_tokens: int):
"""Update the speculative decoding acceptance histogram.
+3 -1
View File
@@ -489,7 +489,9 @@ def release_kv_cache(req: Req, tree_cache: BasePrefixCache, is_insert: bool = Tr
page_size = global_server_args.page_size
spec_algo = global_server_args.speculative_algorithm
if spec_algo is None:
# strip_thinking_cache intentionally reports output tokens as overallocated
# so they fall into the free path below (#22373).
if spec_algo is None and not global_server_args.strip_thinking_cache:
assert (
start_p == end_p
), f"Unexpected overallocated KV cache, {req.kv_committed_len=}, {req.kv_allocated_len=}"
+8
View File
@@ -443,6 +443,7 @@ class ServerArgs:
file_storage_path: str = "sglang_storage"
enable_cache_report: bool = False
reasoning_parser: Optional[str] = None
strip_thinking_cache: bool = False
tool_call_parser: Optional[str] = None
tool_server: Optional[str] = None
sampling_defaults: str = "model"
@@ -4904,6 +4905,13 @@ class ServerArgs:
default=ServerArgs.reasoning_parser,
help=f"Specify the parser for reasoning models, supported parsers are: {list(ReasoningParser.DetectorMap.keys())}.",
)
parser.add_argument(
"--strip-thinking-cache",
action="store_true",
help="Skip caching reasoning-model output (thinking + answer) in the "
"radix tree on finish; keep only the prompt prefix. Opt-in: changes "
"cache contents.",
)
tool_call_parser_choices = list(FunctionCallParser.ToolCallParserEnum.keys())
parser.add_argument(
"--tool-call-parser",