Skip to content

Conversation

Krishn1412
Copy link
Contributor

Added two tests to check if the subclassing logic defined in TorchAOBaseTensor _init_subclass _ works correctly. Refer the comment.

The first test fails for:

self.assertIsNot(Parent._ATEN_OP_TABLE, Child._ATEN_OP_TABLE)
self.assertIsNot(Parent._TORCH_FN_TABLE, Child._TORCH_FN_TABLE)

telling us that Parent._ATEN_OP_TABLE and Child._ATEN_OP_TABLE are the same object, meaning the class attribute dictionary was shared, not re-initialized per subclass.

Copy link

pytorch-bot bot commented Oct 10, 2025

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

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 Oct 10, 2025
@Krishn1412
Copy link
Contributor Author

Is that the expected behavior @jerryzh168 ? If it is, then the if statement will work. But it does seem problematic. Shouldn't each subclass rebuilt its own copy of the dict?

)

def test_subclassing(self):
class Parent(TorchAOBaseTensor):
Copy link
Contributor

@jerryzh168 jerryzh168 Oct 10, 2025

Choose a reason for hiding this comment

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

the test should be doing this:

1. define Parent class
2. Parent class implements some method

# can do a real implementation or just some dummy one
@Parent.implements(aten.cat)
def _(..):
   ...
   
   
3. some child class inherits from parent: Child(Parent)

4. make sure the same op still works
by calling the op (e.g. aten.cat) and make sure it works (make sure it's called)

can do some additional table checks

tensor_data_names = ["qdata"]
tensor_attribute_names = ["attr"]

# ensure child has copied parent ops
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah I think idea is the same, but just changing it to real op implementation with implements and implements_torch_function would be better

Parent._ATEN_OP_TABLE[Parent]["new_op"] = "added_later"
self.assertNotIn("new_op", Child._ATEN_OP_TABLE[Child])

def test_multiple_inheritance(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah testing multiple inheritance is helpful as well, thanks

@jerryzh168
Copy link
Contributor

Is that the expected behavior @jerryzh168 ? If it is, then the if statement will work. But it does seem problematic. Shouldn't each subclass rebuilt its own copy of the dict?

yeah, each sublcass should have their own dict, I think the current impl is problematic

the new test with real op fails for the same line
self.assertIsNot(Parent._ATEN_OP_TABLE, Child._ATEN_OP_TABLE).

Also when it is called using the child tensor, it works.

For the inheritance test case,
it fails at
self.assertEqual(C._ATEN_OP_TABLE[C]["shared"], "from_b")
AssertionError: 'from_a' != 'from_b'
@Krishn1412
Copy link
Contributor Author

@jerryzh168 ,
the new test with real op fails for the same line
self.assertIsNot(Parent._ATEN_OP_TABLE, Child._ATEN_OP_TABLE).

Also when it is called using the child tensor, it works.

For the inheritance test case,
it fails at
self.assertEqual(C._ATEN_OP_TABLE[C]["shared"], "from_b")
AssertionError: 'from_a' != 'from_b'.

Should we start working on changing the implementation?

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants