Files
pytorch/docs/source
Sherlock Huang 10e69a6e17 Preserve user annotation in graph (#163673)
```
import torch
import torch.fx.traceback as fx_traceback
import torch.export

class M(torch.nn.Module):
    def forward(self, x):
        with fx_traceback.annotate({"pp_stage": 0}):
            with fx_traceback.annotate({"fdsp_bucket": 0}):
                x = x + 1
            x = x - 2
            with fx_traceback.annotate({"cuda_stream": 2, "fsdp_bucket": 1}):
                x = x * 2
        x = x / 3
        return x

m = M()

with fx_traceback.preserve_node_meta():
    ep = torch.export.export(m, (torch.randn(10),))

for node in ep.graph.nodes:
    if node.op == "call_function":
        print(f"{node.target}, {node.meta.get("custom", {})}")

```

prints

```
aten.add.Tensor, {'pp_stage': 0, 'fdsp_bucket': 0}
aten.sub.Tensor, {'pp_stage': 0}
aten.mul.Tensor, {'pp_stage': 0, 'cuda_stream': 2, 'fsdp_bucket': 1}
aten.div.Tensor, {}
```

TODOs:
- run_decomposition is failing
- Need to test with the new full graph capture + aot_export_joint apis
- Need to make the annotation propagate through autograd engine to reach the bw nodes. Sample impl here: https://github.com/pytorch/pytorch/pull/83558
- Edward want to restrict the key in custom field to be top-level singleton objects only
- also need to take care of metadata merging when passes are fusing nodes

Thanks @angelayi  for contributing the dynamo fixes.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/163673
Approved by: https://github.com/albanD, https://github.com/angelayi
2025-09-25 15:50:15 +00:00
..
2025-09-22 21:01:27 +00:00
2025-04-16 21:35:19 +00:00
2025-09-22 21:01:27 +00:00
2025-04-27 09:56:42 +00:00
2025-06-11 23:00:52 +00:00
2025-06-11 23:00:52 +00:00
2025-06-11 23:00:52 +00:00
2025-06-13 04:39:55 +00:00
2025-06-08 21:17:31 +00:00
2025-06-08 21:17:31 +00:00
2025-06-08 21:17:31 +00:00
2025-09-12 05:56:25 +00:00
2024-03-29 18:05:28 +00:00
2025-04-16 21:35:19 +00:00
2025-07-10 14:15:37 +00:00
2025-06-21 02:47:32 +00:00
2025-04-14 16:47:40 +00:00
2025-08-14 01:49:35 +00:00