Skip to content

Commit cf094f5

Browse files
author
Gin
committed
add onnx model script
1 parent e19ce85 commit cf094f5

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Print onnx model metadata for debugging and ensuring the metadata is correct
4+
5+
Usage:
6+
python print_onnx_model_metadata.py <input_onnx_file>
7+
"""
8+
9+
import onnx
10+
import sys
11+
import os
12+
13+
14+
def clean_onnx_metadata(model_path, output_path=None):
15+
"""
16+
Remove privacy-sensitive metadata from an ONNX model.
17+
18+
Args:
19+
model_path: Path to input ONNX model
20+
output_path: Path to save cleaned model (defaults to overwriting input)
21+
"""
22+
print(f"Loading ONNX model from: {model_path}")
23+
model = onnx.load(model_path)
24+
25+
# What we don't clean currently:
26+
# model.doc_string: top-level doc string
27+
# model.metadata_props: top-level metadata
28+
# model.graph.doc_string: graph doc string
29+
# model.graph.node[...].docstring: node's doc string
30+
# model.graph.initializer[...].metadata_props: intializer (tensor)'s metadata
31+
# model.functions[...].doc_string: function doc string
32+
33+
# Track what we're cleaning
34+
changes_made = []
35+
removed_values = set()
36+
37+
# Clean metadata_props from nodes (this is where PyTorch stores stack traces)
38+
node_metadata_cleaned = 0
39+
for node in model.graph.node:
40+
if not node.metadata_props:
41+
continue
42+
# Remove metadata containing paths or stack traces
43+
kept_props = []
44+
for prop in node.metadata_props:
45+
# Remove stack traces and name scopes that contain file paths
46+
if prop.key in ['pkg.torch.onnx.stack_trace', 'pkg.torch.onnx.name_scopes'] and ('/' in prop.value or '\\' in prop.value):
47+
removed_values.add(prop.value)
48+
node_metadata_cleaned += 1
49+
continue # Skip this prop
50+
kept_props.append(prop)
51+
52+
# Clear and re-add only kept props:
53+
# We can't directly assign value to node.metadat_props, otherwise will have following error:
54+
# "Assignment not allowed to map or repeated field "metadata_props" in protocol message object."
55+
while len(node.metadata_props) > 0:
56+
node.metadata_props.pop()
57+
for prop in kept_props:
58+
node.metadata_props.append(prop)
59+
60+
if node_metadata_cleaned > 0:
61+
print(f" Removed {node_metadata_cleaned} metadata_props entries from nodes")
62+
changes_made.append(f"{node_metadata_cleaned} node metadata_props")
63+
64+
# Verify the model is still valid
65+
print(" Verifying cleaned model...")
66+
try:
67+
onnx.checker.check_model(model)
68+
print(" ✓ Model validation passed")
69+
except Exception as e:
70+
print(f" ✗ Model validation failed: {e}")
71+
print(" Aborting - model may be corrupted")
72+
return False
73+
74+
# Save the cleaned model
75+
if output_path is None:
76+
output_path = model_path
77+
78+
print(f"Saving cleaned model to: {output_path}")
79+
onnx.save(model, output_path)
80+
81+
# Report what was cleaned
82+
if changes_made:
83+
print(f"\nCleaned metadata:")
84+
for change in changes_made:
85+
print(f" - {change}")
86+
87+
print(f"Removed {len(removed_values)} values")
88+
else:
89+
print("\nNo privacy-sensitive filepath metadata found")
90+
91+
# Verify the paths are gone
92+
print("\nVerifying paths are removed...")
93+
with open(output_path, 'rb') as f:
94+
content = f.read()
95+
# Check for common path indicators
96+
if b'/Users/' in content or b'C:\\' in content or b'/home/' in content:
97+
print(" ⚠ Warning: Some path-like strings may still be present")
98+
print(" (This could be in tensor names or other non-metadata)")
99+
else:
100+
print(" ✓ No obvious file paths detected in binary")
101+
102+
print(f"\n✓ Successfully cleaned ONNX model!")
103+
return True
104+
105+
106+
def main():
107+
if len(sys.argv) < 2:
108+
print(__doc__)
109+
print("\nError: No input onnx file specified")
110+
sys.exit(1)
111+
112+
model_path = sys.argv[1]
113+
114+
if not os.path.exists(model_path):
115+
print(f"Error: Input onnx file not found: {model_path}")
116+
sys.exit(1)
117+
118+
print(f"Loading ONNX model from: {model_path}")
119+
model = onnx.load(model_path)
120+
print(f"doc_string={model.doc_string}")
121+
print(f"domain={model.domain}")
122+
print(f"ir_version={model.ir_version}")
123+
print(f"model_version={model.model_version}")
124+
print(f"producer_name={model.producer_name}")
125+
print(f"producer_version={model.producer_version}")
126+
if model.metadata_props:
127+
print("metadata_props:")
128+
for prop in model.metadata_props:
129+
print(f" {prop.key}={prop.value}")
130+
else:
131+
print(f"metadata_props={model.metadata_props}")
132+
133+
134+
if __name__ == "__main__":
135+
main()

0 commit comments

Comments
 (0)