tracer.h 1.9 KB
Newer Older
1
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
X
xiexionghang 已提交
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

17 18 19
#include <atomic>
#include <future>  // NOLINT
#include <memory>
X
xiexionghang 已提交
20 21 22
#include <string>
#include <unordered_map>
#include <vector>
23
#include "ThreadPool.h"
X
xiexionghang 已提交
24 25
#include "paddle/fluid/imperative/engine.h"
#include "paddle/fluid/imperative/layer.h"
26
#include "paddle/fluid/platform/macros.h"
X
xiexionghang 已提交
27 28 29 30 31

namespace paddle {
namespace imperative {

class Tracer {
32 33
  DISABLE_COPY_AND_ASSIGN(Tracer);

X
xiexionghang 已提交
34
 public:
35
  Tracer() : engine_(new BasicEngine()) {}
X
xiexionghang 已提交
36

37
  ~Tracer() = default;
X
xiexionghang 已提交
38

39 40 41 42 43 44 45 46 47 48 49
  void TraceOp(const std::string& type, const NameVarBaseMap& ins,
               const NameVarBaseMap& outs, framework::AttributeMap attrs,
               const platform::Place& place, bool trace_bacward);

  bool ComputeRequiredGrad(const NameVarBaseMap& ins,
                           const NameVarBaseMap& outs, bool trace_backward);

  void TraceBackward(const std::shared_ptr<OpBase>& fwd_op,
                     const framework::OpDesc& fwd_op_desc,
                     const NameVarBaseMap& ins, const NameVarBaseMap& outs);
  Engine* GetDefaultEngine() const { return engine_.get(); }
X
xiexionghang 已提交
50 51

 private:
52 53 54 55
  static size_t GenerateUniqueId() {
    static std::atomic<size_t> id{0};
    return id.fetch_add(1);
  }
X
xiexionghang 已提交
56

57 58
 private:
  std::unique_ptr<Engine> engine_;
X
xiexionghang 已提交
59 60 61 62
};

}  // namespace imperative
}  // namespace paddle