forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose paddle.framework by pybind11 (PaddlePaddle#2793)
* Expose paddle.framework by pybind11 * Export paddle.framework.{Scope, Variable} to paddle.v2.framework.core. * See python/paddle/v2/framework/tests/test_scope.py for Python usage * See paddle/pybind/pybind.cc for C++ bind code. * add copyright
- Loading branch information
1 parent
ef67d08
commit 27b196b
Showing
13 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,6 @@ third_party/ | |
|
||
# clion workspace. | ||
cmake-build-* | ||
|
||
# generated while compiling | ||
python/paddle/v2/framework/core.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
INCLUDE(ExternalProject) | ||
|
||
SET(PYBIND_SOURCE_DIR ${THIRD_PARTY_PATH}/pybind) | ||
|
||
INCLUDE_DIRECTORIES(${PYBIND_SOURCE_DIR}/src/extern_pybind/include) | ||
|
||
ExternalProject_Add( | ||
extern_pybind | ||
${EXTERNAL_PROJECT_LOG_ARGS} | ||
GIT_REPOSITORY "https://github.com/pybind/pybind11.git" | ||
GIT_TAG "v2.1.1" | ||
PREFIX ${PYBIND_SOURCE_DIR} | ||
UPDATE_COMMAND "" | ||
CONFIGURE_COMMAND "" | ||
BUILD_COMMAND "" | ||
INSTALL_COMMAND "" | ||
TEST_COMMAND "" | ||
) | ||
|
||
if (${CMAKE_VERSION} VERSION_LESS "3.3.0") | ||
set(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/pybind_dummy.c) | ||
file(WRITE ${dummyfile} "const char * dummy_any = \"${dummyfile}\";") | ||
add_library(pybind STATIC ${dummyfile}) | ||
else() | ||
add_library(pybind INTERFACE) | ||
endif() | ||
|
||
add_dependencies(pybind extern_pybind) | ||
|
||
LIST(APPEND external_project_dependencies pybind) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cc_library(paddle_pybind SHARED SRCS pybind.cc DEPS pybind python) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
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/framework/scope.h> | ||
#include <pybind11/pybind11.h> | ||
|
||
namespace py = pybind11; | ||
namespace pd = paddle::framework; | ||
|
||
PYBIND11_PLUGIN(core) { | ||
py::module m("core", "C++ core of Paddle Paddle"); | ||
|
||
py::class_<pd::Variable>(m, "Variable", R"DOC(Variable Class. | ||
All parameter, weight, gradient are variables in Paddle. | ||
)DOC") | ||
.def("is_int", [](const pd::Variable& var) { return var.IsType<int>(); }) | ||
.def("set_int", | ||
[](pd::Variable& var, int val) -> void { | ||
*var.GetMutable<int>() = val; | ||
}) | ||
.def("get_int", | ||
[](const pd::Variable& var) -> int { return var.Get<int>(); }); | ||
|
||
py::class_<pd::Scope, std::shared_ptr<pd::Scope>>(m, "Scope") | ||
.def(py::init<const std::shared_ptr<pd::Scope>&>()) | ||
.def("get_var", | ||
&pd::Scope::GetVariable, | ||
py::return_value_policy::reference) | ||
.def("create_var", | ||
&pd::Scope::CreateVariable, | ||
py::return_value_policy::reference); | ||
|
||
return m.ptr(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
add_python_test(test_framework test_protobuf.py) | ||
add_python_test(test_framework test_protobuf.py test_scope.py) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import paddle.v2.framework.core | ||
import unittest | ||
|
||
|
||
class TestScope(unittest.TestCase): | ||
def test_create_destroy(self): | ||
paddle_c = paddle.v2.framework.core | ||
scope = paddle_c.Scope(None) | ||
self.assertIsNotNone(scope) | ||
scope_with_parent = paddle_c.Scope(scope) | ||
self.assertIsNotNone(scope_with_parent) | ||
|
||
def test_none_variable(self): | ||
paddle_c = paddle.v2.framework.core | ||
scope = paddle_c.Scope(None) | ||
self.assertIsNone(scope.get_var("test")) | ||
|
||
def test_create_var_get_var(self): | ||
paddle_c = paddle.v2.framework.core | ||
scope = paddle_c.Scope(None) | ||
var_a = scope.create_var("var_a") | ||
self.assertIsNotNone(var_a) | ||
self.assertIsNotNone(scope.get_var('var_a')) | ||
scope2 = paddle_c.Scope(scope) | ||
self.assertIsNotNone(scope2.get_var('var_a')) | ||
|
||
def test_var_get_int(self): | ||
paddle_c = paddle.v2.framework.core | ||
scope = paddle_c.Scope(None) | ||
var = scope.create_var("test_int") | ||
var.set_int(10) | ||
self.assertTrue(var.is_int()) | ||
self.assertEqual(10, var.get_int()) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters