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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
#include "caffe2/opt/nql/graphmatcher.h"
#include <gtest/gtest.h>
using namespace nom::nql;
using namespace nom::repr;
/// \brief Create tensor-nodes in \param graph with names specified in \param
/// names and \return a name->NodeRef map.
std::unordered_map<std::string, NNGraph::NodeRef> genTensors(
NNGraph& graph,
std::vector<std::string> names) {
std::unordered_map<std::string, NNGraph::NodeRef> result;
for (auto& name : names) {
result[name] = graph.createNode(std::make_unique<Tensor>(name));
}
return result;
}
TEST(Basic, MatchSingleNode) {
NNGraph graph;
auto reluInput = graph.createNode(std::make_unique<Tensor>("reluInput"));
auto relu = graph.createNode(std::make_unique<Relu>());
auto reluOutput = graph.createNode(std::make_unique<Tensor>("reluOutput"));
graph.createEdge(reluInput, relu);
graph.createEdge(relu, reluOutput);
GraphMatcher gm;
gm.initFromString(R"NQL(
def my_nn {
%x = Relu(%y)
})NQL");
EXPECT_TRUE(gm.findSubgraph(graph));
GraphMatcher gmMismatch;
gmMismatch.initFromString(R"NQL(
def my_nn {
%x = Foo(%y)
})NQL");
EXPECT_FALSE(gmMismatch.findSubgraph(graph));
}
TEST(Basic, SyntaxError) {
NNGraph graph;
auto reluInput = graph.createNode(std::make_unique<Tensor>("reluInput"));
auto relu = graph.createNode(std::make_unique<Relu>());
auto reluOutput = graph.createNode(std::make_unique<Tensor>("reluOutput"));
graph.createEdge(reluInput, relu);
graph.createEdge(relu, reluOutput);
GraphMatcher gm;
gm.initFromString(R"NQL(
def my_nn {
%x =
})NQL");
EXPECT_FALSE(gm.findSubgraph(graph));
}
TEST(Basic, Diamond) {
NNGraph graph;
/*
The graph we're building looks like this:
a b
\ /
Concat
/ \
c d
| |
Relu1 Relu2
| |
e f
\ /
Sum
|
x
*/
auto tensors = genTensors(graph, {"a", "b", "c", "d", "e", "f", "x"});
auto relu1 = graph.createNode(std::make_unique<Relu>());
auto relu2 = graph.createNode(std::make_unique<Relu>());
auto concat = graph.createNode(std::make_unique<Concat>());
auto sum = graph.createNode(std::make_unique<Sum>());
graph.createEdge(tensors["a"], concat);
graph.createEdge(tensors["b"], concat);
graph.createEdge(concat, tensors["c"]);
graph.createEdge(concat, tensors["d"]);
graph.createEdge(tensors["c"], relu1);
graph.createEdge(relu1, tensors["e"]);
graph.createEdge(tensors["d"], relu2);
graph.createEdge(relu2, tensors["f"]);
graph.createEdge(tensors["e"], sum);
graph.createEdge(tensors["f"], sum);
graph.createEdge(sum, tensors["x"]);
GraphMatcher gm1;
gm1.initFromString(R"NQL(
def my_nn {
%c, %d = Concat(%a, %b)
%e = Relu(%c)
%f = Relu(%d)
%x = Sum(%e, %f)
})NQL");
EXPECT_TRUE(gm1.findSubgraph(graph));
// Check that syntax with inlining works too.
GraphMatcher gm2;
gm2.initFromString(R"NQL(
def my_nn {
%c, %d = Concat(%a, %b)
%x = Sum(Relu(%c), Relu(%d))
})NQL");
EXPECT_TRUE(gm2.findSubgraph(graph));
// Check that we understand that the Relu nodes should use output from the
// same Concat node.
GraphMatcher gm3;
gm3.initFromString(R"NQL(
def my_nn {
%c = Concat(%a)
%d = Concat(%b)
%x = Sum(Relu(%c), Relu(%d))
})NQL");
EXPECT_FALSE(gm3.findSubgraph(graph));
}
TEST(Basic, BadDiamond) {
NNGraph graph;
/*
The graph we're building looks like this:
a b
| |
Concat1 Concat2
| |
c d
| |
Relu1 Relu2
| |
e f
\ /
Sum
|
x
*/
auto tensors = genTensors(graph, {"a", "b", "c", "d", "e", "f", "x"});
auto relu1 = graph.createNode(std::make_unique<Relu>());
auto relu2 = graph.createNode(std::make_unique<Relu>());
auto concat1 = graph.createNode(std::make_unique<Concat>());
auto concat2 = graph.createNode(std::make_unique<Concat>());
auto sum = graph.createNode(std::make_unique<Sum>());
graph.createEdge(tensors["a"], concat1);
graph.createEdge(tensors["b"], concat2);
graph.createEdge(concat1, tensors["c"]);
graph.createEdge(concat2, tensors["d"]);
graph.createEdge(tensors["c"], relu1);
graph.createEdge(relu1, tensors["e"]);
graph.createEdge(tensors["d"], relu2);
graph.createEdge(relu2, tensors["f"]);
graph.createEdge(tensors["e"], sum);
graph.createEdge(tensors["f"], sum);
graph.createEdge(sum, tensors["x"]);
// Check that we don't match this graph when looking for a diamond shape.
GraphMatcher gm;
gm.initFromString(R"NQL(
def my_nn {
%c, %d = Concat(%a, %b)
%x = Sum(Relu(%c), Relu(%d))
})NQL");
EXPECT_FALSE(gm.findSubgraph(graph));
EXPECT_EQ(gm.getMatchMap().size(), 0);
GraphMatcher gm2;
gm2.initFromString(R"NQL(
def my_nn {
%c = Concat(%a)
%d = Concat(%b)
%x = Sum(Relu(%c), Relu(%d))
})NQL");
EXPECT_TRUE(gm2.findSubgraph(graph));
auto matchMap = gm2.getMatchMap();
EXPECT_EQ(matchMap["%a"], tensors["a"]);
EXPECT_EQ(matchMap["%b"], tensors["b"]);
EXPECT_EQ(matchMap["%c"], tensors["c"]);
EXPECT_EQ(matchMap["%d"], tensors["d"]);
EXPECT_EQ(matchMap["%x"], tensors["x"]);
EXPECT_EQ(matchMap["Sum"], sum);
EXPECT_TRUE(
(matchMap["Concat"] == concat1) || (matchMap["Concat"] == concat2));
EXPECT_TRUE((matchMap["Relu"] == relu1) || (matchMap["Relu"] == relu2));
}
TEST(Basic, StarInputs) {
NNGraph graph;
/*
The graph we're building looks like this:
a b c d
| | | |
Relu Flatten FC Sum
| | | |
e f g h
\ | | /
\ | | /
\ | | /
Concat
|
x
*/
auto tensors =
genTensors(graph, {"a", "b", "c", "d", "e", "f", "g", "h", "x"});
auto concat = graph.createNode(std::make_unique<Concat>());
auto relu = graph.createNode(std::make_unique<Relu>());
auto flat = graph.createNode(std::make_unique<Flatten>());
auto fc = graph.createNode(std::make_unique<FC>());
auto sum = graph.createNode(std::make_unique<Sum>());
graph.createEdge(tensors["a"], relu);
graph.createEdge(relu, tensors["e"]);
graph.createEdge(tensors["b"], flat);
graph.createEdge(flat, tensors["f"]);
graph.createEdge(tensors["c"], fc);
graph.createEdge(fc, tensors["g"]);
graph.createEdge(tensors["d"], sum);
graph.createEdge(sum, tensors["h"]);
graph.createEdge(tensors["e"], concat);
graph.createEdge(tensors["f"], concat);
graph.createEdge(tensors["g"], concat);
graph.createEdge(tensors["h"], concat);
graph.createEdge(concat, tensors["x"]);
GraphMatcher gm1;
gm1.initFromString(R"NQL(
def my_nn {
%e = Relu(%a)
%f = Flatten(%b)
%g = FC(%c)
%h = Sum(%d)
%x = Concat(%e, %f, %g, %h)
})NQL");
EXPECT_TRUE(gm1.findSubgraph(graph));
EXPECT_EQ(gm1.getMatchMap()["Concat"], concat);
EXPECT_EQ(gm1.getMatchMap()["Relu"], relu);
EXPECT_EQ(gm1.getMatchMap()["Flatten"], flat);
EXPECT_EQ(gm1.getMatchMap()["FC"], fc);
EXPECT_EQ(gm1.getMatchMap()["Sum"], sum);
EXPECT_EQ(gm1.getMatchMap()["%e"], tensors["e"]);
EXPECT_EQ(gm1.getMatchMap()["%f"], tensors["f"]);
EXPECT_EQ(gm1.getMatchMap()["%g"], tensors["g"]);
EXPECT_EQ(gm1.getMatchMap()["%h"], tensors["h"]);
GraphMatcher gm2;
gm2.initFromString(R"NQL(
def my_nn {
%x = Concat(*)
})NQL");
EXPECT_TRUE(gm2.findSubgraph(graph));
EXPECT_EQ(gm2.getMatchMap()["Concat"], concat);
GraphMatcher gm3;
gm3.initFromString(R"NQL(
def my_nn {
%e = Relu(%a)
%x = Concat(%e, *)
})NQL");
EXPECT_TRUE(gm3.findSubgraph(graph));
EXPECT_EQ(gm3.getMatchMap()["Concat"], concat);
EXPECT_EQ(gm3.getMatchMap()["Relu"], relu);
EXPECT_EQ(gm3.getMatchMap()["%e"], tensors["e"]);
GraphMatcher gm4;
gm4.initFromString(R"NQL(
def my_nn {
%x = Concat(Sum(%a), *)
})NQL");
EXPECT_FALSE(gm4.findSubgraph(graph));
GraphMatcher gm5;
gm5.initFromString(R"NQL(
def my_nn {
%x = Concat(*, Sum(%d))
})NQL");
// '*' greedily matches all inputs, and then we fail to match an extra Sum
// input.
EXPECT_FALSE(gm5.findSubgraph(graph));
}
TEST(Basic, StarOutputs) {
NNGraph graph;
/*
The graph we're building looks like this:
a b c
\ | /
Concat
/ | \
d e f
*/
auto tensors = genTensors(graph, {"a", "b", "c", "d", "e", "f"});
auto concat = graph.createNode(std::make_unique<Concat>());
graph.createEdge(tensors["a"], concat);
graph.createEdge(tensors["b"], concat);
graph.createEdge(tensors["c"], concat);
graph.createEdge(concat, tensors["d"]);
graph.createEdge(concat, tensors["e"]);
graph.createEdge(concat, tensors["f"]);
GraphMatcher gm1;
gm1.initFromString(R"NQL(
def my_nn {
%a, %b, %c = Concat(%d, %e, %f)
})NQL");
EXPECT_TRUE(gm1.findSubgraph(graph));
EXPECT_EQ(gm1.getMatchMap()["Concat"], concat);
GraphMatcher gm2;
gm2.initFromString(R"NQL(
def my_nn {
* = Concat(*)
})NQL");
EXPECT_TRUE(gm2.findSubgraph(graph));
EXPECT_EQ(gm2.getMatchMap()["Concat"], concat);
GraphMatcher gm3;
gm3.initFromString(R"NQL(
def my_nn {
%a, * = Concat(*)
})NQL");
EXPECT_TRUE(gm3.findSubgraph(graph));
EXPECT_EQ(gm3.getMatchMap()["Concat"], concat);
GraphMatcher gm4;
gm4.initFromString(R"NQL(
def my_nn {
%a, %b, * = Concat(%d, %e, *)
})NQL");
EXPECT_TRUE(gm4.findSubgraph(graph));
EXPECT_EQ(gm4.getMatchMap()["Concat"], concat);
GraphMatcher gm5;
gm5.initFromString(R"NQL(
def my_nn {
%a = Concat(%d, %e, *)
})NQL");
// We ignore mismatches in outputs
EXPECT_TRUE(gm5.findSubgraph(graph));
EXPECT_EQ(gm5.getMatchMap()["Concat"], concat);
GraphMatcher gm6;
gm6.initFromString(R"NQL(
def my_nn {
%a, %b, %c, %x = Concat(%d, %e, %f)
})NQL");
// We ignore mismatches in outputs
EXPECT_TRUE(gm6.findSubgraph(graph));
EXPECT_EQ(gm6.getMatchMap()["Concat"], concat);
GraphMatcher gm7;
gm7.initFromString(R"NQL(
def my_nn {
%a, %b, %c = Concat(%d, %e)
})NQL");
// We don't ignore mismatches in inputs
EXPECT_FALSE(gm7.findSubgraph(graph));
}
TEST(Caffe2ToNQL, Basic) {
NNGraph graph;
/*
The graph we're building looks like this:
a
|
Concat
|
b
|
Relu
|
c
*/
auto tensors = genTensors(graph, {"a", "b", "c"});
auto relu = graph.createNode(std::make_unique<Relu>());
auto concat = graph.createNode(std::make_unique<Concat>());
graph.createEdge(tensors["a"], concat);
graph.createEdge(concat, tensors["b"]);
graph.createEdge(tensors["b"], relu);
graph.createEdge(relu, tensors["c"]);
EXPECT_EQ(convertToNQLString(graph), R"NQL(def nn {
%b = Concat(%a)
%c = Relu(%b)
}
)NQL");
}
TEST(Caffe2ToNQL, TensorsNameDeduplication) {
NNGraph graph;
/*
The graph we're building looks like this:
a
|
Concat
|
b
|
Relu
|
c
*/
std::unordered_map<std::string, NNGraph::NodeRef> tensors;
// Manually create tensors with the same names. NQL will have to disambiguate
// the names by adding a suffix.
tensors["a"] = graph.createNode(std::make_unique<Tensor>("tensor"));
tensors["b"] = graph.createNode(std::make_unique<Tensor>("tensor"));
tensors["c"] = graph.createNode(std::make_unique<Tensor>("tensor"));
auto relu = graph.createNode(std::make_unique<Relu>());
auto concat = graph.createNode(std::make_unique<Concat>());
graph.createEdge(tensors["a"], concat);
graph.createEdge(concat, tensors["b"]);
graph.createEdge(tensors["b"], relu);
graph.createEdge(relu, tensors["c"]);
EXPECT_EQ(convertToNQLString(graph), R"NQL(def nn {
%tensor_0 = Concat(%tensor)
%tensor_1 = Relu(%tensor_0)
}
)NQL");
}
|