def METHOD_NAME(gallery: Union[list, np.ndarray, Tensor],
                  num: int) -> np.ndarray:
    """Random select some elements from the gallery.
    It seems that Pytorch's implementation is slower than numpy so we use
    numpy to randperm the indices.
    Args:
        gallery (list | np.ndarray | torch.Tensor): The gallery from
            which to sample.
        num (int): The number of elements to sample.
    """
    assert len(gallery) >= num
    if isinstance(gallery, list):
        gallery = np.array(gallery)
    cands = np.arange(len(gallery))
    np.random.shuffle(cands)
    rand_inds = cands[:num]
    if not isinstance(gallery, np.ndarray):
        rand_inds = torch.from_numpy(rand_inds).long().to(gallery.device)
    return gallery[rand_inds]