Skip to content

Commit f56db25

Browse files
justinchubyCopilot
andauthored
fix(tests): release/ignore Windows ONNX temp-file lock in llada & unet parity tests (#417)
## Summary - use `TemporaryDirectory(ignore_cleanup_errors=True)` for every temporary ONNX model in the LLaDA and UNet parity tests - document that ONNX Runtime keeps model files memory-mapped on Windows - preserve all inference and parity assertions unchanged ## Root cause ONNX Runtime can retain a memory-mapped handle to the `.onnx` model on Windows. Temporary-directory teardown then raises `PermissionError` because Windows does not allow deleting an open file, unlike POSIX. ## Verification - `python3 -m pytest src/mobius/models/unet_parity_test.py src/mobius/models/llada_test.py -q` — 3 passed, 4 skipped - `ruff format --check src/mobius/models/llada_test.py src/mobius/models/unet_parity_test.py` - `ruff check src/mobius/models/llada_test.py src/mobius/models/unet_parity_test.py` `lintrunner` was also attempted with only these paths; the installed adapter invokes an older Ruff that cannot parse the repository configuration rule `RUF067`. Co-authored-by: Copilot <[email protected]>
1 parent cce80a4 commit f56db25

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

‎src/mobius/models/llada_test.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from __future__ import annotations
2121

2222
import tempfile
23+
from pathlib import Path
2324

2425
import numpy as np
2526
import pytest
@@ -167,11 +168,11 @@ def _build_onnx_session(config: ArchitectureConfig, state: dict[str, torch.Tenso
167168
model = MaskedDiffusionTask().build(module, config)["model"]
168169
apply_weights(model, module.preprocess_weights(state))
169170

170-
with tempfile.NamedTemporaryFile(suffix=".onnx") as handle:
171-
onnx_ir.save(model, handle.name)
172-
# onnxruntime reads the model fully at construction, so the temp file
173-
# can be removed as soon as the session exists.
174-
return ort.InferenceSession(handle.name)
171+
# Windows keeps the ORT model file mapped; ignore cleanup errors so the dir can be removed.
172+
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as temp_dir:
173+
model_path = Path(temp_dir) / "model.onnx"
174+
onnx_ir.save(model, model_path)
175+
return ort.InferenceSession(model_path)
175176

176177

177178
def test_llada_matches_torch_reference():

‎src/mobius/models/unet_parity_test.py‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import re
1414
import tempfile
15+
from pathlib import Path
1516

1617
import numpy as np
1718
import pytest
@@ -63,9 +64,11 @@ def test_cross_attention_block_matches_diffusers():
6364
model = _make_model(graph)
6465
apply_weights(model, _remap_transformer(hf.state_dict()))
6566

66-
with tempfile.NamedTemporaryFile(suffix=".onnx") as handle:
67-
onnx_ir.save(model, handle.name)
68-
session = ort.InferenceSession(handle.name)
67+
# Windows keeps the ORT model file mapped; ignore cleanup errors so the dir can be removed.
68+
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as temp_dir:
69+
model_path = Path(temp_dir) / "model.onnx"
70+
onnx_ir.save(model, model_path)
71+
session = ort.InferenceSession(model_path)
6972
actual = session.run(None, {"hidden": hidden.numpy(), "context": context.numpy()})[0]
7073
assert np.abs(actual - expected).max() < 1e-4
7174

@@ -115,9 +118,11 @@ def test_unet_matches_diffusers():
115118
model = DenoisingTask().build(module, config)["model"]
116119
apply_weights(model, module.preprocess_weights(dict(hf.state_dict())))
117120

118-
with tempfile.NamedTemporaryFile(suffix=".onnx") as handle:
119-
onnx_ir.save(model, handle.name)
120-
session = ort.InferenceSession(handle.name)
121+
# Windows keeps the ORT model file mapped; ignore cleanup errors so the dir can be removed.
122+
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as temp_dir:
123+
model_path = Path(temp_dir) / "model.onnx"
124+
onnx_ir.save(model, model_path)
125+
session = ort.InferenceSession(model_path)
121126
actual = session.run(
122127
None,
123128
{
@@ -185,9 +190,11 @@ def test_unet_sd1x_mixed_block_types_matches_diffusers():
185190
model = DenoisingTask().build(module, config)["model"]
186191
apply_weights(model, module.preprocess_weights(dict(hf.state_dict())))
187192

188-
with tempfile.NamedTemporaryFile(suffix=".onnx") as handle:
189-
onnx_ir.save(model, handle.name)
190-
session = ort.InferenceSession(handle.name)
193+
# Windows keeps the ORT model file mapped; ignore cleanup errors so the dir can be removed.
194+
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as temp_dir:
195+
model_path = Path(temp_dir) / "model.onnx"
196+
onnx_ir.save(model, model_path)
197+
session = ort.InferenceSession(model_path)
191198
actual = session.run(
192199
None,
193200
{
@@ -271,9 +278,11 @@ def test_unet_lora_gate_parity():
271278
weights.update(remap_diffusers_unet_lora(lora_state, "test"))
272279
apply_weights(model, weights)
273280

274-
with tempfile.NamedTemporaryFile(suffix=".onnx") as handle:
275-
onnx_ir.save(model, handle.name)
276-
session = ort.InferenceSession(handle.name)
281+
# Windows keeps the ORT model file mapped; ignore cleanup errors so the dir can be removed.
282+
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as temp_dir:
283+
model_path = Path(temp_dir) / "model.onnx"
284+
onnx_ir.save(model, model_path)
285+
session = ort.InferenceSession(model_path)
277286
feed = {
278287
"sample": sample.numpy(),
279288
"timestep": timestep.numpy().astype(np.int64),

0 commit comments

Comments
 (0)