_conversions.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import torch
  2. import torch._prims_common as utils
  3. # Utilities should come BEFORE this import
  4. from torch._decomp import register_decomposition
  5. from torch._prims_common import check, TensorLikeType
  6. from torch._prims_common.wrappers import out_wrapper
  7. from torch._refs import _broadcast_shapes
  8. # Data conversion references.
  9. #
  10. # Note: this module breaks the usual _refs to torch naming scheme where
  11. # _refs.foo.bar is a ref for torch.foo.bar. The following definitions are not
  12. # part of _refs/__init__.py to avoid name clashes with Python builtin types
  13. # (like int).
  14. __all__ = [
  15. # dtypes
  16. "bfloat16",
  17. "bool",
  18. "byte",
  19. "cdouble",
  20. "cfloat",
  21. "chalf",
  22. "char",
  23. "double",
  24. "float",
  25. "half",
  26. "int",
  27. "long",
  28. "short",
  29. # misc
  30. "complex",
  31. ]
  32. def _make_conversion_method(name: str, dtype: torch.dtype):
  33. def fn(
  34. self: TensorLikeType, memory_format: torch.memory_format = torch.preserve_format
  35. ) -> TensorLikeType:
  36. return self.to(dtype, memory_format=memory_format) # type: ignore[call-overload]
  37. fn.__name__ = name
  38. return fn
  39. bfloat16 = _make_conversion_method("bfloat16", torch.bfloat16)
  40. bool = _make_conversion_method("bool", torch.bool)
  41. byte = _make_conversion_method("byte", torch.uint8)
  42. cdouble = _make_conversion_method("cdouble", torch.cdouble)
  43. cfloat = _make_conversion_method("cfloat", torch.cfloat)
  44. chalf = _make_conversion_method("chalf", torch.complex32)
  45. char = _make_conversion_method("char", torch.int8)
  46. double = _make_conversion_method("double", torch.double)
  47. float = _make_conversion_method("float", torch.float)
  48. half = _make_conversion_method("half", torch.half)
  49. int = _make_conversion_method("int", torch.int)
  50. long = _make_conversion_method("long", torch.long)
  51. short = _make_conversion_method("short", torch.short)
  52. @register_decomposition(torch._ops.ops.aten.complex)
  53. # Note: complex has type promotion tests disabled due to different semantics.
  54. # exact_dtype is for compat with complex_check_dtype from core.
  55. @out_wrapper(exact_dtype=True)
  56. def complex(real: TensorLikeType, imag: TensorLikeType) -> TensorLikeType:
  57. allowed_dtypes = (torch.float32, torch.float64, torch.float16)
  58. check(
  59. real.dtype in allowed_dtypes and imag.dtype in allowed_dtypes,
  60. lambda: (
  61. f"Expected both inputs to be Half, Float or Double tensors but got "
  62. f"{real.dtype} and {imag.dtype}"
  63. ),
  64. )
  65. check(
  66. real.dtype == imag.dtype,
  67. lambda: (
  68. f"Expected object of scalar type {real.dtype} but got "
  69. f"scalar type {imag.dtype} for second argument"
  70. ),
  71. )
  72. result_dtype = utils.corresponding_complex_dtype(real.dtype) # type: ignore[arg-type]
  73. common_shape = _broadcast_shapes(real.shape, imag.shape)
  74. result = real.new_empty(
  75. common_shape,
  76. dtype=result_dtype,
  77. layout=real.layout,
  78. device=real.device,
  79. # pin_memory=real.is_pinned(), # NYI
  80. )
  81. result.real = real
  82. result.imag = imag
  83. return result