ssa_graph_builder.h 2.9 KB
Newer Older
Y
Yu Yang 已提交
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

Y
Yu Yang 已提交
17 18
#include <memory>
#include <string>
X
Xin Pan 已提交
19
#include <vector>
Y
Yu Yang 已提交
20

X
clean  
Xin Pan 已提交
21 22 23
#include "paddle/fluid/framework/details/op_handle_base.h"
#include "paddle/fluid/framework/details/var_handle.h"

Y
Yu Yang 已提交
24 25 26
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/platform/place.h"

X
Xin Pan 已提交
27
#include "paddle/fluid/framework/ir/graph.h"
28
#include "paddle/fluid/framework/ir/pass.h"
X
Xin Pan 已提交
29

Y
Yu Yang 已提交
30 31 32 33
namespace paddle {
namespace framework {
namespace details {

X
clean  
Xin Pan 已提交
34 35 36 37 38
// all variable in each devices.
// The outside vector is the device vector. Each element of this vector is a
// map from variable name to variables. The variables, who have the same name,
// will have a differsent version. The offset in the
// `std::vector<std::unique_ptr<VarHandle>>` is the version of varaibles.
X
Xin Pan 已提交
39 40 41
typedef std::vector<
    std::unordered_map<std::string, std::vector<std::unique_ptr<VarHandle>>>>
    GraphVars;
X
Xin Pan 已提交
42
const char kGraphVars[] = "vars";
X
clean  
Xin Pan 已提交
43 44

// aux variables to represent dependency. Useful to resolve data hazard.
X
Xin Pan 已提交
45
typedef std::unordered_set<std::unique_ptr<VarHandleBase>> GraphDepVars;
X
Xin Pan 已提交
46
const char kGraphDepVars[] = "dep_vars";
X
clean  
Xin Pan 已提交
47 48 49

// all operators. NOTE that even we use a vector here, the operators is
// unordered.
X
Xin Pan 已提交
50
typedef std::vector<std::unique_ptr<OpHandleBase>> GraphOps;
X
Xin Pan 已提交
51
const char kGraphOps[] = "ops";
X
Xin Pan 已提交
52

X
Xin Pan 已提交
53
typedef std::unordered_map<std::string, int> ShardedVarDevice;
X
Xin Pan 已提交
54
const char kShardedVarDevice[] = "sharded_var_device";
X
Xin Pan 已提交
55

56
class SSAGraphBuilder : public ir::Pass {
Y
Yu Yang 已提交
57 58 59
 public:
  SSAGraphBuilder() {}
  virtual ~SSAGraphBuilder() {}
60

Y
Yu Yang 已提交
61 62 63
  DISABLE_COPY_AND_ASSIGN(SSAGraphBuilder);

 protected:
X
Xin Pan 已提交
64 65 66 67 68 69
  /*
    Dependency graph has been constructed. However, there are still data
    hazards need to be handled.
  */
  static void PolishGraphToSupportDataHazards(ir::Graph *graph);

X
Xin Pan 已提交
70
  static VarHandle *CreateOrGetLatestVarHandle(ir::Graph *graph, ir::Node *node,
Y
Yu Yang 已提交
71 72 73
                                               const platform::Place &place,
                                               size_t place_offset);

Y
Yu Yang 已提交
74 75
  // Add an output variable (each_var_name, place, place_offset) to op_handle,
  // which belongs to graph
X
Xin Pan 已提交
76
  static void CreateOpOutput(ir::Graph *graph, OpHandleBase *op_handle,
X
polish  
Xin Pan 已提交
77
                             ir::Node *new_node, const platform::Place &place,
78
                             size_t place_offset);
Y
Yu Yang 已提交
79

X
Xin Pan 已提交
80
  static void AddOutputToLeafOps(ir::Graph *graph);
Y
Yu Yang 已提交
81 82 83 84
};
}  // namespace details
}  // namespace framework
}  // namespace paddle