def get_classes_recursively(module):
    """
    Recursively get all classes from a module and its submodules
    """
    classes = get_classes_in_module(module)

    if hasattr(module, "__path__"):  # Only packages have __path__
        for _, submodule_name, is_pkg in pkgutil.walk_packages(
            module.__path__, module.__name__ + "."
        ):
            try:
                submodule = importlib.import_module(submodule_name)
                classes.update(get_classes_recursively(submodule))
            except ModuleNotFoundError:
                pass  # Ignore any submodules that fail to import

    return classes

    Recursively get all classes from a module and its submodules
    
coremltools/test/optimize/torch/utils.py