tracer.cc 7.2 KB
Newer Older
J
Jiabin Yang 已提交
1
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// 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"
H
hong 已提交
15
#include <set>
M
minqiyang 已提交
16
#include <unordered_set>
17
#include <utility>
C
chengduo 已提交
18
#include "paddle/fluid/platform/profiler.h"
19
namespace paddle {
M
minqiyang 已提交
20 21
namespace imperative {

22 23 24 25 26 27 28 29 30
static std::shared_ptr<Tracer> g_current_tracer(nullptr);

const std::shared_ptr<Tracer>& GetCurrentTracer() { return g_current_tracer; }

void SetCurrentTracer(const std::shared_ptr<Tracer>& tracer) {
  g_current_tracer = tracer;
  VLOG(6) << "Set current tracer: " << g_current_tracer;
}

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
static void ClearNoNeedBufferInputs(OpBase* op) {
  auto& inferer = op->Info().NoNeedBufferVarsInferer();
  if (!inferer) return;
  auto* ins = op->GetMutableInsMap();
  const auto& no_need_buffer_slots =
      inferer(*ins, op->GetOutsMap(), op->Attrs());
  if (no_need_buffer_slots.empty()) return;

  for (auto& slot : no_need_buffer_slots) {
    auto iter = ins->find(slot);
    if (iter == ins->end()) continue;
    VLOG(2) << "Clear data buffer of " << slot << " in " << op->Type();

    for (auto& each_var : iter->second) {
      if (!each_var) continue;

      auto& var = each_var->Var();
      PADDLE_ENFORCE_EQ(var.IsType<framework::LoDTensor>(), true,
                        "Only support LoDTensor");
      // TODO(zjl): support higher order derivatives
      auto new_var = new VarBase(false, each_var->Name());
      auto* new_tensor =
          new_var->MutableVar()->GetMutable<framework::LoDTensor>();
      auto& old_tensor = var.Get<framework::LoDTensor>();
      new_tensor->Resize(old_tensor.dims());
56
      new_tensor->set_lod(old_tensor.lod());
57 58 59 60 61
      each_var.reset(new_var);
    }
  }
}

H
hong 已提交
62 63 64 65 66
static std::vector<std::unique_ptr<OpBase>> CreateGradOpBases(
    const OpBase* fw_op_base, const NameVarBaseMap& in,
    const NameVarBaseMap& out) {
  if (fw_op_base->Info().dygraph_grad_op_maker_) {
    return fw_op_base->Info().dygraph_grad_op_maker_(fw_op_base, in, out);
J
Jiabin Yang 已提交
67 68
  } else {
    return {};
X
Xin Pan 已提交
69
  }
M
minqiyang 已提交
70 71
}

72 73 74 75 76 77 78 79 80 81
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);
    }
  }
}

J
Jiabin Yang 已提交
82 83 84 85 86
void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins,
                     const NameVarBaseMap& outs, framework::AttributeMap attrs,
                     const platform::Place& place, bool trace_backward) {
  VLOG(1) << "Trace Op: " << type;
  size_t op_id = GenerateUniqueId();
H
hong 已提交
87
  auto op = OpBase::Create(op_id, type, ins, outs, attrs, place);
J
Jiabin Yang 已提交
88 89
  op->Run(ins, outs);

90 91 92 93 94
  if (enable_program_desc_tracing_) {
    VLOG(5) << "Trace op " << type << " into ProgramDesc";
    program_desc_tracer_->InsertOp(type, ins, outs, op->Attrs());
  }

J
Jiabin Yang 已提交
95
  if (ComputeRequiredGrad(ins, outs, trace_backward)) {
H
hong 已提交
96
    TraceBackward(op, ins, outs);
97 98
  } else {
    VLOG(3) << "No Grad to track for Op: " << type;
99
  }
M
minqiyang 已提交
100 101
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins,
                     const NameVarBaseMap& outs,
                     framework::AttributeMap attrs) {
  VLOG(1) << "Trace Op: " << type;
  size_t op_id = GenerateUniqueId();
  auto op =
      OpBase::Create(op_id, type, ins, outs, std::move(attrs), expected_place_);
  op->Run(ins, outs);

  if (enable_program_desc_tracing_) {
    VLOG(5) << "Trace op " << type << " into ProgramDesc";
    program_desc_tracer_->InsertOp(type, ins, outs, op->Attrs());
  }

  if (ComputeRequiredGrad(ins, outs, no_grad_)) {
    TraceBackward(op, ins, outs);
  } else {
    VLOG(3) << "No Grad to track for Op: " << type;
  }
}

J
Jiabin Yang 已提交
123
bool Tracer::ComputeRequiredGrad(const NameVarBaseMap& ins,
124
                                 const NameVarBaseMap& outs,
J
Jiabin Yang 已提交
125
                                 bool trace_backward) {
126 127 128 129 130 131 132 133 134 135 136 137 138
  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;
      }
    }
  }
  return false;
M
minqiyang 已提交
139 140
}

J
Jiabin Yang 已提交
141 142 143 144 145 146
void Tracer::TraceBackward(const std::shared_ptr<OpBase>& fwd_op,
                           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;
147

J
Jiabin Yang 已提交
148
  // Get grad_op_desc using fwd_op_desc
H
hong 已提交
149 150
  std::vector<std::unique_ptr<OpBase>> grad_op_bases_ =
      CreateGradOpBases(fwd_op.get(), ins, outs);
151

H
hong 已提交
152
  size_t grad_op_num = grad_op_bases_.size();
153

H
hong 已提交
154 155 156 157
  std::set<VarBase*> set_input_vars;
  for (auto& fwd_in_it : ins) {
    for (auto& var_base_it : fwd_in_it.second) {
      set_input_vars.insert(var_base_it.get());
M
minqiyang 已提交
158 159 160
    }
  }

H
hong 已提交
161 162 163
  for (auto& fwd_out_it : outs) {
    for (auto& var_base_it : fwd_out_it.second) {
      set_input_vars.insert(var_base_it.get());
M
minqiyang 已提交
164 165 166
    }
  }

H
hong 已提交
167
  for (size_t i = 0; i < grad_op_num; ++i) {
J
Jiabin Yang 已提交
168 169
    size_t trace_id = fwd_op->id();

H
hong 已提交
170 171 172 173 174 175 176 177 178 179 180 181
    std::shared_ptr<OpBase> grad_op = std::move(grad_op_bases_[i]);
    grad_op->SetId(trace_id);
    grad_op->SetPlace(fwd_op->place());
    grad_op->CreateOperatorBase();

    auto& grad_in = *(grad_op->GetMutableInsMap());
    auto& grad_out = *(grad_op->GetMutableOutsMap());
    for (auto& grad_in_it : grad_in) {
      for (auto& var_base_it : grad_in_it.second) {
        if (set_input_vars.count(var_base_it.get()) == 0) {
          var_base_it->AddGradOps(grad_op);
          engine_->InsertGradVar(var_base_it.get());
J
Jiabin Yang 已提交
182 183 184
        }
      }
    }
185

186
    std::set<OpBase*> visited_preceding_ops;
H
hong 已提交
187 188 189 190 191 192 193 194 195
    for (auto& grad_out_it : grad_out) {
      bool flag_clear_list = false;
      for (auto& var_base_it : grad_out_it.second) {
        if ((!var_base_it->OverridedStopGradient()) ||
            (grad_out_it.second.size() > 1)) {
          auto preceding_ops = var_base_it->GradOps();
          if (!preceding_ops.empty()) {
            for (const auto& op : preceding_ops) {
              visited_preceding_ops.insert(op);
J
Jiabin Yang 已提交
196 197 198
            }
          }
        } else {
H
hong 已提交
199
          flag_clear_list = true;
M
minqiyang 已提交
200 201
        }
      }
H
hong 已提交
202 203 204
      if (flag_clear_list) {
        grad_out_it.second.clear();
      }
M
minqiyang 已提交
205
    }
H
hong 已提交
206 207 208 209 210 211 212
    std::vector<OpBase*> vec_preceding_ops(visited_preceding_ops.begin(),
                                           visited_preceding_ops.end());

    grad_op->SetGradPendingOps(std::move(vec_preceding_ops));

    // this OpBase* is just used to manage op's life time
    engine_->InsertOp(grad_op.get(), grad_op);
213
    ClearNoNeedBufferInputs(grad_op.get());
M
minqiyang 已提交
214 215
  }
}
J
Jiabin Yang 已提交
216

M
minqiyang 已提交
217
}  // namespace imperative
218
}  // namespace paddle