tracer.cc 10.4 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"
15
#include <map>
H
hong 已提交
16
#include <set>
M
minqiyang 已提交
17
#include <unordered_set>
18
#include <utility>
19
#include "paddle/fluid/framework/op_registry.h"
20
#include "paddle/fluid/imperative/amp_auto_cast.h"
21
#include "paddle/fluid/imperative/op_base.h"
22
#include "paddle/fluid/platform/denormal.h"
C
chengduo 已提交
23
#include "paddle/fluid/platform/profiler.h"
24 25
#include "paddle/fluid/string/string_helper.h"

26
DECLARE_bool(use_mkldnn);
27 28
DECLARE_string(tracer_mkldnn_ops_on);
DECLARE_string(tracer_mkldnn_ops_off);
29

30
namespace paddle {
M
minqiyang 已提交
31 32
namespace imperative {

Z
Zeng Jinle 已提交
33 34
thread_local bool Tracer::has_grad_ = true;

35 36 37 38 39 40 41 42 43
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;
}

44
void PassStopGradient(const NameVarBaseMap& outs, bool generate_grad) {
45 46 47 48 49 50 51 52 53 54 55 56
  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 "
57
              << generate_grad;
58
      var->InnerSetOverridedStopGradient(generate_grad);
59 60 61 62
    }
  }
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
void IncreaseVarbaseReferenceCountUntilCopyComplete(
    const std::shared_ptr<imperative::VarBase>& var,
    const platform::Place& place) {
  // Note(zhiqiu): Follow the logic of TensorCopy to determine the place that we
  // need to add callback, see tensor_utils.cc:245
  auto place_ = platform::is_gpu_place(place) ? place : var->Place();

  auto tracer = imperative::GetCurrentTracer();
  auto gc = tracer->MutableGarbageCollectorIfNotExists(place_);

  // Note(zhiqiu): This is an empty callback, the only way is to "reference"
  // var, so it will not be destructed until the kernels launched at current
  // stream of given place is finished.
  auto callback = [var, place_]() {
    VLOG(4) << "Run callback of var:" << var->Name() << " at place " << place_;
  };

  gc->DirectClearCallback(callback);
}

paddle::framework::GarbageCollector* Tracer::MutableGarbageCollectorIfNotExists(
    const platform::Place& place) {
  // if not exists, create a new GarbageCollector at given place
  if (gcs_.count(place) == 0) {
    std::unique_ptr<framework::GarbageCollector> gc;
    if (platform::is_gpu_place(place)) {
Z
zhulei 已提交
89
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
90 91 92 93 94 95 96 97 98 99
      gc.reset(new framework::DefaultStreamGarbageCollector(
          BOOST_GET_CONST(platform::CUDAPlace, place), 0));

      VLOG(10) << "Created GarbageCollector at " << place;
#else
      PADDLE_THROW(platform::errors::PermissionDenied(
          "Paddle can't use CUDA device since it's not compiled with CUDA,"
          "Please recompile or reinstall Paddle with GPU support."));
#endif
    } else if (platform::is_cuda_pinned_place(place)) {
Z
zhulei 已提交
100
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      gc.reset(new framework::CUDAPinnedGarbageCollector(
          BOOST_GET_CONST(platform::CUDAPinnedPlace, place), 0));

      VLOG(10) << "Created GarbageCollector at " << place;
#else
      PADDLE_THROW(platform::errors::PermissionDenied(
          "Paddle can't use CUDAPinned device since it's not compiled with "
          "CUDA,"
          "Please recompile or reinstall Paddle with GPU support."));
#endif
    } else if (platform::is_xpu_place(place)) {
#if defined(PADDLE_WITH_XPU)
      gc.reset(new framework::XPUGarbageCollector(
          BOOST_GET_CONST(platform::XPUPlace, place), 0));
      VLOG(10) << "Created GarbageCollector at " << place;
#else
      PADDLE_THROW(platform::errors::PermissionDenied(
          "Paddle can't use XPU device since it's not compiled with XPU,"
          "Please recompile or reinstall Paddle with XPU support."));
#endif
    } else if (platform::is_cpu_place(place)) {
      gc.reset(new framework::CPUGarbageCollector(
          BOOST_GET_CONST(platform::CPUPlace, place), 0));
      VLOG(10) << "Created GarbageCollector at " << place;
125 126 127 128 129 130 131 132 133 134 135
    } else if (platform::is_npu_place(place)) {
#if defined(PADDLE_WITH_ASCEND_CL)
      // TODO(zhiqiu): fix bugs and enable NPUDefaultStreamGarbageCollector.
      gc.reset(new framework::NPUUnsafeFastGarbageCollector(
          BOOST_GET_CONST(platform::NPUPlace, place), 0));
      VLOG(10) << "Created GarbageCollector at " << place;
#else
      PADDLE_THROW(platform::errors::PermissionDenied(
          "Paddle can't use NPU device since it's not compiled with NPU,"
          "Please recompile or reinstall Paddle with NPU support."));
#endif
136 137 138 139 140 141 142 143 144 145
    } else {
      PADDLE_THROW(platform::errors::PreconditionNotMet(
          "Unsupported place for garbage collection"));
    }
    gcs_.emplace(place, std::move(gc));
  }

  return gcs_.at(place).get();
}

J
Jiabin Yang 已提交
146 147
void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins,
                     const NameVarBaseMap& outs, framework::AttributeMap attrs,
148 149
                     const platform::Place& place, bool trace_backward,
                     const std::map<std::string, std::string>& inplace_map) {
150
  platform::RecordEvent op_type_record_event(type);
151
  platform::ScopedFlushDenormal flush;
J
Jiabin Yang 已提交
152
  VLOG(1) << "Trace Op: " << type;
153
  if (FLAGS_use_mkldnn) {
154 155 156 157 158 159 160 161 162 163 164
    // 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;
    }
165
  }
166 167 168 169
  auto op = framework::OpRegistry::CreateOp(type, {}, {}, {}, false);
  const auto& op_info = op->Info();
  auto* attr_checker = op_info.Checker();
  if (attr_checker) {
170
    attr_checker->Check(&attrs, true, /*only_check_exist_value=*/true);
171 172
  }

173 174 175 176 177
  static paddle::framework::AttributeMap empty_attrs_map = {};
  const paddle::framework::AttributeMap& default_attrs =
      attr_checker == nullptr ? empty_attrs_map
                              : attr_checker->GetDefaultAttrMap();

178
  NameVarBaseMap new_ins = ins;
179
  if (amp_level_ == 1) {
180 181
    VLOG(5) << "Auto mixed precision run operator: " << type;
    new_ins = AutoCastInputs(type, ins);
182 183 184
  } else if (amp_level_ == 2) {
    VLOG(5) << "Pure fp16 run operator: " << type;
    new_ins = CastPureFp16Inputs(type, ins);
185 186
  }

187
  try {
188 189 190 191 192 193 194 195 196 197 198 199 200 201
    if (platform::is_gpu_place(place)) {
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
      platform::SetDeviceId(BOOST_GET_CONST(platform::CUDAPlace, place).device);
#else
      PADDLE_THROW(platform::errors::PreconditionNotMet(
          "PaddlePaddle should compile with GPU if use CUDAPlace."));
#endif
    } else if (platform::is_xpu_place(place)) {
#ifdef PADDLE_WITH_XPU
      platform::SetXPUDeviceId(
          BOOST_GET_CONST(platform::XPUPlace, place).device);
#else
      PADDLE_THROW(platform::errors::PreconditionNotMet(
          "PaddlePaddle should compile with XPU if use XPUPlace."));
H
houj04 已提交
202 203 204 205 206 207 208 209
#endif
    } else if (platform::is_npu_place(place)) {
#ifdef PADDLE_WITH_ASCEND_CL
      platform::SetNPUDeviceId(
          BOOST_GET_CONST(platform::NPUPlace, place).device);
#else
      PADDLE_THROW(platform::errors::PreconditionNotMet(
          "PaddlePaddle should compile with NPU if use NPUPlace."));
210 211 212
#endif
    }

213
    OpBase::Run(*op, new_ins, outs, attrs, default_attrs, place);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  } 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 已提交
229

230 231
  if (enable_program_desc_tracing_) {
    VLOG(5) << "Trace op " << type << " into ProgramDesc";
232
    program_desc_tracer_->InsertOp(type, new_ins, outs, attrs);
233 234
  }

235
  if (ComputeRequiredGrad(new_ins, outs, trace_backward)) {
236 237
    CreateGradOpNode(*op, new_ins, outs, attrs, default_attrs, place,
                     inplace_map);
238 239
  } else {
    VLOG(3) << "No Grad to track for Op: " << type;
240
  }
M
minqiyang 已提交
241 242
}

243
void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins,
244 245 246 247
                     const NameVarBaseMap& outs, framework::AttributeMap attrs,
                     const std::map<std::string, std::string>& inplace_map) {
  TraceOp(type, ins, outs, std::move(attrs), expected_place_, has_grad_,
          inplace_map);
248 249
}

W
WangXi 已提交
250 251 252 253
void Tracer::SetExpectedPlace(platform::Place place) {
  expected_place_ = place;
}

J
Jiabin Yang 已提交
254
bool Tracer::ComputeRequiredGrad(const NameVarBaseMap& ins,
255
                                 const NameVarBaseMap& outs,
J
Jiabin Yang 已提交
256
                                 bool trace_backward) {
257 258 259 260 261 262 263 264 265 266 267 268 269
  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 已提交
270 271 272
}

}  // namespace imperative
273
}  // namespace paddle