From 38ad2517384cf59d01be1f42ca9ce2f5a94d1b2a Mon Sep 17 00:00:00 2001 From: Muqi Li Date: Sat, 28 Mar 2026 04:42:46 +0800 Subject: [PATCH] feat: add gc_threshold arg (#21481) Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- python/sglang/srt/entrypoints/engine.py | 8 ++++++++ python/sglang/srt/server_args.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/python/sglang/srt/entrypoints/engine.py b/python/sglang/srt/entrypoints/engine.py index 35ebbf1bc..bf4b24fa0 100644 --- a/python/sglang/srt/entrypoints/engine.py +++ b/python/sglang/srt/entrypoints/engine.py @@ -616,6 +616,7 @@ class Engine(EngineBase): configure_logger(server_args) _set_envs_and_config(server_args) server_args.check_server_args() + _set_gc(server_args) # Allocate ports for inter-process communications if port_args is None: @@ -1179,6 +1180,13 @@ def _set_envs_and_config(server_args: ServerArgs): mp.set_start_method("spawn", force=True) +def _set_gc(server_args: ServerArgs): + if gc_threshold := server_args.gc_threshold: + import gc + + gc.set_threshold(*gc_threshold) + + def _wait_for_scheduler_ready( scheduler_pipe_readers: List, scheduler_procs: List, diff --git a/python/sglang/srt/server_args.py b/python/sglang/srt/server_args.py index c770f3d16..d9210c656 100644 --- a/python/sglang/srt/server_args.py +++ b/python/sglang/srt/server_args.py @@ -661,6 +661,7 @@ class ServerArgs: enable_deterministic_inference: bool = False rl_on_policy_target: Optional[str] = None enable_attn_tp_input_scattered: bool = False + gc_threshold: Optional[List[int]] = None # Context parallelism used in the long sequence prefill phase of DeepSeek v3.2 enable_nsa_prefill_context_parallel: bool = False nsa_prefill_cp_mode: str = "round-robin-split" @@ -5600,6 +5601,12 @@ class ServerArgs: action="store_true", help="Enable fused moe triton and sum all reduce.", ) + parser.add_argument( + "--gc-threshold", + type=int, + nargs="+", + help="Set the garbage collection thresholds (the collection frequency). Accepts 1 to 3 integers.", + ) # Dynamic batch tokenizer parser.add_argument( @@ -6116,6 +6123,12 @@ class ServerArgs: "When enabling two batch overlap, moe_a2a_backend cannot be 'none'." ) + if self.gc_threshold: + if not (1 <= len(self.gc_threshold) <= 3): + raise ValueError( + "When setting gc_threshold, it must contain 1 to 3 integers." + ) + def check_lora_server_args(self): assert self.max_loras_per_batch > 0, "max_loras_per_batch must be positive"