Caffe2 - C++ API
A deep learning, cross platform ML framework
alias_info.h
1 #pragma once
2 #include <unordered_set>
3 #include <vector>
4 #include <ATen/core/interned_strings.h>
5 #include <c10/util/Exception.h>
6 
7 namespace c10 {
18 class AliasInfo {
19  public:
20  // Symbol for the set that can alias anything
21  static Symbol wildcardSet() {
22  static const Symbol wc = Symbol::fromQualString("alias::*");
23  return wc;
24  }
25  static AliasInfo createWildcard() {
26  AliasInfo ret;
27  ret.addBeforeSet(wildcardSet());
28  return ret;
29  }
30 
31  void setIsWrite(bool isWrite) {
32  isWrite_ = isWrite;
33  }
34 
35  bool isWrite() const {
36  return isWrite_;
37  }
38 
39  void addBeforeSet(Symbol aliasSet) {
40  beforeSets_.insert(aliasSet);
41  }
42 
43  void addAfterSet(Symbol aliasSet) {
44  afterSets_.insert(aliasSet);
45  }
46 
47  const std::unordered_set<Symbol>& beforeSets() const {
48  return beforeSets_;
49  }
50 
51  const std::unordered_set<Symbol>& afterSets() const {
52  return afterSets_;
53  }
54 
55  Symbol beforeSet() const {
56  AT_ASSERT(beforeSets_.size() == 1);
57  return *beforeSets_.begin();
58  }
59 
60  bool isWildcard() const {
61  return beforeSets_.count(wildcardSet()) != 0;
62  }
63 
64  // the alias info for the contained types of the type
65  // e.g. if this is an annotation on List[T], `sets` refers to
66  // the alias sets that the list may be in
67  // while containedTypes()[0] refers to the sets that members of the list
68  // may be in
69  void addContainedType(AliasInfo aliasInfo) {
70  containedTypes_.push_back(std::move(aliasInfo));
71  }
72  const std::vector<AliasInfo>& containedTypes() const {
73  return containedTypes_;
74  }
75 
76  private:
77  std::unordered_set<Symbol> beforeSets_;
78  std::unordered_set<Symbol> afterSets_;
79  std::vector<AliasInfo> containedTypes_;
80  bool isWrite_ = false;
81 };
82 
83 // DEBUG ONLY; this does not match the way things are represented in the schema
84 inline std::ostream& operator<<(std::ostream& out, const AliasInfo& aliasInfo) {
85  out << "(";
86  bool first = true;
87  for (const auto& set : aliasInfo.beforeSets()) {
88  if (first) {
89  first = false;
90  } else {
91  out << "|";
92  }
93  out << set.toUnqualString();
94  }
95  out << ")";
96 
97  if (!aliasInfo.containedTypes().empty()) {
98  out << " CONTAINS " << aliasInfo.containedTypes()[0];
99  }
100  return out;
101 }
102 } // namespace c10
class AliasInfo
Definition: alias_info.h:18
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
Definition: alias_info.h:7