File size: 3,550 Bytes
25a49bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: spopov@google.com (Stefan Popov)
"""Miscellaneous utilities."""

import dataclasses
import re
from typing import Any, Callable, Iterable, Optional, TypeVar, Union

import numpy as np
import torch as t

InputTensor = Union[t.Tensor, np.ndarray, int, float, Iterable]
TorchDevice = Union[t.device, str, None]
T = TypeVar("T")


class TensorContainerMixin:
  """Allows unified operation on all tensors contained in a dataclass."""

  def _apply(self, fn: Callable[[t.Tensor], t.Tensor]):
    result = []
    for field in dataclasses.fields(self):
      field = getattr(self, field.name)
      if t.is_tensor(field):
        field = fn(field)
      elif isinstance(field, list) or isinstance(field, tuple):
        field = [fn(e) if t.is_tensor(e) else e for e in field]
      elif isinstance(field, TensorContainerMixin):
        field = field._apply(fn)
      result.append(field)
    return type(self)(*result)

  def cuda(self: T) -> T:
    return self._apply(lambda v: v.cuda())

  def cpu(self: T) -> T:
    return self._apply(lambda v: v.cpu())

  def detach(self: T) -> T:
    return self._apply(lambda v: v.detach())

  def numpy(self: T) -> T:
    return self._apply(lambda v: v.numpy())

  def to(self: T, device: TorchDevice) -> T:
    return self._apply(lambda v: v.to(device))

  def __getitem__(self: T, index: Any) -> T:
    return self._apply(lambda v: v[index])

  def get_structure(self) -> dict[str, str]:
    """Debugging routine, returns type and shape of the each field."""
    result = {}
    for field in dataclasses.fields(self):
      v = getattr(self, field.name)
      if t.is_tensor(v):
        v: t.Tensor = v.detach()
        dtype = re.sub(r"^torch\.", "", str(v.dtype))
        structure = f"t.{dtype}{list(v.shape)}({v.device})"
      elif isinstance(v, np.ndarray):
        structure = f"np.{v.dtype.name}{list(v.shape)}"
      elif isinstance(v, list):
        structure = f"list[{len(v)}]"
      elif isinstance(v, tuple):
        structure = f"tuple[{len(v)}]"
      else:
        structure = f"{type(v).__name__}"
      result[field.name] = structure
    return result


def to_tensor(v: InputTensor, dtype: t.dtype,
              device: Optional[Union[t.device, str]] = None) -> t.Tensor:
  """Converts a value to tensor, checking the type.

  Args:
    v: The value to convert. If it is already a tensor or an array, this
      function checks that the type is equal to dtype. Otherwise, uses
      torch.as_tensor to convert it to tensor.
    dtype: The required type.
    device: The target tensor device (optional(.

  Returns:
    The resulting tensor

  """
  if not t.is_tensor(v):
    if hasattr(v, "__array_interface__"):
      # Preserve the types of arrays. The must match the given type.
      v = t.as_tensor(v)
    else:
      v = t.as_tensor(v, dtype=dtype)

  if v.dtype != dtype:
    raise ValueError(f"Expecting type '{dtype}', found '{v.dtype}'")

  if device is not None:
    v = v.to(device)

  return v