ssa_graph_builder.h 2.8 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
clean  
Xin Pan 已提交
42 43

// aux variables to represent dependency. Useful to resolve data hazard.
X
Xin Pan 已提交
44
typedef std::unordered_set<std::unique_ptr<VarHandleBase>> GraphDepVars;
X
clean  
Xin Pan 已提交
45 46 47

// all operators. NOTE that even we use a vector here, the operators is
// unordered.
X
Xin Pan 已提交
48 49
typedef std::vector<std::unique_ptr<OpHandleBase>> GraphOps;

50
class SSAGraphBuilder : public ir::Pass {
Y
Yu Yang 已提交
51 52 53
 public:
  SSAGraphBuilder() {}
  virtual ~SSAGraphBuilder() {}
54

Y
yi.wu 已提交
55
  virtual int GetVarDeviceID(const std::string &var_name) const = 0;
Y
Yu Yang 已提交
56 57 58 59

  DISABLE_COPY_AND_ASSIGN(SSAGraphBuilder);

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

X
Xin Pan 已提交
66
  static VarHandle *CreateOrGetLatestVarHandle(ir::Graph *graph, ir::Node *node,
Y
Yu Yang 已提交
67 68 69
                                               const platform::Place &place,
                                               size_t place_offset);

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

X
Xin Pan 已提交
76
  static void AddOutputToLeafOps(ir::Graph *graph);
Y
Yu Yang 已提交
77 78 79 80
};
}  // namespace details
}  // namespace framework
}  // namespace paddle