TensorGeometry.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include <ATen/core/TensorBase.h>
  3. #include <c10/core/WrapDimMinimal.h>
  4. namespace at {
  5. // Return if the tensor geometry represented by `sizes` and `strides` is
  6. // contiguous Although we cache is_contiguous in tensor now, this is till useful
  7. // because it allows checking if a particular geometry is contiguous without
  8. // explicitly constructing a tensor, e.g., when you want to choose a kernel
  9. // strategy based on whether a subgeometry is contiguous.
  10. TORCH_API bool geometry_is_contiguous(IntArrayRef sizes, IntArrayRef strides);
  11. struct TORCH_API TensorGeometry {
  12. TensorGeometry() = default;
  13. explicit TensorGeometry(c10::SymIntArrayRef sizes)
  14. : sizes_(sizes.vec()),
  15. strides_(sizes.size()),
  16. has_symbolic_sizes_strides_(
  17. !c10::asIntArrayRefSlowOpt(sizes).has_value()) {
  18. int64_t dim = sizes.size();
  19. c10::SymInt expected_stride = 1;
  20. for (int64_t i = dim - 1; i >= 0; i--) {
  21. strides_[i] = expected_stride;
  22. expected_stride *= sizes_[i];
  23. }
  24. numel_ = expected_stride;
  25. }
  26. explicit TensorGeometry(const TensorBase& t)
  27. : sizes_(t.sym_sizes().vec()),
  28. strides_(t.sym_strides().vec()),
  29. storage_offset_(t.sym_storage_offset()),
  30. numel_(t.sym_numel()),
  31. has_symbolic_sizes_strides_(
  32. t.unsafeGetTensorImpl()->has_symbolic_sizes_strides()) {}
  33. // true if the tensor is contiguous
  34. bool is_contiguous() const;
  35. int64_t dim() const {
  36. return sizes_.size();
  37. }
  38. int64_t size(int64_t dim) const {
  39. TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
  40. dim = c10::maybe_wrap_dim(dim, this->dim());
  41. return sizes_.at(static_cast<size_t>(dim)).as_int_unchecked();
  42. }
  43. c10::IntArrayRef sizes() const {
  44. TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
  45. return c10::asIntArrayRefUnchecked(sizes_);
  46. }
  47. int64_t stride(int64_t dim) const {
  48. TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
  49. dim = c10::maybe_wrap_dim(dim, this->dim());
  50. return strides_.at(static_cast<size_t>(dim)).as_int_unchecked();
  51. }
  52. c10::IntArrayRef strides() const {
  53. TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
  54. return c10::asIntArrayRefUnchecked(strides_);
  55. }
  56. int64_t storage_offset() const {
  57. TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
  58. return storage_offset_.as_int_unchecked();
  59. }
  60. int64_t numel() const {
  61. TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
  62. return numel_.as_int_unchecked();
  63. }
  64. c10::SymInt sym_size(int64_t dim) const {
  65. dim = c10::maybe_wrap_dim(dim, this->dim());
  66. return sizes_.at(static_cast<size_t>(dim));
  67. }
  68. c10::SymIntArrayRef sym_sizes() const {
  69. return sizes_;
  70. }
  71. c10::SymInt sym_stride(int64_t dim) const {
  72. dim = c10::maybe_wrap_dim(dim, this->dim());
  73. return strides_.at(static_cast<size_t>(dim));
  74. }
  75. c10::SymIntArrayRef sym_strides() const {
  76. return strides_;
  77. }
  78. c10::SymInt sym_storage_offset() const {
  79. return storage_offset_;
  80. }
  81. c10::SymInt sym_numel() const {
  82. return numel_;
  83. }
  84. TensorGeometry transpose(int64_t dim0, int64_t dim1) {
  85. TensorGeometry r = *this; // copy
  86. TORCH_CHECK(
  87. dim0 < dim(),
  88. "transpose: dim0=",
  89. dim0,
  90. " out of range (dim=",
  91. dim(),
  92. ")")
  93. TORCH_CHECK(
  94. dim1 < dim(),
  95. "transpose: dim1=",
  96. dim1,
  97. " out of range (dim=",
  98. dim(),
  99. ")")
  100. std::swap(r.sizes_[dim0], r.sizes_[dim1]);
  101. std::swap(r.strides_[dim0], r.strides_[dim1]);
  102. return r;
  103. }
  104. private:
  105. std::vector<c10::SymInt> sizes_;
  106. std::vector<c10::SymInt> strides_;
  107. c10::SymInt storage_offset_;
  108. c10::SymInt numel_;
  109. bool has_symbolic_sizes_strides_{false};
  110. };
  111. } // namespace at