Utils#
Some useful utility functions.
thunder.torch.utils
#
get_device(x)
#
Infer device of torch.Tensor or nn.Module instance. Parameters
x: Union[torch.Tensor, nn.Module] Returns
device: torch.device
Source code in thunder/torch/utils.py
last_checkpoint(root)
#
Load most fresh last.ckpt file based on time. Parameters
root: Union[Path, str] Path to folder, where last.ckpt or its symbolic link supposed to be. Returns
checkpoint_path: Union[Path, str] If last.ckpt exists - returns Path to it. Otherwise, returns 'last'.
Source code in thunder/torch/utils.py
maybe_from_np(*x, device='cpu')
#
Recursively converts numpy arrays to torch.Tensor. Parameters
*x: Any device: Union[torch.device, str] Device to move to, default is CPU. Returns
Collection of tensors. Examples
x, y # np.ndarray z = maybe_from_np(x) # convert to torch.Tensor x, y = maybe_from_np(x, y) # x and y are now tensors x, y, z = maybe_from_np(x, y, z) # maybe_from_np converts np arrays and tensors and does not affect other types dict_of_tensors = to_np(dict_of_np) # maybe_from_np converts any collection
Source code in thunder/torch/utils.py
tensor2np(x)
#
Detaches, moves torch.Tensor to CPU and converts into numpy array. Parameters
x: torch.Tensor Returns
np.ndarray
to_np(*x)
#
Converts collection of tensors into numpy arrays. Parameters
*x: Any Returns
Collection of numpy arrays Examples
x, y # torch.Tensor z = to_np(x) # convert to numpy array x, y = to_np(x, y) # x and y are now numpy arrays x, y, z = to_np(x, y, z) # to_np converts only tensors and does not affect other types dict_of_np = to_np(dict_of_tensors) # to_np converts any collection