tracer.cc 10.3 KB
Newer Older
1
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
X
xiexionghang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//
// 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.
#include "paddle/fluid/imperative/tracer.h"
#include <unordered_set>
#include <utility>
#include "paddle/fluid/platform/profiler.h"

namespace paddle {
namespace imperative {

22 23 24 25 26 27 28 29 30 31
static std::vector<std::unique_ptr<framework::OpDesc>> CreateGradOpDescs(
    const framework::OpInfo& op_info, const framework::OpDesc& op_desc,
    const std::unordered_set<std::string>& no_grad_set,
    const std::vector<framework::BlockDesc*>& grad_sub_block,
    std::unordered_map<std::string, std::string>* grad_to_var) {
  if (op_info.grad_op_maker_) {
    return op_info.grad_op_maker_(op_desc, no_grad_set, grad_to_var,
                                  grad_sub_block);
  } else {
    return {};
X
xiexionghang 已提交
32 33 34
  }
}

35 36 37 38 39 40
static void PassStopGradient(const NameVarBaseMap& outs, bool generate_grad) {
  for (const auto& name_pair : outs) {
    for (const auto& vb : name_pair.second) {
      VLOG(6) << "Set output: " << vb->Name() << "'s OverridedStopGradient as "
              << generate_grad;
      vb->InnerSetOverridedStopGradient(generate_grad);
X
xiexionghang 已提交
41 42 43 44
    }
  }
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins,
                     const NameVarBaseMap& outs, framework::AttributeMap attrs,
                     const platform::Place& place, bool trace_backward) {
  platform::RecordEvent event(type);
  VLOG(1) << "Trace Op: " << type;
  size_t op_id = GenerateUniqueId();
  auto op = OpBase::Create(op_id, type, ins, outs, std::move(attrs), place);
  op->Run(ins, outs);

  if (ComputeRequiredGrad(ins, outs, trace_backward)) {
    TraceBackward(op, framework::OpDesc(op->Type(), op->InputNameMap(),
                                        op->OutputNameMap(), op->Attrs()),
                  ins, outs);
  } else {
    VLOG(3) << "No Grad to track for Op: " << type;
X
xiexionghang 已提交
60 61 62
  }
}

63 64 65 66 67 68 69 70 71 72 73 74
bool Tracer::ComputeRequiredGrad(const NameVarBaseMap& ins,
                                 const NameVarBaseMap& outs,
                                 bool trace_backward) {
  if (!trace_backward) return false;

  for (const auto& name_pair : ins) {
    for (const auto& var_base : name_pair.second) {
      if (!var_base->OverridedStopGradient()) {
        VLOG(6) << "Find out input: " << var_base->Name()
                << "'s GeneratedGrad is True";
        PassStopGradient(outs, var_base->OverridedStopGradient());
        return true;
X
xiexionghang 已提交
75 76 77
      }
    }
  }
78
  return false;
X
xiexionghang 已提交
79 80
}

81 82 83 84 85 86 87
void Tracer::TraceBackward(const std::shared_ptr<OpBase>& fwd_op,
                           const framework::OpDesc& fwd_op_desc,
                           const NameVarBaseMap& ins,
                           const NameVarBaseMap& outs) {
  // grad_to_var is a map of framework::GradVarName(in_var_name/out_var_name) ->
  // in_var_name/out_var_name
  std::unordered_map<std::string, std::string> grad_to_var;
X
xiexionghang 已提交
88

89 90 91
  // Get grad_op_desc using fwd_op_desc
  std::vector<std::unique_ptr<framework::OpDesc>> grad_op_descs_ =
      CreateGradOpDescs(fwd_op->Info(), fwd_op_desc, {}, {}, &grad_to_var);
X
xiexionghang 已提交
92

93
  // Create grad_ops using grad_op_descs
X
xiexionghang 已提交
94

95
  size_t grad_op_num = grad_op_descs_.size();
X
xiexionghang 已提交
96

97 98
  VLOG(3) << "Create " << grad_op_num << " grad op desc(s) to op "
          << fwd_op->Type();
X
xiexionghang 已提交
99

100 101
  if (grad_op_num == 0) {
    return;
X
xiexionghang 已提交
102
  }
103 104 105 106 107 108 109 110 111 112 113
  // Build a map to record var_name -> std::shared_ptr<VarBase>*,
  // so that we can find suitable var in grad op descs
  std::unordered_map<std::string, const std::shared_ptr<VarBase>*> name_to_var;
  for (auto& pair : ins) {
    for (auto& var : pair.second) {
      auto& var_ptr = name_to_var[var->Name()];
      PADDLE_ENFORCE_EQ(var_ptr == nullptr || var_ptr->get() == var.get(), true,
                        "There are different variables with same name %s",
                        var->Name());
      var_ptr = &var;
    }
X
xiexionghang 已提交
114 115
  }

116 117 118 119 120 121 122 123
  for (auto& pair : outs) {
    for (auto& var : pair.second) {
      auto& var_ptr = name_to_var[var->Name()];
      PADDLE_ENFORCE_EQ(var_ptr == nullptr || var_ptr->get() == var.get(), true,
                        "There are different variables with same name %s",
                        var->Name());
      var_ptr = &var;
    }
X
xiexionghang 已提交
124 125
  }

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  // Build backward ins and outs

  for (size_t i = 0; i < grad_op_num; i++) {
    // Step1: build grad op and add them to engine

    // Use trace id to decide the order of gradient sum in sorted sum mode
    size_t trace_id = fwd_op->id();
    std::shared_ptr<OpBase> grad_op =
        OpBase::Create(trace_id, (*(grad_op_descs_[i].get())), fwd_op->place());

    // this OpBase* is just used to manage op's life time
    engine_->InsertOp(grad_op.get(), grad_op);

    std::unordered_set<OpBase*> visited_preceding_ops;
    // Step2 : prepare grad_in vars and bind them with grad_op,
    // set inputs' grad_op as current grad_op
    for (const auto& grad_ins : grad_op_descs_[i]->Inputs()) {
      if (grad_ins.second.empty()) continue;
      auto& bwd_in = (*grad_op->GetMutableInsMap())[grad_ins.first];
      bwd_in.reserve(grad_ins.second.size());

      for (auto& grad_in_var_name : grad_ins.second) {
        auto iter = grad_to_var.find(grad_in_var_name);

        if (iter != grad_to_var.end()) {
          // If it is a grad var, find its coresponding forward var
          auto& fwd_var_name = iter->second;
          auto fwd_var_iter = name_to_var.find(fwd_var_name);
          PADDLE_ENFORCE_EQ(fwd_var_iter != name_to_var.end(), true,
                            "Cannot find forward variable named %s",
                            fwd_var_name);
          const auto& tmp = (*(fwd_var_iter->second))->GradVarBase();
          PADDLE_ENFORCE_NOT_NULL(
              tmp.get(),
              "Grad of %s should "
              "not be NULL when we Track_Backward Input of %s",
              (*(fwd_var_iter->second))->Name(), grad_op->Type());
          // Create grad_in's dim in tensor for Grad Dependency compute
          auto* tensor = tmp->MutableVar()->GetMutable<framework::LoDTensor>();
          tensor->Resize((*(fwd_var_iter->second))
                             ->Var()
                             .Get<framework::LoDTensor>()
                             .dims());
          // Add Grad Op for grad_in
          tmp->AddGradOps(grad_op);
          VLOG(3) << "Add Grad Op " << grad_op->Type() << " for :"
                  << (*(fwd_var_iter->second))->GradVarBase()->Name();
          // Add Grad var input to engine set
          engine_->InsertGradVar(tmp.get());
          VLOG(3) << "Add Grad: " << tmp->Name() << " in to Engine";
          bwd_in.emplace_back((*(fwd_var_iter->second))->GradVarBase());
        } else {
          // If it is a forward var, just add it
          auto fwd_var_iter = name_to_var.find(grad_in_var_name);
          PADDLE_ENFORCE_EQ(fwd_var_iter != name_to_var.end(), true,
                            "Cannot find forward variable named %s",
                            grad_in_var_name);
          bwd_in.emplace_back(*(fwd_var_iter->second));
X
xiexionghang 已提交
184
        }
185 186 187
        VLOG(3) << "Set backward input from fwd var" << grad_ins.first << " of "
                << grad_op->Type() << " to be "
                << (bwd_in.back() ? bwd_in.back()->Name() : "nullptr");
X
xiexionghang 已提交
188
      }
189
    }
X
xiexionghang 已提交
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    // Step3: prepare grad_out vars and using their grad_ops to set current
    // grad_op's preceding op
    for (auto& grad_outs : grad_op_descs_[i]->Outputs()) {
      if (grad_outs.second.empty()) continue;
      auto& bwd_out = (*grad_op->GetMutableOutsMap())[grad_outs.first];
      bwd_out.reserve(grad_outs.second.size());

      for (auto& grad_out_var_name : grad_outs.second) {
        auto iter = grad_to_var.find(grad_out_var_name);
        PADDLE_ENFORCE_EQ(iter != grad_to_var.end(), true,
                          "Cannot find output of input grad %s in op %s",
                          grad_out_var_name, fwd_op->Type());
        auto fwd_var_iter = name_to_var.find(iter->second);
        PADDLE_ENFORCE_EQ(fwd_var_iter != name_to_var.end(), true,
                          "Cannot find forward variable named %s",
                          iter->second);
        const auto& tmp = (*(fwd_var_iter->second))->GradVarBase();

        PADDLE_ENFORCE_NOT_NULL(tmp.get(),
                                "Grad output: %s of op: %s should not be NULL",
                                (tmp->Name(), grad_op->Type()));

        if ((!tmp->OverridedStopGradient()) || (grad_outs.second.size() > 1)) {
          VLOG(3) << "Set backward output " << grad_outs.first << " of "
                  << grad_op->Type() << " to be " << tmp->Name()
                  << ". Its Overrided Stop_Gradient is: False";
          bwd_out.emplace_back(tmp);
          auto grad_pending_ops =
              (*(fwd_var_iter->second))->GradVarBase()->GradOps();
          if (VLOG_IS_ON(3) && !grad_pending_ops.empty()) {
            VLOG(3) << "Add grad_pending Op of :"
                    << (*(fwd_var_iter->second))->GradVarBase()->Name()
                    << " It's grad_pending Op are: ";
            for (const auto& op : grad_pending_ops) {
              VLOG(3) << op->Type();
            }
          }
          if (!grad_pending_ops.empty()) {
            for (const auto& op : grad_pending_ops) {
              PADDLE_ENFORCE_NOT_NULL(op,
                                      "No nullptr should be grad_pending op");
              if (visited_preceding_ops.count(op) == 0) {
                visited_preceding_ops.insert(op);
                grad_op->InsertGradPendingOps(op);
              }
            }
          } else {
            VLOG(5) << "Hit leaf VarBase"
                    << (*(fwd_var_iter->second))->GradVarBase()->Name();
          }
        } else {
          VLOG(3) << "Skip backward output " << grad_outs.first << " of "
                  << grad_op->Type() << " Named: " << tmp->Name()
                  << ", since its Overrided Stop_Gradient is: True";
X
xiexionghang 已提交
245 246 247
        }
      }
    }
248 249
    // To ensure numeric stability as static graph
    grad_op->SortGradPendingOps();
X
xiexionghang 已提交
250 251
  }
}
252

X
xiexionghang 已提交
253 254
}  // namespace imperative
}  // namespace paddle