lock_free_optimize_pass.h 4.7 KB
Newer Older
M
minqiyang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

15
#pragma once
M
minqiyang 已提交
16 17 18 19 20 21

#include <string>
#include <vector>

#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/ir/pass.h"
22
#include "paddle/fluid/string/string_helper.h"
M
minqiyang 已提交
23 24 25 26 27 28

namespace paddle {
namespace framework {
namespace ir {

class Node;
W
wanghuancoder 已提交
29
class Graph;
M
minqiyang 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

/*
* Remove the sum op of all gradients of the backward op.
* And remove the dependecies of the optimizer related to the
* same backward op.
*
* Before this pass:
*
* forward_op1 forward_op2
*     |            |
*  grad_op1    grad_op2
*        \      /
*          \  /
*         sum_op
*           |
*         sgd_op
*
* After this pass:
* forward_op1 forward_op2
*     |            |
*  grad_op1    grad_op2
*     |            |
*  sgd_op1      sgd_op2
*
* sgd_op1 and sgd_op2 will update the same weight which holds the same
* memory, so we could benefits from the acceleration
*/
class LockFreeOptimizePass : public Pass {
 public:
  virtual ~LockFreeOptimizePass() {}

 protected:
62
  void ApplyImpl(ir::Graph* graph) const override;
M
minqiyang 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

 private:
  // Create a new sgd node via current optimizer node
  ir::Node* CreateNewSGDNode(ir::Graph* graph, ir::Node* forward_node,
                             ir::Node* backward_node, ir::Node* grad_sum_node,
                             ir::Node* optimize_node) const;

  // Replace the input weight's optimizers
  void ReplaceUpstreamNode(ir::Node* upstream_node,
                           ir::Node* old_optimizer_node,
                           ir::Node* new_optimizer_node) const;

  // Replace the output weight's optimizers
  void ReplaceAllDownstreamNode(ir::Node* old_optimizer_node,
                                ir::Node* new_optimizer_node) const;

  // Find all weight variables in graph
  bool FindAllWeightVars(ir::Graph* graph) const;

  // Find the forward_op node via the backward_op node
  ir::Node* FindForwardOpViaBackwardOp(ir::Graph* graph,
                                       ir::Node* backward_node) const;

  std::vector<ir::Node*> FindConnectedNode(ir::Node* upstream_node,
                                           ir::Node* downstream_node) const;

  inline bool IsOpNamed(ir::Node* node, const std::string& name) const {
90 91 92
    PADDLE_ENFORCE_NOT_NULL(node,
                            platform::errors::InvalidArgument(
                                "Input argument node cannot be nullptr."));
M
minqiyang 已提交
93 94 95 96 97

    return node->NodeType() == Node::Type::kOperation && node->Name() == name;
  }

  inline bool IsVarNamed(ir::Node* node, const std::string& name) const {
98 99 100
    PADDLE_ENFORCE_NOT_NULL(node,
                            platform::errors::InvalidArgument(
                                "Input argument node cannot be nullptr."));
M
minqiyang 已提交
101 102 103 104 105

    return node->NodeType() == Node::Type::kVariable && node->Name() == name;
  }

  inline bool IsVarNameEndsWith(ir::Node* node, const std::string& name) const {
106 107 108
    PADDLE_ENFORCE_NOT_NULL(node,
                            platform::errors::InvalidArgument(
                                "Input argument node cannot be nullptr."));
M
minqiyang 已提交
109 110

    return node->NodeType() == Node::Type::kVariable &&
111
           paddle::string::ends_with(node->Name(), name);
M
minqiyang 已提交
112 113 114
  }

  inline bool IsVarNameContains(ir::Node* node, const std::string& name) const {
115 116 117
    PADDLE_ENFORCE_NOT_NULL(node,
                            platform::errors::InvalidArgument(
                                "Input argument node cannot be nullptr."));
M
minqiyang 已提交
118 119 120 121 122 123

    return node->NodeType() == Node::Type::kVariable &&
           node->Name().find(name) != std::string::npos;
  }

  inline bool IsControlDepFrom(ir::Node* ctrl_dep_node, ir::Node* node) const {
124 125 126 127 128 129
    PADDLE_ENFORCE_NOT_NULL(
        ctrl_dep_node, platform::errors::InvalidArgument(
                           "Input argument ctrl_dep_node cannot be nullptr."));
    PADDLE_ENFORCE_NOT_NULL(node,
                            platform::errors::InvalidArgument(
                                "Input argument node cannot be nullptr."));
M
minqiyang 已提交
130 131 132 133 134 135 136 137 138 139

    return IsControlDepVar(*ctrl_dep_node) &&
           ctrl_dep_node->inputs.size() >= 1u &&
           ctrl_dep_node->inputs[0] == node;
  }
};

}  // namespace ir
}  // namespace framework
}  // namespace paddle