tracer.cc 5.3 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>
18
#include "paddle/fluid/framework/op_registry.h"
C
chengduo 已提交
19
#include "paddle/fluid/platform/profiler.h"
20 21
#include "paddle/fluid/string/string_helper.h"

22
namespace paddle {
M
minqiyang 已提交
23 24
namespace imperative {

25 26 27 28 29 30 31 32 33
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;
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
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
54
      auto new_var = new VariableWrapper(each_var->Name());
55 56 57 58
      auto* new_tensor =
          new_var->MutableVar()->GetMutable<framework::LoDTensor>();
      auto& old_tensor = var.Get<framework::LoDTensor>();
      new_tensor->Resize(old_tensor.dims());
59
      new_tensor->set_lod(old_tensor.lod());
60
      each_var.reset(new_var);
61
      op->AddAllowedEmptyVar(new_var);
62 63 64 65
    }
  }
}

66 67 68 69 70 71
static std::vector<std::shared_ptr<OpBase>> CreateGradOpBases(
    const framework::OpInfo& info, const std::string& type,
    const NameVarBaseMap& in, const NameVarBaseMap& out,
    const framework::AttributeMap& attrs) {
  if (info.dygraph_grad_op_maker_) {
    return info.dygraph_grad_op_maker_(type, in, out, attrs);
J
Jiabin Yang 已提交
72 73
  } else {
    return {};
X
Xin Pan 已提交
74
  }
M
minqiyang 已提交
75 76
}

77 78 79 80 81 82 83 84 85 86
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 已提交
87 88 89 90
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;
91 92 93 94 95 96 97 98
  auto op = framework::OpRegistry::CreateOp(type, {}, {}, {}, false);
  const auto& op_info = op->Info();
  auto* attr_checker = op_info.Checker();
  if (attr_checker) {
    attr_checker->Check(&attrs, true);
  }

  OpBase::Run(*op, ins, outs, attrs, place);
J
Jiabin Yang 已提交
99

100 101
  if (enable_program_desc_tracing_) {
    VLOG(5) << "Trace op " << type << " into ProgramDesc";
102
    program_desc_tracer_->InsertOp(type, ins, outs, attrs);
103 104
  }

J
Jiabin Yang 已提交
105
  if (ComputeRequiredGrad(ins, outs, trace_backward)) {
106
    TraceBackward(op_info, type, ins, outs, attrs, place);
107 108
  } else {
    VLOG(3) << "No Grad to track for Op: " << type;
109
  }
M
minqiyang 已提交
110 111
}

112 113 114
void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins,
                     const NameVarBaseMap& outs,
                     framework::AttributeMap attrs) {
115
  TraceOp(type, ins, outs, std::move(attrs), expected_place_, no_grad_);
116 117
}

J
Jiabin Yang 已提交
118
bool Tracer::ComputeRequiredGrad(const NameVarBaseMap& ins,
119
                                 const NameVarBaseMap& outs,
J
Jiabin Yang 已提交
120
                                 bool trace_backward) {
121 122 123 124 125 126 127 128 129 130 131 132 133
  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 已提交
134 135
}

136 137 138 139 140 141 142 143 144 145 146 147
void Tracer::TraceBackward(const framework::OpInfo& info,
                           const std::string& type, const NameVarBaseMap& ins,
                           const NameVarBaseMap& outs,
                           const framework::AttributeMap& attrs,
                           const platform::Place& place) {
  auto grad_op_bases = CreateGradOpBases(info, type, ins, outs, attrs);
  auto grad_op_num = grad_op_bases.size();
  if (grad_op_num == 0) return;

  size_t trace_id = GenerateUniqueId();
  for (auto& grad_op : grad_op_bases) {
    grad_op->SetPlace(place);
H
hong 已提交
148
    grad_op->SetId(trace_id);
149
    ClearNoNeedBufferInputs(grad_op.get());
M
minqiyang 已提交
150 151
  }
}
J
Jiabin Yang 已提交
152

M
minqiyang 已提交
153
}  // namespace imperative
154
}  // namespace paddle