Skip to content

fix(tensorrt): hash UNet engine cache key so ONNX export path stays under Windows MAX_PATH#29

Open
forkni wants to merge 1 commit into
dotsimulate:SDTD_040_beta_releasefrom
forkni:fix/unet-engine-path-maxpath
Open

fix(tensorrt): hash UNet engine cache key so ONNX export path stays under Windows MAX_PATH#29
forkni wants to merge 1 commit into
dotsimulate:SDTD_040_beta_releasefrom
forkni:fix/unet-engine-path-maxpath

Conversation

@forkni

@forkni forkni commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Problem

Loading StreamDiffusionTD with sd-turbo + fp8: true + acceleration: tensorrt crashes during TensorRT engine build:

FileNotFoundError: [Errno 2] No such file or directory:
'...\stabilityai\sd-turbo--tiny_vae-True--min_batch-1--max_batch-4--use_cached_attn-False--fi-False--fp8v3--sbatch1--batch-2--cachef4--optlvl4--mode-img2img--trt10.16.1.11--cc120--res-512x512\unet.engine.onnx'
→ Exception: Acceleration has failed

Root cause

EngineManager.get_engine_path encodes every UNet build flag verbatim into the on-disk directory name. With a realistic engine_dir and a config using fp8 + static batch + pin_cache_frames + optlvl + resolution, the generated directory was 246 chars; the derived unet.engine.onnx path was 263 and unet.engine.opt.onnx was 267 — both over Windows' 260-char MAX_PATH.

Because the directory fit but the file didn't, mkdir silently succeeded while torch.onnx.exportopen(onnx_path, "wb") raised a bare FileNotFoundError. wrapper.py's OOM-only fallback doesn't catch this, so it re-raises as Acceleration has failed.

Fix

For EngineType.UNET only, hash the fully-assembled flag prefix (sha1, first 12 hex chars) — reusing the existing _lora_signature hashing idiom — keeping model name, fp8, and resolution human-readable in the folder name.

  • Cache-key precision is unchanged: identical configs still hash identically, differing configs still diverge (sha1, no realistic collision).
  • Path drops from ~267 chars to ~132.
  • VAE/ControlNet directory naming is untouched — existing VAE/ControlNet engines are not invalidated. UNet engines rebuild once under their new hashed folder name.
  • Adds a belt-and-suspenders warning if a derived path would still approach MAX_PATH (e.g. an unusually deep user engine_dir), so a future added flag can't silently regress this again.

Testing

Adds tests/unit/test_engine_path_length.py, which reproduces the exact crash-log config and asserts:

  • the derived .onnx / .opt.onnx paths stay under MAX_PATH (red before the fix, green after)
  • the path is deterministic across repeated calls (cache stability)
  • distinct configs (e.g. differing static_batch_size) still produce distinct directories (no collisions)
pytest tests/unit/test_engine_path_length.py -v

3 passed.

🤖 Generated with Claude Code

…ws MAX_PATH

EngineManager.get_engine_path encoded every UNet build flag verbatim into the
on-disk directory name. With a realistic engine_dir (fp8 + static batch +
pin_cache_frames + optlvl + resolution), the folder grew to 246 chars, and the
derived unet.engine.onnx / unet.engine.opt.onnx paths hit 263/267 chars --
over Windows' 260-char MAX_PATH. Because the *directory* fit but the *file*
didn't, mkdir succeeded while torch.onnx.export's open(onnx_path, "wb") raised
FileNotFoundError, which wrapper.py's OOM-only fallback doesn't catch, so the
build failed with "Acceleration has failed: [Errno 2] No such file or
directory: ...unet.engine.onnx".

Fix: for EngineType.UNET only, hash the fully-assembled flag prefix (sha1,
first 12 hex chars) using the existing _lora_signature hashing idiom, keeping
model name + fp8 + resolution human-readable in the folder name. Cache-key
precision is unchanged (identical configs still hash identically, differing
configs still diverge) -- only the on-disk encoding is compacted, from ~267
chars down to ~132. VAE/ControlNet directory naming is untouched, so existing
VAE engines are not invalidated; UNet engines rebuild once under their new
hashed folder name. Also adds a belt-and-suspenders warning if a derived path
would still approach MAX_PATH (e.g. an unusually deep user engine_dir), and
casts the "filename" config lookup to str to keep the type checker precise
about self._configs' heterogeneous per-key value types.

Adds a regression test (test_engine_path_length.py) that reproduces the exact
crash-log config and asserts the derived .onnx / .opt.onnx paths stay under
MAX_PATH, plus determinism and no-collision checks.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant