File: graphmatcher.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (281 lines) | stat: -rw-r--r-- 8,470 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "graphmatcher.h"
#include "ast.h"
#include "nomnigraph/Graph/Algorithms.h"

#include <mutex>

static std::mutex mtx_;

namespace nom {
namespace nql {
using namespace nom::repr;

NNGraph::NodeRef MatchedSubgraph::operator[](const std::string& key) const {
  auto search = matchMap.find(key);
  CAFFE_ENFORCE(
      search != matchMap.end(), "Could not find key in map of matches:", key);
  return search->second;
}

TestMatchGraph::NodeRef GraphMatcher::genMatcherFromASTExpr(
    ASTExpr* expr,
    bool insertTemp = false) {
  if (!expr->isCall()) {
    if (expr->starInputs()) {
      return matchGraph_.createNode(std::move(
          testMatchPredicate(Criteria("*")).starCount().nonTerminal()));
    }
    if (!varMap_.count(expr->name)) {
      varMap_[expr->name] = matchGraph_.createNode(
          std::move(testMatchPredicate(Criteria("*")).nonTerminal()));
    }
    return varMap_[expr->name];
  }

  std::vector<TestMatchGraph::NodeRef> children;
  for (auto child : expr->children) {
    children.push_back(genMatcherFromASTExpr(child, true));
  }

  auto res = matchGraph_.createNode(testMatchPredicate(Criteria(expr->name)));
  callMap_[expr->name] = res;
  for (auto child : children) {
    matchGraph_.createEdge(child, res);
  }

  if (insertTemp) {
    auto temp = matchGraph_.createNode(testMatchPredicate(Criteria("*")));
    matchGraph_.createEdge(res, temp);
    res = temp;
  }

  return res;
}

TestMatchGraph::NodeRef GraphMatcher::genMatcherFromASTStmt(ASTStmt* stmt) {
  auto right = genMatcherFromASTExpr(stmt->rhs);
  auto res = right;
  /* For cases like
   %x, %y = Foo(%z)
   for now we just say that both %x and %y are defined by node Foo, we don't
   distinguish them (i.e. we don't keep any information about their order. */
  for (auto v : stmt->lhs) {
    res = matchGraph_.createNode(testMatchPredicate(Criteria("*")));
    matchGraph_.createEdge(right, res);
    varMap_[v] = res;
  }
  return res;
}

void deallocTokenStrings() {
  for (auto p : tokens) {
    delete (std::string*)p;
  }
  tokens.clear();

  for (auto p : tokenVectors) {
    delete (std::vector<void*>*)p;
  }
  tokenVectors.clear();
}

TestMatchGraph::NodeRef GraphMatcher::genMatcherFromASTGraph(ASTGraph* ast) {
  matchGraph_ = TestMatchGraph();
  // TODO: Cleanup this.
  TestMatchGraph::NodeRef last = nullptr;
  if (ast->stmts.empty()) {
    syntaxIsValid_ = false; // Temporary solution, which works because we don't
                            // allow empty graphs.
  }

  for (auto stmt : ast->stmts) {
    auto r = genMatcherFromASTStmt(stmt);
    if (r) {
      last = r;
    }
  }

  return last;
}

TestMatchGraph::NodeRef GraphMatcher::genMatcherFromIRFile(const char* fname) {
  std::lock_guard<std::mutex> lock(mtx_);
  ASTGraph g;
  parseFile(fname, &g);
  matchGraphRootNode_ = genMatcherFromASTGraph(&g);
  deallocTokenStrings();
  return matchGraphRootNode_;
}

TestMatchGraph::NodeRef GraphMatcher::genMatcherFromIRStr(const char* str) {
  std::lock_guard<std::mutex> lock(mtx_);
  ASTGraph g;
  parseString(str, &g);
  matchGraphRootNode_ = genMatcherFromASTGraph(&g);
  deallocTokenStrings();
  return matchGraphRootNode_;
}

TestMatchPredicate testMatchPredicate(const Criteria& criteria) {
  auto predicate =
      TestMatchPredicate([criteria](nom::repr::NNGraph::NodeRef nodeRef) {
        std::string nodeLabel = getNodeName(nodeRef);
        return (criteria == "*" || criteria == nodeLabel);
      });
  predicate.setDebugString(criteria);
  return predicate;
}

// Helper function for convertToNQLString function.
// Given a node and a renameMap return the unique name for this node.
static std::string getNameForBlob(
    NNGraph::NodeRef node,
    const std::unordered_map<NNGraph::NodeRef, std::string>& renameMap) {
  if (renameMap.count(node)) {
    return renameMap.at(node);
  }
  return getNodeName(node);
}

// Helper function for convertToNQLString function.
// Given a node and a renameMap return a string representing the node, which
// looks something like:
//   %a = Op(%b, %c, %d)
static const std::string getNQLStringForBlob(
    NNGraph::NodeRef node,
    const std::unordered_map<NNGraph::NodeRef, std::string>& renameMap) {
  if (!nn::is<Data>(node) || !nn::hasProducer(node)) {
    return "";
  }
  NNGraph::NodeRef defOp = nn::getProducer(node);

  std::string result =
      getNameForBlob(node, renameMap) + " = " + getNodeName(defOp) + "(";
  int i = 0;
  for (auto inputTensor : nn::getInputs(defOp)) {
    if (i) {
      result += ", ";
    }
    result += getNameForBlob(inputTensor, renameMap);
    i++;
  }
  result += ")";
  return result;
}

// Helper function for convertToNQLString function.
// It takes a list of nodes and returns a map node->unique_name. The new names
// are based on the existing ones, but are also unique.
static std::unordered_map<NNGraph::NodeRef, std::string> computeDedupRenameMap(
    const std::vector<NNGraph::NodeRef>& nodes) {
  std::unordered_map<NNGraph::NodeRef, std::string> renameMap;
  std::unordered_set<std::string> takenNames;
  takenNames.clear();
  for (auto node : nodes) {
    std::string name = getNodeName(node);
    if (!isa<Data>(node->data())) {
      continue;
    }
    std::string newName = name;
    int dedupCounter = 0;
    while (takenNames.count(newName)) {
      newName = name + "_" + caffe2::to_string(dedupCounter);
      dedupCounter++;
    }
    renameMap[node] = newName;
    takenNames.insert(newName);
  }
  return renameMap;
}

std::vector<MatchedSubgraph> GraphMatcher::getMatches(
    nom::repr::NNGraph& df) const {
  std::vector<MatchedSubgraph> matches;
  if (!syntaxIsValid_) {
    return matches;
  }
  // Attempt to match at each node
  for (const auto& node : df.getMutableNodes()) {
    auto match = matchGraph_.isSubgraphMatch(node, matchGraphRootNode_, true);
    if (match.isMatch()) {
      MatchedSubgraph ms;
      ms.subgraph = *match.getMatchedSubgraph();
      // This is a map from the the internal TestMatchGraph to the nodes in the
      // NNGraph
      auto match_graph_map = match.getMatchNodeMap();
      // We iterate through the "varMap_" map (string ->
      // TestMatchGraph::NodeRef) to generate string -> NNGraph::NodeRef
      for (auto p : varMap_) {
        auto iter = match_graph_map->find(p.second);
        if (iter != match_graph_map->end()) {
          ms.matchMap[p.first] = iter->second;
        }
      }
      for (auto p : callMap_) {
        auto iter = match_graph_map->find(p.second);
        if (iter != match_graph_map->end()) {
          ms.matchMap[p.first] = iter->second;
        }
      }
      matches.emplace_back(ms);
    }
  }
  return matches;
}

// \brief Return a short string name for the given \param node.
// The function works with both tensors and operators.
std::string getNodeName(const NNGraph::NodeRef node) {
  if (!node) {
    return "";
  }
  if (nn::is<NeuralNetOperator>(node)) {
    if (auto* op = nn::get<NeuralNetOperator>(node)) {
      return op->getName();
    }
  }
  if (nn::is<NeuralNetData>(node)) {
    if (auto tensor = nn::get<NeuralNetData>(node)) {
      return "%" + tensor->getName();
    }
  }
  return "";
}

// \brief Return a string representing the given graph \param g.
// The returned string is a valid NQL query.
std::string convertToNQLString(NNGraph& g) {
  // Order nodes in a topological order.
  // TODO: Currently tarjans mutates the graph, and that's the only reason we
  // are not using const reference for `g`. We need to fix tarjans so that it
  // doesn't mutate the graph and use const reference in this function too.
  auto topoMatch = nom::algorithm::tarjans(&g);
  std::vector<NNGraph::NodeRef> nodes;
  int sccNum = 0;
  for (auto scc : topoMatch) {
    sccNum++;
    for (auto node : scc.getNodes()) {
      nodes.emplace_back(node);
    }
  }
  std::reverse(nodes.begin(), nodes.end());

  // Different nodes might have the same name. We want to change that so that
  // they are distinguishable by the name. NQL assumes that names are unique.
  std::unordered_map<NNGraph::NodeRef, std::string> renameMap =
      computeDedupRenameMap(nodes);

  // Going from top to bottom (nodes are in topological order), print all
  // nodes.
  std::string result = "def nn {\n";
  for (auto node : nodes) {
    std::string r = getNQLStringForBlob(node, renameMap);
    if (!r.empty()) {
      result += "  " + r + "\n";
    }
  }
  result += "}\n";
  return result;
}
}; // namespace nql
}; // namespace nom