feat(export): normalize exported ONNX in-place via optimize_onnx#681
Merged
Conversation
added 2 commits
May 20, 2026 14:34
Adds a post-export normalization step inside `export_pytorch()` so that every export path (the `winml export` CLI, `build_hf_model`, the `winml build` command, and direct Python API callers) benefits from graph normalization automatically. The new helper `_normalize_exported_model()`: - logs `Normalizing model` - runs `optimize_onnx()` into a `tempfile.mkdtemp()` working directory - on success, replaces the original `.onnx` + sidecar in-place via `copy_onnx_model()` (which overwrites both files correctly — confirmed by new overwrite tests in `tests/unit/onnx/test_external_data.py`) - on failure, logs a warning and leaves the original export untouched - removes the temp directory in a `finally` clause either way - records the outcome in `stats["model_normalization_succeeded"]` Tests: - `tests/unit/export/test_pytorch_export.py`: success path populates `value_info`; mocked-failure path leaves the model un-shape-inferenced. - `tests/unit/onnx/test_external_data.py`: `copy_onnx_model` overwrites a pre-existing dst correctly with and without external data sidecars.
DingmaomaoBJTU
approved these changes
May 20, 2026
timenick
reviewed
May 20, 2026
Collaborator
Code reviewFound 2 issues:
winml-cli/src/winml/modelkit/export/pytorch.py Lines 96 to 100 in 39e73f7
winml-cli/src/winml/modelkit/export/pytorch.py Lines 87 to 100 in 39e73f7 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
added 3 commits
May 20, 2026 15:26
… docstring - export_pytorch: new `normalize: bool = True` kwarg. Callers who need the raw torch.onnx.export output can opt out without dropping the normalize step for everyone else. - Stats key renamed `model_normalization_succeeded` (bool) -> `model_normalization_status` (str), values `"not_run"` / `"succeeded"` / `"failed"`. - `_normalize_exported_model`: keep the broad `except`, but pass `exc_info=True` so the traceback lands in the warning log -- a future helper-refactor bug now shows up instead of silently flipping a flag. - Soften the docstring: optimize_onnx failures leave the original untouched, but copy_onnx_model writes directly to the destination, so a copy failure can corrupt the dest. The previous docstring overclaimed. Tests: - Both existing tests updated to assert on the new status key. - New `test_normalize_false_skips_normalization` verifies the kwarg short-circuits the helper and yields `"not_run"`.
Pass `dir=output_path.parent` to `tempfile.mkdtemp()` so the staging directory lives on the same volume as the export. For multi-GB models (the primary WinML target) this avoids a cross-volume data transfer in `copy_onnx_model` and keeps large sidecars off the system drive's `%TEMP%`. Addresses the perf review comment on #681.
Move the `_normalize_exported_model` call inside the `warnings.catch_warnings()` block so the optimizer's shape-inference and ORT warnings stay suppressed for callers. PR #460 centralized warning suppression here; the new normalization call had escaped it and was leaking warnings to stderr. Addresses DingmaomaoBJTU's first review finding on #681.
DingmaomaoBJTU
approved these changes
May 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #677.
Summary
export_pytorch(), the single function used by every export path (thewinml exportCLI,build_hf_model, thewinml buildcommand, and direct Python API callers), so normalization runs automatically for all of them._normalize_exported_model(output_path) -> boolinsrc/winml/modelkit/export/pytorch.py:Normalizing modeloptimize_onnx()into a freshtempfile.mkdtemp().onnx(and.datasidecar, if any) viacopy_onnx_model()— confirmed to overwrite both files correctly by new tests intests/unit/onnx/test_external_data.pyfinallyclause either waystats["model_normalization_succeeded"]Tests
tests/unit/export/test_pytorch_export.py— success path populatesvalue_info; mocked-failure path leaves the model un-shape-inferenced and reportsmodel_normalization_succeeded=False.tests/unit/onnx/test_external_data.py— two new overwrite tests coveringcopy_onnx_modelagainst a pre-existing dst with and without external data sidecars.