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

avoid .to() followed by inplace mutation to appease export #1387

Open
wants to merge 1 commit into
base: gh/bdhirsh/1/base
Choose a base branch
from

Conversation

bdhirsh
Copy link
Contributor

@bdhirsh bdhirsh commented Dec 6, 2024

Export currently has a restriction, where inplace mutations on the output of a call to aten.to() results in an error.

We should really lift this restriction, but in the meantime, this PR avoids hitting that problem in the quantization logic.

I tested by adding the following lines to the bottom of https://github.com/hustvl/ViTMatte/blob/main/modeling%2Fbackbone%2Fvit.py and confirming that export did not error:

quantize_(m, int8_dynamic_activation_int8_weight())
m = unwrap_tensor_subclass(m)

x = torch.randn(4, 3, 128, 128)
m = torch.export.export(m, (x,))
m2 = m.run_decompositions()
print(m2)

Stack from ghstack (oldest at bottom):

Copy link

pytorch-bot bot commented Dec 6, 2024

🔗 Helpful Links

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

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 08bbd50 with merge base 8a805d0 (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.

bdhirsh added a commit that referenced this pull request Dec 6, 2024
ghstack-source-id: b979b7637c8d062b5f5a61c56e1e78edf1b70290
Pull Request resolved: #1387
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 6, 2024
@bdhirsh bdhirsh requested a review from jerryzh168 December 6, 2024 15:11
@bdhirsh
Copy link
Contributor Author

bdhirsh commented Dec 6, 2024

fyi @tugsbayasgalan @yushangdi

@bdhirsh bdhirsh added the topic: bug fix Use this tag for PRs that fix bugs label Dec 6, 2024
@bhack
Copy link

bhack commented Dec 6, 2024

/opt/conda/lib/python3.11/site-packages/torchao/dtypes/uintx/block_sparse_layout.py:        y += bias
/opt/conda/lib/python3.11/site-packages/torchao/dtypes/uintx/plain_layout.py:        y += bias
/opt/conda/lib/python3.11/site-packages/torchao/dtypes/uintx/semi_sparse_layout.py:        y += bias
/opt/conda/lib/python3.11/site-packages/torchao/dtypes/uintx/tensor_core_tiled_layout.py:        y += bias
/opt/conda/lib/python3.11/site-packages/torchao/dtypes/uintx/uint4_layout.py:                y += bias
/opt/conda/lib/python3.11/site-packages/torchao/prototype/quantization/autoquant_v2.py:            y += bias
/opt/conda/lib/python3.11/site-packages/torchao/prototype/quantization/autoquant_v2.py:            y += bias
/opt/conda/lib/python3.11/site-packages/torchao/quantization/autoquant.py:            y += bias
/opt/conda/lib/python3.11/site-packages/torchao/quantization/autoquant.py:            y += bias
/opt/conda/lib/python3.11/site-packages/torchao/quantization/subclass.py:            y += bias
/opt/conda/lib/python3.11/site-packages/torchao/quantization/subclass.py:            y += bias
/opt/conda/lib/python3.11/site-packages/torchao/quantization/weight_only.py:            y += self.bias
```

@jerryzh168
Copy link
Contributor

thanks, we can't use ghstack in torchao btw, you can use https://github.com/modularml/stack-pr or just normal git push

@jerryzh168
Copy link
Contributor

I'm not sure if changing this would have perf implications actually, probably need to benchmark a bit for the popular code paths

@bhack
Copy link

bhack commented Dec 7, 2024

I think it is better to have export coverage in the CI if we can.

@bhack
Copy link

bhack commented Dec 7, 2024

Related pytorch/pytorch#138606

@bhack
Copy link

bhack commented Dec 7, 2024

/cc @yushangdi

yanbing-j pushed a commit to yanbing-j/ao that referenced this pull request Dec 9, 2024
Wording of error message to include AOTI package
@yushangdi
Copy link
Contributor

yushangdi commented Dec 9, 2024

Related pytorch/pytorch#138606

fyi, @jerryzh168 @bhack , the approach in the PR linked above doesn't work. I'm trying out a new approach, mostly this:

if we have a.to(b), and b has a different dtype with a, then it must be a copy. In this case, we do not need to freeze the tensor. Instead, we use torch.ops.aten._assert_tensor_metadata.default to ensure that a must not have the same dtype as b.

cc @tugsbayasgalan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. topic: bug fix Use this tag for PRs that fix bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants