Skip to content

Conversation

orangeH25
Copy link
Contributor

This PR fixes a bug in _get_to_kwargs where args is a tuple but the code attempted to call args.remove(arg), leading to:

    def _get_to_kwargs(self, *args, **kwargs):
        # `torch._C._nn._parse_to` can't handle `layout` argument
        for arg in args:
            if isinstance(arg, torch.layout):
>               args.remove(arg)
                ^^^^^^^^^^^
E               AttributeError: 'tuple' object has no attribute 'remove'

torchao/utils.py:722: AttributeError

Temporary test case in test/test_utils.py::TestTorchAOBaseTensor(unittest.TestCase):

    def test__get_to_kwargs_with_layout(self):
        MyClass = TorchAOBaseTensor()
        import torch
        MyClass._get_to_kwargs(torch.strided,device="cuda")

Temporary test command:

pytest --count=1 test/test_utils.py::TestTorchAOBaseTensor::test__get_to_kwargs_with_layout

Copy link

pytorch-bot bot commented Sep 17, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit dc8d607 with merge base 58c3064 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@meta-cla meta-cla 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 Sep 17, 2025
@orangeH25
Copy link
Contributor Author

orangeH25 commented Sep 17, 2025

Hey @jerryzh168 @huydhn , Please help to review this one,thank you.

@jerryzh168
Copy link
Contributor

@orangeH25 thanks! can you add the test to test/test_utils.py?

@orangeH25
Copy link
Contributor Author

@orangeH25 thanks! can you add the test to test/test_utils.py?

Got it, I'll add the test

@orangeH25
Copy link
Contributor Author

Hey @jerryzh168 , the test case has been added. Please review it again. Thanks!

self._test_default_impls_helper(lp_tensor, lp_tensor_for_copy)

def test__get_to_kwargs_with_layout(self):
MyClass = TorchAOBaseTensor()
Copy link
Contributor

@jerryzh168 jerryzh168 Sep 18, 2025

Choose a reason for hiding this comment

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

this is not the intended use for TorchAOBaseTensor, maybe you can add a test item in

def _test_default_impls_helper(self, lp_tensor, lp_tensor_for_copy):
since _get_to_kwargs is also a default impl

Copy link
Contributor

@jerryzh168 jerryzh168 Sep 18, 2025

Choose a reason for hiding this comment

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

we can probably expand the op test for to:

ao/test/test_utils.py

Lines 148 to 152 in 18dbe87

original_device = lp_tensor.device
lp_tensor = lp_tensor.to("cuda")
self.assertEqual(lp_tensor.device.type, "cuda")
lp_tensor = lp_tensor.to(original_device)
self.assertEqual(lp_tensor.device, original_device)

and also add an explicit test for _get_to_kwargs as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll modify the test

Comment on lines +148 to +149
_ = lp_tensor._get_to_kwargs(torch.strided, device="cuda")
_ = lp_tensor._get_to_kwargs(layout=torch.strided, device="cuda")
Copy link
Contributor

Choose a reason for hiding this comment

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

btw, testing the higher level call to to op directly might be better here, lp_tensor.to(...)

@jerryzh168 jerryzh168 added topic: improvement Use this tag if this PR is an improvement (doesn't fit into any of the other categories) topic: bug fix Use this tag for PRs that fix bugs and removed topic: improvement Use this tag if this PR is an improvement (doesn't fit into any of the other categories) labels Sep 22, 2025
@jerryzh168
Copy link
Contributor

I'll merge for now and follow up to change the call to to

@jerryzh168 jerryzh168 merged commit 3d48174 into pytorch:main Sep 26, 2025
20 of 21 checks passed
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.

2 participants