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
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 60 61 62 63 64 65 66

  DISABLE_COPY_AND_ASSIGN(SSAGraphBuilder);

 protected:
  /**
   * We only handle write after read(WAR), since it should not have a write
   * after write in program. If there are write after write operators, we need
   * prune them.
   *
   * https://en.wikipedia.org/wiki/Hazard_(computer_architecture)#Write_after_read_(WAR)
   */
X
Xin Pan 已提交
67
  static void PolishGraphToSupportDataHazards(Graph *graph);
Y
Yu Yang 已提交
68

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

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

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