tracer.h 3.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 19
#include <atomic>
#include <future>  // NOLINT
#include <memory>
20
#include <string>
21
#include <unordered_map>
22
#include <vector>
J
Jiabin Yang 已提交
23
#include "ThreadPool.h"
24
#include "paddle/fluid/imperative/engine.h"
25
#include "paddle/fluid/imperative/jit/program_desc_tracer.h"
26
#include "paddle/fluid/imperative/layer.h"
J
Jiabin Yang 已提交
27
#include "paddle/fluid/platform/macros.h"
28 29 30 31

namespace paddle {
namespace imperative {

32 33 34 35
class UniqueNameGenerator {
 public:
  explicit UniqueNameGenerator(std::string prefix = "") : prefix_(prefix) {}
  std::string Generate(std::string key = "tmp") {
L
Leo Chen 已提交
36
    return prefix_ + key + "_" + std::to_string(id_++);
37 38 39 40 41 42 43
  }

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

44
class Tracer {
J
Jiabin Yang 已提交
45 46
  DISABLE_COPY_AND_ASSIGN(Tracer);

47
 public:
48 49
  Tracer()
      : engine_(new BasicEngine()),
50
        program_desc_tracer_(new jit::ProgramDescTracer()),
51 52 53
        generator_(new UniqueNameGenerator()) {
    expected_place_ = platform::CPUPlace();
  }
54

J
Jiabin Yang 已提交
55
  ~Tracer() = default;
56

J
Jiabin Yang 已提交
57 58 59 60
  void TraceOp(const std::string& type, const NameVarBaseMap& ins,
               const NameVarBaseMap& outs, framework::AttributeMap attrs,
               const platform::Place& place, bool trace_bacward);

61 62 63
  void TraceOp(const std::string& type, const NameVarBaseMap& ins,
               const NameVarBaseMap& outs, framework::AttributeMap attrs);

64 65
  bool ComputeRequiredGrad(const NameVarBaseMap& ins,
                           const NameVarBaseMap& outs, bool trace_backward);
J
Jiabin Yang 已提交
66 67 68

  void TraceBackward(const std::shared_ptr<OpBase>& fwd_op,
                     const NameVarBaseMap& ins, const NameVarBaseMap& outs);
69

J
Jiabin Yang 已提交
70
  Engine* GetDefaultEngine() const { return engine_.get(); }
71

72 73 74 75 76 77 78 79 80 81 82 83
  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();
  }

84 85 86 87
  std::string GenerateUniqueName(std::string key = "tmp") {
    return generator_->Generate(key);
  }

88 89 90 91 92 93 94 95 96 97 98
  platform::Place ExpectedPlace() const { return expected_place_; }

  template <typename PlaceType>
  void SetExpectedPlace(PlaceType place) {
    expected_place_ = place;
  }

  bool NoGrad() const { return no_grad_; }

  void SetNoGrad(bool no_grad) { no_grad_ = no_grad; }

99
 private:
J
Jiabin Yang 已提交
100 101 102 103
  static size_t GenerateUniqueId() {
    static std::atomic<size_t> id{0};
    return id.fetch_add(1);
  }
M
minqiyang 已提交
104

J
Jiabin Yang 已提交
105 106
 private:
  std::unique_ptr<Engine> engine_;
107 108
  std::unique_ptr<jit::ProgramDescTracer> program_desc_tracer_;
  bool enable_program_desc_tracing_{false};
109
  std::unique_ptr<UniqueNameGenerator> generator_;
110 111
  platform::Place expected_place_;
  bool no_grad_{false};
112 113
};

114 115 116 117
// To access static variable current_tracer
const std::shared_ptr<Tracer>& GetCurrentTracer();
void SetCurrentTracer(const std::shared_ptr<Tracer>& tracer_);

118 119
}  // namespace imperative
}  // namespace paddle