tracer.cc 3.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"
19
#include "paddle/fluid/imperative/op_base.h"
C
chengduo 已提交
20
#include "paddle/fluid/platform/profiler.h"
21 22
#include "paddle/fluid/string/string_helper.h"

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

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

35 36 37 38 39 40 41 42 43 44
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 已提交
45 46 47 48
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;
49 50 51 52 53 54 55 56
  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 已提交
57

58 59
  if (enable_program_desc_tracing_) {
    VLOG(5) << "Trace op " << type << " into ProgramDesc";
60
    program_desc_tracer_->InsertOp(type, ins, outs, attrs);
61 62
  }

J
Jiabin Yang 已提交
63
  if (ComputeRequiredGrad(ins, outs, trace_backward)) {
64
    CreateGradOpNode(*op, ins, outs, attrs, place);
65 66
  } else {
    VLOG(3) << "No Grad to track for Op: " << type;
67
  }
M
minqiyang 已提交
68 69
}

70 71 72
void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins,
                     const NameVarBaseMap& outs,
                     framework::AttributeMap attrs) {
73
  TraceOp(type, ins, outs, std::move(attrs), expected_place_, no_grad_);
74 75
}

J
Jiabin Yang 已提交
76
bool Tracer::ComputeRequiredGrad(const NameVarBaseMap& ins,
77
                                 const NameVarBaseMap& outs,
J
Jiabin Yang 已提交
78
                                 bool trace_backward) {
79 80 81 82 83 84 85 86 87 88 89 90 91
  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 已提交
92 93 94
}

}  // namespace imperative
95
}  // namespace paddle