imperative.cc 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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/pybind/imperative.h"
16 17 18 19 20 21

#include <pybind11/chrono.h>
#include <pybind11/complex.h>
#include <pybind11/functional.h>
#include <pybind11/stl.h>

22 23
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/imperative/tracer.h"
M
minqiyang 已提交
24
#include "paddle/fluid/imperative/type_defs.h"
25

26 27
#include "paddle/fluid/pybind/pybind_boost_headers.h"

28 29 30 31
namespace paddle {
namespace pybind {

// Bind Methods
M
minqiyang 已提交
32
void BindTracer(pybind11::module* m) {
33 34
  pybind11::class_<imperative::Tracer>(*m, "Tracer", "")
      .def("__init__",
M
minqiyang 已提交
35
           [](imperative::Tracer& self, framework::BlockDesc* root_block) {
M
minqiyang 已提交
36
             new (&self) imperative::Tracer(root_block);
37
           })
M
minqiyang 已提交
38 39 40
      .def("trace",
           [](imperative::Tracer& self, imperative::OpBase* op,
              const imperative::VarBasePtrMap& inputs,
M
minqiyang 已提交
41
              imperative::VarBasePtrMap* outputs,
42
              framework::AttributeMap attrs_map,
M
minqiyang 已提交
43 44
              const platform::CPUPlace expected_place,
              const bool stop_gradient = false) {
45
             return self.Trace(op, inputs, outputs, attrs_map, expected_place,
M
minqiyang 已提交
46
                               stop_gradient);
M
minqiyang 已提交
47 48 49 50
           })
      .def("trace",
           [](imperative::Tracer& self, imperative::OpBase* op,
              const imperative::VarBasePtrMap& inputs,
M
minqiyang 已提交
51
              imperative::VarBasePtrMap* outputs,
52
              framework::AttributeMap attrs_map,
M
minqiyang 已提交
53 54
              const platform::CUDAPlace expected_place,
              const bool stop_gradient = false) {
55
             return self.Trace(op, inputs, outputs, attrs_map, expected_place,
M
minqiyang 已提交
56
                               stop_gradient);
M
minqiyang 已提交
57
           })
X
Xin Pan 已提交
58 59
      .def("py_trace", &imperative::Tracer::PyTrace,
           pybind11::return_value_policy::take_ownership);
60 61 62 63
}

}  // namespace pybind
}  // namespace paddle