diff --git a/python/sglang/srt/environ.py b/python/sglang/srt/environ.py index 12c42a3ca..edb8874d3 100644 --- a/python/sglang/srt/environ.py +++ b/python/sglang/srt/environ.py @@ -43,6 +43,14 @@ class EnvField: # NOTE: environ can only accept str values, so we need a flag to indicate # whether the env var is explicitly set to None. self._set_to_none = False + # Env vars are startup config that does not change mid-run, so the parsed + # value is cached after the first read (hot paths read flags per-layer / + # per-compose). Every mutation path through this API (set/override/clear) + # invalidates the cache. The only env vars mutated at runtime to pass + # information are NOT EnvFields (e.g. SGLANG_TMP_NCCL_COMM_VALUE, read via + # raw getenv), so they are unaffected by this cache. + self._cache_valid = False + self._cache = None def __set_name__(self, owner, name): assert EnvField._allow_set_name, "Usage like `a = envs.A` is not allowed" @@ -52,6 +60,13 @@ class EnvField: raise NotImplementedError() def get(self) -> Any: + if self._cache_valid: + return self._cache + self._cache = self._compute() + self._cache_valid = True + return self._cache + + def _compute(self) -> Any: value = os.getenv(self.name) # Explicitly set to None @@ -77,6 +92,7 @@ class EnvField: def set(self, value: Any): self._set_to_none = value is None os.environ[self.name] = str(value) + self._cache_valid = False @contextmanager def override(self, value: Any): @@ -90,10 +106,12 @@ class EnvField: else: os.environ.pop(self.name, None) self._set_to_none = backup_set_to_none + self._cache_valid = False def clear(self): os.environ.pop(self.name, None) self._set_to_none = False + self._cache_valid = False def __bool__(self): raise RuntimeError( diff --git a/python/sglang/srt/server_args.py b/python/sglang/srt/server_args.py index 39adfcf3c..56e0b2c73 100644 --- a/python/sglang/srt/server_args.py +++ b/python/sglang/srt/server_args.py @@ -3606,9 +3606,9 @@ class ServerArgs: self.enable_deterministic_inference = True # For VLM - os.environ["SGLANG_VLM_CACHE_SIZE_MB"] = "0" + envs.SGLANG_VLM_CACHE_SIZE_MB.set("0") # TODO remove this environment variable as a whole - os.environ["SGLANG_ENABLE_DETERMINISTIC_INFERENCE"] = "1" + envs.SGLANG_ENABLE_DETERMINISTIC_INFERENCE.set("1") if self.enable_deterministic_inference: if self.enable_aiter_allreduce_fusion: