Repository navigation
[dynamo] Migrate AutogradFunctionVariable.apply attribute access to t… - #197828
kostasrigatos wants to merge 3 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/197828
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (8 Unrelated Failures)As of commit a511b9f with merge base fdba4b1 ( NEW FAILURES SUPPRESSED BY AI (non-blocking) - The following jobs failed, but the AI CI Advisor judged the failures not to be evidence against this change:
UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
|
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows. |
|
|
|
@pytorchbot label "topic: not user facing" |
…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
56fadaa to
bdb1360
Compare
|
@claude review these changes |
|
Claude finished @jansel's task in 2m 28s —— View job The getter change keeps the old behavior, but I'd request changes: Full reviewPR Review: #197828SummaryThis PR moves the API Design
Code Quality
Testing
RecommendationRequest Changes The main blocker is the design question: |
…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).
|
@jansel thanks for the thorough review. You were right on all three points. Pushed a fix:
|
|
@claude review again |
|
Claude finished @jansel's task in 2m 36s —— View job The update fixes the problems from the last review, but I'd still request changes: the new Full reviewPR Review: #197828SummaryThe PR now handles attribute reads of Code Quality
Testing
RecommendationRequest Changes The blocker is the Note: I couldn't run tests in this environment (command execution needed approval), so the |
|
@Jansell thanks for the review. You were right on all three points. Pushed a fix:
|
…spatch to apply only Addresses a second round of review feedback. The previous fix checked lookup_tp_method, which matches every entry in tp_methods -- not just apply, but backward too, since both are declared there. That meant a bare reference to MyFn.backward (e.g. f = MyFn.backward; f(ctx, g)) now got routed into call_backward, which asserts type(args[0].value) is FakeBackwardCFunction. That assertion only holds in the narrow context of compiled autograd actually invoking backward -- a plain fake ctx object, or any real user code referencing .backward directly, now hit a hard AssertionError instead of tracing normally through the existing, correct staticmethod path. This narrows the check to name == "apply" specifically -- the only entry whose real underlying attribute is a C-level classmethod descriptor, which is why it needs CallMethodVariable in the first place. backward is a genuine staticmethod and now falls through unchanged to _resolve_staticmethod, exactly as it did before this PR's first commit. Also removed GetAttrVariable.call_obj_hasattr, which is now dead code: since apply is intercepted by the name == "apply" check before GetAttrVariable is ever constructed for it, that override can never run. The equivalent logic already lives on CallMethodVariable from the previous commit. Verified the regression directly: reproduced the AssertionError against the pre-fix code via git stash, confirmed it disappears with the fix restored. Added test_backward_reference_still_uses_staticmethod_path, which fails on the previous commit and passes with this one. Full suite: 94 passed, 4 skipped (GPU-only).
|
@jansel The re-runs across lint.yml, docs-build.yml, and pull.yml are all failing with the same Credentials could not be loaded / AWS upload error, unrelated to the diff — looks like a CI credentials issue rather than anything in this PR. |
|
|
||
| return object_richcompare(self, tx, other, op) | ||
|
|
||
| def call_obj_hasattr( |
There was a problem hiding this comment.
Is this changing anything or just unrelated code motion?
There was a problem hiding this comment.
Yes, it's a real change, not just code motion. Before this PR, apply reads went through GetAttrVariable, which is why the hasattr/generate_vmap_rule special case lived there. Now that tp_getattro_impl routes apply through CallMethodVariable instead, GetAttrVariable is never constructed for apply — so this had to move here too, or the special case would silently stop firing and test_vmap_generate_rule_compiled would break again.
|
|
||
| if name == "apply": | ||
| return GetAttrVariable(self, name, py_type=types.MethodType, source=source) | ||
| return CallMethodVariable(self, name, source=source) |
There was a problem hiding this comment.
Is this right? Why is getattrro resulting in a method call?
There was a problem hiding this comment.
Yes. tp_getattro_impl isn't calling anything here, it's returning a value like every other branch in this method. CallMethodVariable is admittedly misleadingly named for what it does at construction: per its own docstring, it's "a method bound to a VariableTracker instance" — i.e. the reference (what f = MyFn.apply produces in real Python), not the result of calling it. reconstruct confirms this too — it emits plain attribute-access bytecode, not a call. The actual call only happens later, in CallMethodVariable.call_function, when Dynamo processes a real CALL opcode on that value.
This also isn't new machinery — it's the same value type object_generic_getattr's own generic path already constructs for any VariableTracker with a tp_methods entry and custom call_method logic (object_protocol.py, Step 4). This override is just doing explicitly, for apply specifically, what the generic path already does for the general case.
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). 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 / macos-py3-arm64 / test (default, 2, 3, macos-m1-stable) Details for Dev Infra teamRaised by workflow job |
|
@jansel the merge failed on trunk / macos-py3-arm64 / test — the actual error is a Homebrew tap-trust issue ("The following taps are not trusted: aws/aws") on the runner, unrelated to this diff. Could you retry @pytorchbot merge when you get a chance? Thanks a lot! |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
[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
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98