_creation.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. """
  2. This module contains tensor creation utilities.
  3. """
  4. import torch
  5. from typing import Optional, List, Tuple, Union, cast
  6. import math
  7. import collections.abc
  8. # Used by make_tensor for generating complex tensor.
  9. complex_to_corresponding_float_type_map = {torch.complex32: torch.float16,
  10. torch.complex64: torch.float32,
  11. torch.complex128: torch.float64}
  12. float_to_corresponding_complex_type_map = {v: k for k, v in complex_to_corresponding_float_type_map.items()}
  13. def make_tensor(
  14. *shape: Union[int, torch.Size, List[int], Tuple[int, ...]],
  15. dtype: torch.dtype,
  16. device: Union[str, torch.device],
  17. low: Optional[float] = None,
  18. high: Optional[float] = None,
  19. requires_grad: bool = False,
  20. noncontiguous: bool = False,
  21. exclude_zero: bool = False
  22. ) -> torch.Tensor:
  23. r"""Creates a tensor with the given :attr:`shape`, :attr:`device`, and :attr:`dtype`, and filled with
  24. values uniformly drawn from ``[low, high)``.
  25. If :attr:`low` or :attr:`high` are specified and are outside the range of the :attr:`dtype`'s representable
  26. finite values then they are clamped to the lowest or highest representable finite value, respectively.
  27. If ``None``, then the following table describes the default values for :attr:`low` and :attr:`high`,
  28. which depend on :attr:`dtype`.
  29. +---------------------------+------------+----------+
  30. | ``dtype`` | ``low`` | ``high`` |
  31. +===========================+============+==========+
  32. | boolean type | ``0`` | ``2`` |
  33. +---------------------------+------------+----------+
  34. | unsigned integral type | ``0`` | ``10`` |
  35. +---------------------------+------------+----------+
  36. | signed integral types | ``-9`` | ``10`` |
  37. +---------------------------+------------+----------+
  38. | floating types | ``-9`` | ``9`` |
  39. +---------------------------+------------+----------+
  40. | complex types | ``-9`` | ``9`` |
  41. +---------------------------+------------+----------+
  42. Args:
  43. shape (Tuple[int, ...]): Single integer or a sequence of integers defining the shape of the output tensor.
  44. dtype (:class:`torch.dtype`): The data type of the returned tensor.
  45. device (Union[str, torch.device]): The device of the returned tensor.
  46. low (Optional[Number]): Sets the lower limit (inclusive) of the given range. If a number is provided it is
  47. clamped to the least representable finite value of the given dtype. When ``None`` (default),
  48. this value is determined based on the :attr:`dtype` (see the table above). Default: ``None``.
  49. high (Optional[Number]): Sets the upper limit (exclusive) of the given range. If a number is provided it is
  50. clamped to the greatest representable finite value of the given dtype. When ``None`` (default) this value
  51. is determined based on the :attr:`dtype` (see the table above). Default: ``None``.
  52. requires_grad (Optional[bool]): If autograd should record operations on the returned tensor. Default: ``False``.
  53. noncontiguous (Optional[bool]): If `True`, the returned tensor will be noncontiguous. This argument is
  54. ignored if the constructed tensor has fewer than two elements.
  55. exclude_zero (Optional[bool]): If ``True`` then zeros are replaced with the dtype's small positive value
  56. depending on the :attr:`dtype`. For bool and integer types zero is replaced with one. For floating
  57. point types it is replaced with the dtype's smallest positive normal number (the "tiny" value of the
  58. :attr:`dtype`'s :func:`~torch.finfo` object), and for complex types it is replaced with a complex number
  59. whose real and imaginary parts are both the smallest positive normal number representable by the complex
  60. type. Default ``False``.
  61. Raises:
  62. ValueError: if ``requires_grad=True`` is passed for integral `dtype`
  63. ValueError: If ``low > high``.
  64. ValueError: If either :attr:`low` or :attr:`high` is ``nan``.
  65. TypeError: If :attr:`dtype` isn't supported by this function.
  66. Examples:
  67. >>> from torch.testing import make_tensor
  68. >>> # Creates a float tensor with values in [-1, 1)
  69. >>> make_tensor((3,), device='cpu', dtype=torch.float32, low=-1, high=1)
  70. tensor([ 0.1205, 0.2282, -0.6380])
  71. >>> # Creates a bool tensor on CUDA
  72. >>> make_tensor((2, 2), device='cuda', dtype=torch.bool)
  73. tensor([[False, False],
  74. [False, True]], device='cuda:0')
  75. """
  76. def _modify_low_high(low, high, lowest, highest, default_low, default_high, dtype):
  77. """
  78. Modifies (and raises ValueError when appropriate) low and high values given by the user (input_low, input_high) if required.
  79. """
  80. def clamp(a, l, h):
  81. return min(max(a, l), h)
  82. low = low if low is not None else default_low
  83. high = high if high is not None else default_high
  84. # Checks for error cases
  85. if low != low or high != high:
  86. raise ValueError("make_tensor: one of low or high was NaN!")
  87. if low > high:
  88. raise ValueError("make_tensor: low must be weakly less than high!")
  89. low = clamp(low, lowest, highest)
  90. high = clamp(high, lowest, highest)
  91. if dtype in [torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64]:
  92. return math.floor(low), math.ceil(high)
  93. return low, high
  94. if len(shape) == 1 and isinstance(shape[0], collections.abc.Sequence):
  95. shape = shape[0] # type: ignore[assignment]
  96. shape = cast(Tuple[int, ...], tuple(shape))
  97. _integral_types = [torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64]
  98. _floating_types = [torch.float16, torch.bfloat16, torch.float32, torch.float64]
  99. _complex_types = [torch.complex32, torch.complex64, torch.complex128]
  100. if requires_grad and dtype not in _floating_types and dtype not in _complex_types:
  101. raise ValueError("make_tensor: requires_grad must be False for integral dtype")
  102. if dtype is torch.bool:
  103. result = torch.randint(0, 2, shape, device=device, dtype=dtype) # type: ignore[call-overload]
  104. elif dtype is torch.uint8:
  105. ranges = (torch.iinfo(dtype).min, torch.iinfo(dtype).max)
  106. low, high = cast(Tuple[int, int], _modify_low_high(low, high, ranges[0], ranges[1], 0, 10, dtype))
  107. result = torch.randint(low, high, shape, device=device, dtype=dtype) # type: ignore[call-overload]
  108. elif dtype in _integral_types:
  109. ranges = (torch.iinfo(dtype).min, torch.iinfo(dtype).max)
  110. low, high = _modify_low_high(low, high, ranges[0], ranges[1], -9, 10, dtype)
  111. result = torch.randint(low, high, shape, device=device, dtype=dtype) # type: ignore[call-overload]
  112. elif dtype in _floating_types:
  113. ranges_floats = (torch.finfo(dtype).min, torch.finfo(dtype).max)
  114. low, high = _modify_low_high(low, high, ranges_floats[0], ranges_floats[1], -9, 9, dtype)
  115. rand_val = torch.rand(shape, device=device, dtype=dtype)
  116. result = high * rand_val + low * (1 - rand_val)
  117. elif dtype in _complex_types:
  118. float_dtype = complex_to_corresponding_float_type_map[dtype]
  119. ranges_floats = (torch.finfo(float_dtype).min, torch.finfo(float_dtype).max)
  120. low, high = _modify_low_high(low, high, ranges_floats[0], ranges_floats[1], -9, 9, dtype)
  121. real_rand_val = torch.rand(shape, device=device, dtype=float_dtype)
  122. imag_rand_val = torch.rand(shape, device=device, dtype=float_dtype)
  123. real = high * real_rand_val + low * (1 - real_rand_val)
  124. imag = high * imag_rand_val + low * (1 - imag_rand_val)
  125. result = torch.complex(real, imag)
  126. else:
  127. raise TypeError(f"The requested dtype '{dtype}' is not supported by torch.testing.make_tensor()."
  128. " To request support, file an issue at: https://github.com/pytorch/pytorch/issues")
  129. if noncontiguous and result.numel() > 1:
  130. result = torch.repeat_interleave(result, 2, dim=-1)
  131. result = result[..., ::2]
  132. if exclude_zero:
  133. if dtype in _integral_types or dtype is torch.bool:
  134. replace_with = torch.tensor(1, device=device, dtype=dtype)
  135. elif dtype in _floating_types:
  136. replace_with = torch.tensor(torch.finfo(dtype).tiny, device=device, dtype=dtype)
  137. else: # dtype in _complex_types:
  138. float_dtype = complex_to_corresponding_float_type_map[dtype]
  139. float_eps = torch.tensor(torch.finfo(float_dtype).tiny, device=device, dtype=float_dtype)
  140. replace_with = torch.complex(float_eps, float_eps)
  141. result[result == 0] = replace_with
  142. if dtype in _floating_types + _complex_types:
  143. result.requires_grad = requires_grad
  144. return result