pass_test.cc 4.3 KB
Newer Older
X
Xin Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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/ir/pass.h"
16
#include <memory>
X
Xin Pan 已提交
17
#include <string>
18
#include <utility>
X
Xin Pan 已提交
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
#include "gtest/gtest.h"
#include "paddle/fluid/framework/ir/graph.h"

namespace paddle {
namespace framework {
namespace ir {
void BuildCircleGraph(Graph* g) {
  ir::Node* o1 = g->CreateEmptyNode("op1", Node::Type::kOperation);
  ir::Node* o2 = g->CreateEmptyNode("op2", Node::Type::kOperation);
  ir::Node* v1 = g->CreateEmptyNode("var1", Node::Type::kVariable);
  ir::Node* v2 = g->CreateEmptyNode("var2", Node::Type::kVariable);

  o1->outputs.push_back(v1);
  o2->inputs.push_back(v1);
  v1->inputs.push_back(o1);
  v1->outputs.push_back(o2);

  o2->outputs.push_back(v2);
  o1->inputs.push_back(v2);
  v2->inputs.push_back(o2);
  v2->outputs.push_back(o1);
}

class TestPass : public Pass {
 protected:
44
  void ApplyImpl(ir::Graph* graph) const {
X
Xin Pan 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    graph->Set<int>("copy_test_pass_attr", new int);
    graph->Set<int>("copy_test_graph_attr", new int);

    int test_pass_attr = this->Get<int>("test_pass_attr");
    graph->Get<int>("copy_test_pass_attr") = test_pass_attr + 1;

    int test_graph_attr = graph->Get<int>("test_graph_attr");
    graph->Get<int>("copy_test_graph_attr") = test_graph_attr + 1;
  }
};

TEST(PassTest, TestPassAttrCheck) {
  ProgramDesc prog;
  auto pass = PassRegistry::Instance().Get("test_pass");
  std::unique_ptr<Graph> graph(new Graph(prog));
  std::string exception;
  try {
62
    graph.reset(pass->Apply(graph.release()));
63
  } catch (paddle::platform::EnforceNotMet& e) {
X
Xin Pan 已提交
64 65
    exception = std::string(e.what());
  }
66 67
  ASSERT_TRUE(exception.find("Required atrribute test_pass_attr for pass < "
                             "test_pass > is not set") != exception.npos);
X
Xin Pan 已提交
68 69 70 71 72

  int val = 1;
  graph.reset(new Graph(prog));
  pass->SetNotOwned<int>("test_pass_attr", &val);

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  for (std::string try_type : {"bool", "const int", "std::string"}) {
    try {
      if (try_type == "bool") {
        pass->Get<bool>("test_pass_attr");
      } else if (try_type == "const int") {
        pass->Get<const int>("test_pass_attr");
      } else if (try_type == "std::string") {
        pass->Get<std::string>("test_pass_attr");
      }
    } catch (paddle::platform::EnforceNotMet& e) {
      exception = std::string(e.what());
    }
    std::string msg = "Invalid type for attritube test_pass_attr, expected: " +
                      try_type + ", actual: int";
    ASSERT_TRUE(exception.find(msg) != exception.npos);
  }

X
Xin Pan 已提交
90
  try {
91
    graph.reset(pass->Apply(graph.release()));
92
  } catch (paddle::platform::EnforceNotMet& e) {
X
Xin Pan 已提交
93 94
    exception = std::string(e.what());
  }
95 96 97
  ASSERT_TRUE(exception.find(
                  "Required atrribute test_graph_attr for graph is not set") !=
              exception.npos);
X
Xin Pan 已提交
98 99 100 101

  graph.reset(new Graph(prog));
  graph->Set<int>("test_graph_attr", new int);
  graph->Get<int>("test_graph_attr") = 1;
102
  graph.reset(pass->Apply(graph.release()));
X
Xin Pan 已提交
103 104 105
  ASSERT_EQ(graph->Get<int>("copy_test_pass_attr"), 2);
  ASSERT_EQ(graph->Get<int>("copy_test_graph_attr"), 2);

X
fix  
Xin Pan 已提交
106 107 108
  // Allow apply more than once.
  graph.reset(new Graph(prog));
  graph->Set<int>("test_graph_attr", new int);
109
  graph.reset(pass->Apply(graph.release()));
X
Xin Pan 已提交
110 111 112 113 114 115 116 117

  pass = PassRegistry::Instance().Get("test_pass");
  pass->SetNotOwned<int>("test_pass_attr", &val);
  graph.reset(new Graph(prog));
  BuildCircleGraph(graph.get());
  graph->Set<int>("test_graph_attr", new int);
  graph->Get<int>("test_graph_attr") = 2;
  try {
118
    pass->Apply(graph.release());
119
  } catch (paddle::platform::EnforceNotMet& e) {
X
Xin Pan 已提交
120 121
    exception = std::string(e.what());
  }
122
  ASSERT_TRUE(exception.find("shouldn't have cycle") != exception.npos);
X
Xin Pan 已提交
123 124 125 126 127 128 129 130 131
}

}  // namespace ir
}  // namespace framework
}  // namespace paddle

REGISTER_PASS(test_pass, paddle::framework::ir::TestPass)
    .RequirePassAttr("test_pass_attr")
    .RequireGraphAttr("test_graph_attr");