[diffusion] fix: copy small files into overlay materialized trees instead of linking them (#41298)

Co-authored-by: Mick Qian <mickqian@users.noreply.github.com>
This commit is contained in:
Mick
2026-09-26 12:22:47 +08:00
committed by GitHub
co-authored by Mick Qian
parent dfdbff931d
commit e538489e0e
2 changed files with 55 additions and 1 deletions
@@ -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
@@ -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)