import torch


def check_random_state(seed, device=torch.device("cuda")):
    """Turn seed into a torch.Generator instance

    Parameters
    ----------
    seed : None | int | instance of torch.Generator
        If seed is None, return the RandomState singleton used by np.random.
        If seed is an int, return a new RandomState instance seeded with seed.
        If seed is already a torch.Generator instance, return it.
        Otherwise raise ValueError.
    device:
    """
    if seed is None:
        g = torch.Generator(device=device)
        g.initial_seed()
        return g
    if isinstance(seed, int):
        g = torch.Generator(device=device)
        g.manual_seed(seed)
        return g
    if isinstance(seed, torch.Generator):
        return seed
    raise ValueError('%r cannot be used to seed a torch.Generator'
                     ' instance' % seed)