tracer.h 4.5 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>
J
Jiabin Yang 已提交
24
#include "ThreadPool.h"
25
#include "paddle/fluid/framework/garbage_collector.h"
26
#include "paddle/fluid/imperative/basic_engine.h"
27
#include "paddle/fluid/imperative/jit/program_desc_tracer.h"
28
#include "paddle/fluid/imperative/layer.h"
J
Jiabin Yang 已提交
29
#include "paddle/fluid/platform/macros.h"
30 31 32 33

namespace paddle {
namespace imperative {

34 35 36 37
using GarbageCollectorMap =
    std::map<platform::Place,
             std::unique_ptr<paddle::framework::GarbageCollector>>;

38 39 40
class UniqueNameGenerator {
 public:
  explicit UniqueNameGenerator(std::string prefix = "") : prefix_(prefix) {}
41
  std::string Generate(std::string key = "dygraph_tmp") {
L
Leo Chen 已提交
42
    return prefix_ + key + "_" + std::to_string(id_++);
43 44 45 46 47 48 49
  }

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

50
class Tracer {
J
Jiabin Yang 已提交
51 52
  DISABLE_COPY_AND_ASSIGN(Tracer);

53
 public:
54
  Tracer()
55
      : basic_engine_(new BasicEngine()),
56
        program_desc_tracer_(new jit::ProgramDescTracer()),
57 58 59
        generator_(new UniqueNameGenerator()) {
    expected_place_ = platform::CPUPlace();
  }
60

J
Jiabin Yang 已提交
61
  ~Tracer() = default;
62

J
Jiabin Yang 已提交
63 64
  void TraceOp(const std::string& type, const NameVarBaseMap& ins,
               const NameVarBaseMap& outs, framework::AttributeMap attrs,
65 66
               const platform::Place& place, bool trace_bacward,
               const std::map<std::string, std::string>& inplace_map = {});
J
Jiabin Yang 已提交
67

68
  void TraceOp(const std::string& type, const NameVarBaseMap& ins,
69 70
               const NameVarBaseMap& outs, framework::AttributeMap attrs,
               const std::map<std::string, std::string>& inplace_map = {});
71

72 73
  bool ComputeRequiredGrad(const NameVarBaseMap& ins,
                           const NameVarBaseMap& outs, bool trace_backward);
J
Jiabin Yang 已提交
74

75 76 77 78 79 80 81 82 83 84 85 86
  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();
  }

87 88 89 90 91 92 93
  // 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.
94
  std::string GenerateUniqueName(std::string key = "dygraph_tmp") {
95 96 97
    return generator_->Generate(key);
  }

98 99
  BasicEngine* GetEngine() const { return basic_engine_.get(); }

100 101
  platform::Place ExpectedPlace() const { return expected_place_; }

W
WangXi 已提交
102
  void SetExpectedPlace(platform::Place place);
103

104
  bool HasGrad() const { return has_grad_; }
105

106
  void SetHasGrad(bool has_grad) { has_grad_ = has_grad; }
107

108 109 110 111
  void SetEnableAutoCast(bool enabled) { enable_autocast_ = enabled; }

  bool IsAutoCastEnabled() const { return enable_autocast_; }

112 113 114
  paddle::framework::GarbageCollector* MutableGarbageCollectorIfNotExists(
      const platform::Place& place);

115
 private:
116
  std::unique_ptr<BasicEngine> basic_engine_;
117 118
  std::unique_ptr<jit::ProgramDescTracer> program_desc_tracer_;
  bool enable_program_desc_tracing_{false};
119
  std::unique_ptr<UniqueNameGenerator> generator_;
120
  platform::Place expected_place_;
121
  bool has_grad_{true};
122
  bool enable_autocast_{false};
123
  GarbageCollectorMap gcs_;
124 125
};

126 127 128
// To access static variable current_tracer
const std::shared_ptr<Tracer>& GetCurrentTracer();
void SetCurrentTracer(const std::shared_ptr<Tracer>& tracer_);
129 130 131
void IncreaseVarbaseReferenceCountUntilCopyComplete(
    const std::shared_ptr<imperative::VarBase>& var,
    const platform::Place& place);
132

133 134
void PassStopGradient(const NameVarBaseMap& outs, bool generate_grad);

135 136
}  // namespace imperative
}  // namespace paddle