Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dynamo][TensorVariable] Support "if param.grad_fn" usecase #126960

Closed
wants to merge 3 commits into from

Conversation

Copy link

pytorch-bot bot commented May 23, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/126960

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (2 Unrelated Failures)

As of commit 365e6c8 with merge base d62b025 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

UNSTABLE - The following job failed but was likely due to flakiness present on trunk and has been marked as unstable:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@anijain2305
Copy link
Contributor Author

anijain2305 commented May 23, 2024

@jansel @bdhirsh With this PR, this simple repro fails

import torch


def fn():
    p = torch.nn.Parameter(torch.empty(4, 4))
    with torch.no_grad():
        # breakpoint()
        return p.add_(1)

print(fn())

opt_fn = torch.compile(fn, backend="eager")
print(opt_fn())

with error message

  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/builder.py", line 1693, in wrap_fx_proxy
    return wrap_fx_proxy_cls(target_cls=TensorVariable, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/builder.py", line 1817, in wrap_fx_proxy_cls
    specialized_props = target_cls.specialize(example_value)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/tensor.py", line 175, in specialize
    "has_grad_fn": value.grad_fn is not None,
                   ^^^^^^^^^^^^^
torch._dynamo.exc.InternalTorchDynamoError: Output 0 of TracableCreateParameterBackward is a view and its base or another view of its base has been modified inplace. This view was created inside a custom Function (or because an input was returned as-is) and the autograd logic to handle view+inplace would override the custom backward associated with the custom Function, leading to incorrect gradients. This behavior is forbidden. You can fix this by cloning the output of the custom Function.

from user code:
   File "/data/users/anijain/pytorch2/examples/repro_param_construction.py", line 8, in fn
    return p.add_(1)

Wondering if there is an easy fix?

UPDATE - I tried adding 'clone in the forward of the autograd.Function as the mesasge suggested, but new serious errors popped. So, removing the clone in the latest commit.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang

[ghstack-poisoned]
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang

[ghstack-poisoned]
@anijain2305 anijain2305 added the topic: not user facing topic category label May 23, 2024
@anijain2305 anijain2305 requested a review from jansel May 23, 2024 19:40
@anijain2305
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label May 24, 2024
@pytorchmergebot
Copy link
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

titaiwangms pushed a commit to titaiwangms/pytorch that referenced this pull request May 28, 2024
@github-actions github-actions bot deleted the gh/anijain2305/343/head branch June 25, 2024 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants