build_cinn_pass_test.cc 20.0 KB
Newer Older
J
jiangcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/framework/paddle2cinn/build_cinn_pass.h"

#include <algorithm>
#include <memory>
19
#include <string>
J
jiangcheng 已提交
20 21 22 23 24 25 26

#include "gtest/gtest.h"

#include "paddle/fluid/framework/details/build_strategy.h"
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/ir/node.h"
#include "paddle/fluid/framework/op_desc.h"
27 28
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
29
#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h"
J
jiangcheng 已提交
30 31
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/var_desc.h"
32
#include "paddle/fluid/operators/cinn/cinn_launch_op.h"
J
jiangcheng 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

namespace paddle {
namespace framework {
namespace paddle2cinn {

using framework::ir::Graph;
using framework::ir::Node;

inline bool CheckNodeExisted(const std::unordered_set<Node*>& nodes,
                             const std::string& op_name) {
  return std::find_if(nodes.begin(), nodes.end(), [&op_name](const Node* node) {
           return node->Name() == op_name;
         }) != nodes.end();
}

inline int CountNode(const std::unordered_set<Node*>& nodes,
                     const std::string& op_name) {
  return std::count_if(
      nodes.begin(), nodes.end(),
      [&op_name](const Node* node) { return node->Name() == op_name; });
}

inline Node* GetNode(const std::unordered_set<Node*>& nodes,
                     const std::string& op_name) {
57 58 59 60
  return *std::find_if(nodes.begin(), nodes.end(),
                       [&op_name](const Node* node) {
                         return node->Name().find(op_name) != std::string::npos;
                       });
J
jiangcheng 已提交
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
inline bool CheckGraphIndependence(const std::unordered_set<Node*>& nodes) {
  auto check_node_ok = [&nodes](Node* n1, Node* n2) -> bool {
    if (n1->IsOp() && !n2->IsVar()) {
      return false;
    }
    if (n1->IsVar() && !n2->IsOp()) {
      return false;
    }
    if (nodes.count(n2) == 0) {
      return false;
    }
    return true;
  };

  for (auto node : nodes) {
    for (auto in : node->inputs) {
      if (!check_node_ok(node, in)) {
        return false;
      }
    }
    for (auto out : node->outputs) {
      if (!check_node_ok(node, out)) {
        return false;
      }
    }
  }
  return true;
}

92 93 94 95 96
// Get compilation_key values
std::vector<std::string> GetCompilationKeys(const Graph& graph) {
  std::vector<std::string> compilation_keys;
  for (auto& node : graph.Nodes()) {
    if (node->IsOp() && node->Name() == kCinnLaunchOp) {
97 98
      compilation_keys.emplace_back(BOOST_GET_CONST(
          std::string, node->Op()->GetAttr(operators::kCompilationKey)));
99 100 101 102 103
    }
  }
  return compilation_keys;
}

J
jiangcheng 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
std::unique_ptr<Graph> BuildNoCinnSubgraph() {
  ProgramDesc prog;
  auto g = std::make_unique<Graph>(prog);
  // var1 --
  //        | --> fake1 --> var3 --> fake2 --> var4
  // var2 --
  OpDesc fake1_op;
  fake1_op.SetType("fake1");
  OpDesc fake2_op;
  fake2_op.SetType("fake2");

  VarDesc var1("var1");
  VarDesc var2("var2");
117 118
  var2.SetPersistable(true);
  var2.SetIsParameter(true);
J
jiangcheng 已提交
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
  VarDesc var3("var3");
  VarDesc var4("var4");

  ir::Node* fake1 = g->CreateOpNode(&fake1_op);
  ir::Node* fake2 = g->CreateOpNode(&fake2_op);

  ir::Node* v1 = g->CreateVarNode(&var1);
  ir::Node* v2 = g->CreateVarNode(&var2);
  ir::Node* v3 = g->CreateVarNode(&var3);
  ir::Node* v4 = g->CreateVarNode(&var4);

  // fill op node
  fake1->inputs = {v1, v2};
  fake1->outputs = {v3};
  fake2->inputs = {v3};
  fake2->outputs = {v4};

  // fill variable node
  v1->outputs = {fake1};
  v2->outputs = {fake1};

  v3->inputs = {fake1};
  v3->outputs = {fake2};

  v4->inputs = {fake2};

  return g;
}

TEST(BuildCinnPassTest, NoCinnSubgraph) {
  auto g = BuildNoCinnSubgraph();
  auto previous_nodes = g->Nodes();

  auto pass =
      paddle::framework::ir::PassRegistry::Instance().Get("build_cinn_pass");
  pass->Apply(g.get());

  // After search, origin graph should no change
  ASSERT_EQ(previous_nodes, g->Nodes());
158
  ASSERT_TRUE(CheckGraphIndependence(g->Nodes()));
J
jiangcheng 已提交
159

160 161
  // After search, there should be no cinn subgraph
  ASSERT_TRUE(GetCompilationKeys(*g).empty());
J
jiangcheng 已提交
162 163 164 165 166 167 168 169 170 171 172 173
}

std::unique_ptr<Graph> BuildAllOpSupportCinnGraph() {
  ProgramDesc prog;
  auto g = std::make_unique<Graph>(prog);

  // v1 --
  //      | --> mul --> v3 --
  // v2 --                   | --> add --> v5 --> relu --> v6
  //                    v4 --

  OpDesc add_op;
174
  add_op.SetType("elementwise_add");
J
jiangcheng 已提交
175 176 177 178 179 180 181
  OpDesc mul_op;
  mul_op.SetType("mul");
  OpDesc relu_op;
  relu_op.SetType("relu");

  VarDesc var1("var1");
  VarDesc var2("var2");
182 183
  var2.SetPersistable(true);
  var2.SetIsParameter(true);
J
jiangcheng 已提交
184 185 186 187 188 189 190 191 192
  VarDesc var3("var3");
  VarDesc var4("var4");
  VarDesc var5("var5");
  VarDesc var6("var6");

  ir::Node* add = g->CreateOpNode(&add_op);
  ir::Node* mul = g->CreateOpNode(&mul_op);
  ir::Node* relu = g->CreateOpNode(&relu_op);

193
  ir::Node* v0 = g->CreateEmptyNode("var0", Node::Type::kVariable);
J
jiangcheng 已提交
194 195 196 197 198 199
  ir::Node* v1 = g->CreateVarNode(&var1);
  ir::Node* v2 = g->CreateVarNode(&var2);
  ir::Node* v3 = g->CreateVarNode(&var3);
  ir::Node* v4 = g->CreateVarNode(&var4);
  ir::Node* v5 = g->CreateVarNode(&var5);
  ir::Node* v6 = g->CreateVarNode(&var6);
200
  ir::Node* v7 = g->CreateControlDepVar();
J
jiangcheng 已提交
201 202

  // fill op node
203
  mul->inputs = {v0, v1, v2};
J
jiangcheng 已提交
204 205 206 207
  mul->outputs = {v3};
  add->inputs = {v3, v4};
  add->outputs = {v5};
  relu->inputs = {v5};
208
  relu->outputs = {v6, v7};
J
jiangcheng 已提交
209 210

  // fill variable node
211
  v0->outputs = {mul};
J
jiangcheng 已提交
212 213 214 215 216 217 218 219 220 221 222 223
  v1->outputs = {mul};
  v2->outputs = {mul};

  v3->inputs = {mul};
  v3->outputs = {add};

  v4->outputs = {add};

  v5->inputs = {add};
  v5->outputs = {relu};

  v6->inputs = {relu};
224
  v7->inputs = {relu};
J
jiangcheng 已提交
225 226 227 228 229 230 231 232 233 234 235 236

  return g;
}

TEST(BuildCinnPassTest, AllOpSupportCinn) {
  auto g = BuildAllOpSupportCinnGraph();

  auto pass =
      paddle::framework::ir::PassRegistry::Instance().Get("build_cinn_pass");
  pass->Apply(g.get());

  // After search, the graph should as following
237 238 239
  // v0 --|
  // v1 --|                   |--> v6
  // v2 --| --> kCinnLaunchOp |--> v7
J
jiangcheng 已提交
240 241
  // v4 --|
  const auto& nodes = g->Nodes();
242
  ASSERT_EQ(nodes.size(), static_cast<size_t>(7));
243
  ASSERT_TRUE(CheckGraphIndependence(nodes));
J
jiangcheng 已提交
244 245 246 247

  // A new op named kCinnLaunchOp should be added
  ASSERT_TRUE(CheckNodeExisted(nodes, kCinnLaunchOp));
  auto* cinn_op = GetNode(nodes, kCinnLaunchOp);
248
  auto* v0 = GetNode(nodes, "var0");
J
jiangcheng 已提交
249 250 251 252
  auto* v1 = GetNode(nodes, "var1");
  auto* v2 = GetNode(nodes, "var2");
  auto* v4 = GetNode(nodes, "var4");
  auto* v6 = GetNode(nodes, "var6");
253
  auto* v7 = GetNode(nodes, Node::kControlDepVarName);
J
jiangcheng 已提交
254 255 256

  ASSERT_EQ(
      std::unordered_set<Node*>(cinn_op->inputs.begin(), cinn_op->inputs.end()),
257 258
      std::unordered_set<Node*>({v0, v1, v2, v4}));
  ASSERT_EQ(cinn_op->outputs, std::vector<Node*>({v6, v7}));
J
jiangcheng 已提交
259 260 261 262 263
  ASSERT_EQ(v1->outputs, std::vector<Node*>({cinn_op}));
  ASSERT_EQ(v6->inputs, std::vector<Node*>({cinn_op}));

  // previous op (mul, add, relu) should all removed
  ASSERT_FALSE(CheckNodeExisted(nodes, "mul"));
264
  ASSERT_FALSE(CheckNodeExisted(nodes, "elementwise_add"));
J
jiangcheng 已提交
265 266 267
  ASSERT_FALSE(CheckNodeExisted(nodes, "relu"));

  // After search, there should has just one cinn subgraph
268 269
  // feed --> v1 --
  //               | --> mul --> v3 --
270
  //          v2 --                   | --> add --> v5 --> relu --> v6 --> fetch
271
  //                    feed --> v4 --
272 273 274 275
  auto compilation_keys = GetCompilationKeys(*g);
  ASSERT_EQ(compilation_keys.size(), static_cast<size_t>(1));
  auto* cinn_compiler = CinnCompiler::GetInstance();
  const auto& subgraph = cinn_compiler->FindGraph(compilation_keys[0]);
J
jiangcheng 已提交
276

277
  const auto& subnodes = subgraph.Nodes();
278
  ASSERT_EQ(subnodes.size(), static_cast<size_t>(12));
279
  ASSERT_TRUE(CheckGraphIndependence(subnodes));
J
jiangcheng 已提交
280 281

  ASSERT_TRUE(CheckNodeExisted(subnodes, "mul"));
282
  ASSERT_TRUE(CheckNodeExisted(subnodes, "elementwise_add"));
J
jiangcheng 已提交
283
  ASSERT_TRUE(CheckNodeExisted(subnodes, "relu"));
284
  ASSERT_EQ(CountNode(subnodes, "feed"), 2);
285
  ASSERT_EQ(CountNode(subnodes, "fetch"), 1);
286 287 288 289 290 291 292 293 294 295 296 297 298

  // No-parameter input should has feed op
  auto new_v1 = GetNode(subnodes, "var1");
  ASSERT_EQ(new_v1->inputs.size(), static_cast<size_t>(1));
  ASSERT_EQ(new_v1->outputs.size(), static_cast<size_t>(1));
  ASSERT_EQ(new_v1->inputs[0]->Name(), "feed");
  ASSERT_EQ(new_v1->outputs[0]->Name(), "mul");

  // Parameter input should not has feed op
  auto new_v2 = GetNode(subnodes, "var2");
  ASSERT_TRUE(new_v2->inputs.empty());
  ASSERT_EQ(new_v2->outputs.size(), static_cast<size_t>(1));
  ASSERT_EQ(new_v2->outputs[0]->Name(), "mul");
299 300 301 302 303 304 305

  // output should has fetch op
  auto new_v6 = GetNode(subnodes, "var6");
  ASSERT_EQ(new_v6->inputs.size(), static_cast<size_t>(1));
  ASSERT_EQ(new_v6->outputs.size(), static_cast<size_t>(1));
  ASSERT_EQ(new_v6->inputs[0]->Name(), "relu");
  ASSERT_EQ(new_v6->outputs[0]->Name(), "fetch");
J
jiangcheng 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
}

std::unique_ptr<Graph> BuildGraphWithOneCinnSubgraph() {
  ProgramDesc prog;
  auto g = std::make_unique<Graph>(prog);

  // fake1 --> v1 --
  //                | --> mul --> v3 --> relu --> v4 --> fake2
  //           v2 --

  OpDesc fake1_op;
  fake1_op.SetType("fake1");
  OpDesc mul_op;
  mul_op.SetType("mul");
  OpDesc relu_op;
  relu_op.SetType("relu");
  OpDesc fake2_op;
  fake2_op.SetType("fake2");

  VarDesc var1("var1");
  VarDesc var2("var2");
327 328
  var2.SetPersistable(true);
  var2.SetIsParameter(true);
J
jiangcheng 已提交
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
  VarDesc var3("var3");
  VarDesc var4("var4");

  ir::Node* fake1 = g->CreateOpNode(&fake1_op);
  ir::Node* mul = g->CreateOpNode(&mul_op);
  ir::Node* relu = g->CreateOpNode(&relu_op);
  ir::Node* fake2 = g->CreateOpNode(&fake2_op);

  ir::Node* v1 = g->CreateVarNode(&var1);
  ir::Node* v2 = g->CreateVarNode(&var2);
  ir::Node* v3 = g->CreateVarNode(&var3);
  ir::Node* v4 = g->CreateVarNode(&var4);

  // fill op node
  fake1->outputs = {v1};
  mul->inputs = {v2, v1};
  mul->outputs = {v3};
  relu->inputs = {v3};
  relu->outputs = {v4};
  fake2->inputs = {v4};

  // fill variable node
  v2->outputs = {mul};

  v1->inputs = {fake1};
  v1->outputs = {mul};

  v3->inputs = {mul};
  v3->outputs = {relu};

  v4->inputs = {relu};
  v4->outputs = {fake2};

  return g;
}

TEST(BuildCinnPassTest, OneCinnSubgraph) {
  auto g = BuildGraphWithOneCinnSubgraph();

  auto pass =
      paddle::framework::ir::PassRegistry::Instance().Get("build_cinn_pass");
  pass->Apply(g.get());

  // After search, the graph should as following
  // fake1 --> v1 --
  //                | --> kCinnLaunchOp --> v4 --> fake2
  //           v2 --
  const auto& nodes = g->Nodes();
  ASSERT_EQ(nodes.size(), static_cast<size_t>(6));
378
  ASSERT_TRUE(CheckGraphIndependence(nodes));
J
jiangcheng 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391

  // A new op named kCinnLaunchOp should be added
  ASSERT_TRUE(CheckNodeExisted(nodes, kCinnLaunchOp));

  // previous op (mul, add, relu) should be removed
  ASSERT_FALSE(CheckNodeExisted(nodes, "mul"));
  ASSERT_FALSE(CheckNodeExisted(nodes, "relu"));

  // previous op (fake1, fake2) should be preserved
  ASSERT_TRUE(CheckNodeExisted(nodes, "fake1"));
  ASSERT_TRUE(CheckNodeExisted(nodes, "fake2"));

  // After search, there should has just one cinn subgraph
392
  // feed --> v1 --
393
  //               | --> mul --> v3 --> relu --> v4 --> fetch
394
  //          v2 --
395 396 397 398
  auto compilation_keys = GetCompilationKeys(*g);
  ASSERT_EQ(compilation_keys.size(), static_cast<size_t>(1));
  auto* cinn_compiler = CinnCompiler::GetInstance();
  const auto& subgraph = cinn_compiler->FindGraph(compilation_keys[0]);
J
jiangcheng 已提交
399

400
  const auto& subnodes = subgraph.Nodes();
401
  ASSERT_EQ(subnodes.size(), static_cast<size_t>(8));
402
  ASSERT_TRUE(CheckGraphIndependence(subnodes));
J
jiangcheng 已提交
403 404 405

  ASSERT_TRUE(CheckNodeExisted(subnodes, "mul"));
  ASSERT_TRUE(CheckNodeExisted(subnodes, "relu"));
406
  ASSERT_EQ(CountNode(subnodes, "feed"), 1);
407
  ASSERT_EQ(CountNode(subnodes, "fetch"), 1);
J
jiangcheng 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
}

std::unique_ptr<Graph> BuildGraphWithMultiCinnSubgraph() {
  ProgramDesc prog;
  auto g = std::make_unique<Graph>(prog);

  // fake1 --> v1 --
  //                | --> mul --> v3 --> fake2 --> v4 --> relu --> v5 --> fake3
  //           v2 --

  OpDesc fake1_op;
  fake1_op.SetType("fake1");
  OpDesc mul_op;
  mul_op.SetType("mul");
  OpDesc relu_op;
  relu_op.SetType("relu");
  OpDesc fake2_op;
  fake2_op.SetType("fake2");
  OpDesc fake3_op;
  fake3_op.SetType("fake3");

  VarDesc var1("var1");
  VarDesc var2("var2");
431 432
  var2.SetPersistable(true);
  var2.SetIsParameter(true);
J
jiangcheng 已提交
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
  VarDesc var3("var3");
  VarDesc var4("var4");
  VarDesc var5("var5");

  ir::Node* fake1 = g->CreateOpNode(&fake1_op);
  ir::Node* mul = g->CreateOpNode(&mul_op);
  ir::Node* relu = g->CreateOpNode(&relu_op);
  ir::Node* fake2 = g->CreateOpNode(&fake2_op);
  ir::Node* fake3 = g->CreateOpNode(&fake3_op);

  ir::Node* v1 = g->CreateVarNode(&var1);
  ir::Node* v2 = g->CreateVarNode(&var2);
  ir::Node* v3 = g->CreateVarNode(&var3);
  ir::Node* v4 = g->CreateVarNode(&var4);
  ir::Node* v5 = g->CreateVarNode(&var5);

  // fill op node
  fake1->outputs = {v1};
  mul->inputs = {v2, v1};
  mul->outputs = {v3};
  fake2->inputs = {v3};
  fake2->outputs = {v4};
  relu->inputs = {v4};
  relu->outputs = {v5};
  fake3->inputs = {v5};

  // fill variable node
  v2->outputs = {mul};

  v1->inputs = {fake1};
  v1->outputs = {mul};

  v3->inputs = {mul};
  v3->outputs = {fake2};

  v4->inputs = {fake2};
  v4->outputs = {relu};

  v5->inputs = {relu};
  v5->outputs = {fake3};

  return g;
}

TEST(BuildCinnPassTest, MultiCinnSubgraph) {
  auto g = BuildGraphWithMultiCinnSubgraph();

  auto pass =
      paddle::framework::ir::PassRegistry::Instance().Get("build_cinn_pass");
  pass->Apply(g.get());

  // After search, the graph should as following
  // fake1 -> v1 -
  //              | -> CinnOp -> v3 -> fake2 -> v4 -> CinnOp ->v5 -> fake3
  //          v2 -
  const auto& nodes = g->Nodes();
  ASSERT_EQ(nodes.size(), static_cast<size_t>(10));
490
  ASSERT_TRUE(CheckGraphIndependence(nodes));
J
jiangcheng 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

  // A new op named kCinnLaunchOp should be added
  ASSERT_TRUE(CheckNodeExisted(nodes, kCinnLaunchOp));
  ASSERT_EQ(CountNode(nodes, kCinnLaunchOp), 2);

  // previous op (mul, add, relu) should be removed
  ASSERT_FALSE(CheckNodeExisted(nodes, "mul"));
  ASSERT_FALSE(CheckNodeExisted(nodes, "relu"));

  // previous op (fake1, fake2) should be preserved
  ASSERT_TRUE(CheckNodeExisted(nodes, "fake1"));
  ASSERT_TRUE(CheckNodeExisted(nodes, "fake2"));
  ASSERT_TRUE(CheckNodeExisted(nodes, "fake3"));

  // After search, there should has two cinn subgraphs,
  // and each of subgraphs just has one node.
507 508
  auto compilation_keys = GetCompilationKeys(*g);
  ASSERT_EQ(compilation_keys.size(), static_cast<size_t>(2));
J
jiangcheng 已提交
509

510
  // subgraph1:
511
  // feed --> v4 --> relu --> v5 --> fetch
512 513
  // subgraph2:
  // feed --> v1 --
514
  //               | --> mul --> v3 --> fetch
515
  //          v2 --
516 517 518
  auto* cinn_compiler = CinnCompiler::GetInstance();
  const auto& subgraph1 = cinn_compiler->FindGraph(compilation_keys[0]);
  const auto& subnodes1 = subgraph1.Nodes();
519
  ASSERT_TRUE(CheckGraphIndependence(subnodes1));
J
jiangcheng 已提交
520

521 522
  const auto& subgraph2 = cinn_compiler->FindGraph(compilation_keys[1]);
  const auto& subnodes2 = subgraph2.Nodes();
523 524 525 526
  ASSERT_TRUE(CheckGraphIndependence(subnodes2));

  if (CheckNodeExisted(subnodes1, "relu")) {
    ASSERT_EQ(subnodes1.size(), static_cast<size_t>(5));
527 528 529 530
    ASSERT_EQ(subnodes2.size(), static_cast<size_t>(6));
  } else {
    ASSERT_EQ(subnodes2.size(), static_cast<size_t>(5));
    ASSERT_EQ(subnodes1.size(), static_cast<size_t>(6));
531
  }
J
jiangcheng 已提交
532 533
}

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
std::unique_ptr<Graph> BuildGraphWithNoNeedBufferInput() {
  ProgramDesc prog;
  auto g = std::make_unique<Graph>(prog);

  // fake1 --> v1 --                 --> v4 --> relu_grad --> v6
  //           v2 -- | --> add_grad |
  //           v3 --                 --> v5 --> fake2

  OpDesc fake1_op;
  fake1_op.SetType("fake1");
  OpDesc add_grad_op;
  add_grad_op.SetType("elementwise_add_grad");
  add_grad_op.SetInput(::paddle::framework::GradVarName("Out"), {"var1"});
  add_grad_op.SetInput("X", {"var2"});
  add_grad_op.SetInput("Y", {"var3"});
  OpDesc relu_grad_op;
  relu_grad_op.SetType("relu_grad");
  OpDesc fake2_op;
  fake2_op.SetType("fake2");

  VarDesc var1("var1");
  VarDesc var2("var2");
  VarDesc var3("var3");
  VarDesc var4("var4");
  VarDesc var5("var5");
  VarDesc var6("var6");

  ir::Node* fake1 = g->CreateOpNode(&fake1_op);
  ir::Node* add_grad = g->CreateOpNode(&add_grad_op);
  ir::Node* relu_grad = g->CreateOpNode(&relu_grad_op);
  ir::Node* fake2 = g->CreateOpNode(&fake2_op);

  ir::Node* v1 = g->CreateVarNode(&var1);
  ir::Node* v2 = g->CreateVarNode(&var2);
  ir::Node* v3 = g->CreateVarNode(&var3);
  ir::Node* v4 = g->CreateVarNode(&var4);
  ir::Node* v5 = g->CreateVarNode(&var5);
  ir::Node* v6 = g->CreateVarNode(&var6);

  // fill op node
  fake1->outputs = {v1};
  add_grad->inputs = {v1, v2, v3};
  add_grad->outputs = {v4, v5};
  relu_grad->inputs = {v4};
  relu_grad->outputs = {v6};
  fake2->inputs = {v5};

  // fill variable node
  v1->inputs = {fake1};
  v1->outputs = {add_grad};

  v2->outputs = {add_grad};
  v3->outputs = {add_grad};

  v4->inputs = {add_grad};
  v4->outputs = {relu_grad};
  v5->inputs = {add_grad};
  v5->outputs = {fake2};

  v6->inputs = {relu_grad};

  return g;
}

TEST(BuildCinnPassTest, NoNeedBufferInput) {
  auto g = BuildGraphWithNoNeedBufferInput();

  auto pass =
      paddle::framework::ir::PassRegistry::Instance().Get("build_cinn_pass");
  pass->Apply(g.get());

  // After search, the graph should as following
  // fake1 --> v1 --                     --> v6
  //           v2 -- | -->kCinnLaunchOp |
  //           v3 --                     --> v5 --> fake2
  const auto& nodes = g->Nodes();
  ASSERT_EQ(nodes.size(), static_cast<size_t>(8));
  ASSERT_TRUE(CheckGraphIndependence(nodes));

  // A new op named kCinnLaunchOp should be added and
  // its input arguments are set correctly
  ASSERT_TRUE(CheckNodeExisted(nodes, kCinnLaunchOp));
  ASSERT_EQ(CountNode(nodes, kCinnLaunchOp), 1);
  auto* cinn_op_node = GetNode(nodes, kCinnLaunchOp);
  ASSERT_EQ(cinn_op_node->Op()->Input(operators::kX),
            std::vector<std::string>({"var1"}));
  auto& no_need_buffer_x = cinn_op_node->Op()->Input(operators::kNoNeedBufferX);
  ASSERT_EQ(std::unordered_set<std::string>(no_need_buffer_x.begin(),
                                            no_need_buffer_x.end()),
            std::unordered_set<std::string>({"var2", "var3"}));

  // previous op (add_grad, relu_grad) should be removed
  ASSERT_FALSE(CheckNodeExisted(nodes, "add_grad"));
  ASSERT_FALSE(CheckNodeExisted(nodes, "relu_grad"));

  // previous op (fake1, fake2) should be preserved
  ASSERT_TRUE(CheckNodeExisted(nodes, "fake1"));
  ASSERT_TRUE(CheckNodeExisted(nodes, "fake2"));

  // After search, there should has just one cinn subgraph
  // feed --> v1 --                                     --> v6 --> fetch
  // feed --> v2 -- | -->add_grad --> v4 --> relu_grad |
  // feed --> v3 --                                     --> v5 --> fetch
  auto compilation_keys = GetCompilationKeys(*g);
  ASSERT_EQ(compilation_keys.size(), static_cast<size_t>(1));
  auto* cinn_compiler = CinnCompiler::GetInstance();
  const auto& subgraph = cinn_compiler->FindGraph(compilation_keys[0]);

  const auto& subnodes = subgraph.Nodes();
  ASSERT_EQ(subnodes.size(), static_cast<size_t>(13));
  ASSERT_TRUE(CheckGraphIndependence(subnodes));

  ASSERT_TRUE(CheckNodeExisted(subnodes, "elementwise_add_grad"));
  ASSERT_TRUE(CheckNodeExisted(subnodes, "relu_grad"));
  ASSERT_EQ(CountNode(subnodes, "feed"), 3);
  ASSERT_EQ(CountNode(subnodes, "fetch"), 2);
  const auto& no_need_buffer_feeds =
      subgraph.Get<std::unordered_set<std::string>>(kNoNeedBufferFeeds);
  ASSERT_EQ(no_need_buffer_feeds.size(), 2);
  ASSERT_EQ(no_need_buffer_feeds,
            std::unordered_set<std::string>({"var2", "var3"}));
}

J
jiangcheng 已提交
657 658 659 660 661
}  // namespace paddle2cinn
}  // namespace framework
}  // namespace paddle

USE_PASS(build_cinn_pass);
662 663 664 665 666
USE_OP(mul);
USE_OP(relu);
USE_OP(elementwise_add);
USE_OP(relu_grad);
USE_OP(elementwise_add_grad);