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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
#include <gtest/gtest.h>
#include "caffe2/core/net.h"
#include "caffe2/core/operator.h"
#include "caffe2/transforms/pattern_net_transform.h"
namespace caffe2 {
namespace {
using transform::Graph;
static std::atomic<int> counter;
class DummyCounterOp final : public OperatorBase {
public:
using OperatorBase::OperatorBase;
bool Run(int /* unused */) override {
counter.fetch_add(1);
return true;
}
};
REGISTER_CPU_OPERATOR(DummyCounterOp1, DummyCounterOp);
REGISTER_CUDA_OPERATOR(DummyCounterOp1, DummyCounterOp);
OPERATOR_SCHEMA(DummyCounterOp1)
.NumInputs(0, INT_MAX)
.NumOutputs(0, INT_MAX)
.AllowInplace({{0, 0}, {1, 1}});
REGISTER_CPU_OPERATOR(DummyCounterOp2, DummyCounterOp);
REGISTER_CUDA_OPERATOR(DummyCounterOp2, DummyCounterOp);
OPERATOR_SCHEMA(DummyCounterOp2)
.NumInputs(0, INT_MAX)
.NumOutputs(0, INT_MAX)
.AllowInplace({{0, 0}, {1, 1}});
REGISTER_CPU_OPERATOR(DummyCounterOp3, DummyCounterOp);
REGISTER_CUDA_OPERATOR(DummyCounterOp3, DummyCounterOp);
OPERATOR_SCHEMA(DummyCounterOp3)
.NumInputs(0, INT_MAX)
.NumOutputs(0, INT_MAX)
.AllowInplace({{0, 0}, {1, 1}});
/**
* P = ---> (Op1) ---> (Op2) --->
*
* R = ---> (Op3) ---> (Op3) --->
*/
TEST(PatternNetTransformTest, TestGenerateTransform) {
Workspace ws;
ws.CreateBlob("in");
NetDef netdef;
AddOp(&netdef, "DummyCounterOp1", {"in"}, {"mid1"});
AddOp(&netdef, "DummyCounterOp2", {"mid1"}, {"mid2"});
AddOp(&netdef, "DummyCounterOp1", {"mid2"}, {"mid3"});
AddOp(&netdef, "DummyCounterOp2", {"mid3"}, {"out"});
NetDef pdef;
AddOp(&pdef, "DummyCounterOp1", {"in"}, {"mid"});
AddOp(&pdef, "DummyCounterOp2", {"mid"}, {"out"});
NetDef rdef;
AddOp(&rdef, "DummyCounterOp3", {"in"}, {"new_mid"});
AddOp(&rdef, "DummyCounterOp3", {"new_mid"}, {"out"});
PatternNetTransform t(pdef, rdef);
// test pattern match
Graph g(netdef);
auto matches = t.PatternMatch(g);
EXPECT_EQ(matches.size(), 2);
t.ReplacePattern(matches, &g);
EXPECT_EQ(g.size(), 8);
for (int i = 0; i < 4; i++) {
EXPECT_FALSE(g.is_node_active(i));
}
for (int i = 4; i < 8; i++) {
EXPECT_TRUE(g.is_node_active(i));
}
EXPECT_TRUE(g.node(4).children.count(5));
EXPECT_TRUE(g.node(5).children.count(6));
EXPECT_TRUE(g.node(6).children.count(7));
for (int i = 4; i < 8; i++) {
EXPECT_EQ(g.node(i).op.input().size(), 1);
EXPECT_EQ(g.node(i).op.output().size(), 1);
}
NetDef replaced_netdef = g.GetNetDef();
EXPECT_EQ(replaced_netdef.op().size(), 4);
EXPECT_EQ(replaced_netdef.op(0).type(), "DummyCounterOp3");
EXPECT_EQ(replaced_netdef.op(1).type(), "DummyCounterOp3");
EXPECT_EQ(replaced_netdef.op(2).type(), "DummyCounterOp3");
EXPECT_EQ(replaced_netdef.op(3).type(), "DummyCounterOp3");
}
/**
* P = ---> (Op1) ---> (Op2) --->
*
* R = ---> (Op3) ---> (Op3) --->
*/
TEST(PatternNetTransformTest, TestRepeatedTransform) {
Workspace ws;
ws.CreateBlob("in");
NetDef netdef;
AddOp(&netdef, "DummyCounterOp1", {"in"}, {"out"});
AddOp(&netdef, "DummyCounterOp2", {"out"}, {"out"});
for (int i = 0; i < 99; i++) {
AddOp(&netdef, "DummyCounterOp1", {"out"}, {"out"});
AddOp(&netdef, "DummyCounterOp2", {"out"}, {"out"});
}
NetDef pdef;
AddOp(&pdef, "DummyCounterOp1", {"in"}, {"mid"});
AddOp(&pdef, "DummyCounterOp2", {"mid"}, {"out"});
NetDef rdef;
AddOp(&rdef, "DummyCounterOp3", {"in"}, {"new_mid"});
AddOp(&rdef, "DummyCounterOp3", {"new_mid"}, {"out"});
PatternNetTransform t(pdef, rdef);
// test pattern match
Graph g(netdef);
auto matches = t.PatternMatch(g);
EXPECT_EQ(matches.size(), 100);
t.ReplacePattern(matches, &g);
NetDef replaced_netdef = g.GetNetDef();
EXPECT_EQ(replaced_netdef.op_size(), 200);
for (int i = 0; i < 200; i++) {
EXPECT_EQ(replaced_netdef.op(i).type(), "DummyCounterOp3");
}
unique_ptr<NetBase> net = CreateNet(replaced_netdef, &ws);
counter.exchange(0);
net.get()->Run();
EXPECT_EQ(200, counter.load());
}
/**
* P = ---> (Op1) ---> (Op3) ---> (Op2) --->
* |------> (Op3) -------|
*
* R = ---> (Op1) --------------> (Op3) --->
* |_(Op3)-->(Op3)-->(Op2)_|
*
*/
TEST(PatternNetTransformTest, TestHardTransform) {
Workspace ws;
ws.CreateBlob("in");
NetDef netdef;
// Segment 1 (differs from P because of type)
AddOp(&netdef, "DummyCounterOp1", {"in"}, {"mid1a_1", "mid1b_1"});
AddOp(&netdef, "DummyCounterOp2", {"mid1a_1"}, {"mid2a_1"});
AddOp(&netdef, "DummyCounterOp3", {"mid1b_1"}, {"mid2b_1"});
AddOp(&netdef, "DummyCounterOp3", {"mid2a_1", "mid2b_1"}, {"out_1"});
// Segment 2 (differs from P because of structure)
AddOp(
&netdef, "DummyCounterOp1", {"out_1"}, {"mid1a_2", "mid1b_2", "mid1c_2"});
AddOp(&netdef, "DummyCounterOp3", {"mid1a_2"}, {"mid2a_2"});
AddOp(&netdef, "DummyCounterOp3", {"mid1b_2"}, {"mid2b_2"});
AddOp(&netdef, "DummyCounterOp3", {"mid1c_2"}, {"mid2c_2"});
AddOp(
&netdef, "DummyCounterOp2", {"mid2a_2", "mid2b_2", "mid2c_2"}, {"out_2"});
// Segment 3
AddOp(&netdef, "DummyCounterOp1", {"out_2"}, {"mid1a_3", "mid1b_3"});
AddOp(&netdef, "DummyCounterOp3", {"mid1a_3"}, {"mid2a_3"});
AddOp(&netdef, "DummyCounterOp3", {"mid1b_3"}, {"mid2b_3"});
AddOp(&netdef, "DummyCounterOp2", {"mid2a_3", "mid2b_3"}, {"out"});
NetDef pdef;
// Should only match Segment 3
AddOp(&pdef, "DummyCounterOp1", {"sub_in"}, {"mid1a", "mid1b"});
AddOp(&pdef, "DummyCounterOp3", {"mid1a"}, {"mid2a"});
AddOp(&pdef, "DummyCounterOp3", {"mid1b"}, {"mid2b"});
AddOp(&pdef, "DummyCounterOp2", {"mid2a", "mid2b"}, {"sub_out"});
NetDef rdef;
AddOp(&rdef, "DummyCounterOp1", {"sub_in"}, {"mid1a", "mid1b"});
AddOp(&rdef, "DummyCounterOp3", {"mid1b"}, {"mid2b"});
AddOp(&rdef, "DummyCounterOp3", {"mid2b"}, {"mid3b"});
AddOp(&rdef, "DummyCounterOp2", {"mid3b"}, {"mid4b"});
AddOp(&rdef, "DummyCounterOp3", {"mid1a", "mid4b"}, {"sub_out"});
PatternNetTransform t(pdef, rdef);
Graph g(netdef);
EXPECT_EQ(g.size(), 13);
auto matches = t.PatternMatch(g);
EXPECT_EQ(matches.size(), 1);
t.ReplacePattern(matches, &g);
EXPECT_EQ(g.size(), 18);
NetDef replaced_netdef = g.GetNetDef();
EXPECT_EQ(replaced_netdef.op_size(), 14);
unique_ptr<NetBase> net = CreateNet(replaced_netdef, &ws);
counter.exchange(0);
net.get()->Run();
EXPECT_EQ(14, counter.load());
}
TEST(PatternNetTransformTest, TestGeneralStringMatching) {
Workspace ws;
ws.CreateBlob("in");
NetDef pdef;
AddOp(&pdef, "*", {"in"}, {"mid"});
AddOp(&pdef, "DummyOp1|DummyOp2", {"mid"}, {"mid2"});
AddOp(&pdef, "DummyOp3", {"mid2"}, {"out"});
NetDef rdef;
AddOp(&rdef, "DummyOp1", {"in"}, {"out"});
NetDef netdef;
AddOp(&netdef, "DummyOp1", {"in"}, {"mid"});
AddOp(&netdef, "DummyOp3", {"mid"}, {"mid"}); // start of match 1
AddOp(&netdef, "DummyOp2", {"mid"}, {"mid"});
AddOp(&netdef, "DummyOp3", {"mid"}, {"mid"}); // end of match 1
AddOp(&netdef, "DummyOp1", {"mid"}, {"mid"}); // start of match 2
AddOp(&netdef, "DummyOp1", {"mid"}, {"mid"});
AddOp(&netdef, "DummyOp3", {"mid"}, {"mid"}); // end of match 2
AddOp(&netdef, "DummyOp3", {"mid"}, {"out"});
PatternNetTransform t(pdef, rdef);
transform::Graph g(netdef);
auto matches = t.PatternMatch(g);
EXPECT_EQ(matches.size(), 2);
}
TEST(PatternNetTransformTest, TestDeviceOptionMatching) {
Workspace ws;
ws.CreateBlob("in");
NetDef pdef;
auto op = AddOp(&pdef, "DummyOp1", {"in"}, {"out"});
op->mutable_device_option()->set_device_type(PROTO_CPU);
NetDef rdef;
op = AddOp(&rdef, "DummyOp1", {"in"}, {"out"});
op->mutable_device_option()->set_device_type(PROTO_CUDA);
NetDef netdef;
op = AddOp(&netdef, "DummyOp1", {"in"}, {"mid"});
op->mutable_device_option()->set_device_type(PROTO_CPU);
op = AddOp(&netdef, "DummyOp1", {"mid"}, {"mid"}); // should not match
op->mutable_device_option()->set_device_type(PROTO_CUDA);
op = AddOp(&netdef, "DummyOp1", {"mid"}, {"out"});
op->mutable_device_option()->set_device_type(PROTO_CPU);
PatternNetTransform t(pdef, rdef);
transform::Graph g(netdef);
auto matches = t.PatternMatch(g);
EXPECT_EQ(matches.size(), 2);
NetDef transformed_net = t.ApplyTo(netdef);
for (const auto& opdef : transformed_net.op()) {
EXPECT_TRUE(opdef.has_device_option());
EXPECT_EQ(opdef.device_option().device_type(), PROTO_CUDA);
}
}
TEST(PatternNetTransformTest, TestEngineMatching) {
Workspace ws;
ws.CreateBlob("in");
NetDef pdef;
auto op = AddOp(&pdef, "DummyOp1", {"in"}, {"out"});
op->set_engine("FakeEng1|FakeEng2");
NetDef rdef;
op = AddOp(&rdef, "DummyOp1", {"in"}, {"out"});
op->set_engine("FakeEng3");
NetDef netdef;
op = AddOp(&netdef, "DummyOp1", {"in"}, {"mid"});
op->set_engine("FakeEng1");
op = AddOp(&netdef, "DummyOp1", {"mid"}, {"mid"});
op->set_engine("FakeEng2");
op = AddOp(&netdef, "DummyOp1", {"mid"}, {"out"}); // should not match
op->set_engine("FakeEng3");
PatternNetTransform t(pdef, rdef);
transform::Graph g(netdef);
auto matches = t.PatternMatch(g);
EXPECT_EQ(matches.size(), 2);
NetDef transformed_net = t.ApplyTo(netdef);
for (const auto& opdef : transformed_net.op()) {
EXPECT_EQ(opdef.engine(), "FakeEng3");
}
}
TEST(PatternNetTransformTest, TestSingularArgumentMatching) {
Workspace ws;
ws.CreateBlob("in");
NetDef pdef;
auto op = AddOp(&pdef, "Conv", {"in"}, {"out"});
{
auto arg = op->add_arg();
arg->set_name("stride_w");
arg->set_i(3);
}
{
auto arg = op->add_arg();
arg->set_name("stride_h");
arg->set_i(3);
}
NetDef rdef;
op = AddOp(&rdef, "Conv", {"in"}, {"out"});
{
auto arg = op->add_arg();
arg->set_name("stride_w");
arg->set_i(5);
}
{
auto arg = op->add_arg();
arg->set_name("stride_h");
arg->set_i(5);
}
NetDef netdef;
op = AddOp(&netdef, "Conv", {"in"}, {"mid"}); // Will match
{
auto arg = op->add_arg();
arg->set_name("stride_w");
arg->set_i(3);
}
{
auto arg = op->add_arg();
arg->set_name("stride_h");
arg->set_i(3);
}
op = AddOp(&netdef, "Conv", {"mid"}, {"mid"}); // Has bad args, will not match
{
auto arg = op->add_arg();
arg->set_name("stride_w");
arg->set_i(4);
}
{
auto arg = op->add_arg();
arg->set_name("stride_h");
arg->set_i(4);
}
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
op = AddOp(&netdef, "Conv", {"mid"}, {"mid"}); // Has no args, will not match
op = AddOp(&netdef, "Conv", {"mid"}, {"out"}); // Has different names
{
auto arg = op->add_arg();
arg->set_name("yolo");
arg->set_i(3);
}
{
auto arg = op->add_arg();
arg->set_name("swag");
arg->set_i(3);
}
op = AddOp(&netdef, "Conv", {"in"}, {"mid"}); // Will match
{
auto arg = op->add_arg();
arg->set_name("stride_w");
arg->set_i(3);
}
{
auto arg = op->add_arg();
arg->set_name("stride_h");
arg->set_i(3);
}
PatternNetTransform t(pdef, rdef);
t.EnableArgumentMatching();
transform::Graph g(netdef);
auto matches = t.PatternMatch(g);
EXPECT_EQ(matches.size(), 2);
NetDef transformed_net = t.ApplyTo(netdef);
EXPECT_EQ(transformed_net.op(0).arg(0).name(), "stride_w");
EXPECT_EQ(transformed_net.op(0).arg(0).i(), 5);
EXPECT_EQ(transformed_net.op(0).arg(1).name(), "stride_h");
EXPECT_EQ(transformed_net.op(0).arg(1).i(), 5);
EXPECT_EQ(transformed_net.op(4).arg(0).name(), "stride_w");
EXPECT_EQ(transformed_net.op(4).arg(0).i(), 5);
EXPECT_EQ(transformed_net.op(4).arg(1).name(), "stride_h");
EXPECT_EQ(transformed_net.op(4).arg(1).i(), 5);
}
/**
* |--(Op2)--|
* P = --->(Op1)----->(Op3)--->
* |--(Op2)--|
*
* R = ---> (Op2) --->
*
* |--(Op2)--|
* -->(Op1)----->(Op3)---
* | |--(Op2)--| |
* G = ---> (Op1) (Op3) --->
* | |--(Op2)--| |
* -->(Op1)----->(Op3)--
* |--(Op2)--|
*
* In this test, the two "parallel" modules have intersecting execution orders.
* We wish to test that the pattern match can still detect the two modules,
* separately.
*
* Furthermore, we will apply the transform to G, TWICE.
* It should reduce G to a single operator.
*/
TEST(PatternNetTransformTest, TestNonStrictTopographicTransform) {
Workspace ws;
ws.CreateBlob("in");
NetDef netdef;
// Head
AddOp(&netdef, "DummyCounterOp1", {"in"}, {"in_1", "in_2"});
// 2 intertwined segments, each matching P. No strict ordering.
AddOp(&netdef, "DummyCounterOp1", {"in_1"}, {"m1_1", "m2_1"});
AddOp(&netdef, "DummyCounterOp1", {"in_2"}, {"m1_2", "m2_2"});
AddOp(&netdef, "DummyCounterOp2", {"m1_1"}, {"out1_1"});
AddOp(&netdef, "DummyCounterOp2", {"m1_2"}, {"out1_2"});
AddOp(&netdef, "DummyCounterOp2", {"m2_1"}, {"out2_1"});
AddOp(&netdef, "DummyCounterOp2", {"m2_2"}, {"out2_2"});
AddOp(&netdef, "DummyCounterOp3", {"out1_1", "out2_1"}, {"out1"});
AddOp(&netdef, "DummyCounterOp3", {"out1_2", "out2_2"}, {"out2"});
// Tail
AddOp(&netdef, "DummyCounterOp3", {"out1", "out2"}, {"out"});
NetDef pdef;
AddOp(&pdef, "DummyCounterOp1", {"myin"}, {"mid1a", "mid1b"});
AddOp(&pdef, "DummyCounterOp2", {"mid1a"}, {"mid2a"});
AddOp(&pdef, "DummyCounterOp2", {"mid1b"}, {"mid2b"});
AddOp(&pdef, "DummyCounterOp3", {"mid2a", "mid2b"}, {"myout"});
NetDef rdef;
AddOp(&rdef, "DummyCounterOp2", {"myin"}, {"myout"});
PatternNetTransform t(pdef, rdef);
NetDef replaced_netdef = t.ApplyTo(netdef);
EXPECT_EQ(replaced_netdef.op_size(), 4);
unique_ptr<NetBase> net = CreateNet(replaced_netdef, &ws);
counter.exchange(0);
net.get()->Run();
EXPECT_EQ(4, counter.load());
// apply the transform again
// the entire net should get transformed this time
NetDef double_transformed_net = t.ApplyTo(replaced_netdef);
EXPECT_EQ(double_transformed_net.op_size(), 1);
}
/**
* --->(Op1)----->(Op2)--->
* | ^
* P = |----------|
* | v
* --->(Op1)----->(Op2)--->
*
* R = ---> (Op3) --->
*
* G = P -> P
*
* In this test, we fuse a subgraph with two inputs and two outputs, into one
* operator.
*
* This will ensure that we can allow a single edge to represent
* multiple blob names (the input and output of R are both 2 blobs).
*
* This will also ensure that patternmatch can traverse "backwards", from a node
* to its parent.
*
* Furthermore, this tests for repeat matches, since matching on either of the
* first two Op1 nodes will produce a match, but they are identical.
* So, the pattern should match 4 times, but only be replaced twice.
*/
TEST(PatternNetTransformTest, TestMultiInputOutputTransform) {
Workspace ws;
ws.CreateBlob("in1");
ws.CreateBlob("in2");
NetDef netdef;
AddOp(&netdef, "DummyCounterOp1", {"in1"}, {"in1"}); // has 2 children
AddOp(&netdef, "DummyCounterOp1", {"in2"}, {"in2"}); // has 2 children
AddOp(&netdef, "DummyCounterOp2", {"in1", "in2"}, {"mid1"});
AddOp(&netdef, "DummyCounterOp2", {"in1", "in2"}, {"mid2"});
AddOp(&netdef, "DummyCounterOp1", {"mid1"}, {"mid1"}); // has 2 children
AddOp(&netdef, "DummyCounterOp1", {"mid2"}, {"mid2"}); // has 2 children
AddOp(&netdef, "DummyCounterOp2", {"mid1", "mid2"}, {"out1"});
AddOp(&netdef, "DummyCounterOp2", {"mid1", "mid2"}, {"out2"});
NetDef pdef;
AddOp(&pdef, "DummyCounterOp1", {"subin1"}, {"subin1"}); // has 2 children
AddOp(&pdef, "DummyCounterOp1", {"subin2"}, {"subin2"}); // has 2 children
AddOp(&pdef, "DummyCounterOp2", {"subin1", "subin2"}, {"subout1"});
AddOp(&pdef, "DummyCounterOp2", {"subin1", "subin2"}, {"subout2"});
NetDef rdef;
AddOp(&rdef, "DummyCounterOp3", {"subin1", "subin2"}, {"subout1", "subout2"});
PatternNetTransform t(pdef, rdef);
Graph g(netdef);
NetDef replaced_netdef = t.ApplyTo(netdef);
EXPECT_EQ(replaced_netdef.op_size(), 2);
unique_ptr<NetBase> net = CreateNet(replaced_netdef, &ws);
counter.exchange(0);
net.get()->Run();
EXPECT_EQ(2, counter.load());
}
} // namespace
} // namespace caffe2
|