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
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
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:
Acceptance Criteria
Related Files