Utils.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #pragma once
  2. #include <ATen/EmptyTensor.h>
  3. #include <ATen/Formatting.h>
  4. #include <ATen/core/ATenGeneral.h>
  5. #include <ATen/core/Generator.h>
  6. #include <c10/core/ScalarType.h>
  7. #include <c10/core/StorageImpl.h>
  8. #include <c10/core/UndefinedTensorImpl.h>
  9. #include <c10/util/ArrayRef.h>
  10. #include <c10/util/Exception.h>
  11. #include <c10/util/accumulate.h>
  12. #include <c10/util/irange.h>
  13. #include <algorithm>
  14. #include <memory>
  15. #include <numeric>
  16. #include <sstream>
  17. #include <typeinfo>
  18. #define AT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
  19. TypeName(const TypeName&) = delete; \
  20. void operator=(const TypeName&) = delete
  21. namespace at {
  22. TORCH_API int _crash_if_asan(int);
  23. // Converts a TensorList (i.e. ArrayRef<Tensor> to vector of TensorImpl*)
  24. // NB: This is ONLY used by legacy TH bindings, and ONLY used by cat.
  25. // Once cat is ported entirely to ATen this can be deleted!
  26. static inline std::vector<TensorImpl*> checked_dense_tensor_list_unwrap(
  27. ArrayRef<Tensor> tensors,
  28. const char* name,
  29. int pos,
  30. DeviceType device_type,
  31. ScalarType scalar_type) {
  32. std::vector<TensorImpl*> unwrapped;
  33. unwrapped.reserve(tensors.size());
  34. for (const auto i : c10::irange(tensors.size())) {
  35. const auto& expr = tensors[i];
  36. if (expr.layout() != Layout::Strided) {
  37. AT_ERROR(
  38. "Expected dense tensor but got ",
  39. expr.layout(),
  40. " for sequence element ",
  41. i,
  42. " in sequence argument at position #",
  43. pos,
  44. " '",
  45. name,
  46. "'");
  47. }
  48. if (expr.device().type() != device_type) {
  49. AT_ERROR(
  50. "Expected object of device type ",
  51. device_type,
  52. " but got device type ",
  53. expr.device().type(),
  54. " for sequence element ",
  55. i,
  56. " in sequence argument at position #",
  57. pos,
  58. " '",
  59. name,
  60. "'");
  61. }
  62. if (expr.scalar_type() != scalar_type) {
  63. AT_ERROR(
  64. "Expected object of scalar type ",
  65. scalar_type,
  66. " but got scalar type ",
  67. expr.scalar_type(),
  68. " for sequence element ",
  69. i,
  70. " in sequence argument at position #",
  71. pos,
  72. " '",
  73. name,
  74. "'");
  75. }
  76. unwrapped.emplace_back(expr.unsafeGetTensorImpl());
  77. }
  78. return unwrapped;
  79. }
  80. template <size_t N>
  81. std::array<int64_t, N> check_intlist(
  82. ArrayRef<int64_t> list,
  83. const char* name,
  84. int pos) {
  85. if (list.empty()) {
  86. // TODO: is this necessary? We used to treat nullptr-vs-not in IntList
  87. // differently with strides as a way of faking optional.
  88. list = {};
  89. }
  90. auto res = std::array<int64_t, N>();
  91. if (list.size() == 1 && N > 1) {
  92. res.fill(list[0]);
  93. return res;
  94. }
  95. if (list.size() != N) {
  96. AT_ERROR(
  97. "Expected a list of ",
  98. N,
  99. " ints but got ",
  100. list.size(),
  101. " for argument #",
  102. pos,
  103. " '",
  104. name,
  105. "'");
  106. }
  107. std::copy_n(list.begin(), N, res.begin());
  108. return res;
  109. }
  110. using at::detail::check_size_nonnegative;
  111. namespace detail {
  112. template <typename T>
  113. TORCH_API Tensor tensor_cpu(ArrayRef<T> values, const TensorOptions& options);
  114. template <typename T>
  115. TORCH_API Tensor
  116. tensor_backend(ArrayRef<T> values, const TensorOptions& options);
  117. template <typename T>
  118. TORCH_API Tensor
  119. tensor_complex_cpu(ArrayRef<T> values, const TensorOptions& options);
  120. template <typename T>
  121. TORCH_API Tensor
  122. tensor_complex_backend(ArrayRef<T> values, const TensorOptions& options);
  123. } // namespace detail
  124. } // namespace at