Skip to content

Commit

Permalink
Raise exception for invalid onnx metadata values types (#104)
Browse files Browse the repository at this point in the history
Co-authored-by: reuvenp <[email protected]>
  • Loading branch information
reuvenperetz and reuvenp authored Sep 16, 2024
1 parent a347912 commit f4f86df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mct_quantizers/pytorch/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def get_metadata(model):
def add_onnx_metadata(model: onnx.ModelProto, metadata: Dict):
"""
Init the metadata dictionary and verify its compliance, then add it to the model metadata_props.
Metadata values have to be of byte type.
Args:
model (ModelProto): onnx model to add the metadata to.
Expand All @@ -101,6 +102,8 @@ def add_onnx_metadata(model: onnx.ModelProto, metadata: Dict):

for k, v in metadata.items():
meta = model.metadata_props.add()
if not isinstance(v, (bytes, str)):
Logger.critical(f"ONNX metadata must be of byte type, but {v} has type {type(v)}")
meta.key, meta.value = k, v
return model

Expand Down
5 changes: 5 additions & 0 deletions tests/pytorch_tests/test_pytorch_load_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,8 @@ def test_save_and_load_metadata(self):
self.assertTrue(get_onnx_metadata(onnx_model) == get_onnx_metadata(loaded_onnx_model))

os.remove(tmp_onnx_file)

# Make sure assertion is raised in cases of invalid metadata value type.
with self.assertRaisesRegex(Exception, r"ONNX metadata must be of byte type, but 4.2 has type <class 'float'>"):
add_onnx_metadata(onnx_model, {'test': 'test456', 'foo': 4.2})

0 comments on commit f4f86df

Please sign in to comment.