TensorNames.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include <ATen/WrapDimUtils.h>
  3. namespace at {
  4. namespace namedinference {
  5. // TensorName and TensorNames are wrappers around Dimname and DimnameList
  6. // that contain helper functions to make writing name inference rules easier.
  7. //
  8. // A TensorName represents a Dimname associated with some DimnameList (from a
  9. // Tensor). This encapsulates all the information that is needed to check if
  10. // names *match* and to *unify* names.
  11. //
  12. // Definition: Two names in two tensors *match* if they are equal, or if at
  13. // least one of them is a wildcard that can be *refined* to the other name.
  14. //
  15. // Definition: unify(name, other) fails if the names do not match. Otherwise,
  16. // it returns the most refined of name and other.
  17. //
  18. // Here is an example of checking if two names match.
  19. // tensor: Tensor[A, None]
  20. // other: Tensor[A]
  21. //
  22. // Let's say we wish to check if tensor.names[-1] matches other.names[-1].
  23. // None (in tensor) cannot match A (in other) because if the None were refined
  24. // to A, `tensor` would have duplicate names [A, A]. Therefore we need to check
  25. // tensor.names [A, None] for the existence of A.
  26. struct TORCH_API TensorName {
  27. explicit TensorName(ArrayRef<Dimname> origin, int origin_idx)
  28. : origin_(origin),
  29. name_(origin[maybe_wrap_dim(origin_idx, origin.size())]),
  30. origin_idx_(origin_idx) {}
  31. // op_name is only used for error reporting.
  32. const TensorName& unify(const TensorName& other, const char* op_name) const;
  33. Dimname toDimname() const;
  34. private:
  35. ArrayRef<Dimname> origin_;
  36. Dimname name_;
  37. int origin_idx_; // A named tensor can have at most 64 dims.
  38. TORCH_API friend std::ostream& operator<<(
  39. std::ostream& out,
  40. const TensorName& tensorname);
  41. };
  42. using TensorNameVec = SmallVector<TensorName, 10>;
  43. struct TORCH_API TensorNames {
  44. explicit TensorNames(ArrayRef<Dimname> names);
  45. // Create TensorNames from names[start:end]. Each individual TensorName stores
  46. // `names`, NOT names[start:end], because the original tensor's names are
  47. // `names`.
  48. explicit TensorNames(ArrayRef<Dimname> names, int64_t start, int64_t end);
  49. // op_name is only used for error reporting.
  50. TensorNames& unifyFromRightInplace(
  51. const TensorNames& other,
  52. const char* op_name = "unify");
  53. void checkUnique(const char* op_name) const;
  54. void append(TensorName&& name);
  55. std::vector<Dimname> toDimnameVec() const;
  56. private:
  57. explicit TensorNames(TensorNameVec&& names) : names_(names){};
  58. TensorNameVec names_;
  59. };
  60. } // namespace namedinference
  61. } // namespace at