| """Utilities used for tensor conversion collections.""" |
|
|
| import dataclasses |
| import operator |
| from abc import ABC |
| from collections import defaultdict, OrderedDict |
| from collections.abc import Mapping, Sequence |
| from copy import copy, deepcopy |
| from functools import partial |
| from typing import Any, Callable, List, Optional, Tuple, Union |
|
|
| import numpy as np |
| import torch |
|
|
|
|
| _CPU_DEVICES = ("cpu", torch.device("cpu")) |
|
|
| __all__ = ['move_data_to_device'] |
|
|
| def to_dtype_tensor( |
| value: Union[int, float, List[Union[int, float]]], dtype: torch.dtype, device: Union[str, torch.device] |
| ) -> torch.Tensor: |
| return torch.tensor(value, dtype=dtype, device=device) |
|
|
|
|
| def from_numpy(value: np.ndarray, device: Union[str, torch.device]) -> torch.Tensor: |
| return torch.from_numpy(value).to(device) |
|
|
|
|
| def to_numpy(value: torch.Tensor) -> np.ndarray: |
| return value.cpu().numpy() |
|
|
|
|
| CONVERSION_DTYPES: List[Tuple[Any, Callable[[Any, Any], torch.Tensor]]] = [ |
| |
| (bool, partial(to_dtype_tensor, dtype=torch.uint8)), |
| (int, partial(to_dtype_tensor, dtype=torch.int)), |
| (np.int32, partial(to_dtype_tensor, dtype=torch.int)), |
| (float, partial(to_dtype_tensor, dtype=torch.float)), |
| (np.float16, partial(to_dtype_tensor, dtype=torch.half)), |
| (np.float32, partial(to_dtype_tensor, dtype=torch.float)), |
| (np.ndarray, from_numpy), |
| ] |
|
|
|
|
| def _is_namedtuple(obj: object) -> bool: |
| |
| return isinstance(obj, tuple) and hasattr(obj, "_asdict") and hasattr(obj, "_fields") |
|
|
|
|
| def _is_dataclass_instance(obj: object) -> bool: |
| |
| return dataclasses.is_dataclass(obj) and not isinstance(obj, type) |
|
|
|
|
| def apply_to_collection( |
| data: Any, |
| dtype: Union[type, Any, Tuple[Union[type, Any]]], |
| function: Callable, |
| *args: Any, |
| wrong_dtype: Optional[Union[type, Tuple[type]]] = None, |
| include_none: bool = True, |
| **kwargs: Any, |
| ) -> Any: |
| """Recursively applies a function to all elements of a certain dtype. |
| |
| Args: |
| data: the collection to apply the function to |
| dtype: the given function will be applied to all elements of this dtype |
| function: the function to apply |
| *args: positional arguments (will be forwarded to calls of ``function``) |
| wrong_dtype: the given function won't be applied if this type is specified and the given collections |
| is of the ``wrong_dtype`` even if it is of type ``dtype`` |
| include_none: Whether to include an element if the output of ``function`` is ``None``. |
| **kwargs: keyword arguments (will be forwarded to calls of ``function``) |
| |
| Returns: |
| The resulting collection |
| """ |
| |
| if isinstance(data, dtype) and (wrong_dtype is None or not isinstance(data, wrong_dtype)): |
| return function(data, *args, **kwargs) |
|
|
| elem_type = type(data) |
|
|
| |
| if isinstance(data, Mapping): |
| out = [] |
| for k, v in data.items(): |
| v = apply_to_collection( |
| v, dtype, function, *args, wrong_dtype=wrong_dtype, include_none=include_none, **kwargs |
| ) |
| if include_none or v is not None: |
| out.append((k, v)) |
| if isinstance(data, defaultdict): |
| return elem_type(data.default_factory, OrderedDict(out)) |
| return elem_type(OrderedDict(out)) |
|
|
| is_namedtuple = _is_namedtuple(data) |
| is_sequence = isinstance(data, Sequence) and not isinstance(data, (str, bytes, bytearray)) |
| if is_namedtuple or is_sequence: |
| out = [] |
| for d in data: |
| v = apply_to_collection( |
| d, dtype, function, *args, wrong_dtype=wrong_dtype, include_none=include_none, **kwargs |
| ) |
| if include_none or v is not None: |
| out.append(v) |
| return elem_type(*out) if is_namedtuple else elem_type(out) |
|
|
| if _is_dataclass_instance(data): |
| |
| |
| |
| fields = {} |
| memo = {} |
| for field in dataclasses.fields(data): |
| field_value = getattr(data, field.name) |
| fields[field.name] = (field_value, field.init) |
| memo[id(field_value)] = field_value |
| result = deepcopy(data, memo=memo) |
| |
| for field_name, (field_value, field_init) in fields.items(): |
| if field_init: |
| v = apply_to_collection( |
| field_value, |
| dtype, |
| function, |
| *args, |
| wrong_dtype=wrong_dtype, |
| include_none=include_none, |
| **kwargs, |
| ) |
| if not field_init or (not include_none and v is None): |
| v = getattr(data, field_name) |
| try: |
| setattr(result, field_name, v) |
| except dataclasses.FrozenInstanceError as e: |
| raise Exception( |
| "A frozen dataclass was passed to `apply_to_collection` but this is not allowed." |
| " HINT: is your batch a frozen dataclass?" |
| ) from e |
| return result |
|
|
| |
| return data |
|
|
|
|
| def apply_to_collections( |
| data1: Optional[Any], |
| data2: Optional[Any], |
| dtype: Union[type, Any, Tuple[Union[type, Any]]], |
| function: Callable, |
| *args: Any, |
| wrong_dtype: Optional[Union[type, Tuple[type]]] = None, |
| **kwargs: Any, |
| ) -> Any: |
| """Zips two collections and applies a function to their items of a certain dtype. |
| |
| Args: |
| data1: The first collection |
| data2: The second collection |
| dtype: the given function will be applied to all elements of this dtype |
| function: the function to apply |
| *args: positional arguments (will be forwarded to calls of ``function``) |
| wrong_dtype: the given function won't be applied if this type is specified and the given collections |
| is of the ``wrong_dtype`` even if it is of type ``dtype`` |
| **kwargs: keyword arguments (will be forwarded to calls of ``function``) |
| |
| Returns: |
| The resulting collection |
| |
| Raises: |
| AssertionError: |
| If sequence collections have different data sizes. |
| """ |
| if data1 is None: |
| if data2 is None: |
| return |
| |
| data1, data2 = data2, None |
|
|
| elem_type = type(data1) |
|
|
| if isinstance(data1, dtype) and data2 is not None and (wrong_dtype is None or not isinstance(data1, wrong_dtype)): |
| return function(data1, data2, *args, **kwargs) |
|
|
| if isinstance(data1, Mapping) and data2 is not None: |
| |
| zipped = {k: (data1[k], data2[k]) for k in data1.keys() | data2.keys()} |
| return elem_type( |
| { |
| k: apply_to_collections(*v, dtype, function, *args, wrong_dtype=wrong_dtype, **kwargs) |
| for k, v in zipped.items() |
| } |
| ) |
|
|
| is_namedtuple = _is_namedtuple(data1) |
| is_sequence = isinstance(data1, Sequence) and not isinstance(data1, str) |
| if (is_namedtuple or is_sequence) and data2 is not None: |
| assert len(data1) == len(data2), "Sequence collections have different sizes" |
| out = [ |
| apply_to_collections(v1, v2, dtype, function, *args, wrong_dtype=wrong_dtype, **kwargs) |
| for v1, v2 in zip(data1, data2) |
| ] |
| return elem_type(*out) if is_namedtuple else elem_type(out) |
|
|
| return apply_to_collection(data1, dtype, function, *args, wrong_dtype=wrong_dtype, **kwargs) |
|
|
|
|
| class TransferableDataType(ABC): |
| """A custom type for data that can be moved to a torch device via ``.to(...)``. |
| |
| Example: |
| |
| >>> isinstance(dict, TransferableDataType) |
| False |
| >>> isinstance(torch.rand(2, 3), TransferableDataType) |
| True |
| >>> class CustomObject: |
| ... def __init__(self): |
| ... self.x = torch.rand(2, 2) |
| ... def to(self, device): |
| ... self.x = self.x.to(device) |
| ... return self |
| >>> isinstance(CustomObject(), TransferableDataType) |
| True |
| """ |
|
|
| @classmethod |
| def __subclasshook__(cls, subclass: Any) -> Union[bool, Any]: |
| if cls is TransferableDataType: |
| to = getattr(subclass, "to", None) |
| return callable(to) |
| return NotImplemented |
|
|
|
|
| def move_data_to_device(batch: Any, device: Union[str, torch.device], |
| non_blocking: Optional[bool] = None) -> Any: |
| """Transfers a collection of data to the given device. Any object that defines a method ``to(device)`` will be |
| moved and all other objects in the collection will be left untouched. |
| |
| Args: |
| batch: A tensor or collection of tensors or anything that has a method ``.to(...)``. |
| See :func:`apply_to_collection` for a list of supported collection types. |
| device: The device to which the data should be moved |
| non_blocking: Force non-blocking copy or not. |
| |
| Return: |
| the same collection but with all contained tensors residing on the new device. |
| |
| See Also: |
| - :meth:`torch.Tensor.to` |
| - :class:`torch.device` |
| """ |
|
|
| def batch_to(data: Any) -> Any: |
| kwargs = {} |
| |
| if isinstance(data, torch.Tensor) and device not in _CPU_DEVICES: |
| kwargs["non_blocking"] = True |
| if non_blocking is not None: |
| kwargs["non_blocking"] = non_blocking |
| data_output = data.to(device, **kwargs) |
| |
| import cruise |
| try: |
| trainer_params = cruise.last_cli().hparams.trainer |
| if trainer_params.strategy.startswith('deepspeed'): |
| if torch.is_floating_point(data_output): |
| dtype = cruise.utilities.PrecisionType.to_dtype(trainer_params.precision) |
| data_output = data_output.to(dtype) |
| except RuntimeError: |
| |
| pass |
| if data_output is not None: |
| return data_output |
| |
| return data |
|
|
| dtype = TransferableDataType |
| return apply_to_collection(batch, dtype=dtype, function=batch_to) |
|
|
|
|
| def convert_to_tensors(data: Any, device: Union[str, torch.device]) -> Any: |
| for src_dtype, conversion_func in CONVERSION_DTYPES: |
| data = apply_to_collection(data, src_dtype, conversion_func, device=device) |
|
|
| def _move_to_device_and_make_contiguous(t: torch.Tensor, device: Union[str, torch.device]) -> torch.Tensor: |
| return t.to(device).contiguous() |
|
|
| data = apply_to_collection(data, torch.Tensor, _move_to_device_and_make_contiguous, device=device) |
| return data |
|
|
|
|
| def convert_tensors_to_numpy(data: Any) -> Any: |
| data = apply_to_collection(data, torch.Tensor, to_numpy) |
| return data |
|
|