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. #define AT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
  15. TypeName(const TypeName&) = delete; \
  16. void operator=(const TypeName&) = delete
  17. namespace at {
  18. TORCH_API int _crash_if_asan(int);
  19. // Converts a TensorList (i.e. ArrayRef<Tensor> to vector of TensorImpl*)
  20. // NB: This is ONLY used by legacy TH bindings, and ONLY used by cat.
  21. // Once cat is ported entirely to ATen this can be deleted!
  22. inline std::vector<TensorImpl*> checked_dense_tensor_list_unwrap(
  23. ArrayRef<Tensor> tensors,
  24. const char* name,
  25. int pos,
  26. c10::DeviceType device_type,
  27. ScalarType scalar_type) {
  28. std::vector<TensorImpl*> unwrapped;
  29. unwrapped.reserve(tensors.size());
  30. for (const auto i : c10::irange(tensors.size())) {
  31. const auto& expr = tensors[i];
  32. if (expr.layout() != Layout::Strided) {
  33. TORCH_CHECK(
  34. false,
  35. "Expected dense tensor but got ",
  36. expr.layout(),
  37. " for sequence element ",
  38. i,
  39. " in sequence argument at position #",
  40. pos,
  41. " '",
  42. name,
  43. "'");
  44. }
  45. if (expr.device().type() != device_type) {
  46. TORCH_CHECK(
  47. false,
  48. "Expected object of device type ",
  49. device_type,
  50. " but got device type ",
  51. expr.device().type(),
  52. " for sequence element ",
  53. i,
  54. " in sequence argument at position #",
  55. pos,
  56. " '",
  57. name,
  58. "'");
  59. }
  60. if (expr.scalar_type() != scalar_type) {
  61. TORCH_CHECK(
  62. false,
  63. "Expected object of scalar type ",
  64. scalar_type,
  65. " but got scalar type ",
  66. expr.scalar_type(),
  67. " for sequence element ",
  68. i,
  69. " in sequence argument at position #",
  70. pos,
  71. " '",
  72. name,
  73. "'");
  74. }
  75. unwrapped.emplace_back(expr.unsafeGetTensorImpl());
  76. }
  77. return unwrapped;
  78. }
  79. template <size_t N>
  80. std::array<int64_t, N> check_intlist(
  81. ArrayRef<int64_t> list,
  82. const char* name,
  83. int pos) {
  84. if (list.empty()) {
  85. // TODO: is this necessary? We used to treat nullptr-vs-not in IntList
  86. // differently with strides as a way of faking optional.
  87. list = {};
  88. }
  89. auto res = std::array<int64_t, N>();
  90. if (list.size() == 1 && N > 1) {
  91. res.fill(list[0]);
  92. return res;
  93. }
  94. if (list.size() != N) {
  95. TORCH_CHECK(
  96. false,
  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