NamedTensorUtils.h 6.6 KB

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