graph.h 14.2 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

17
#include <gflags/gflags.h>
18

S
sneaxiy 已提交
19 20 21
#include <map>
#include <memory>
#include <string>
22
#include <unordered_set>
S
sneaxiy 已提交
23 24 25 26 27 28
#include <vector>

#include "paddle/fluid/framework/ir/node.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/variant.h"
29
#include "paddle/utils/any.h"
S
sneaxiy 已提交
30

31 32
DECLARE_bool(convert_all_blocks);

W
wanghuancoder 已提交
33 34 35 36 37 38 39
namespace paddle {
namespace framework {
class OpDesc;
class VarDesc;
}  // namespace framework
}  // namespace paddle

S
sneaxiy 已提交
40 41
namespace paddle {
namespace framework {
Y
Yancey1989 已提交
42 43

namespace details {
Y
Yancey1989 已提交
44 45 46

// This attr is not recommended, because the graph should not dependence
// the program once it is built.
X
Xin Pan 已提交
47
constexpr char kStaleProgramOpDescs[] = "stale_program_op_descs";
Y
Yancey1989 已提交
48 49
}  //  namespace details

S
sneaxiy 已提交
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
namespace ir {

/*
 * The graph is a Directed Acyclic Single Static Assignment Graph.
 *
 * In more detail, the following properties must hold:
 *
 *   The graph shouldn't contain cycle. Each node is a black-box to the graph
 *   so the node itself could be a loop operator.
 *
 *   Each Variable-type node has only one input (thus single static assignment).
 *
 *   The output/input of operator is variable and the output/input of variable
 *   is operator.
 *
 * The following data harzards in Program are addressed in the Graph:
 *
 *   Write-After-Read
 *     a = op1(x)
 *     x = op2(b)
 *     A control-dependency connection is created bettwen op1 and op2 such that
 *     op1->op2, so as to ensure correct order.
 *
 *   Write-After-Write
 *     x = op1(a)
 *     x = op2(b)
 *     A control-dependency connection is created between op1 and op2 such that
 *     op1->op2, so as to ensure correct order.
 *
 * Other properties currently hold, but is not enforced yet:
 *
 *   Variable-type node (not control dep) with the same variable name share
 *   the same underlying VarDesc.
 */
class Graph {
 public:
86
  // Construct a main_graph with some sub_graphs
S
sneaxiy 已提交
87
  explicit Graph(const ProgramDesc &program);
88 89 90

  // Construct a main_graph with some sub_graphs, and the 1st sub_graph is
  // constructed with ops[start_op_index, end_op_index)
91 92
  Graph(const ProgramDesc &program,
        const int64_t start_op_index,
93 94 95 96 97 98
        const int64_t end_op_index);

  // Construct a sub_graph
  Graph(const BlockDesc &block, const Graph *main_graph);

  // Construct a sub_graph with ops[start_op_index, end_op_index)
99 100 101 102
  Graph(const BlockDesc &block,
        const Graph *main_graph,
        const int64_t start_op_index,
        const int64_t end_op_index);
S
sneaxiy 已提交
103 104 105 106 107 108 109 110 111

  virtual ~Graph() {
    for (auto &attr : attrs_) {
      attr_dels_[attr.first]();
    }
    attrs_.clear();
    attr_dels_.clear();
  }

112 113 114 115 116 117 118 119
  bool IsConstructedByPartialProgram() const {
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->IsConstructedByPartialProgram();
      }
    }
    return is_partial_;
  }
120

S
sneaxiy 已提交
121
  bool Has(const std::string &attr_name) const {
122 123 124 125 126
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->Has(attr_name);
      }
    }
S
sneaxiy 已提交
127
    return attrs_.count(attr_name) > 0;
S
sneaxiy 已提交
128 129
  }

130 131
  template <typename AttrType>
  AttrType &GetOrInit(const std::string &attr_name) {
132 133 134 135 136
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->GetOrInit<AttrType>(attr_name);
      }
    }
137 138 139 140 141 142
    if (!Has(attr_name)) {
      Set(attr_name, new AttrType);
    }
    return Get<AttrType>(attr_name);
  }

S
sneaxiy 已提交
143 144
  template <typename AttrType>
  AttrType &Get(const std::string &attr_name) const {
145 146 147 148 149
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->Get<AttrType>(attr_name);
      }
    }
150
    PADDLE_ENFORCE_EQ(
151 152
        Has(attr_name),
        true,
153 154
        platform::errors::PreconditionNotMet(
            "%s attribute not registered for current graph.", attr_name));
S
sneaxiy 已提交
155
    try {
156 157
      return *paddle::any_cast<AttrType *>(attrs_.at(attr_name));
    } catch (paddle::bad_any_cast &) {
158 159
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Invalid attribute type of %s, expected: %s, received: %s.",
160 161
          attr_name,
          platform::demangle(typeid(AttrType *).name()),  // NOLINT
162
          platform::demangle(attrs_.at(attr_name).type().name())));
S
sneaxiy 已提交
163
    }
S
sneaxiy 已提交
164 165 166 167
  }

  template <typename AttrType>
  void Set(const std::string &attr_name, AttrType *attr) {
168 169 170 171 172
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->Set<AttrType>(attr_name, attr);
      }
    }
173
    PADDLE_ENFORCE_EQ(
174 175
        attrs_.count(attr_name),
        0,
176
        platform::errors::AlreadyExists(
177 178
            "The attribute %s to be set already exists in the graph.",
            attr_name));
S
sneaxiy 已提交
179 180
    attrs_[attr_name] = attr;
    attr_dels_[attr_name] = [attr, attr_name]() {
M
minqiyang 已提交
181
      VLOG(3) << "deleting " << attr_name;
S
sneaxiy 已提交
182 183 184 185 186 187
      delete attr;
    };
  }

  template <typename AttrType>
  void SetNotOwned(const std::string &attr_name, AttrType *attr) {
188 189 190 191 192
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->SetNotOwned<AttrType>(attr_name, attr);
      }
    }
193
    PADDLE_ENFORCE_EQ(
194 195
        attrs_.count(attr_name),
        0,
196 197 198
        platform::errors::AlreadyExists("The attribute %s to be set(not owned) "
                                        "already exists in the graph.",
                                        attr_name));
S
sneaxiy 已提交
199 200 201 202
    attrs_[attr_name] = attr;
    attr_dels_[attr_name] = []() {};
  }

X
Xin Pan 已提交
203
  void Erase(const std::string &attr_name) {
204 205 206 207 208
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->Erase(attr_name);
      }
    }
209
    PADDLE_ENFORCE_NE(
210 211
        attrs_.count(attr_name),
        0,
212
        platform::errors::NotFound(
213 214
            "The attribute %s to be erased does not exist in the graph.",
            attr_name));
X
Xin Pan 已提交
215 216 217 218 219
    attr_dels_[attr_name]();
    attrs_.erase(attr_name);
    attr_dels_.erase(attr_name);
  }

220 221 222 223 224 225 226 227
  const std::unordered_set<ir::Node *> &Nodes() const {
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->Nodes();
      }
    }
    return node_set_;
  }
S
sneaxiy 已提交
228 229

  // Create a normal variable with non-null VarDesc.
230
  ir::Node *CreateVarNode(VarDesc *var_desc, int block_id = -1) {
231 232 233 234 235
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->CreateVarNode(var_desc);
      }
    }
236
    PADDLE_ENFORCE_NOT_NULL(
237 238 239
        var_desc,
        platform::errors::InvalidArgument(
            "The VarDesc used to create variable node is null."));
240 241
    auto *x =
        AddNode(new ir::Node(var_desc, block_id == -1 ? block_id_ : block_id));
242
    x->SetId(num_node_created_++);
243
    x->SetGraphId(block_id_);
244
    return x;
S
sneaxiy 已提交
245 246 247 248
  }

  // Create a normal runnable operator with OpDesc.
  ir::Node *CreateOpNode(OpDesc *op_desc) {
249 250 251 252 253
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->CreateOpNode(op_desc);
      }
    }
254
    PADDLE_ENFORCE_NOT_NULL(
255 256 257
        op_desc,
        platform::errors::InvalidArgument(
            "The OpDesc used to create operator node is null."));
258 259
    auto *x = AddNode(new ir::Node(op_desc));
    x->SetId(num_node_created_++);
260
    x->SetGraphId(block_id_);
261
    return x;
S
sneaxiy 已提交
262 263 264 265 266 267
  }

  // Create a control dependency var that connects 2 operations. The
  // var doesn't hold any data. Other than that, it's no different from
  // other var, considering dependency analysis.
  ir::Node *CreateControlDepVar() {
268 269 270 271 272
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->CreateControlDepVar();
      }
    }
S
sneaxiy 已提交
273
    // TODO(panyx0718): control var name should be really unique.
274 275 276 277
    const std::string name =
        string::Sprintf("%s@%llu",
                        static_cast<const char *>(ir::Node::kControlDepVarName),
                        num_node_created_);
278
    auto *x = AddNode(new ir::Node(name, ir::Node::Type::kVariable, block_id_));
279
    x->SetId(num_node_created_++);
280
    x->SetGraphId(block_id_);
281
    return x;
S
sneaxiy 已提交
282 283 284 285 286
  }

  // A more free style way of creating a graph node. Mostly use for test
  // or "copy" from another node. Avoid using it if possible.
  ir::Node *CreateEmptyNode(const std::string &name, ir::Node::Type type) {
287 288 289 290 291
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->CreateEmptyNode(name, type);
      }
    }
292
    auto *x = AddNode(new ir::Node(name, type, block_id_));
293
    x->SetId(num_node_created_++);
294
    x->SetGraphId(block_id_);
295
    return x;
S
sneaxiy 已提交
296 297 298 299 300
  }

  // Clear all node information of the graph and return the ownership of the
  // nodes.
  std::vector<std::unique_ptr<ir::Node>> ReleaseNodes() {
301 302 303 304 305
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->ReleaseNodes();
      }
    }
S
sneaxiy 已提交
306 307 308 309 310 311 312 313 314
    std::vector<std::unique_ptr<ir::Node>> ret;
    for (auto &n : nodes_) {
      ret.emplace_back(n.second.release());
    }
    nodes_.clear();
    node_set_.clear();
    return ret;
  }

Y
Yancey1989 已提交
315
  std::unique_ptr<ir::Node> RemoveNode(ir::Node *node) {
316 317 318 319 320
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->RemoveNode(node);
      }
    }
321 322
    PADDLE_ENFORCE_EQ(node_set_.find(node) != node_set_.end(),
                      true,
323 324
                      platform::errors::PreconditionNotMet(
                          "The node to be removed does not exist."));
Y
Yancey1989 已提交
325 326 327 328 329 330 331
    std::unique_ptr<ir::Node> ret;
    ret.reset(nodes_.at(node).release());
    nodes_.erase(node);
    node_set_.erase(node);
    return ret;
  }

S
sneaxiy 已提交
332
  // NOTE low performance, but simple and secure.
333
  Node *RetrieveNode(int id) {
334 335 336 337 338
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->RetrieveNode(id);
      }
    }
S
sneaxiy 已提交
339 340 341 342 343 344 345 346
    for (auto &node : nodes_) {
      if (node.second->id() == id) {
        return node.second.get();
      }
    }
    return nullptr;
  }

X
fix  
Xin Pan 已提交
347 348 349 350
  // Returns reference to the original program.
  // WARN: After a series of passes, the current graph can be quite
  // different from OriginProgram. Caller shouldn't assume much from
  // the returned OriginProgram.
351 352 353 354 355 356 357 358
  const ProgramDesc &OriginProgram() const {
    if (FLAGS_convert_all_blocks) {
      if (!IsMainGraph()) {
        return main_graph_->OriginProgram();
      }
    }
    return program_;
  }
X
fix  
Xin Pan 已提交
359

S
sneaxiy 已提交
360 361
  // This method takes ownership of `node`.
  ir::Node *AddNode(ir::Node *node) {
362 363 364 365 366
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->AddNode(node);
      }
    }
367 368
    PADDLE_ENFORCE_EQ(node_set_.find(node) == node_set_.end(),
                      true,
369 370
                      platform::errors::PreconditionNotMet(
                          "The node to be added already exists."));
S
sneaxiy 已提交
371 372 373 374 375
    nodes_[node].reset(node);
    node_set_.insert(node);
    return node;
  }

Y
Yancey1989 已提交
376 377 378
  void ResolveHazard(
      const std::map<std::string, std::vector<ir::Node *>> &var_nodes);

379 380 381 382
  // Create a new and duplicated graph.
  // WARN: The method only clones the graph structure, not its attributes.
  std::shared_ptr<Graph> Clone();

383 384 385 386
  bool IsMainGraph() const { return main_graph_ == nullptr; }

  Graph *GetSubGraph(const size_t idx) const {
    PADDLE_ENFORCE_EQ(
387 388
        this->IsMainGraph(),
        true,
389 390
        platform::errors::InvalidArgument("This graph is not main_graph"));
    PADDLE_ENFORCE_LT(
391 392
        idx,
        sub_graphs_.size(),
393 394 395 396
        platform::errors::InvalidArgument("Invalid sub_graph index"));
    return sub_graphs_.at(idx).get();
  }

397 398 399 400 401 402 403 404 405
  int GetBlockId() const {
    if (FLAGS_convert_all_blocks) {
      if (IsMainGraph()) {
        return GetSubGraph(0)->block_id_;
      }
    }
    return block_id_;
  }

406 407
  size_t SubGraphsSize() const {
    PADDLE_ENFORCE_EQ(
408 409
        this->IsMainGraph(),
        true,
410 411 412 413
        platform::errors::InvalidArgument("This graph is not main_graph"));
    return sub_graphs_.size();
  }

Y
Yancey1989 已提交
414
 private:
415 416
  // TODO(levi): delete this interface after when we can convert all
  // blocks into sub_graphs.
Y
Yancey1989 已提交
417
  std::map<std::string, std::vector<ir::Node *>> InitFromProgram(
418 419
      const ProgramDesc &program,
      const int64_t start_op_index,
420 421 422
      const int64_t end_op_index);

  std::map<std::string, std::vector<ir::Node *>> InitFromBlock(
423 424
      const BlockDesc &block,
      const int64_t start_op_index,
425 426 427 428
      const int64_t end_op_index);

  void ReleaseSubGraphs() {
    PADDLE_ENFORCE_EQ(
429 430
        this->IsMainGraph(),
        true,
431 432 433 434 435 436
        platform::errors::InvalidArgument("This graph is not main_graph"));
    sub_graphs_.clear();
  }

  void AddSubGraph(std::unique_ptr<Graph> sub_graph) {
    PADDLE_ENFORCE_EQ(
437 438
        this->IsMainGraph(),
        true,
439
        platform::errors::InvalidArgument("This graph is not main_graph"));
440 441
    PADDLE_ENFORCE_EQ(sub_graphs_.size(),
                      sub_graph->block_id_,
442 443
                      platform::errors::InvalidArgument(
                          "sub_graph idx is not equal to block_id_"));
444 445 446 447
    sub_graphs_.push_back(std::move(sub_graph));
  }

  std::unique_ptr<Graph> CloneSubGraph(const size_t idx);
Y
Yancey1989 已提交
448

S
sneaxiy 已提交
449 450
  // NOTE: program_ shouldn't be exposed to user.
  const ProgramDesc program_;
451 452 453 454 455
  // NOTE: main_graph_ doesn't hold any node. It's used as a container of
  // sub_graphs, and the sub_graph holds the nodes.
  const Graph *main_graph_;  // not owned.
  std::vector<std::unique_ptr<Graph>> sub_graphs_;

456
  std::map<std::string, paddle::any> attrs_;
S
sneaxiy 已提交
457 458 459
  std::map<std::string, std::function<void(void)>> attr_dels_;
  std::map<ir::Node *, std::unique_ptr<ir::Node>> nodes_;
  std::unordered_set<ir::Node *> node_set_;
460
  size_t num_node_created_{0};  // help to generate a unique node id.
461 462 463 464 465
  // NOTE(Aurelius84): Whether is constructed with partial ProgramDesc.
  // In case of @to_static, whole trainning program is splited into two
  // parts: forward graph and backward graph, which can be executed
  // independently.
  bool is_partial_{false};
466 467
  // The block this SubGraph belongs to.
  int block_id_{0};
S
sneaxiy 已提交
468 469 470 471 472 473
};

bool IsControlDepVar(const ir::Node &var);
}  // namespace ir
}  // namespace framework
}  // namespace paddle