Skip to content

[Pass] Remove metadata_props and doc_string from the model #2182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions onnxscript/ir/passes/common/unused_removal.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,33 @@
modified |= self._process_graph_like(function, used_domains=set())

return ir.passes.PassResult(model, modified=modified)


class RemoveUnusedMetadataAndDocStringPass(ir.passes.InPlacePass):
def call(self, model: ir.Model) -> ir.passes.PassResult:
modified = False

Check warning on line 193 in onnxscript/ir/passes/common/unused_removal.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/common/unused_removal.py#L193

Added line #L193 was not covered by tests
# 1. Clean up all of the nodes metadata properties
for node in ir.traversal.RecursiveGraphIterator(model.graph):
if len(node.metadata_props) > 0:
modified = True
logger.debug("Removed metadata from %s nodes", node.name)
node.metadata_props.clear()

Check warning on line 199 in onnxscript/ir/passes/common/unused_removal.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/common/unused_removal.py#L197-L199

Added lines #L197 - L199 were not covered by tests

# TODO: Use model.graphs to get the graph instead of node.graph
# 2. Clean up the main graph metadata properties
# and doc_string
assert node.graph is not None

Check warning on line 204 in onnxscript/ir/passes/common/unused_removal.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/common/unused_removal.py#L204

Added line #L204 was not covered by tests
if len(node.graph.metadata_props) > 0 or node.graph.doc_string is not None:
modified = True
logger.debug("Removed metadata from %s graph", node.graph.name)
node.graph.metadata_props.clear()
node.graph.doc_string = None

Check warning on line 209 in onnxscript/ir/passes/common/unused_removal.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/common/unused_removal.py#L206-L209

Added lines #L206 - L209 were not covered by tests

# 3. Clean up all of the functions metadata properties
for function in model.functions.values():
if len(function.metadata_props) > 0 or function.doc_string is not None:
modified = True
logger.debug("Removed metadata from %s function", function.name)
function.metadata_props.clear()
function.doc_string = None
return ir.passes.PassResult(model, modified=modified)

Check warning on line 218 in onnxscript/ir/passes/common/unused_removal.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/common/unused_removal.py#L214-L218

Added lines #L214 - L218 were not covered by tests
Loading