Skip to content

Conversation

@jsmonson
Copy link
Collaborator

@jsmonson jsmonson commented Oct 30, 2025

This PR updates qonnx with metadata preservation during transformation. This operates on the following rules:

  1. If a new node is created alongside an existing node the information from the existing node is copied to the new node.
  2. If a new node (or nodes) is (are) created to replace an existing node the metadata from the existing node is copied to the new node(s).
  3. If a new node is created to replace multiple existing nodes (i.e. node fusion) author should select the most relevant node to source the metadata from. In this PR, the author made his best judgement on which metadata to preserve. Since there is no standard approach for fusing metadata from multiple nodes, this approach leaves it to transformation authors to determine which metadata to preserve.

@jsmonson jsmonson requested a review from maltanar October 30, 2025 23:16
Copy link
Collaborator

@maltanar maltanar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's hard to reason about how metadata should behave when nodes are created/deleted it will be tricky to test this in general, but we can approach this as a general infrastructure feature. A few questions and requests in addition:

  1. Is it only node metadata you care about? What about tensor metadata when tensors get created or erased? (e.g. FINN makes use of tensor metadata to indicate inferred datatypes, structured sparsity when lowering depthwise convolutions)
  2. How should copy_metadata_props behave if the same named attribute exists in both source node and destination node (or multiple source nodes)? Can you add this in a unit test?
  3. For the metadata use-cases you can think of for particular transformations, please add a few unit tests that check how the metadata is expected to behave.
  4. Why are some transformations left out, while others are included? e.g. BatchNormToAffine also creates new nodes but copy_metadata_props is not added there.

Comment on lines +355 to +384
def copy_metadata_props(source_node, target_node):
"""Copy metadata properties from source node(s) to target node.
Parameters
----------
source_node : onnx.NodeProto or list of onnx.NodeProto
Source node(s) from which to copy metadata_props. If a list is provided,
metadata from all nodes will be merged into the target node.
target_node : onnx.NodeProto
Target node to which metadata_props will be copied.
Returns
-------
None
Modifies target_node in place by extending its metadata_props.
Examples
--------
>>> # Copy from single node
>>> copy_metadata_props(old_node, new_node)
>>>
>>> # Copy from multiple nodes (e.g., when fusing)
>>> copy_metadata_props([quant_node, dequant_node], fused_node)
"""
# Handle both single node and list of nodes
source_nodes = source_node if isinstance(source_node, list) else [source_node]

for node in source_nodes:
if hasattr(node, "metadata_props"):
target_node.metadata_props.extend(node.metadata_props)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide some unit test(s) for this new utility function, ideally also covering edge cases (e.g. source node has no attributes, source and target node have attribute with the same name).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants