pytorch/torch
Shangdi Yu 1e86fa2e5b Add stack trace to Inductor IR nodes if inductor.config.trace.provenance_tracing=True (#158576)
Summary:
- Split `create_mapping` to `create_mapping_pre_post_grad_nodes` and  ` create_node_mapping_kernel_to_post_grad`
- Store a mapping from pre_grad graph node names to stack traces in `_inductor_pre_grad_node_stack_trace`
- Add `stack_traces` member to ir.Node and add it to the string representation of ir.Node
- When we create an IR node, if `inductor.config.trace.provenance_tracing=True`, we populate `stack_traces` from `origins`. The nodes in `origins` are post_grad graph nodes. If a node has `node.stack_trace`, we store the stack_trace directly. This is particularly important for backward graph nodes because they don't have a mapping to pre-grad graph nodes. If a node doesn't have `.stack_trace ` (such as `linear`-> `addmm` nodes), we use the stack trace of the pre_grad graph nodes that it maps to.
  - A post grad graph node might not have stack trace if it correspond to multiple pre grad graph nodes, e.g. [GroupLinearFusion](a00442421a/torch/_inductor/fx_passes/group_batch_fusion.py (L299))

Example:

```
scheduling ExternKernelOut(
  python_kernel_name='extern_kernels.mm',
  name=buf0,
  layout=FixedLayout('cuda:0', torch.float32, size=[8, 16], stride=[16, 1]),
  inputs=[InputBuffer(name='arg2_1', layout=FixedLayout('cuda:0', torch.float32, size=[8, 10], stride=[10, 1])), ReinterpretView(
    StorageBox(
      ConstantBuffer(name='fc1_weight', layout=FixedLayout('cuda:0', torch.float32, size=[16, 10], stride=[10, 1]))
    ),
    FixedLayout('cuda:0', torch.float32, size=[10, 16], stride=[1, 10]),
    origins=OrderedSet([mm_default_1]),
    stack_traces = {,
    File "/data/users/shangdiy/fbsource/buck-out/v2/gen/fbcode/7b4b7a52e15abb17/scripts/shangdiy/__aot__/aot#link-tree/scripts/shangdiy/aot.py", line 29, in forward,
        x = self.fc1(x),
      File "/data/users/shangdiy/fbsource/buck-out/v2/gen/fbcode/7b4b7a52e15abb17/scripts/shangdiy/__aot__/aot#link-tree/torch/nn/modules/linear.py", line 125, in forward,
        return F.linear(input, self.weight, self.bias),
    }
  )],
  constant_args=(),
  kwargs={},
  output_view=None,
  python_kernel_name=extern_kernels.mm,
  cpp_kernel_name=at::mm_out,
  ordered_kwargs_for_cpp_kernel=(),
  op_overload=None,
  arg_properties=[{}, {}],
  allarg_properties={},
  kwarg_properties=None,
  unbacked_bindings={},
  mutation_outputs=[],
  origin_node=mm_default_1,
  origins=OrderedSet([mm_default_1]),
  stack_traces = {,
  File "/data/users/shangdiy/fbsource/buck-out/v2/gen/fbcode/7b4b7a52e15abb17/scripts/shangdiy/__aot__/aot#link-tree/scripts/shangdiy/aot.py", line 29, in forward,
      x = self.fc1(x),
    File "/data/users/shangdiy/fbsource/buck-out/v2/gen/fbcode/7b4b7a52e15abb17/scripts/shangdiy/__aot__/aot#link-tree/torch/nn/modules/linear.py", line 125, in forward,
      return F.linear(input, self.weight, self.bias),
  }
)
```

Test Plan:
```
buck2 run mode/dev-nosan fbcode//caffe2/test/inductor:provenance_tracing
```

Rollback Plan:

Differential Revision: D78365534

Pull Request resolved: https://github.com/pytorch/pytorch/pull/158576
Approved by: https://github.com/angelayi
2025-07-18 04:05:17 +00:00
..
_awaits
_C Add unified memory APIs for torch.accelerator (#152932) 2025-07-17 01:56:01 +00:00
_C_flatbuffer
_custom_op
_decomp [BE][2/16] fix typos in torch/ (torch/_*/) (#156312) 2025-07-12 05:47:06 +00:00
_dispatch Improve torch.ops typing (#154555) 2025-06-22 15:52:27 +00:00
_dynamo [Dynamo][Better Engineering] Add typing for comptime, cache, and convert_frame (#158379) 2025-07-18 02:11:57 +00:00
_export Add FP8 Types (#158430) 2025-07-17 18:09:56 +00:00
_functorch Make Inductor imports TYPE_CHECKING only (#158524) 2025-07-17 19:55:19 +00:00
_higher_order_ops remove allow-untyped-defs from torch/_higher_order_ops/run_const_graph.py (#157847) 2025-07-12 15:42:12 +00:00
_inductor Add stack trace to Inductor IR nodes if inductor.config.trace.provenance_tracing=True (#158576) 2025-07-18 04:05:17 +00:00
_lazy [BE][2/16] fix typos in torch/ (torch/_*/) (#156312) 2025-07-12 05:47:06 +00:00
_library [BE] remove torch deploy - conditionals (#158288) 2025-07-17 05:56:07 +00:00
_logging [Bugfix][Inductor] Fix dependency list merged incorrectly for a custom op with multiple mutated inputs and None return type. (#157133) 2025-07-11 09:06:31 +00:00
_numpy Fix torch._numpy to match NumPy when empty ellipsis causes advanced indexing separation (#158297) 2025-07-16 08:11:53 +00:00
_prims [BE][2/16] fix typos in torch/ (torch/_*/) (#156312) 2025-07-12 05:47:06 +00:00
_prims_common [BE][2/16] fix typos in torch/ (torch/_*/) (#156312) 2025-07-12 05:47:06 +00:00
_refs [BE][2/16] fix typos in torch/ (torch/_*/) (#156312) 2025-07-12 05:47:06 +00:00
_strobelight [BE][2/16] fix typos in torch/ (torch/_*/) (#156312) 2025-07-12 05:47:06 +00:00
_subclasses Revert "DDE-Free select with unbacked index. (#157605)" 2025-07-17 16:20:02 +00:00
_vendor
accelerator Add unified memory APIs for torch.accelerator (#152932) 2025-07-17 01:56:01 +00:00
amp Issue warning with reference to user code rather than torch (#155112) 2025-07-14 05:24:23 +00:00
ao Use new type statement to fix public API of types (#158487) 2025-07-17 18:46:44 +00:00
autograd Fix types in graphs.py (#158192) 2025-07-15 19:49:38 +00:00
backends Expose opt_einsum in torch.backends (#157740) 2025-07-15 17:46:43 +00:00
compiler [Dynamo][Better Engineering] Add type coverage to decorators (#158509) 2025-07-17 23:31:26 +00:00
contrib
cpu
csrc [BE]Clean up old APIs in AOTI c shim (#158400) 2025-07-17 23:24:50 +00:00
cuda [BE] remove torch deploy - conditionals (#158288) 2025-07-17 05:56:07 +00:00
distributed Make torch.distributed.breakpoint() set a long timeout (#158481) 2025-07-18 02:18:43 +00:00
distributions [BE][1/16] fix typos in torch/ (#156311) 2025-07-09 11:02:22 +00:00
export [Dynamo][Better Engineering] Add enhanced typing support to _dynamo/eval_frame.py (#158276) 2025-07-16 23:31:10 +00:00
fft [BE][PYFMT] migrate PYFMT for torch/[e-n]*/ to ruff format (#144553) 2025-06-17 08:18:47 +00:00
func
futures Simplify the base classes of _PyFutureMeta (#157757) 2025-07-08 15:39:56 +00:00
fx Shunt fx_interpreter graphmodule print on error into tlparse (#158469) 2025-07-18 02:18:43 +00:00
headeronly [BE] Get rid of final mentions of BUILD_SPLIT_CUDA (#158453) 2025-07-17 06:47:10 +00:00
jit [4/n] Remove references to TorchScript in PyTorch docs (#158317) 2025-07-16 20:01:34 +00:00
legacy
lib [2/N] Fix cppcoreguidelines-init-variables suppression (#146237) 2025-06-19 23:26:42 +00:00
linalg
masked [Pyrefly][Refactor] Replace dict() calls with literal dict syntax for improved readability (#157735) 2025-07-08 18:10:33 +00:00
monitor
mps [BE][12/16] fix typos in torch/ (#156602) 2025-07-02 22:55:29 +00:00
mtia [BE][PYFMT] migrate PYFMT for torch/[e-n]*/ to ruff format (#144553) 2025-06-17 08:18:47 +00:00
multiprocessing [BE][12/16] fix typos in torch/ (#156602) 2025-07-02 22:55:29 +00:00
nativert [NativeRT] Remove makeProxyExecutor from ModelRunner interface (#158587) 2025-07-18 03:20:40 +00:00
nested Add check nested_tensor_from_jagged param jagged_dim >= 1 (#157770) 2025-07-10 00:34:39 +00:00
nn Add better typing to avaialbe kernel options for flex attention (#158383) 2025-07-16 06:06:29 +00:00
onnx [ONNX] Remove legacy registration and dispatcher (#158283) 2025-07-15 21:00:49 +00:00
optim [Pyrefly][Refactor] Replace dict() calls with literal dict syntax for improved readability (#157735) 2025-07-08 18:10:33 +00:00
package [BE][Ez]: Update ruff to 0.12.2 (#157937) 2025-07-11 15:16:20 +00:00
profiler [BE][Ez]: Autotype torch/profiler with ruff ANN (#157923) 2025-07-09 22:07:50 +00:00
quantization [BE][6/16] fix typos in torch/ (#156316) 2025-06-23 02:57:34 +00:00
signal [BE][6/16] fix typos in torch/ (#156316) 2025-06-23 02:57:34 +00:00
sparse [build] modernize build-frontend: python setup.py develop/install -> [uv ]pip install --no-build-isolation [-e ]. (#156027) 2025-07-09 11:24:27 +00:00
special [BE][6/16] fix typos in torch/ (#156316) 2025-06-23 02:57:34 +00:00
testing [ca] reset between tests (#158418) 2025-07-17 20:14:29 +00:00
utils Add torch._C._log_api_usage_once to datapipes (mapper) (#155489) 2025-07-17 19:01:49 +00:00
xpu [BE][6/16] fix typos in torch/ (#156316) 2025-06-23 02:57:34 +00:00
__config__.py
__future__.py
__init__.py [BE] remove torch deploy - conditionals (#158288) 2025-07-17 05:56:07 +00:00
_appdirs.py
_classes.py remove allow-untyped-defs from torch/_classes.py (#157231) 2025-07-08 00:11:52 +00:00
_compile.py
_custom_ops.py
_environment.py
_guards.py Revert "[dynamo][fsdp] Consistent behavior of int attributes (#157262)" 2025-07-10 23:11:18 +00:00
_jit_internal.py [BE][1/16] fix typos in torch/ (#156311) 2025-07-09 11:02:22 +00:00
_linalg_utils.py Update is_sparse doc to mention that it is sparse_coo specific (#157378) 2025-07-09 18:22:14 +00:00
_lobpcg.py [BE][1/16] fix typos in torch/ (#156311) 2025-07-09 11:02:22 +00:00
_lowrank.py [BE][1/16] fix typos in torch/ (#156311) 2025-07-09 11:02:22 +00:00
_meta_registrations.py Revert "DDE-Free select with unbacked index. (#157605)" 2025-07-17 16:20:02 +00:00
_namedtensor_internals.py
_ops.py [BE] remove torch deploy - conditionals (#158288) 2025-07-17 05:56:07 +00:00
_python_dispatcher.py
_size_docs.py
_sources.py
_storage_docs.py
_streambase.py
_tensor_docs.py Documentation Fix: torch.tensor.scatter_ docs (#157929) 2025-07-10 19:22:52 +00:00
_tensor_str.py
_tensor.py [PyTorch] Deprecate numpy serialization for MTIA (#157884) 2025-07-11 17:57:33 +00:00
_thread_safe_fork.py
_torch_docs.py Revert "[cuda][cupy] Improve cupy device placement when device is provided (#158320)" 2025-07-16 19:15:33 +00:00
_utils_internal.py [BE] remove torch deploy - conditionals (#158288) 2025-07-17 05:56:07 +00:00
_utils.py [BE][1/16] fix typos in torch/ (#156311) 2025-07-09 11:02:22 +00:00
_VF.py
_vmap_internals.py
_weights_only_unpickler.py
CMakeLists.txt Migrate c10/macros/cmake_macros.h.in to torch/headeronly (#158035) 2025-07-15 19:52:59 +00:00
custom_class_detail.h
custom_class.h [BE][1/16] fix typos in torch/ (#156311) 2025-07-09 11:02:22 +00:00
extension.h
functional.py [BE][1/16] fix typos in torch/ (#156311) 2025-07-09 11:02:22 +00:00
header_only_apis.txt Reapply D77381084 / #156964: Rename torch::standalone to headeronly (#157251) 2025-06-30 23:25:30 +00:00
hub.py [BE][1/16] fix typos in torch/ (#156311) 2025-07-09 11:02:22 +00:00
library.h [BE][1/16] fix typos in torch/ (#156311) 2025-07-09 11:02:22 +00:00
library.py [BE] remove torch deploy - conditionals (#158288) 2025-07-17 05:56:07 +00:00
overrides.py [BE][1/16] fix typos in torch/ (#156311) 2025-07-09 11:02:22 +00:00
py.typed
quasirandom.py
random.py
return_types.py
script.h
serialization.py Reduce random reads for offset metadata when calling torch.load under FakeTensorMode (#157931) 2025-07-17 22:17:52 +00:00
storage.py
torch_version.py
types.py
version.py.tpl