Commit 847d3fe
authored
Fix ONNX save crash when ModelProto.ByteSize() raises EncodeError (microsoft#2355)
## Summary
This PR fixes a crash in ONNX model saving for large/complex models
where `model.ByteSize()` can raise a protobuf `EncodeError` (`Failed to
serialize proto`).
## Problem
In `model_proto_to_file`, we probe model size with `model.ByteSize()`
when `save_as_external_data=False`.
For some models, that call fails before save, causing the pass to crash.
## Root Cause
`ByteSize()` can trigger protobuf serialization internals and fail with:
`google.protobuf.message.EncodeError: Failed to serialize proto`.
## Fix
- Wrap `model.ByteSize()` in `try/except`.
- If size probing fails, log a warning and force
`save_as_external_data=True`.
- Preserve existing large-model fallback behavior (`<= 0` or `>=
onnx.checker.MAXIMUM_PROTOBUF`).
## Validation
- Reproduced failure before fix in `mq:matmulnbitstoqdq` flow.
- Re-ran with fix and workflow completed without the previous
`ByteSize()` crash.
## Impact
- Improves robustness for large ONNX exports.
- No behavior change for normal models where `ByteSize()` works.1 parent 43f81fb commit 847d3fe
1 file changed
Lines changed: 19 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
142 | 152 | | |
143 | 153 | | |
144 | 154 | | |
| |||
0 commit comments