def METHOD_NAME(
    model,
    ancestor_class: Optional[Set[str]] = None,
    search_class=None,
    exclude_children_of=None,
):
    """
    Find all modules of a certain class (or union of classes) that are direct or
    indirect descendants of other modules of a certain class (or union of classes).
    Returns all matching modules, along with the parent of those moduless and the
    names they are referenced by as the full ansestor name.
    This is a copy of _find_modules_v2 from lora.py. It was copied instead of
    refactored to keep the implementation in lora.py in sync with cloneofsimo.
    """
    # Get the targets we should replace all linears under
    if exclude_children_of is None:
        exclude_children_of = [
            LoraInjectedLinear,
            LoraInjectedConv2d,
        ]
    if search_class is None:
        search_class = [nn.Linear]
    if ancestor_class is not None:
        ancestors = (
            (name, module)
            for name, module in model.named_modules()
            if module.__class__.__name__ in ancestor_class
        )
    else:
        # this, incase you want to naively iterate over all modules.
        ancestors = [(name, module) for name, module in model.named_modules()]
    # For each target find every linear_class module that isn't a child of a LoraInjectedLinear
    for anc_name, ancestor in ancestors:
        for fullname, module in ancestor.named_modules():
            if any([isinstance(module, _class) for _class in search_class]):
                # Find the direct parent if this is a descendant, not a child, of target
                *path, name = fullname.split(".")
                parent = ancestor
                while path:
                    parent = parent.get_submodule(path.pop(0))
                # Skip this linear if it's a child of a LoraInjectedLinear
                if exclude_children_of and any(
                    [isinstance(parent, _class)
                     for _class in exclude_children_of]
                ):
                    continue
                # Otherwise, yield it
                yield parent, name, module, fullname, anc_name