from typing import Union
import torch
from typing import List


def detach_to_device(x: Union[torch.Tensor, float, List[float], None], device: torch.device):
    """Detach a tensor from the computational graph, clone and place it on the given device"""
    if x is None:
        return None

    if isinstance(x, torch.Tensor):
        return x.detach().clone().to(device)

    return torch.tensor(x, device=device, dtype=torch.float)