diff --git a/python/sglang/multimodal_gen/runtime/utils/model_overlay.py b/python/sglang/multimodal_gen/runtime/utils/model_overlay.py index 6d957a3dff..d205c64eff 100644 --- a/python/sglang/multimodal_gen/runtime/utils/model_overlay.py +++ b/python/sglang/multimodal_gen/runtime/utils/model_overlay.py @@ -277,11 +277,19 @@ def _find_missing_required_paths( return missing +# Smaller files are copied, not linked: materializers rewrite configs in place, +# and a write through a link rewrites the Hugging Face cache blob behind it. +_OVERLAY_LINK_MIN_BYTES = 64 * 1024 * 1024 + + def _link_or_copy_file(src: str, dst: str) -> None: src = os.path.realpath(src) _ensure_dir(os.path.dirname(dst)) if os.path.lexists(dst): os.remove(dst) + if os.path.getsize(src) < _OVERLAY_LINK_MIN_BYTES: + shutil.copy2(src, dst) + return try: os.link(src, dst) return diff --git a/python/sglang/multimodal_gen/test/unit/test_model_overlay.py b/python/sglang/multimodal_gen/test/unit/test_model_overlay.py index cbacb77ce5..aa7c93879d 100644 --- a/python/sglang/multimodal_gen/test/unit/test_model_overlay.py +++ b/python/sglang/multimodal_gen/test/unit/test_model_overlay.py @@ -1,6 +1,10 @@ -"""Unit tests for diffusion model-overlay cache paths.""" +"""Unit tests for diffusion model-overlay cache paths and materialized trees.""" +import os + +from sglang.multimodal_gen.runtime.utils import model_overlay from sglang.multimodal_gen.runtime.utils.model_overlay import ( + _copytree_link_or_copy, get_diffusion_cache_root, ) @@ -17,3 +21,45 @@ def test_explicit_diffusion_cache_root_takes_precedence(monkeypatch): monkeypatch.setenv("XDG_CACHE_HOME", "/tmp/sglang-xdg-cache") assert get_diffusion_cache_root() == "/tmp/sglang-diffusion-cache" + + +def _hf_snapshot(tmp_path): + """A snapshot laid out like the HF cache: files are symlinks into blobs/.""" + blobs = tmp_path / "blobs" + blobs.mkdir() + config = blobs / "config-blob" + config.write_text('{"source": true}') + weights = blobs / "weights-blob" + weights.write_bytes(b"w" * 4096) + snapshot = tmp_path / "snapshot" + snapshot.mkdir() + (snapshot / "config.json").symlink_to(config) + (snapshot / "model.safetensors").symlink_to(weights) + return snapshot, config, weights + + +def test_rewriting_a_materialized_config_leaves_the_cache_blob_alone( + tmp_path, monkeypatch +): + # The LTX-2.3 overlay rewrites text_encoder/config.json after linking the + # tree; through a link that rewrote the shared cache's blob. + monkeypatch.setattr(model_overlay, "_OVERLAY_LINK_MIN_BYTES", 1024) + snapshot, config, _ = _hf_snapshot(tmp_path) + out = tmp_path / "materialized" + + _copytree_link_or_copy(str(snapshot), str(out)) + with open(out / "config.json", "w") as f: + f.write('{"source": false}') + + assert config.read_text() == '{"source": true}' + assert not os.path.islink(out / "config.json") + + +def test_large_files_are_still_shared_with_the_cache(tmp_path, monkeypatch): + monkeypatch.setattr(model_overlay, "_OVERLAY_LINK_MIN_BYTES", 1024) + snapshot, _, weights = _hf_snapshot(tmp_path) + out = tmp_path / "materialized" + + _copytree_link_or_copy(str(snapshot), str(out)) + + assert os.path.samefile(out / "model.safetensors", weights)