data_flow_graph_tester.cc 6.2 KB
Newer Older
1 2
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Y
Yan Chunwei 已提交
3 4 5
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
6

Y
Yan Chunwei 已提交
7
http://www.apache.org/licenses/LICENSE-2.0
8

Y
Yan Chunwei 已提交
9 10 11 12 13
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. */
14 15

#include "paddle/fluid/inference/analysis/data_flow_graph.h"
16
#include "paddle/fluid/framework/program_desc.h"
17 18 19 20 21 22 23
#include "paddle/fluid/inference/analysis/ut_helper.h"

namespace paddle {
namespace inference {
namespace analysis {

TEST(DataFlowGraph, BFS) {
Y
Yan Chunwei 已提交
24
  auto desc = LoadProgramDesc(FLAGS_inference_model_dir + "/__model__");
25 26 27
  auto dfg = ProgramDescToDFG(desc);
  dfg.Build();

28
  for (auto* in : dfg.inputs()) {
29 30 31
    LOG(INFO) << "inputs: " << in->name() << " "
              << static_cast<int>(in->type());
  }
32
  for (auto* out : dfg.outputs()) {
33 34 35 36
    LOG(INFO) << "outputs: " << out->name() << " "
              << static_cast<int>(out->type());
  }

Y
Yancey1989 已提交
37
  size_t count = 0;
38 39
  for (auto& node : GraphTraits<DataFlowGraph>(dfg).nodes()) {
    LOG(INFO) << "visiting " << node.name();
40 41 42 43 44 45
    ++count;
  }
  ASSERT_EQ(count, dfg.nodes.size());
}

TEST(DataFlowGraph, DFS) {
Y
Yan Chunwei 已提交
46
  auto desc = LoadProgramDesc(FLAGS_inference_model_dir + "/__model__");
47 48
  DataFlowGraph dfg;
  dfg.Build(desc);
Y
Yancey1989 已提交
49
  size_t count = 0;
50 51
  for (auto& node : GraphTraits<DataFlowGraph>(dfg).nodes_in_DFS()) {
    LOG(INFO) << "visiting " << node.name();
52 53 54 55 56
    ++count;
  }
  ASSERT_EQ(count, dfg.nodes.size());
}

Y
Yan Chunwei 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
// Topological sorting.
/*
 * Graph topology
 * inputs: 0, 1, 2
 * 0 -> 4
 * 0 -> 5
 * 1 -> 6
 * 2 -> 7
 * 4 -> 5
 * 4 -> 7
 * 4 -> 3
 * 7 -> 3
 */
TEST(DataFlowGraph, TS) {
  DataFlowGraph graph;

  for (int i = 0; i < 8; i++) {
74
    auto* node = graph.nodes.Create(Node::Type::kValue);
Y
Yan Chunwei 已提交
75 76 77 78
    node->SetName("node-" + std::to_string(i));
  }

  auto add_link = [&](int i, int j) {
79 80
    Node* source = graph.nodes.GetMutable(i);
    Node* target = graph.nodes.GetMutable(j);
Y
Yan Chunwei 已提交
81 82 83 84 85 86 87 88 89 90 91 92
    target->inlinks.push_back(source);
    source->outlinks.push_back(target);
  };

  add_link(0, 4);
  add_link(0, 5);
  add_link(1, 6);
  add_link(2, 7);
  add_link(4, 5);
  add_link(4, 7);
  add_link(4, 3);
  add_link(7, 3);
93
  graph.Build();
Y
Yan Chunwei 已提交
94

95
  auto its = GraphTraits<DataFlowGraph>(graph).nodes_in_TS();
Y
Yan Chunwei 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  std::vector<int> sorted_ids;
  for (auto it = its.begin(); it != its.end(); ++it) {
    LOG(INFO) << it->name();
    sorted_ids.push_back(it->id());
  }

  // Assert a occurs prior to b in the sorted_ids.
  auto assert_positive_sequence_pair = [&](int a, int b) {
    auto a_offset = std::find(sorted_ids.begin(), sorted_ids.end(), a);
    auto b_offset = std::find(sorted_ids.begin(), sorted_ids.end(), b);
    ASSERT_LT(a_offset, b_offset);
  };

  assert_positive_sequence_pair(2, 7);
  assert_positive_sequence_pair(7, 3);
  assert_positive_sequence_pair(4, 3);
  assert_positive_sequence_pair(0, 4);
  assert_positive_sequence_pair(0, 5);
  assert_positive_sequence_pair(1, 6);
  assert_positive_sequence_pair(4, 5);
  assert_positive_sequence_pair(4, 7);
}

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
TEST(DataFlowGraph, Build_ProgramDesc) {
  auto desc = LoadProgramDesc(FLAGS_inference_model_dir + "/__model__");
  DataFlowGraph graph;
  graph.Build(desc);
  ASSERT_EQ(graph.nodes.size(), 38UL);
}

void SetOp(framework::ProgramDesc* prog, const std::string& type,
           const std::vector<std::string>& inputs,
           const std::vector<std::string>& outputs) {
  auto* op = prog->MutableBlock(0)->AppendOp();
  op->SetType(type);
  op->SetInput("Xs", inputs);
  op->SetOutput("Xs", outputs);
}

TEST(DataFlowGraph, Build_IR_Graph) {
  framework::ProgramDesc prog;
  for (auto& v : std::vector<std::string>({"a", "b", "c", "d", "e", "f"})) {
    auto* var = prog.MutableBlock(0)->Var(v);
    var->SetType(framework::proto::VarType::SELECTED_ROWS);
    if (v == "c") {
      var->SetPersistable(true);
    }
  }

  SetOp(&prog, "OP0", std::vector<std::string>({"a"}),
        std::vector<std::string>({"b"}));
  SetOp(&prog, "OP1", std::vector<std::string>({"a"}),
        std::vector<std::string>({"c"}));
  SetOp(&prog, "mul", std::vector<std::string>({"b", "c"}),
        std::vector<std::string>({"d"}));
  SetOp(&prog, "elementwise_add", std::vector<std::string>({"d", "e"}),
        std::vector<std::string>({"f"}));

  DataFlowGraph graph;

  framework::ir::Graph ir_graph(prog);

  graph.Build(ir_graph);

  ASSERT_EQ(graph.nodes.size(), ir_graph.Nodes().size());
}

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
// FlexibleDFS
/*
 * Graph topology
 * inputs: 0
 * 0 -> 1
 * 1 -> 2
 * 1 -> 3
 * 3 -> 4
 * 4 -> 5
 * 5 -> 2
 */
TEST(DataFlowGraph, flexibledfs) {
  DataFlowGraph graph;

  for (int i = 0; i < 6; i++) {
    auto* node = graph.nodes.Create(Node::Type::kValue);
    node->SetName("node-" + std::to_string(i));
  }

  auto add_link = [&](int i, int j) {
    Node* source = graph.nodes.GetMutable(i);
    Node* target = graph.nodes.GetMutable(j);
    target->inlinks.push_back(source);
    source->outlinks.push_back(target);
  };

  add_link(0, 1);
  add_link(1, 2);
  add_link(1, 3);
  add_link(3, 4);
  add_link(4, 5);
  add_link(5, 2);
  graph.Build();

  std::vector<const Node*> order;
  FlexibleDFS(graph.inputs(), false, nullptr, [&order](const Node* n) {
    order.push_back(n);
    return true;
  });

  ASSERT_EQ(order.size(), 6UL);

  order.clear();
  // reverse dfs
  FlexibleDFS(graph.outputs(), true, nullptr, [&order](const Node* n) {
    order.push_back(n);
    return true;
  });

  ASSERT_EQ(order.size(), 6UL);

  // If we delete
  Node* last_node = graph.nodes.GetMutable(2);
  Node* direct_node = graph.nodes.GetMutable(1);
  std::vector<Node*> source_nodes;
  for (Node* node : last_node->inlinks) {
    if (node != direct_node) source_nodes.push_back(node);
  }

  bool has_cycle = false;
  FlexibleDFS(source_nodes, true, nullptr,
              [&has_cycle, direct_node](const Node* n) {
                if (n == direct_node) {
                  has_cycle = true;
                  return false;
                }
                return true;
              });
  ASSERT_TRUE(has_cycle);
}

234 235 236
}  // namespace analysis
}  // namespace inference
}  // namespace paddle