# Understanding torch.fx.traceback.preserve\_node\_meta()

**URL:** <https://dev-discuss.pytorch.org/t/understanding-torch-fx-traceback-preserve-node-meta/2321>\
**Category:** FX\
**Created:** [July 26, 2024, 8:04pm UTC](https://dev-discuss.pytorch.org/t/understanding-torch-fx-traceback-preserve-node-meta/2321 "2024-07-26T20:04:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![TharinduRusira](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/tharindurusira/32/1432_2.png) [@TharinduRusira](https://dev-discuss.pytorch.org/u/TharinduRusira)\
**Post date:** [July 26, 2024, 8:04pm UTC](https://dev-discuss.pytorch.org/t/understanding-torch-fx-traceback-preserve-node-meta/2321/1 "2024-07-26T20:04:36Z")

</div>

I previously posted this topic on [PyTorch Forums](https://discuss.pytorch.org/t/understanding-torch-fx-traceback-preserve-node-meta/205319) but I think this is a topic best suited for the dev forum.

I’m trying to understand the scope of `torch.fx.traceback.preserve_node_meta()`. Can someone please explain the scope of `torch.fx.Node`’s meta preservation in the context of the following example?

```auto
# A simple torch module
class SimpleAdd(torch.nn.Module):
    def __init__ (self):
        super(). __init__ ()

    def forward(self, x, y):
        return x + y

class SimpleTransform(torch.fx.Transformer):
    '''
    A simple FX transformer
    '''
    def __init__ (self, module):
        super(). __init__ (module)
    
    def run_node(self, n: torch.fx.Node):
        '''
       Traverse FX graph and add a custom meta field for each node
        '''
        n.meta["node_op"] = n.op
        return super().run_node(n)
    
model = SimpleAdd()

with torch.fx.traceback.preserve_node_meta():
    fxg = torch.fx.symbolic_trace(model)

    # node meta prior to transform
    for node in fxg.graph.nodes:
        """
        Current node meta
        x {'seq_nr': -1}
        y {'seq_nr': -1} 
        add {'seq_nr': -1}
        output {'seq_nr': -1}
        """
        print(node, node.meta)

    # node traversal to add new meta
    SimpleTransform(fxg).run()

    for node in fxg.graph.nodes:
        """
        Updated meta. Previous meta is preserved.
        x {'seq_nr': -1, 'node_op': 'placeholder'}
        y {'seq_nr': -1, 'node_op': 'placeholder'}
        add {'seq_nr': -1, 'node_op': 'call_function'}
        output {'seq_nr': -1, 'node_op': 'output'}
        """
        print(node, node.meta)

    # No real transformation, call super().transform()
    fxgn = SimpleTransform(fxg).transform()

    # node meta after transform
    for node in fxgn.graph.nodes:
        """
        Node meta after calling transform().
        x {}
        y {}
        add {'from_node': [('add', <built-in function add>)], 'seq_nr': -1}
        output {}
        """
        print(node, node.meta)

```

**Question** : Is this the correct behavior of `preserve_node_meta()`? Given that `transform()` returns a new graph, should it still preserve node meta from the old graph?

The example code was run against Pytorch commit [eff01bc](https://github.com/pytorch/pytorch/commit/eff01bce21c862a3e39e0d55b47eef8f33e41ef0).

Thanks!
