keras/src/tree/optree_impl.py/_yield_flat_up_to
def _yield_flat_up_to(shallow_tree, input_tree, path=()):
    if isinstance(shallow_tree, (str, bytes)) or not (
        isinstance(
            shallow_tree, (collections.abc.Mapping, collections.abc.Sequence)
        )
        or optree.is_namedtuple(shallow_tree)
    ):
        yield (path, input_tree)
    else:
        input_tree = dict(_yield_sorted_items(input_tree))
        for shallow_key, shallow_subtree in _yield_sorted_items(shallow_tree):
            subpath = path + (shallow_key,)
            input_subtree = input_tree[shallow_key]
            for leaf_path, leaf_value in _yield_flat_up_to(
                shallow_subtree, input_subtree, path=subpath
            ):
                yield (leaf_path, leaf_value)