import torch


def repeat_rank_stats(fn, runs=100):
    """Repeats a ranking function multiple times and records how often each
    item gets placed at each rank.

    Args:
        fn: The function that generates rankings.
        runs: How many runs to repeat.
    """
    shape = fn().shape
    out = torch.zeros((shape[0], shape[1], shape[1]))
    for i in range(runs):
        ranking = fn()
        for batch in range(shape[0]):
            for j, r_j in enumerate(ranking[batch, :]):
                out[batch, j, r_j] += 1
    return out / runs