|
|
|
@ -14,6 +14,7 @@ limitations under the License. */
|
|
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
#include <paddle/framework/op_registry.h>
|
|
|
|
|
#include <paddle/framework/operator.h>
|
|
|
|
|
#include <paddle/framework/scope.h>
|
|
|
|
|
#include <paddle/pybind/tensor_bind.h>
|
|
|
|
|
#include <pybind11/numpy.h>
|
|
|
|
@ -26,10 +27,7 @@ namespace py = pybind11;
|
|
|
|
|
namespace pd = paddle::framework;
|
|
|
|
|
|
|
|
|
|
USE_OP(add_two);
|
|
|
|
|
USE_OP(softmax);
|
|
|
|
|
USE_OP(mul);
|
|
|
|
|
USE_OP(rowwise_add);
|
|
|
|
|
USE_OP(sigmoid);
|
|
|
|
|
USE_OP_WITHOUT_KERNEL(fc);
|
|
|
|
|
|
|
|
|
|
PYBIND11_PLUGIN(core) {
|
|
|
|
|
py::module m("core", "C++ core of Paddle Paddle");
|
|
|
|
@ -53,7 +51,9 @@ PYBIND11_PLUGIN(core) {
|
|
|
|
|
self.mutable_data<int>(paddle::platform::CPUPlace());
|
|
|
|
|
})
|
|
|
|
|
.def("set", paddle::pybind::PyTensorSetFromArray<float>)
|
|
|
|
|
.def("set", paddle::pybind::PyTensorSetFromArray<int>);
|
|
|
|
|
.def("set", paddle::pybind::PyTensorSetFromArray<int>)
|
|
|
|
|
.def("shape",
|
|
|
|
|
[](pd::Tensor& self) { return pd::vectorize(self.dims()); });
|
|
|
|
|
|
|
|
|
|
py::class_<pd::Variable>(m, "Variable", R"DOC(Variable Class.
|
|
|
|
|
|
|
|
|
@ -83,15 +83,16 @@ All parameter, weight, gradient are variables in Paddle.
|
|
|
|
|
|
|
|
|
|
//! @note: Be careful! PyBind will return std::string as an unicode, not
|
|
|
|
|
//! Python str. If you want a str object, you should cast them in Python.
|
|
|
|
|
m.def("get_all_op_protos", []() -> std::vector<std::string> {
|
|
|
|
|
m.def("get_all_op_protos", []() -> std::vector<py::bytes> {
|
|
|
|
|
auto& protos = pd::OpRegistry::protos();
|
|
|
|
|
std::vector<std::string> ret_values;
|
|
|
|
|
std::vector<py::bytes> ret_values;
|
|
|
|
|
for (auto it = protos.begin(); it != protos.end(); ++it) {
|
|
|
|
|
PADDLE_ENFORCE(it->second.IsInitialized(),
|
|
|
|
|
"OpProto must all be initialized");
|
|
|
|
|
ret_values.emplace_back();
|
|
|
|
|
PADDLE_ENFORCE(it->second.SerializeToString(&ret_values.back()),
|
|
|
|
|
std::string str;
|
|
|
|
|
PADDLE_ENFORCE(it->second.SerializeToString(&str),
|
|
|
|
|
"Serialize OpProto Error. This could be a bug of Paddle.");
|
|
|
|
|
ret_values.push_back(py::bytes(str));
|
|
|
|
|
}
|
|
|
|
|
return ret_values;
|
|
|
|
|
});
|
|
|
|
@ -101,17 +102,26 @@ All parameter, weight, gradient are variables in Paddle.
|
|
|
|
|
.def("empty", pd::OperatorBase::EMPTY_VAR_NAME)
|
|
|
|
|
.def("temp", pd::OperatorBase::TMP_VAR_NAME);
|
|
|
|
|
|
|
|
|
|
py::class_<paddle::platform::DeviceContext>(m, "DeviceContext")
|
|
|
|
|
.def_static("cpu_context", []() -> paddle::platform::DeviceContext* {
|
|
|
|
|
return new paddle::platform::CPUDeviceContext();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
py::class_<pd::OperatorBase, pd::OperatorPtr>(m, "Operator")
|
|
|
|
|
.def("__str__", &pd::OperatorBase::DebugString)
|
|
|
|
|
.def_static("create", [](const std::string& protobin) {
|
|
|
|
|
pd::OpDesc desc;
|
|
|
|
|
PADDLE_ENFORCE(desc.ParsePartialFromString(protobin),
|
|
|
|
|
"Cannot parse user input to OpDesc");
|
|
|
|
|
PADDLE_ENFORCE(desc.IsInitialized(),
|
|
|
|
|
"User OpDesc is not initialized, reason %s",
|
|
|
|
|
desc.InitializationErrorString());
|
|
|
|
|
return pd::OpRegistry::CreateOp(desc);
|
|
|
|
|
});
|
|
|
|
|
.def_static("create",
|
|
|
|
|
[](py::bytes protobin) {
|
|
|
|
|
pd::OpDesc desc;
|
|
|
|
|
PADDLE_ENFORCE(desc.ParsePartialFromString(protobin),
|
|
|
|
|
"Cannot parse user input to OpDesc");
|
|
|
|
|
PADDLE_ENFORCE(desc.IsInitialized(),
|
|
|
|
|
"User OpDesc is not initialized, reason %s",
|
|
|
|
|
desc.InitializationErrorString());
|
|
|
|
|
return pd::OpRegistry::CreateOp(desc);
|
|
|
|
|
})
|
|
|
|
|
.def("infer_shape", &pd::OperatorBase::InferShape)
|
|
|
|
|
.def("run", &pd::OperatorBase::Run)
|
|
|
|
|
.def("outputs", [](const pd::OperatorPtr& op) { return op->outputs_; });
|
|
|
|
|
|
|
|
|
|
return m.ptr();
|
|
|
|
|
}
|
|
|
|
|