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 4 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
41 changes: 41 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,44 @@ def call(self, model: ir.Model) -> ir.passes.PassResult:
modified |= self._process_graph_like(function, used_domains=set())

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


class RemoveMetadataAndDocStringPass(ir.passes.InPlacePass):
def call(self, model: ir.Model) -> ir.passes.PassResult:
modified = False
# 0. Clean up all of the model metadata properties
if model.metadata_props or model.doc_string:
modified = True
logger.debug("Removed metadata from model")
model.metadata_props.clear()
model.doc_string = None

checked_graphs = set()
# 1. Clean up all of the nodes metadata properties
for node in ir.traversal.RecursiveGraphIterator(model.graph):
if node.metadata_props:
modified = True
logger.debug("Removed metadata from %s nodes", node.name)
node.metadata_props.clear()

# 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
if node.graph not in checked_graphs and (
node.graph.metadata_props or node.graph.doc_string
):
modified = True
logger.debug("Removed metadata from %s graph", node.graph.name)
node.graph.metadata_props.clear()
node.graph.doc_string = None
checked_graphs.add(node.graph)

# 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)
Loading