Repository navigation
[dynamo] Migrate AutogradFunctionVariable methods - #195180
AayushMainali-Github wants to merge 2 commits into
Conversation
The class dispatched apply and backward through a call_method if-chain. Register those methods in tp_methods while preserving static and class method fallback handling. Test Plan: ``` git diff --check python -m py_compile torch/_dynamo/variables/misc.py ``` AI-assisted implementation.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195180
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit fbc0e9a with merge base 6a211a9 ( BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
|
@pytorchbot label "topic: not user facing" |
| if name in self.tp_methods: | ||
| return super().call_method(tx, name, args, kwargs) |
There was a problem hiding this comment.
apply is a C method and backward is a @staticmethod, so they would miss the Method handlers without that hand-off. Left a comment explaining why.
apply is a C method and backward is a @staticmethod, so call_method must route those names to the Method table before the static/classmethod fallback. Without that hand-off, apply would graph-break and backward would take the generic staticmethod path. Test Plan: ``` git diff --check python -m py_compile torch/_dynamo/variables/misc.py ``` AI-assisted implementation.
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 1 checks: torchtitan-test / torchtitan-x-pytorch-test / test (torchtitan_models_integration, 1, 1, mt-l-x86aavx2-189-704-a10g-8) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 1 jobs have failed, first few of them are: trunk / linux-jammy-cuda13.2-py3.10-gcc11 / test (pr_time_benchmarks, 1, 1, mt-l-bx86iavx512-94-344-t4-8) Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 1 checks: torchtitan-test / torchtitan-x-pytorch-test / test (torchtitan_models_integration, 1, 1, mt-l-x86aavx2-189-704-a10g-8) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
## Summary Migrate `AutogradFunctionVariable` off a `call_method` if-chain onto declarative `tp_methods`, as in pytorch#195165. `apply` and `backward` are now `Method` handlers; static/class method handling is unchanged. Part of pytorch#195165 (does not close the umbrella). ## Test plan - [x] `git diff --check` - [x] `python -m py_compile torch/_dynamo/variables/misc.py` > AI-assisted implementation. Pull Request resolved: pytorch#195180 Approved by: https://github.com/guilhermeleobas
…p_getset apply's call dispatch was already migrated to the declarative tp_methods table in pytorch#195180, but referencing .apply as a bare attribute and without calling it, it still went through a hardcoded if name == "apply": branch in tp_getattro_impl. This moves that branch to a tp_getset entry instead by following the same pattern. It uses unmodeled_setter instead of a readonly_setter. The reason is that eager Python allows you to write to MyFn.apply directly. Dynamo simply does not model the write. Therefore it should graph-break instead of raise AttributeError. Part of pytorch#195165
…le instead of tp_getset Addresses review feedback on the tp_getset-based approach: apply is a classmethod_descriptor on _FunctionBase (a tp_methods entry, already declared as such since pytorch#195180), not a real CPython data descriptor, so declaring it in tp_getset as well misrepresented its type to the object protocol. The unmodeled_setter was also unreachable in practice — writes to MyFn.apply go through BuiltinVariable._call_setattr, which AutogradFunctionVariable never hooked into, so the setter was never actually exercised. This instead has tp_getattro_impl consult lookup_tp_method directly and return a CallMethodVariable, mirroring the same construction object_generic_getattr already uses generically for any VT with a tp_methods entry and custom call_method logic (object_protocol.py). Removed _get_apply and the tp_getset entry, both now unused, along with the redundant tp_getset lookup at the top of tp_getattro_impl (the base class's default tp_getattro_impl already consults it before dispatching to overrides). This surfaced a real regression during testing: GetAttrVariable has a call_obj_hasattr override specific to AutogradFunctionVariable.apply, needed for torch.vmap's internal hasattr(func, "__name__") check when generate_vmap_rule is set. CallMethodVariable has no equivalent, so test_vmap_generate_rule_compiled broke under the new dispatch. Added the same override to CallMethodVariable (adjusted for its self.method_name attribute instead of GetAttrVariable's self.name). Added test_apply_reference_does_not_recompile, covering a bare, uncalled reference to MyFn.apply (previously untested per review) and confirming it doesn't cause spurious recompilation across repeated calls. Full test/dynamo/test_autograd_function.py suite: 93 passed, 4 skipped (GPU-only).
#197828) [dynamo] Migrate AutogradFunctionVariable.apply attribute access to tp_getset apply's call dispatch was already migrated to the declarative tp_methods table in #195180, but referencing .apply as a bare attribute and without calling it, it still went through a hardcoded if name == "apply": branch in tp_getattro_impl. This moves that branch to a tp_getset entry instead by following the same pattern. It uses unmodeled_setter instead of a readonly_setter. The reason is that eager Python allows you to write to MyFn.apply directly. Dynamo simply does not model the write. Therefore it should graph-break instead of raise AttributeError. Part of #195165 Pull Request resolved: #197828 Approved by: https://github.com/jansel
Summary
Migrate
AutogradFunctionVariableoff acall_methodif-chain onto declarativetp_methods, as in #195165.applyandbackwardare nowMethodhandlers; static/class method handling is unchanged.Part of #195165 (does not close the umbrella).
Test plan
git diff --checkpython -m py_compile torch/_dynamo/variables/misc.pycc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98