Skip to content

[dynamo] Migrate AutogradFunctionVariable methods - #195180

Closed
AayushMainali-Github wants to merge 2 commits into
pytorch:mainfrom
AayushMainali-Github:fix/autograd-function-variable-195165
Closed

AayushMainali-Github wants to merge 2 commits into
pytorch:mainfrom
AayushMainali-Github:fix/autograd-function-variable-195165

Conversation

@AayushMainali-Github

Copy link
Copy Markdown
Contributor

Summary

Migrate AutogradFunctionVariable off a call_method if-chain onto declarative tp_methods, as in #195165.

apply and backward are now Method handlers; static/class method handling is unchanged.

Part of #195165 (does not close the umbrella).

Test plan

  • git diff --check
  • python -m py_compile torch/_dynamo/variables/misc.py

AI-assisted implementation.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98

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.
@pytorch-bot

pytorch-bot Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

🔗 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 (image):

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.

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Aug 28, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@AayushMainali-Github
AayushMainali-Github marked this pull request as ready for review August 28, 2026 17:47
@AayushMainali-Github

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Aug 28, 2026
Comment on lines +1485 to +1486
if name in self.tp_methods:
return super().call_method(tx, name, args, kwargs)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this?

@AayushMainali-Github AayushMainali-Github Sep 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@drisspg drisspg added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 31, 2026
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.
@guilhermeleobas

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -i

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 1, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 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 team Raised by workflow job

@guilhermeleobas

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
## 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
kostasrigatos added a commit to kostasrigatos/pytorch that referenced this pull request Sep 21, 2026
…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
kostasrigatos added a commit to kostasrigatos/pytorch that referenced this pull request Sep 23, 2026
…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).
pytorchmergebot pushed a commit that referenced this pull request Sep 30, 2026
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: dynamo open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants