import torch


def to(tensors, *args, **kwargs):
    """Transfer tensors via their to-method and return them.

    We handle case where tensors are just one tensor or dict of tensors.
    """

    def _convert(tnsr):
        if isinstance(tnsr, torch.Tensor):
            tnsr = tnsr.to(*args, **kwargs)
        return tnsr

    if isinstance(tensors, dict):
        tensors_to = {}
        for key, tens in tensors.items():
            tensors_to[key] = _convert(tens)
    else:
        tensors_to = _convert(tensors)

    return tensors_to