-
Notifications
You must be signed in to change notification settings - Fork 345
Testing subclassing #3153
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
base: main
Are you sure you want to change the base?
Testing subclassing #3153
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -392,6 +392,49 @@ def fake_linear(func, types, args, kwargs): | |
counter["calls"], 2, "Expected fake_linear to be called via aten.t.default" | ||
) | ||
|
||
def test_subclassing(self): | ||
class Parent(TorchAOBaseTensor): | ||
tensor_data_names = ["qdata"] | ||
tensor_attribute_names = ["attr"] | ||
|
||
Parent._ATEN_OP_TABLE[Parent]["op_parent"] = "parent_impl" | ||
Parent._TORCH_FN_TABLE[Parent]["fn_parent"] = "parent_fn_impl" | ||
|
||
class Child(Parent): | ||
tensor_data_names = ["qdata"] | ||
tensor_attribute_names = ["attr"] | ||
|
||
# ensure child has copied parent ops | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
self.assertEqual(Child._ATEN_OP_TABLE[Child]["op_parent"], "parent_impl") | ||
self.assertEqual(Child._TORCH_FN_TABLE[Child]["fn_parent"], "parent_fn_impl") | ||
|
||
# ensure the top-level dicts are distinct (not inherited) | ||
self.assertIsNot(Parent._ATEN_OP_TABLE, Child._ATEN_OP_TABLE) | ||
self.assertIsNot(Parent._TORCH_FN_TABLE, Child._TORCH_FN_TABLE) | ||
|
||
# change the parent's op after subclass creation — should not leak | ||
Parent._ATEN_OP_TABLE[Parent]["new_op"] = "added_later" | ||
self.assertNotIn("new_op", Child._ATEN_OP_TABLE[Child]) | ||
|
||
def test_multiple_inheritance(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah testing multiple inheritance is helpful as well, thanks |
||
class A(TorchAOBaseTensor): | ||
tensor_data_names = ["a"] | ||
tensor_attribute_names = ["b"] | ||
|
||
class B(TorchAOBaseTensor): | ||
tensor_data_names = ["a"] | ||
tensor_attribute_names = ["b"] | ||
|
||
A._ATEN_OP_TABLE[A]["shared"] = "from_a" | ||
B._ATEN_OP_TABLE[B]["shared"] = "from_b" | ||
|
||
class C(A, B): | ||
tensor_data_names = ["a"] | ||
tensor_attribute_names = ["b"] | ||
|
||
# C(A, B) should inherit from A then B, so B wins | ||
self.assertEqual(C._ATEN_OP_TABLE[C]["shared"], "from_b") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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: