def ChunkedSourceIterator(source_items: List, num_instances: int=1, instance_rank: int=0) -> CheckpointableIterator:
    """
    Cuts source list into chunks, one per instance, and serves out items in chunk corresponding to instance_rank

    This is a source iterator:
    It is meant to be used at the beginning of a data loading pipeline.
    As such, it takes a list as its source and not a CheckpointableIterator.

    Args:
        source_items: input list, must not be empty and must be small enough to fit into RAM entirely, ownership of the list and the data goes to the iterator, do not modify it!
        num_instances: number of instances of this iterator. Meant for use with multi-process data loading, e.g., in distributed training.
        instance_rank: rank of this instance of the iterator. Meant for use with multi-process data loading, e.g., in distributed training.
    """
    if instance_rank >= num_instances:
        raise ValueError("invalid instance_rank")
    # we split the data into num_instances consecutive parts
    # that differ by at most 1 in size
    num_items_per_rank = len(source_items) // num_instances
    ranks_with_additional_item = len(source_items) - num_instances * num_items_per_rank
    def boundary(rank):
        return rank * num_items_per_rank + min(rank, ranks_with_additional_item)
    items = source_items[boundary(instance_rank):boundary(instance_rank + 1)]
    return NativeCheckpointableIterator(items)

    Cuts source list into chunks, one per instance, and serves out items in chunk corresponding to instance_rank

    This is a source iterator:
    It is meant to be used at the beginning of a data loading pipeline.
    As such, it takes a list as its source and not a CheckpointableIterator.

    Args:
        source_items: input list, must not be empty and must be small enough to fit into RAM entirely, ownership of the list and the data goes to the iterator, do not modify it!
        num_instances: number of instances of this iterator. Meant for use with multi-process data loading, e.g., in distributed training.
        instance_rank: rank of this instance of the iterator. Meant for use with multi-process data loading, e.g., in distributed training.
    
kosmos-2/infinibatch/infinibatch/iterators.py