imperative.cc 2.5 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 41
      .def("trace",
           [](imperative::Tracer& self, imperative::OpBase* op,
              const imperative::VarBasePtrMap& inputs,
              const imperative::VarBasePtrMap& outputs,
42
              framework::AttributeMap attrs_map,
M
minqiyang 已提交
43 44
              const platform::CPUPlace expected_place,
              const bool stop_gradient = false) {
M
minqiyang 已提交
45
             pybind11::gil_scoped_release release;
46
             return self.Trace(op, inputs, outputs, attrs_map, expected_place,
M
minqiyang 已提交
47
                               stop_gradient);
M
minqiyang 已提交
48 49 50 51 52
           })
      .def("trace",
           [](imperative::Tracer& self, imperative::OpBase* op,
              const imperative::VarBasePtrMap& inputs,
              const imperative::VarBasePtrMap& outputs,
53
              framework::AttributeMap attrs_map,
M
minqiyang 已提交
54 55
              const platform::CUDAPlace expected_place,
              const bool stop_gradient = false) {
M
minqiyang 已提交
56
             pybind11::gil_scoped_release release;
57
             return self.Trace(op, inputs, outputs, attrs_map, expected_place,
M
minqiyang 已提交
58
                               stop_gradient);
M
minqiyang 已提交
59
           })
X
Xin Pan 已提交
60 61
      .def("py_trace", &imperative::Tracer::PyTrace,
           pybind11::return_value_policy::take_ownership);
62 63 64 65
}

}  // namespace pybind
}  // namespace paddle