pytorch/test/dynamo
William Wen 1d42148fee [dynamo] preserve some FX node metadata of GraphModules (#107067)
Requested from @tugsbayasgalan: we want dynamo to preserve some FX node metadata when we trace `GraphModule`s (`nn_module_stack`, `source_fn`, `stack_trace`). This is helpful for the case when we export an aten-level `GraphModule`, add some (possibly non-torch or non-aten) ops, and we want to transform the graph back into an aten-level graph. Without preserving metadata, future passes that look at metadata (e.g. quantization passes) won't work.

This feature also has the additional benefit of being able to preserve origin line of code when `print_readable`'ing a `GraphModule`. This is helpful when debugging graphs that have passed through dynamo several times.

The added unit test demonstrates the added functionality of this PR.

~This PR is currently a proof-of-concept implementation that shows that preserving node metadata across dynamo is possible.~ This PR preserves node metadata across dynamo by doing the following:
- ~inject a counter variable into the `GraphModule` source code, which is incremented every time a node is run~
- Construct a line number -> node index map in `GraphModule` as the source code is being generated.
- pass a list of node metadata and the line number map to dynamo's bytecode analyzer
- ~dynamo traces the counter as a `ConstantVariable`, so when we create a new proxy, we can determine which original node index this proxy corresponds by looking at the value of the traced counter~
- When we create a new proxy, get the current instruction's line number, and get the node index using the line number map
- index into the original node metadata ~using the counter variable's tracked value.~

~Some things that should be addressed off the top of my head:~
- ~Is this feature even desirable? (Do we really want Dynamo to have special behavior for `GraphModules`? Should we expect users to re-export `GraphModules`?)~
- ~Is there a better approach than to use a counter? We considered using node names, line numbers, and assuming that proxies are created in the same order as the nodes, but each of these 3 have shortcomings. For node names, we only have access to new node names, not the old ones. Using line number is fragile. The third is problematic since not all created nodes go through `create_proxy` (e.g. inputs). We currently generate a line number to node index map when the `GraphModule`'s code is generated.~
- ~What's the best way to send data across the "CPython gap"? That is, it is not obvious how to cleanly pass data from dynamo's `eval_frame.py:_TorchDynamoContext.__call__` to `symbolic_convert.py:InstructionTranslatorBase.__init__`. In this PR, we use a global.~

Pull Request resolved: https://github.com/pytorch/pytorch/pull/107067
Approved by: https://github.com/jansel
2023-09-11 17:11:51 +00:00
..
mock_modules
__init__.py
test_activation_checkpointing.py [activation checkpointing] Add default autocast keys to functional rng wrappers (#107934) 2023-08-25 18:22:02 +00:00
test_after_aot.py Preserve leaf-ness and requires_grad-ness in minified repros (#102899) 2023-06-05 19:56:00 +00:00
test_aot_autograd.py Fix aot sequence_nr to reset bwd flag (#107210) 2023-08-24 16:58:12 +00:00
test_autograd_function.py Dynamo support for autograd.Function w/ once_differentiable (#108686) 2023-09-08 16:10:32 +00:00
test_backends.py Remove dynamo+nvfuser (#105789) 2023-08-08 22:29:32 +00:00
test_compile.py [BE] Enable ruff's UP rules and autoformat dynamo / functorch and refs (#105432) 2023-07-19 13:48:44 +00:00
test_comptime.py Revert "[dynamo] Add BACKEND_MATCH guard to detect and recompile when backend changes (#107337)" 2023-09-08 02:03:48 +00:00
test_config.py Switch dynamic_shapes to True by default (#103597) 2023-06-15 15:16:20 +00:00
test_ctx_manager.py Revert "Flash Attention v2 (#105602)" (#108827) 2023-09-08 07:43:04 +00:00
test_cudagraphs.py [ROCm] enable cudagraph inductor UTs on ROCm (#105662) 2023-08-01 20:55:27 +00:00
test_decorators.py Add API to mark input tensors static for cudagraphs (#107154) 2023-08-16 04:38:19 +00:00
test_dynamic_shapes.py When patching dynamic shapes test class, don't run the original tests (#108681) 2023-09-07 02:13:59 +00:00
test_exc.py Do not mutate SymNode expression. (#107492) 2023-08-22 12:38:05 +00:00
test_export_mutations.py Change _dynamo.export to be export(f)(*args, **kwargs) (#106109) 2023-07-27 21:41:13 +00:00
test_export.py [dynamo] preserve some FX node metadata of GraphModules (#107067) 2023-09-11 17:11:51 +00:00
test_functions.py Functools partial support in dynamo (#108846) 2023-09-09 17:25:02 +00:00
test_global.py [Dynamo] Remove cross import in dynamo unit tests (#100851) 2023-05-11 17:07:25 +00:00
test_higher_order_ops.py Automatically turn on dynamo in cond (#108028) 2023-08-28 10:16:41 +00:00
test_input_attr_tracking.py Lift user defined attributes into inputs for certain cases (user defined types and tensors) (#103386) 2023-06-20 23:45:19 +00:00
test_interop.py
test_logging.py Add create_graph_input debug log (#108836) 2023-09-08 23:00:57 +00:00
test_minifier.py Preserve leaf-ness and requires_grad-ness in minified repros (#102899) 2023-06-05 19:56:00 +00:00
test_misc.py Support Optional typehint without graph breaking (#108970) 2023-09-11 16:42:44 +00:00
test_model_output.py Fix graph break from 'hasattr: HFPretrainedConfigVariable()' (#98119) 2023-04-02 02:56:45 +00:00
test_modules.py Revert "[Dynamo][Test]Add a testcase for module with training state (#108750)" 2023-09-10 04:45:00 +00:00
test_nops.py
test_optimizers.py Cudagraphs support for compiled optimizers (#107504) 2023-08-31 20:47:18 +00:00
test_pre_dispatch.py pre_dispatch tracing: support autocast and no_grad/enable_grad ctx managers, add a pre_dispatch_eager dynamo backend (#103024) 2023-06-29 14:17:42 +00:00
test_profiler.py [dynamo] Add "Torch-Compiled Region" profiler event (#108462) 2023-09-08 02:10:17 +00:00
test_python_autograd.py
test_recompile_ux.py Switch automatic_dynamic_shapes to True by default in fbcode (#104883) 2023-07-13 17:37:57 +00:00
test_recompiles.py Enable dynamic shapes of torch.nn.Parameter (#105855) 2023-08-08 05:40:01 +00:00
test_replay_record.py Add graph break logging option instead of config flag (#103202) 2023-06-12 19:52:31 +00:00
test_repros.py Handle empty lists properly (#107803) 2023-08-24 01:42:29 +00:00
test_skip_non_tensor.py
test_sources.py Simple Source traversal util (#103450) 2023-06-14 20:04:20 +00:00
test_subclasses.py better support for fakeifying and dynamoing through torch_dispatch subclasses (with dynamic shapes) (#107415) 2023-08-29 02:36:48 +00:00
test_subgraphs.py Dynamo tensor aliasing guards, dedup graphargs (#104921) 2023-07-13 22:18:08 +00:00
test_unspec.py NumPy support in torch.compile (#106211) 2023-08-11 00:39:32 +00:00
test_verify_correctness.py remove ipex backend (#104329) 2023-07-04 09:21:27 +00:00
utils.py Grab bag of DTensor enablement stuff (Enable whole graph capture for DTensor) (#105787) 2023-07-30 00:17:45 +00:00