Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit c2b5ae5

Browse files
authored
Make 'validate_onnx' function robust against the models loaded without external data (#317)
* initial commit * addressing PR comments
1 parent 837e5a4 commit c2b5ae5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/sparsezoo/utils/onnx.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
_LOGGER = logging.getLogger(__name__)
2828

2929
__all__ = [
30+
"onnx_includes_external_data",
3031
"save_onnx",
3132
"validate_onnx",
3233
"load_model",
@@ -50,6 +51,29 @@
5051
]
5152

5253

54+
def onnx_includes_external_data(model: ModelProto) -> bool:
55+
"""
56+
Check whether the ModelProto in memory includes the external
57+
data or not.
58+
59+
If the model.onnx does not contain the external data, then the
60+
initializers of the model are pointing to the external data file
61+
(they are not empty)
62+
63+
:param model: the ModelProto to check
64+
:return True if the model was loaded with external data, False otherwise.
65+
"""
66+
67+
initializers = model.graph.initializer
68+
69+
is_data_saved_to_disk = any(
70+
initializer.external_data for initializer in initializers
71+
)
72+
is_data_included_in_model = not is_data_saved_to_disk
73+
74+
return is_data_included_in_model
75+
76+
5377
def save_onnx(
5478
model: ModelProto, model_path: str, external_data_file: Optional[str] = None
5579
) -> bool:
@@ -121,6 +145,14 @@ def validate_onnx(model: Union[str, ModelProto]):
121145
return
122146
onnx.checker.check_model(onnx_model)
123147
except Exception as err:
148+
if not onnx_includes_external_data(model):
149+
_LOGGER.warning(
150+
"Attempting to validate an in-memory ONNX model "
151+
"that has been loaded without external data. "
152+
"This is currently not supported by the ONNX checker. "
153+
"The validation will be skipped."
154+
)
155+
return
124156
raise ValueError(f"Invalid onnx model: {err}")
125157

126158

0 commit comments

Comments
 (0)