Skip to content

Fix ONNX save crash when ModelProto.ByteSize() raises EncodeError#2355

Open
anujj wants to merge 1 commit intomicrosoft:mainfrom
anujj:ajalota/fix-model-proto-bytesize-encodeerror
Open

Fix ONNX save crash when ModelProto.ByteSize() raises EncodeError#2355
anujj wants to merge 1 commit intomicrosoft:mainfrom
anujj:ajalota/fix-model-proto-bytesize-encodeerror

Conversation

@anujj
Copy link
Contributor

@anujj anujj commented Mar 11, 2026

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.

@anujj
Copy link
Contributor Author

anujj commented Mar 11, 2026

@jambayk for review

@anskumar01
Copy link

Also adding @devang-ml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants