_creation.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. """
  2. This module contains tensor creation utilities.
  3. """
  4. import collections.abc
  5. import math
  6. import warnings
  7. from typing import cast, List, Optional, Tuple, Union
  8. import torch
  9. _INTEGRAL_TYPES = [
  10. torch.uint8,
  11. torch.int8,
  12. torch.int16,
  13. torch.int32,
  14. torch.int64,
  15. torch.uint16,
  16. torch.uint32,
  17. torch.uint64,
  18. ]
  19. _FLOATING_TYPES = [torch.float16, torch.bfloat16, torch.float32, torch.float64]
  20. _FLOATING_8BIT_TYPES = [
  21. torch.float8_e4m3fn,
  22. torch.float8_e5m2,
  23. torch.float8_e4m3fnuz,
  24. torch.float8_e5m2fnuz,
  25. ]
  26. _COMPLEX_TYPES = [torch.complex32, torch.complex64, torch.complex128]
  27. _BOOLEAN_OR_INTEGRAL_TYPES = [torch.bool, *_INTEGRAL_TYPES]
  28. _FLOATING_OR_COMPLEX_TYPES = [*_FLOATING_TYPES, *_COMPLEX_TYPES]
  29. def _uniform_random_(t: torch.Tensor, low: float, high: float) -> torch.Tensor:
  30. # uniform_ requires to-from <= std::numeric_limits<scalar_t>::max()
  31. # Work around this by scaling the range before and after the PRNG
  32. if high - low >= torch.finfo(t.dtype).max:
  33. return t.uniform_(low / 2, high / 2).mul_(2)
  34. else:
  35. return t.uniform_(low, high)
  36. def make_tensor(
  37. *shape: Union[int, torch.Size, List[int], Tuple[int, ...]],
  38. dtype: torch.dtype,
  39. device: Union[str, torch.device],
  40. low: Optional[float] = None,
  41. high: Optional[float] = None,
  42. requires_grad: bool = False,
  43. noncontiguous: bool = False,
  44. exclude_zero: bool = False,
  45. memory_format: Optional[torch.memory_format] = None,
  46. ) -> torch.Tensor:
  47. r"""Creates a tensor with the given :attr:`shape`, :attr:`device`, and :attr:`dtype`, and filled with
  48. values uniformly drawn from ``[low, high)``.
  49. If :attr:`low` or :attr:`high` are specified and are outside the range of the :attr:`dtype`'s representable
  50. finite values then they are clamped to the lowest or highest representable finite value, respectively.
  51. If ``None``, then the following table describes the default values for :attr:`low` and :attr:`high`,
  52. which depend on :attr:`dtype`.
  53. +---------------------------+------------+----------+
  54. | ``dtype`` | ``low`` | ``high`` |
  55. +===========================+============+==========+
  56. | boolean type | ``0`` | ``2`` |
  57. +---------------------------+------------+----------+
  58. | unsigned integral type | ``0`` | ``10`` |
  59. +---------------------------+------------+----------+
  60. | signed integral types | ``-9`` | ``10`` |
  61. +---------------------------+------------+----------+
  62. | floating types | ``-9`` | ``9`` |
  63. +---------------------------+------------+----------+
  64. | complex types | ``-9`` | ``9`` |
  65. +---------------------------+------------+----------+
  66. Args:
  67. shape (Tuple[int, ...]): Single integer or a sequence of integers defining the shape of the output tensor.
  68. dtype (:class:`torch.dtype`): The data type of the returned tensor.
  69. device (Union[str, torch.device]): The device of the returned tensor.
  70. low (Optional[Number]): Sets the lower limit (inclusive) of the given range. If a number is provided it is
  71. clamped to the least representable finite value of the given dtype. When ``None`` (default),
  72. this value is determined based on the :attr:`dtype` (see the table above). Default: ``None``.
  73. high (Optional[Number]): Sets the upper limit (exclusive) of the given range. If a number is provided it is
  74. clamped to the greatest representable finite value of the given dtype. When ``None`` (default) this value
  75. is determined based on the :attr:`dtype` (see the table above). Default: ``None``.
  76. .. deprecated:: 2.1
  77. Passing ``low==high`` to :func:`~torch.testing.make_tensor` for floating or complex types is deprecated
  78. since 2.1 and will be removed in 2.3. Use :func:`torch.full` instead.
  79. requires_grad (Optional[bool]): If autograd should record operations on the returned tensor. Default: ``False``.
  80. noncontiguous (Optional[bool]): If `True`, the returned tensor will be noncontiguous. This argument is
  81. ignored if the constructed tensor has fewer than two elements. Mutually exclusive with ``memory_format``.
  82. exclude_zero (Optional[bool]): If ``True`` then zeros are replaced with the dtype's small positive value
  83. depending on the :attr:`dtype`. For bool and integer types zero is replaced with one. For floating
  84. point types it is replaced with the dtype's smallest positive normal number (the "tiny" value of the
  85. :attr:`dtype`'s :func:`~torch.finfo` object), and for complex types it is replaced with a complex number
  86. whose real and imaginary parts are both the smallest positive normal number representable by the complex
  87. type. Default ``False``.
  88. memory_format (Optional[torch.memory_format]): The memory format of the returned tensor. Mutually exclusive
  89. with ``noncontiguous``.
  90. Raises:
  91. ValueError: If ``requires_grad=True`` is passed for integral `dtype`
  92. ValueError: If ``low >= high``.
  93. ValueError: If either :attr:`low` or :attr:`high` is ``nan``.
  94. ValueError: If both :attr:`noncontiguous` and :attr:`memory_format` are passed.
  95. TypeError: If :attr:`dtype` isn't supported by this function.
  96. Examples:
  97. >>> # xdoctest: +SKIP
  98. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  99. >>> from torch.testing import make_tensor
  100. >>> # Creates a float tensor with values in [-1, 1)
  101. >>> make_tensor((3,), device='cpu', dtype=torch.float32, low=-1, high=1)
  102. >>> # xdoctest: +SKIP
  103. tensor([ 0.1205, 0.2282, -0.6380])
  104. >>> # Creates a bool tensor on CUDA
  105. >>> make_tensor((2, 2), device='cuda', dtype=torch.bool)
  106. tensor([[False, False],
  107. [False, True]], device='cuda:0')
  108. """
  109. def modify_low_high(
  110. low: Optional[float],
  111. high: Optional[float],
  112. *,
  113. lowest_inclusive: float,
  114. highest_exclusive: float,
  115. default_low: float,
  116. default_high: float,
  117. ) -> Tuple[float, float]:
  118. """
  119. Modifies (and raises ValueError when appropriate) low and high values given by the user (input_low, input_high)
  120. if required.
  121. """
  122. def clamp(a: float, l: float, h: float) -> float:
  123. return min(max(a, l), h)
  124. low = low if low is not None else default_low
  125. high = high if high is not None else default_high
  126. if any(isinstance(value, float) and math.isnan(value) for value in [low, high]):
  127. raise ValueError(
  128. f"`low` and `high` cannot be NaN, but got {low=} and {high=}"
  129. )
  130. elif low == high and dtype in _FLOATING_OR_COMPLEX_TYPES:
  131. warnings.warn(
  132. "Passing `low==high` to `torch.testing.make_tensor` for floating or complex types "
  133. "is deprecated since 2.1 and will be removed in 2.3. "
  134. "Use `torch.full(...)` instead.",
  135. FutureWarning,
  136. stacklevel=3,
  137. )
  138. elif low >= high:
  139. raise ValueError(f"`low` must be less than `high`, but got {low} >= {high}")
  140. elif high < lowest_inclusive or low >= highest_exclusive:
  141. raise ValueError(
  142. f"The value interval specified by `low` and `high` is [{low}, {high}), "
  143. f"but {dtype} only supports [{lowest_inclusive}, {highest_exclusive})"
  144. )
  145. low = clamp(low, lowest_inclusive, highest_exclusive)
  146. high = clamp(high, lowest_inclusive, highest_exclusive)
  147. if dtype in _BOOLEAN_OR_INTEGRAL_TYPES:
  148. # 1. `low` is ceiled to avoid creating values smaller than `low` and thus outside the specified interval
  149. # 2. Following the same reasoning as for 1., `high` should be floored. However, the higher bound of
  150. # `torch.randint` is exclusive, and thus we need to ceil here as well.
  151. return math.ceil(low), math.ceil(high)
  152. return low, high
  153. if len(shape) == 1 and isinstance(shape[0], collections.abc.Sequence):
  154. shape = shape[0] # type: ignore[assignment]
  155. shape = cast(Tuple[int, ...], tuple(shape))
  156. if noncontiguous and memory_format is not None:
  157. raise ValueError(
  158. f"The parameters `noncontiguous` and `memory_format` are mutually exclusive, "
  159. f"but got {noncontiguous=} and {memory_format=}"
  160. )
  161. if requires_grad and dtype in _BOOLEAN_OR_INTEGRAL_TYPES:
  162. raise ValueError(
  163. f"`requires_grad=True` is not supported for boolean and integral dtypes, but got {dtype=}"
  164. )
  165. if dtype is torch.bool:
  166. low, high = cast(
  167. Tuple[int, int],
  168. modify_low_high(
  169. low,
  170. high,
  171. lowest_inclusive=0,
  172. highest_exclusive=2,
  173. default_low=0,
  174. default_high=2,
  175. ),
  176. )
  177. result = torch.randint(low, high, shape, device=device, dtype=dtype)
  178. elif dtype in _BOOLEAN_OR_INTEGRAL_TYPES:
  179. low, high = cast(
  180. Tuple[int, int],
  181. modify_low_high(
  182. low,
  183. high,
  184. lowest_inclusive=torch.iinfo(dtype).min,
  185. highest_exclusive=torch.iinfo(dtype).max
  186. # In theory, `highest_exclusive` should always be the maximum value + 1. However, `torch.randint`
  187. # internally converts the bounds to an int64 and would overflow. In other words: `torch.randint` cannot
  188. # sample 2**63 - 1, i.e. the maximum value of `torch.int64` and we need to account for that here.
  189. + (1 if dtype is not torch.int64 else 0),
  190. # This is incorrect for `torch.uint8`, but since we clamp to `lowest`, i.e. 0 for `torch.uint8`,
  191. # _after_ we use the default value, we don't need to special case it here
  192. default_low=-9,
  193. default_high=10,
  194. ),
  195. )
  196. result = torch.randint(low, high, shape, device=device, dtype=dtype)
  197. elif dtype in _FLOATING_OR_COMPLEX_TYPES:
  198. low, high = modify_low_high(
  199. low,
  200. high,
  201. lowest_inclusive=torch.finfo(dtype).min,
  202. highest_exclusive=torch.finfo(dtype).max,
  203. default_low=-9,
  204. default_high=9,
  205. )
  206. result = torch.empty(shape, device=device, dtype=dtype)
  207. _uniform_random_(
  208. torch.view_as_real(result) if dtype in _COMPLEX_TYPES else result, low, high
  209. )
  210. elif dtype in _FLOATING_8BIT_TYPES:
  211. low, high = modify_low_high(
  212. low,
  213. high,
  214. lowest_inclusive=torch.finfo(dtype).min,
  215. highest_exclusive=torch.finfo(dtype).max,
  216. default_low=-9,
  217. default_high=9,
  218. )
  219. result = torch.empty(shape, device=device, dtype=torch.float32)
  220. _uniform_random_(result, low, high)
  221. result = result.to(dtype)
  222. else:
  223. raise TypeError(
  224. f"The requested dtype '{dtype}' is not supported by torch.testing.make_tensor()."
  225. " To request support, file an issue at: https://github.com/pytorch/pytorch/issues"
  226. )
  227. if noncontiguous and result.numel() > 1:
  228. result = torch.repeat_interleave(result, 2, dim=-1)
  229. result = result[..., ::2]
  230. elif memory_format is not None:
  231. result = result.clone(memory_format=memory_format)
  232. if exclude_zero:
  233. result[result == 0] = (
  234. 1 if dtype in _BOOLEAN_OR_INTEGRAL_TYPES else torch.finfo(dtype).tiny
  235. )
  236. if dtype in _FLOATING_OR_COMPLEX_TYPES:
  237. result.requires_grad = requires_grad
  238. return result