tracer.h 7.1 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 15 16
//
// 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.

#pragma once

J
Jiabin Yang 已提交
17 18
#include <atomic>
#include <future>  // NOLINT
19
#include <map>
J
Jiabin Yang 已提交
20
#include <memory>
21
#include <string>
22
#include <unordered_map>
23
#include <vector>
24

J
Jiabin Yang 已提交
25
#include "ThreadPool.h"
26
#include "paddle/fluid/framework/garbage_collector.h"
L
Leo Chen 已提交
27
#include "paddle/fluid/imperative/amp_auto_cast.h"
28
#include "paddle/fluid/imperative/basic_engine.h"
29
#include "paddle/fluid/imperative/jit/program_desc_tracer.h"
30
#include "paddle/fluid/imperative/layer.h"
J
Jiabin Yang 已提交
31
#include "paddle/fluid/platform/macros.h"
32
#include "paddle/phi/core/compat/arg_map_context.h"
33 34 35 36

namespace paddle {
namespace imperative {

L
Leo Chen 已提交
37 38
enum class AmpLevel;

39 40
enum class AmpDtype;

41 42 43 44
using GarbageCollectorMap =
    std::map<platform::Place,
             std::unique_ptr<paddle::framework::GarbageCollector>>;

45 46 47
class UniqueNameGenerator {
 public:
  explicit UniqueNameGenerator(std::string prefix = "") : prefix_(prefix) {}
48
  std::string Generate(std::string key = "dygraph_tmp") {
L
Leo Chen 已提交
49
    return prefix_ + key + "_" + std::to_string(id_++);
50 51 52 53 54 55 56
  }

 private:
  std::atomic<int> id_{0};
  std::string prefix_;
};

57
class Tracer {
J
Jiabin Yang 已提交
58 59
  DISABLE_COPY_AND_ASSIGN(Tracer);

60
 public:
61
  Tracer()
62
      : basic_engine_(new BasicEngine()),
63
        program_desc_tracer_(new jit::ProgramDescTracer()),
64 65 66
        generator_(new UniqueNameGenerator()) {
    expected_place_ = platform::CPUPlace();
  }
67

J
Jiabin Yang 已提交
68
  ~Tracer() = default;
69

J
Jiabin Yang 已提交
70 71 72 73 74 75
  template <typename VarType>
  void TraceOp(const std::string& type, const NameVarMap<VarType>& ins,
               const NameVarMap<VarType>& outs, framework::AttributeMap attrs,
               const platform::Place& place, bool trace_backward,
               const std::map<std::string, std::string>& inplace_map = {},
               paddle::framework::AttributeMap* passed_default_attrs_ = nullptr,
76
               bool use_default_attr_map = true);
J
Jiabin Yang 已提交
77

W
wanghuancoder 已提交
78 79 80 81 82 83 84 85 86 87
  template <typename VarType>
  void TraceOpImpl(
      const std::string& type, const NameVarMap<VarType>& ins,
      const NameVarMap<VarType>& outs,
      framework::AttributeMap& attrs,  // NOLINT
      const platform::Place& place, bool trace_backward,
      const std::map<std::string, std::string>& inplace_map = {},
      paddle::framework::AttributeMap* passed_default_attrs_ = nullptr,
      bool use_default_attr_map = true);

J
Jiabin Yang 已提交
88 89
  void TraceOp(const std::string& type, const NameVarBaseMap& ins,
               const NameVarBaseMap& outs, framework::AttributeMap attrs,
90
               const std::map<std::string, std::string>& inplace_map = {});
J
Jiabin Yang 已提交
91

J
Jiabin Yang 已提交
92
  void TraceOp(const std::string& type, const NameTensorMap& ins,
W
wanghuancoder 已提交
93 94
               const NameTensorMap& outs,
               paddle::framework::AttributeMap& attrs,  // NOLINT
J
Jiabin Yang 已提交
95 96 97
               const std::map<std::string, std::string>& inplace_map = {});

  void TraceOp(const std::string& type, const NameTensorMap& ins,
W
wanghuancoder 已提交
98 99 100 101 102 103
               const NameTensorMap& outs,
               paddle::framework::AttributeMap attrs);

  void TraceOp(const std::string& type, const NameTensorMap& ins,
               const NameTensorMap& outs,
               paddle::framework::AttributeMap& attrs,  // NOLINT
J
Jiabin Yang 已提交
104 105
               const paddle::platform::Place& place,
               paddle::framework::AttributeMap* default_attrs,
106
               bool use_default_attr_map,
107
               const std::map<std::string, std::string>& inplace_map = {});
108

109 110
  bool ComputeRequiredGrad(const NameVarBaseMap& ins,
                           const NameVarBaseMap& outs, bool trace_backward);
J
Jiabin Yang 已提交
111 112
  bool ComputeRequiredGrad(const NameTensorMap& ins, const NameTensorMap& outs,
                           bool trace_backward);
J
Jiabin Yang 已提交
113

114 115 116 117 118 119 120 121 122 123 124 125
  void SetEnableProgramDescTracing(bool enabled) {
    enable_program_desc_tracing_ = enabled;
  }

  bool IsProgramDescTracingEnabled() const {
    return enable_program_desc_tracing_;
  }

  jit::ProgramDescTracer* GetProgramDescTracer() {
    return program_desc_tracer_.get();
  }

126 127 128 129 130 131 132
  // Note(Aurelius84): The `tmp` is used as prefix key while naming a temporary
  // intermediate var both in imperative and static mode. But the
  // `UniqueNameGenerator` in C++ and `unique_name.py` in Python doesn't share
  // the same auto-increment id. It will create a variable repeatedly with same
  // name like `tmp_0` in some cases when transform dygraph into static layers.
  // So we modify the default prefix key into `eager_tmp` to distinguish with
  // static graph.
133
  std::string GenerateUniqueName(std::string key = "dygraph_tmp") {
134 135 136
    return generator_->Generate(key);
  }

137 138
  BasicEngine* GetEngine() const { return basic_engine_.get(); }

139 140
  platform::Place ExpectedPlace() const { return expected_place_; }

W
WangXi 已提交
141
  void SetExpectedPlace(platform::Place place);
142

143
  bool HasGrad() const { return has_grad_; }
144

145
  void SetHasGrad(bool has_grad) { has_grad_ = has_grad; }
146

L
Leo Chen 已提交
147 148 149 150
  void SetAmpLevel(AmpLevel level) {
    VLOG(4) << "set amp_level to " << static_cast<unsigned int>(level);
    amp_level_ = level;
  }
151

L
Leo Chen 已提交
152
  AmpLevel GetAmpLevel() const { return amp_level_; }
153

154 155 156
  void SetAmpDtype(std::string amp_dtype) {
    VLOG(4) << "set amp_dtype to " << amp_dtype;
    if (amp_dtype == "float16") {
157
      amp_dtype_ = phi::DataType::FLOAT16;
158
    } else if (amp_dtype == "bfloat16") {
159
      amp_dtype_ = phi::DataType::BFLOAT16;
160
    } else {
161
      amp_dtype_ = phi::DataType::FLOAT32;
162 163 164 165
    }
  }

  std::string GetAmpDtype() const {
166
    if (amp_dtype_ == phi::DataType::FLOAT16) {
167
      return std::string("float16");
168
    } else if (amp_dtype_ == phi::DataType::BFLOAT16) {
169 170 171 172 173 174
      return std::string("bfloat16");
    } else {
      return std::string("float32");
    }
  }

175
  phi::KernelSignature GetExpectedKernelSignature(
176 177
      const std::string& type, const NameTensorMap& ins,
      const NameTensorMap& outs, framework::AttributeMap attrs) const;
178

179 180 181
  paddle::framework::GarbageCollector* MutableGarbageCollectorIfNotExists(
      const platform::Place& place);

182
 private:
183
  std::unique_ptr<BasicEngine> basic_engine_;
184
  std::unique_ptr<jit::ProgramDescTracer> program_desc_tracer_;
185
  std::unique_ptr<UniqueNameGenerator> generator_;
186
  platform::Place expected_place_;
187
  GarbageCollectorMap gcs_;
188 189

  static thread_local bool enable_program_desc_tracing_;
Z
Zeng Jinle 已提交
190
  static thread_local bool has_grad_;
191
  static thread_local AmpLevel amp_level_;
192
  static thread_local phi::DataType amp_dtype_;
193 194
};

195 196 197
// To access static variable current_tracer
const std::shared_ptr<Tracer>& GetCurrentTracer();
void SetCurrentTracer(const std::shared_ptr<Tracer>& tracer_);
198 199 200
void IncreaseVarbaseReferenceCountUntilCopyComplete(
    const std::shared_ptr<imperative::VarBase>& var,
    const platform::Place& place);
201

202 203
void PassStopGradient(const NameVarBaseMap& outs, bool generate_grad);

204 205
}  // namespace imperative
}  // namespace paddle