From 968726f3cea0f6d6f44bea84797e5e5914658f4e Mon Sep 17 00:00:00 2001 From: ADiL Date: Tue, 6 Oct 2026 00:33:49 +0200 Subject: [PATCH] [HiCache] Route --file-storage-path to the file storage backend (#33883) Signed-off-by: adlashab --- .../sglang/srt/arg_groups/fields/serving.py | 4 +- .../sglang/srt/mem_cache/hicache_storage.py | 6 +- .../srt/mem_cache/unified_radix_cache.py | 5 + .../test_hicache_file_storage_path_unit.py | 91 +++++++++++++++++++ 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 test/registered/unit/mem_cache/test_hicache_file_storage_path_unit.py diff --git a/python/sglang/srt/arg_groups/fields/serving.py b/python/sglang/srt/arg_groups/fields/serving.py index 1cb15c02e4..3a00b879c9 100644 --- a/python/sglang/srt/arg_groups/fields/serving.py +++ b/python/sglang/srt/arg_groups/fields/serving.py @@ -183,9 +183,9 @@ class Serving(msgspec.Struct): "The buliltin completion template name or the path of the completion template file. This is only used for OpenAI-compatible API server. only for code completion currently.", ] = None file_storage_path: A[ - str, + Optional[str], "The path of the file storage in backend.", - ] = "sglang_storage" + ] = None enable_cache_report: A[ bool, "Return number of cached tokens in usage.prompt_tokens_details for each openai request.", diff --git a/python/sglang/srt/mem_cache/hicache_storage.py b/python/sglang/srt/mem_cache/hicache_storage.py index 75552d7071..fd795d55d6 100644 --- a/python/sglang/srt/mem_cache/hicache_storage.py +++ b/python/sglang/srt/mem_cache/hicache_storage.py @@ -382,7 +382,11 @@ class HiCacheFile(HiCacheStorage): def __init__( self, storage_config: HiCacheStorageConfig, file_path: str = "/tmp/hicache" ): - self.file_path = envs.SGLANG_HICACHE_FILE_BACKEND_STORAGE_DIR.get() or file_path + self.file_path = ( + envs.SGLANG_HICACHE_FILE_BACKEND_STORAGE_DIR.get() + or (storage_config.extra_config or {}).get("file_storage_path") + or file_path + ) tp_rank, tp_size, pp_rank, pp_size, model_name, is_mla_model = ( storage_config.tp_rank, diff --git a/python/sglang/srt/mem_cache/unified_radix_cache.py b/python/sglang/srt/mem_cache/unified_radix_cache.py index 8515fd4890..e333f8bbcc 100644 --- a/python/sglang/srt/mem_cache/unified_radix_cache.py +++ b/python/sglang/srt/mem_cache/unified_radix_cache.py @@ -442,6 +442,11 @@ class UnifiedRadixCache(BasePrefixCache): ) = HybridCacheController.parse_storage_backend_extra_config( get_memory().hicache_storage_backend_extra_config ) + if server_args.file_storage_path: + storage_extra_config = dict(storage_extra_config or {}) + storage_extra_config.setdefault( + "file_storage_path", server_args.file_storage_path + ) storage_prefetch_threshold = ( self._storage_attachment.resolve_prefetch_threshold( storage_prefetch_threshold diff --git a/test/registered/unit/mem_cache/test_hicache_file_storage_path_unit.py b/test/registered/unit/mem_cache/test_hicache_file_storage_path_unit.py new file mode 100644 index 0000000000..1f188707e2 --- /dev/null +++ b/test/registered/unit/mem_cache/test_hicache_file_storage_path_unit.py @@ -0,0 +1,91 @@ +""" +Unit test for --file-storage-path routing to the file HiCache backend, plus the +regression guard that an UNSET flag keeps HiCacheFile on its /tmp/hicache default +instead of routing the arg's default value into the backend. + +Pure CPU test; no server, no CUDA. +Run with: + python3 -m pytest test/registered/unit/mem_cache/test_hicache_file_storage_path_unit.py -v +""" + +from sglang.test.ci.ci_register import register_cpu_ci + +register_cpu_ci(est_time=10, suite="base-a-test-cpu") + +import os +import unittest + +import msgspec + +from sglang.srt.mem_cache.hicache_storage import HiCacheFile, HiCacheStorageConfig +from sglang.srt.server_args import ServerArgs +from sglang.test.test_utils import CustomTestCase + +_ENV = "SGLANG_HICACHE_FILE_BACKEND_STORAGE_DIR" + + +def _make_config(extra_config): + # tp_rank=1 so HiCacheFile does not create the storage dir as a side effect. + return HiCacheStorageConfig( + tp_rank=1, + tp_size=1, + pp_rank=0, + pp_size=1, + attn_cp_rank=0, + attn_cp_size=1, + is_mla_model=True, + enable_storage_metrics=False, + is_page_first_layout=True, + model_name="testmodel", + extra_config=extra_config, + ) + + +def _inject(file_storage_path, extra_config=None): + # Mirrors the HiRadixCache / UnifiedRadixCache routing: only route the arg + # when it is set, so an unset (default) value is left out of extra_config. + extra_config = dict(extra_config or {}) + if file_storage_path: + extra_config.setdefault("file_storage_path", file_storage_path) + return extra_config + + +class TestFileStoragePathRouting(CustomTestCase): + def setUp(self): + self._saved = os.environ.pop(_ENV, None) + + def tearDown(self): + if self._saved is not None: + os.environ[_ENV] = self._saved + else: + os.environ.pop(_ENV, None) + + def test_arg_default_is_unset(self): + # If the default were a real path, an unset flag would silently reroute the + # backend off its /tmp/hicache default, so keep it None. + # ServerArgs is assembled as a msgspec Struct now, so introspect the + # declared default the same way server_args._declared_default does. + field = {f.name: f for f in msgspec.structs.fields(ServerArgs)}[ + "file_storage_path" + ] + self.assertIsNone(field.default) + + def test_unset_flag_keeps_tmp_hicache(self): + extra = _inject(None) + self.assertNotIn("file_storage_path", extra) + self.assertEqual(HiCacheFile(_make_config(extra)).file_path, "/tmp/hicache") + + def test_set_flag_is_routed(self): + extra = _inject("/mnt/nvme/hicache") + self.assertEqual( + HiCacheFile(_make_config(extra)).file_path, "/mnt/nvme/hicache" + ) + + def test_env_var_wins_over_flag(self): + os.environ[_ENV] = "/env/hicache" + extra = _inject("/mnt/nvme/hicache") + self.assertEqual(HiCacheFile(_make_config(extra)).file_path, "/env/hicache") + + +if __name__ == "__main__": + unittest.main()