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

Fix and refactor get_all_subclasses() util in shampoo_types_test.py #75

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions distributed_shampoo/tests/shampoo_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,37 @@
SubclassesType = TypeVar("SubclassesType")


def get_all_subclasses(cls: SubclassesType) -> list[SubclassesType]:
def get_all_subclasses(
cls: SubclassesType, include_cls_self: bool = True
) -> list[SubclassesType]:
"""
Retrieves all subclasses of a given class, optionally including the class itself.

This function uses a helper function to recursively find all unique subclasses
of the specified class.

Args:
cls (SubclassesType): The class for which to find subclasses.
include_cls_self (bool): Whether to include the class itself in the result. (Default: True)

Returns:
list[SubclassesType]: A list of all unique subclasses of the given class.
"""

def get_all_unique_subclasses(cls: SubclassesType) -> set[SubclassesType]:
"""Gets all unique subclasses of a given class recursively."""
assert (
subclasses := getattr(cls, "__subclasses__", lambda: None)()
) is not None, f"{cls} does not have __subclasses__."
return reduce(or_, map(get_all_unique_subclasses, subclasses), set())
return reduce(or_, map(get_all_unique_subclasses, subclasses), {cls})

return list(get_all_unique_subclasses(cls))
return list(get_all_unique_subclasses(cls) - (set() if include_cls_self else {cls}))


class AdaGradGraftingConfigSubclassesTest(unittest.TestCase):
def test_illegal_epsilon(self) -> None:
epsilon = 0.0
for cls in [AdaGradGraftingConfig] + get_all_subclasses(AdaGradGraftingConfig):
for cls in get_all_subclasses(AdaGradGraftingConfig):
with self.subTest(cls=cls):
self.assertRaisesRegex(
ValueError,
Expand All @@ -53,7 +69,7 @@ def test_illegal_beta2(
self,
) -> None:
for cls, beta2 in itertools.product(
[RMSpropGraftingConfig] + get_all_subclasses(RMSpropGraftingConfig),
get_all_subclasses(RMSpropGraftingConfig),
(-1.0, 0.0, 1.3),
):
with self.subTest(cls=cls, beta2=beta2):
Expand All @@ -70,7 +86,8 @@ def test_illegal_beta2(
class PreconditionerConfigSubclassesTest(unittest.TestCase):
def test_illegal_num_tolerated_failed_amortized_computations(self) -> None:
num_tolerated_failed_amortized_computations = -1
for cls in get_all_subclasses(PreconditionerConfig):
# Not testing for the base class PreconditionerConfig because it is an abstract class.
for cls in get_all_subclasses(PreconditionerConfig, include_cls_self=False):
with self.subTest(cls=cls):
self.assertRaisesRegex(
ValueError,
Expand Down
Loading