Warm-save the compile cache by default, compile U-Net denoisers whole-module
diffusion_compile_cache: auto mode now saves the Mega-cache bundle after the first compiled generation (UNSLOTH_DIFFUSION_COMPILE_CACHE_SAVE=0 opts out), so users get warm restarts without the distributor env; a bundle hit starts clean (no pointless rewrite of the just-loaded artifacts) and explicit mode 1/on keeps the distributor-style re-save. New register_shape + manifest shape coverage: a STATIC compile produces new artifacts per (width, height, batch), so the generate path registers each generation's shape and an uncovered shape re-dirties the context, growing the bundle to cover every shape the session used. Measured (B200, real backend): Qwen-Image deferred gen-3 hitch 29.1 -> 22.2 s warm with bit-identical output (7.9 MB bundle, ~0.5 s save); SDXL gen-3 115.7 -> 24.7 s and a mid-session 768px recompile 65.8 -> 12.6 s (bundle 63.6 -> 98.7 MB after the 768 re-save). diffusion_speed: U-Net denoisers (UNet2DConditionModel; no _repeated_blocks, so the regional compile never reached them) now get a whole-module STATIC torch.compile on the default tier, plus fused QKV projections and a compiled VAE decode. Measured on SDXL (30 steps / 7.0 / 1024px, 4 prompts, LPIPS vs the bit-exact reference): 6.16 -> 3.14 s end to end (1.96x) at LPIPS 0.035, steady state 0.70-0.88 s/image through the real backend. Rejected on measurement: dynamic=True whole-module (366 s compile for 39.3 ms/step vs static's 73 s for 26.9), regional BasicTransformerBlock only (45.0 ms/step; ResNet convs stay eager), max-autotune + inductor flags (25.9 ms/step for a 445 s warmup), channels-last UNet alone (neutral). DiT tiers unchanged: fused QKV measured exactly neutral under the regional compile (Qwen-Image 6.53 vs 6.52 s), so it stays max-only there, and the DiT VAE decode stays eager (a few % of a DiT generation). compiled_shapes_are_static tells the cache layer which loads are per-shape (max tier, U-Net whole-module). diffusion: register each generation's shape with the compile cache before the save, pass pipe.unet to the cache fingerprint when the pipe has no transformer, and correct the transformer_quant resolved reason on dense loads (it claimed a GGUF transformer was loaded on every non-quantized pipeline load). Tests: 333 passing across the related suites (speed 42, compile_cache 27, cache 40, precision 20, backend, base_precision, transformer_quant, memory); ruff clean. Full measurement record: outputs/image_optim_round2_audit.md.
This commit is contained in:
parent
6cb44270fc
commit
352fb40089
5 changed files with 372 additions and 17 deletions
|
|
@ -190,15 +190,83 @@ def test_save_then_load_roundtrip(monkeypatch, tmp_path, fake_megacache):
|
|||
assert ctx2.key == ctx.key
|
||||
|
||||
|
||||
def test_no_save_in_auto_mode(monkeypatch, tmp_path, fake_megacache):
|
||||
def test_auto_mode_saves_by_default(monkeypatch, tmp_path, fake_megacache):
|
||||
monkeypatch.setenv(cc._ENV_MODE, "auto")
|
||||
monkeypatch.delenv(cc._ENV_SAVE, raising = False)
|
||||
monkeypatch.setenv(cc._ENV_DIR, str(tmp_path))
|
||||
ctx = cc.begin(transformer = _transformer(), **_BEGIN_KW)
|
||||
assert cc.save(ctx) is False # auto without SAVE opt-in does not write
|
||||
assert cc.save(ctx) is True # first-run warm: auto saves the bundle
|
||||
assert ctx.bundle.exists() and ctx.manifest_path.exists()
|
||||
|
||||
# The next load with the same fingerprint hits the just-saved bundle...
|
||||
ctx2 = cc.begin(transformer = _transformer(), **_BEGIN_KW)
|
||||
assert ctx2.hit is True
|
||||
# ...and does NOT rewrite it under auto (the artifacts on disk are the ones loaded).
|
||||
before = ctx2.bundle.stat().st_mtime_ns
|
||||
assert cc.save(ctx2) is False
|
||||
assert ctx2.bundle.stat().st_mtime_ns == before
|
||||
|
||||
|
||||
def test_save_env_zero_disables_auto_save(monkeypatch, tmp_path, fake_megacache):
|
||||
monkeypatch.setenv(cc._ENV_MODE, "auto")
|
||||
monkeypatch.setenv(cc._ENV_SAVE, "0")
|
||||
monkeypatch.setenv(cc._ENV_DIR, str(tmp_path))
|
||||
ctx = cc.begin(transformer = _transformer(), **_BEGIN_KW)
|
||||
assert cc.save(ctx) is False # explicit load-only override
|
||||
assert not ctx.bundle.exists()
|
||||
|
||||
|
||||
def test_on_mode_resaves_after_hit(monkeypatch, tmp_path, fake_megacache):
|
||||
monkeypatch.setenv(cc._ENV_MODE, "on")
|
||||
monkeypatch.setenv(cc._ENV_DIR, str(tmp_path))
|
||||
ctx = cc.begin(transformer = _transformer(), **_BEGIN_KW)
|
||||
assert cc.save(ctx) is True
|
||||
ctx2 = cc.begin(transformer = _transformer(), **_BEGIN_KW)
|
||||
assert ctx2.hit is True
|
||||
# Distributor mode refreshes the bundle even on a hit (new variants get captured).
|
||||
assert cc.save(ctx2) is True
|
||||
|
||||
|
||||
def test_new_static_shape_redirties_a_hit(monkeypatch, tmp_path, fake_megacache):
|
||||
monkeypatch.setenv(cc._ENV_MODE, "auto")
|
||||
monkeypatch.delenv(cc._ENV_SAVE, raising = False)
|
||||
monkeypatch.setenv(cc._ENV_DIR, str(tmp_path))
|
||||
|
||||
# Cold session at 1024: the save records the shape coverage in the manifest.
|
||||
ctx = cc.begin(transformer = _transformer(), **_BEGIN_KW)
|
||||
cc.register_shape(ctx, (1024, 1024, 1), static = True)
|
||||
assert cc.save(ctx) is True
|
||||
manifest = json.loads(ctx.manifest_path.read_text())
|
||||
assert manifest["shapes"] == [[1024, 1024, 1]]
|
||||
|
||||
# Warm session: the covered shape does not dirty the context...
|
||||
ctx2 = cc.begin(transformer = _transformer(), **_BEGIN_KW)
|
||||
assert ctx2.hit is True and ctx2.saved is True
|
||||
assert ctx2.shapes == {(1024, 1024, 1)}
|
||||
cc.register_shape(ctx2, (1024, 1024, 1), static = True)
|
||||
assert cc.save(ctx2) is False
|
||||
# ...but a NEW static shape (its compile just produced new artifacts) does, and the
|
||||
# rewritten manifest covers both.
|
||||
cc.register_shape(ctx2, (768, 768, 1), static = True)
|
||||
assert ctx2.saved is False
|
||||
assert cc.save(ctx2) is True
|
||||
manifest = json.loads(ctx2.manifest_path.read_text())
|
||||
assert manifest["shapes"] == [[768, 768, 1], [1024, 1024, 1]]
|
||||
|
||||
|
||||
def test_dynamic_compile_never_dirties(monkeypatch, tmp_path, fake_megacache):
|
||||
monkeypatch.setenv(cc._ENV_MODE, "auto")
|
||||
monkeypatch.setenv(cc._ENV_DIR, str(tmp_path))
|
||||
ctx = cc.begin(transformer = _transformer(), **_BEGIN_KW)
|
||||
cc.save(ctx)
|
||||
ctx2 = cc.begin(transformer = _transformer(), **_BEGIN_KW)
|
||||
assert ctx2.hit is True
|
||||
# A dynamic-shape compile reuses one artifact across shapes: no re-save.
|
||||
cc.register_shape(ctx2, (768, 768, 1), static = False)
|
||||
assert cc.save(ctx2) is False
|
||||
cc.register_shape(None, (768, 768, 1), static = True) # no context: no-op
|
||||
|
||||
|
||||
def test_fingerprint_mismatch_falls_back(monkeypatch, tmp_path, fake_megacache):
|
||||
monkeypatch.setenv(cc._ENV_MODE, "on")
|
||||
monkeypatch.setenv(cc._ENV_DIR, str(tmp_path))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue