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

24
DECLARE_bool(use_mkldnn);
25 26
DECLARE_string(tracer_mkldnn_ops_on);
DECLARE_string(tracer_mkldnn_ops_off);
27

28
namespace paddle {
M
minqiyang 已提交
29 30
namespace imperative {

31 32 33 34 35 36 37 38 39
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;
}

40
static void PassStopGradient(const NameVarBaseMap& outs, bool generate_grad) {
41 42 43 44 45 46 47 48 49 50 51 52
  for (const auto& pair : outs) {
    for (const auto& var : pair.second) {
      // NOTE(zhiqiu): this happends when None output are passed from python
      // side. For example, fake_quantize_dequantize_moving_average_abs_max may
      // pass None OutAccum in eval mode.
      // It can be refined by generate several different pybind interface for
      // one operator with different function signature.
      if (var == nullptr) {
        VLOG(4) << pair.first << " is NULL";
        continue;
      }
      VLOG(6) << "Set output: " << var->Name() << "'s OverridedStopGradient as "
53
              << generate_grad;
54
      var->InnerSetOverridedStopGradient(generate_grad);
55 56 57 58
    }
  }
}

J
Jiabin Yang 已提交
59 60 61 62
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;
63
  if (FLAGS_use_mkldnn) {
64 65 66 67 68 69 70 71 72 73 74
    // if both lists are empty all ops are enabled (default for
    // FLAGS_use_mkldnn=1)
    // if ops_on list is not empty only ops from that list are enabled
    if (!FLAGS_tracer_mkldnn_ops_on.empty()) {
      auto is_on = FLAGS_tracer_mkldnn_ops_on.find(type) != std::string::npos;
      attrs["use_mkldnn"] = is_on;
    } else {
      // if ops_on list is empty all ops are enabled except types from off_list
      auto is_off = FLAGS_tracer_mkldnn_ops_off.find(type) != std::string::npos;
      attrs["use_mkldnn"] = !is_off;
    }
75
  }
76 77 78 79 80 81 82
  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);
  }

83 84 85 86 87 88
  NameVarBaseMap new_ins = ins;
  if (enable_autocast_) {
    VLOG(5) << "Auto mixed precision run operator: " << type;
    new_ins = AutoCastInputs(type, ins);
  }

89
  try {
90
    OpBase::Run(*op, new_ins, outs, attrs, place);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  } catch (platform::EnforceNotMet& exception) {
    framework::AppendErrorOpHint(type, &exception);
    throw std::move(exception);
  } catch (std::exception& ex) {
    PADDLE_THROW(platform::errors::Fatal(
        "Operator %s raises an %s exception.\n"
        "The exception content is\n:%s.",
        type, platform::demangle(typeid(ex).name()), ex.what()));
  } catch (...) {
    // NOTE: this branch represents a very serious bug with
    // low probability of occurrence, and we can't get its
    // exception content here.
    PADDLE_THROW(platform::errors::Fatal(
        "Operator %s raises an unknown exception.", type));
  }
J
Jiabin Yang 已提交
106

107 108
  if (enable_program_desc_tracing_) {
    VLOG(5) << "Trace op " << type << " into ProgramDesc";
109
    program_desc_tracer_->InsertOp(type, new_ins, outs, attrs);
110 111
  }

112 113
  if (ComputeRequiredGrad(new_ins, outs, trace_backward)) {
    CreateGradOpNode(*op, new_ins, outs, attrs, place);
114 115
  } else {
    VLOG(3) << "No Grad to track for Op: " << type;
116
  }
M
minqiyang 已提交
117 118
}

119 120 121
void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins,
                     const NameVarBaseMap& outs,
                     framework::AttributeMap attrs) {
122
  TraceOp(type, ins, outs, std::move(attrs), expected_place_, has_grad_);
123 124
}

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

}  // namespace imperative
144
}  // namespace paddle