Utils.h 4.1 KB

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