Skip to content

wmk quantize crashes with FileExistsError when output .onnx.data sidecar already exists #185

@DingmaomaoBJTU

Description

@DingmaomaoBJTU

Summary

Re-running `wmk quantize` with the same output path fails with `FileExistsError` if a `.onnx.data` sidecar file from a previous run already exists. The command has no overwrite or cleanup logic for external data files.

Repro

```bash

First run — succeeds

wmk quantize -m resnet_optimized.onnx --precision int8 -o resnet_quant_int8.onnx

Second run — crashes

wmk quantize -m resnet_optimized.onnx --precision int8 -o resnet_quant_int8.onnx
```

Error

```
Quantization failed:
Traceback (most recent call last):
File "src/winml/modelkit/quant/quantizer.py", line 166, in quantize_onnx
quantize(...)
...
File "onnx/external_data_helper.py", line 150, in convert_model_to_external_data
raise FileExistsError(f"External data file exists in {location}.")
FileExistsError: External data file exists in resnet_quant_int8.onnx.data.
```

Root Cause

In `src/winml/modelkit/quant/quantizer.py:163-164`, when `use_external_data=True` (the default), ORT's `quantize()` is called with `use_external_data_format=True`. ORT's `convert_model_to_external_data` raises `FileExistsError` if the sidecar `.data` file already exists — it does not overwrite.

`quantize_onnx` deletes/overwrites the `.onnx` file but does not check for or remove the accompanying `.onnx.data` sidecar before calling ORT quantization.

```python

quantizer.py:162-170

if use_external_data:
qdq_config.use_external_data_format = True
logger.info("Applying quantization...")
quantize(
model_input=str(model_path),
model_output=str(output_path), # .onnx written, but .onnx.data not cleaned up
quant_config=qdq_config,
)
```

Expected Behavior

`wmk quantize` should handle existing output paths gracefully — either:

  • Delete the stale `.onnx.data` sidecar before writing (consistent with overwrite semantics for the `.onnx` file), or
  • Raise a clear, user-friendly error message explaining that the sidecar file must be removed first

Acceptance Criteria

  • Re-running `wmk quantize` with the same `-o` path succeeds without manual cleanup
  • If `output_path.with_suffix(".onnx.data")` or any external data sidecar exists, it is deleted before quantization
  • A test covers the re-run scenario (run quantize twice on same output, assert second run succeeds)

Related Files

  • `src/winml/modelkit/quant/quantizer.py:86-90` — output path resolution
  • `src/winml/modelkit/quant/quantizer.py:162-170` — where external data format is set and `quantize()` called
  • `src/winml/modelkit/onnx/external_data.py` — `get_external_data_files()` can be used to enumerate sidecar files to clean up

Metadata

Metadata

Labels

No labels
No labels

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions