NamedTensorUtils.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #pragma once
  2. #include <ATen/NamedTensor.h>
  3. #include <ATen/TensorNames.h>
  4. #include <ATen/WrapDimUtilsMulti.h>
  5. #include <ATen/core/DimVector.h>
  6. #include <ATen/core/Tensor.h>
  7. #include <functional>
  8. namespace at {
  9. using NameVector = SmallVector<Dimname, kDimVectorStaticSize>;
  10. inline bool has_names(ITensorListRef tensors) {
  11. return std::any_of(tensors.begin(), tensors.end(), [](const Tensor& t) {
  12. return t.has_names();
  13. });
  14. }
  15. // Converts dim to an positional index. Errors if `dim` cannot be used to
  16. // refer to any dimension of tensor.
  17. TORCH_API int64_t dimname_to_position(const Tensor& tensor, Dimname dim);
  18. TORCH_API std::vector<int64_t> dimnames_to_positions(
  19. const Tensor& tensor,
  20. DimnameList dims);
  21. // Unifies two DimnameList to produce a third. This is useful for implementing
  22. // the named inference rule for binary broadcasting operations like add.
  23. //
  24. // There are three main constraints:
  25. // 1) Check matching: Names must match positionally from the right.
  26. // 2) Check misaligned: If a name `n` is in `names`, then it must appear at
  27. // the same index from the right in other.
  28. // 3) The output names are obtained by unifying the names individually from the
  29. // right.
  30. TORCH_API std::vector<Dimname> unify_from_right(
  31. DimnameList names,
  32. DimnameList other,
  33. const char* action = "broadcast");
  34. [[noreturn]] inline void reportNYIDimnameOverload(const char* op_name) {
  35. TORCH_CHECK(
  36. false,
  37. op_name,
  38. ": You passed a dimname (string) to this op in place of a dimension "
  39. "index but it does not yet support this behavior. Please pass a dimension "
  40. "index to work around this.");
  41. }
  42. // [NOTE] Writing name inference rules
  43. //
  44. // Operators that support named tensors are either composed of operations that
  45. // support named tensors or implement some name inference rule. An op that
  46. // implements its own name inference rule generally looks like the following:
  47. //
  48. // Tensor op(...) {
  49. // perform_shape_checks(...);
  50. // # (1)
  51. // auto maybe_outnames = compute_outnames(...);
  52. // auto result = [&]() {
  53. // NoNamesGuard guard;
  54. // return op_impl(...);
  55. // }();
  56. // # (2)
  57. // propagate_names_if_nonempty(result, maybe_outnames);
  58. //
  59. // Each op has (1) a compute outnames step and (2) a propagate names step.
  60. //
  61. // compute_outnames is responsible for checking that input names match and
  62. // determining what the output names should be. It returns either:
  63. // - {} (if the inputs tensors are all unnamed)
  64. // - non-empty outnames.
  65. //
  66. // propagate_names_if_nonempty propagates the outnames if they exist to the
  67. // result tensors.
  68. //
  69. // The {} case is an optimization; if the user does not use named tensors they
  70. // pay no perf cost for it.
  71. namespace namedinference {
  72. const Tensor& propagate_names_if_present_and_nonempty(
  73. const Tensor& result,
  74. c10::optional<DimnameList> maybe_names,
  75. bool validate_names = false);
  76. // Propagates `names` to `result` if `names` is not empty.
  77. // `names` can be empty; see [NOTE] Writing name inference rules
  78. // If `names` is not empty, `names.size()` should equal `result.dim()`.
  79. // When in doubt, use this overload instead of the others.
  80. TORCH_API const Tensor& propagate_names_if_nonempty(
  81. const Tensor& result,
  82. DimnameList maybe_names,
  83. bool validate_names = false);
  84. // Propagates `names` to `result`. Only use this if we are certain that there
  85. // are names to propagate (that names is not empty).
  86. TORCH_API const Tensor& propagate_names(
  87. const Tensor& result,
  88. DimnameList names,
  89. bool validate_names = false);
  90. // Propagates all names from src to result.
  91. TORCH_API void propagate_names(const Tensor& result, const Tensor& src);
  92. // Propagates all names except for those at the excluded_idxs.
  93. TORCH_API void propagate_names_except(
  94. const Tensor& result,
  95. const Tensor& src,
  96. IntArrayRef excluded_idxs);
  97. // Used for reduction ops that have a `keepdim` arg.
  98. TORCH_API void propagate_names_for_reduction(
  99. const Tensor& result,
  100. const Tensor& src,
  101. IntArrayRef excluded_idxs,
  102. bool keepdim);
  103. TORCH_API void propagate_names_for_expand(
  104. const Tensor& result,
  105. const Tensor& self);
  106. TORCH_API std::vector<Dimname> compute_cat_outnames(
  107. const MaterializedITensorListRef& tensors);
  108. TORCH_API std::vector<Dimname> compute_broadcast_outnames(
  109. const Tensor& self,
  110. const Tensor& other);
  111. TORCH_API std::vector<Dimname> broadcast_to_outnames(
  112. const Tensor& tensor,
  113. const Tensor& reference_tensor,
  114. const char* op_name);
  115. TORCH_API std::vector<Dimname> compute_matmul_outnames(
  116. const Tensor& self,
  117. const Tensor& other);
  118. TORCH_API std::vector<Dimname> compute_cdist_outnames(
  119. const Tensor& self,
  120. const Tensor& other);
  121. TORCH_API std::vector<Dimname> compute_bmm_outnames(
  122. const Tensor& result,
  123. const Tensor& self,
  124. const Tensor& other);
  125. TORCH_API std::vector<Dimname> compute_squeeze_outnames(const Tensor& tensor);
  126. TORCH_API std::vector<Dimname> compute_squeeze_outnames(
  127. const Tensor& tensor,
  128. std::bitset<dim_bitset_size> dims);
  129. std::vector<Dimname> compute_diagonal_outnames(
  130. const Tensor& tensor,
  131. int64_t dim1,
  132. int64_t dim2);
  133. // TensorImpl* overloads for Legacy TH/THC code. Use these sparingly.
  134. TORCH_API TensorImpl* propagate_names_if_nonempty(
  135. TensorImpl* result,
  136. DimnameList maybe_names,
  137. bool validate_names = false);
  138. TORCH_API TensorImpl* propagate_names(
  139. TensorImpl* result,
  140. DimnameList names,
  141. bool validate_names = false);
  142. TORCH_API void propagate_names(TensorImpl* result, /*const */ TensorImpl* src);
  143. TORCH_API inline void propagate_names(
  144. const TensorBase& result,
  145. DimnameList names,
  146. bool validate_names = false) {
  147. propagate_names(result.unsafeGetTensorImpl(), names, validate_names);
  148. }
  149. TORCH_API inline void propagate_names_if_nonempty(
  150. const TensorBase& result,
  151. DimnameList names,
  152. bool validate_names = false) {
  153. propagate_names_if_nonempty(
  154. result.unsafeGetTensorImpl(), names, validate_names);
  155. }
  156. TORCH_API inline void propagate_names(
  157. const TensorBase& result,
  158. const TensorBase& src) {
  159. propagate_names(result.unsafeGetTensorImpl(), src.unsafeGetTensorImpl());
  160. }
  161. // result = m1 @ m2 + bias
  162. TORCH_API std::vector<Dimname> propagate_names_for_addmm(
  163. const Tensor& m1,
  164. const Tensor& m2,
  165. const Tensor& bias);
  166. TORCH_API std::vector<Dimname> propagate_names_for_addmv(
  167. const Tensor& mat,
  168. const Tensor& vec,
  169. const Tensor& bias);
  170. TORCH_API void check_names_for_dot(TensorImpl* vec1, TensorImpl* vec2);
  171. TORCH_API std::vector<Dimname> compute_baddbmm_outnames(
  172. const Tensor& result,
  173. const Tensor& self,
  174. const Tensor& other,
  175. const Tensor& bias);
  176. TORCH_API bool are_names_equal(TensorImpl* self, TensorImpl* other);
  177. } // namespace namedinference
  178. } // namespace at