pytorch/torch/utils
Ryan Guo f46f0e465c [dynamo] Initial support for nonstrict_trace (#146367)
## Context
> **Note:** `mark_traceable` got renamed to `nonstrict_trace` after
> offline discussion. The reasons are (1) it aligns with `torch.export`'s
> `nonstrict` notion, and (2) it's more definitive in behavior suggestion.

1. [Overall Design](https://docs.google.com/document/d/1O-dR2ZQaJQVt_v67AVcDCw2yJLtqgkZFwoXK0buEWRg/edit?tab=t.0)
2. [Dynamo graph representation with `torch._higher_order_ops.flat_apply`](https://docs.google.com/document/d/1YHl5nPTJvYeCPE5TO9uA18DPWNgUYGE4gCn6bFvXcBM/edit?tab=t.0#heading=h.xtw3hhbro4gn)

## Summary
This patch adds a `torch._dynamo.nonstrict_trace` decorator, which
currently is an enhanced version of `torch._dynamo.allow_in_graph` (see
docstring for their differences). Specifically, this patch focuses on
the UI and functionality prototyping/plumbing.

The main enhancement is supporting more input types, and the
implementation challenge lies in reconstructing the input objects from
Dynamo `VariableTracker` (while accounting for buffered side-effects and
guards).  This patch takes a middle-ground (simple implementation with a
bit of user labor), by
1. asking the user to provide pytree registration for non-proxy-able
   input types,
2. letting Dynamo trace through `pytree_flatten` (which accounts for
   buffered side-effects and guards automatically),
3. and passing in the TreeSpec as a graph attribute constant into
   `torch._higher_order_ops.flat_apply` (which unflattens the inputs and
   invokes the underlying function).

## Next Steps
In subsequent patches, we will try to support the following:
- annotating on class method
- reads to global tensors
- inputs that contains `pytree.register_constant`-ed instances.
- function as input
- more output types (e.g., any pytree-registered type)
- `torch.nn.Module` as inputs

Pull Request resolved: https://github.com/pytorch/pytorch/pull/146367
Approved by: https://github.com/zou3519
ghstack dependencies: #146714
2025-02-26 19:47:39 +00:00
..
_strobelight [BE]: Enable ruff rule SIM113 (#147290) 2025-02-16 22:41:16 +00:00
_sympy cpp_wrapper: fix test_torchinductor* tests (#146424) 2025-02-25 19:50:37 +00:00
backcompat
benchmark PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
bottleneck PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
data Update ruff linter for PEP585 (#147540) 2025-02-22 04:45:17 +00:00
hipify [ROCm] OCP FP8 Support for new GPUs (#146632) 2025-02-24 22:47:52 +00:00
jit PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
model_dump PEP585: More UP006 fixes (#146392) 2025-02-20 06:18:13 +00:00
serialization Add option to serialization config to reduce random reads from get_record_offset when loading with mmap=True (#143880) 2025-01-31 17:09:20 +00:00
tensorboard PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
viz Fix ReferenceError: weakly-referenced object no longer exists in cycle detector (#146922) 2025-02-24 22:27:39 +00:00
__init__.py
_backport_slots.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
_config_module.py Enable ruff rule S324 (#147665) 2025-02-25 18:27:34 +00:00
_config_typing.pyi
_content_store.py Enable ruff rule S324 (#147665) 2025-02-25 18:27:34 +00:00
_contextlib.py
_cpp_embed_headers.py [BE] Strip #pragma once when embedding the headers (#146871) 2025-02-11 16:49:00 +00:00
_cpp_extension_versioner.py xpu: support sycl with torch.utils.cpp_extension APIs (#132945) 2025-02-16 16:50:59 +00:00
_cxx_pytree.py [BE] add missing overload annotations for tree_map_only (#147699) 2025-02-23 20:21:07 +00:00
_device.py
_exposed_in.py
_filelock.py
_foreach_utils.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
_freeze.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
_functools.py
_get_clean_triton.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
_import_utils.py
_mode_utils.py
_ordered_set.py [BE]: Make OrderedSet reversible (#146904) 2025-02-13 15:11:48 +00:00
_python_dispatch.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
_pytree.py [dynamo] Initial support for nonstrict_trace (#146367) 2025-02-26 19:47:39 +00:00
_stats.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
_thunk.py
_traceback.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
_triton.py
_typing_utils.py Revert "Fix type annotation of Linear.bias (#142326)" 2025-01-26 03:41:00 +00:00
_zip.py
backend_registration.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
bundled_inputs.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
checkpoint.py
collect_env.py
cpp_backtrace.py
cpp_extension.py xpu: torch.xpu.get_arch_list() to return [] if xpu not compiled (#147431) 2025-02-24 01:35:54 +00:00
deterministic.py
dlpack.py
file_baton.py
flop_counter.py [NJT] fix flop counter for SDPA & test (#147032) 2025-02-13 07:14:58 +00:00
hooks.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
mkldnn.py
mobile_optimizer.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
model_zoo.py
module_tracker.py PEP585 update - torch/utils (#145201) 2025-01-21 21:04:10 +00:00
show_pickle.py Use typing.IO[bytes] instead of io.BytesIO in annotations (#144994) 2025-01-27 18:08:07 +00:00
throughput_benchmark.py
weak.py