imperative.cc 4.1 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
32
void BindImperative(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) {
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
           })
      .def("trace",
           [](imperative::Tracer& self, imperative::OpBase* op,
              const imperative::VarBasePtrMap& inputs,
M
minqiyang 已提交
52
              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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

  // define parallel context
  pybind11::class_<imperative::ParallelStrategy> parallel_strategy(
      *m, "ParallelStrategy", "");
  parallel_strategy.def(pybind11::init())
      .def_property(
          "nranks",
          [](const imperative::ParallelStrategy& self) { return self.nranks_; },
          [](imperative::ParallelStrategy& self, int nranks) {
            self.nranks_ = nranks;
          })
      .def_property("local_rank",
                    [](const imperative::ParallelStrategy& self) {
                      return self.local_rank_;
                    },
                    [](imperative::ParallelStrategy& self, int local_rank) {
                      self.local_rank_ = local_rank;
                    })
      .def_property(
          "trainer_endpoints",
          [](const imperative::ParallelStrategy& self) {
            return self.trainer_endpoints_;
          },
          [](imperative::ParallelStrategy& self, std::vector<std::string> eps) {
            self.trainer_endpoints_ = eps;
          })
      .def_property("current_endpoint",
                    [](const imperative::ParallelStrategy& self) {
                      return self.current_endpoint_;
                    },
                    [](imperative::ParallelStrategy& self,
                       const std::string& ep) { self.current_endpoint_ = ep; });
#if defined(PADDLE_WITH_CUDA) && !defined(_WIN32)
  pybind11::class_<imperative::NCCLParallelContext> nccl_ctx(
      *m, "NCCLParallelContext");

  nccl_ctx
      .def(pybind11::init<const imperative::ParallelStrategy&,
                          const platform::CUDAPlace&>())
      .def("init", [](imperative::NCCLParallelContext& self) { self.Init(); });
#endif
103 104 105 106
}

}  // namespace pybind
}  // namespace paddle