-
Notifications
You must be signed in to change notification settings - Fork 5.7k
[Cpp Extension] Support Cpp Extension #49893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jiahy0825
merged 47 commits into
PaddlePaddle:develop
from
jiahy0825:support_C++_extension
Feb 20, 2023
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
df60146
update include directory
jiahy0825 1a8266e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jiahy0825 a088e18
fully support C++ extension, pass unittest
jiahy0825 cfd2b50
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jiahy0825 716a7be
fix include directory
jiahy0825 3c0c182
support both extension and operator in one file
jiahy0825 4c24472
polish testcase
jiahy0825 2352468
add jit unittest
jiahy0825 98c9ef0
update third_party.cmake, pass CI test
jiahy0825 36d39ef
fix cmake
jiahy0825 c6ff3d7
fix setup
jiahy0825 8330201
fix inference, fix unittest precision
jiahy0825 1e42f72
fix unittest precision
jiahy0825 2b77782
fix inference_lib cmake
jiahy0825 3ea9292
try fix setup, try fix inference_lib
jiahy0825 81c3982
try fix inference_lib pybind
jiahy0825 e2cbfaa
fix mix_op_extension, fix inference_lib
jiahy0825 be02319
fix mix_op_extension, fix inference_lib
jiahy0825 7cb1dbf
change cmake
jiahy0825 2013aaa
change cmake
jiahy0825 21e71ef
add compile flags
jiahy0825 dfa0cb4
add Python.h headerfile
jiahy0825 adc6129
add test_custom_plugin_creater cmake
jiahy0825 a32ef46
comment compile flag
jiahy0825 1a55aef
pass all CI
jiahy0825 4ef6175
pass all CI
jiahy0825 af05e34
comment compile flag
jiahy0825 4c34a8b
try solve test_custom_plugin_creater link error
jiahy0825 7c822c3
try solve test_custom_plugin_creater link error
jiahy0825 65f184d
polish codes
jiahy0825 79a15d3
remove windows compile flag
jiahy0825 a71887f
remove python_include_path
jiahy0825 e05ed87
update pybind11, 2.4.3->2.6.0
jiahy0825 9738fb9
update pybind11, 2.6.0->2.10.0
jiahy0825 23e63e6
update pybind11, 2.10.0->2.6.0b1
jiahy0825 63cf859
update pybind11, 2.6.0b1->2.6.0, start fix unittest
jiahy0825 95c5be8
fix pybind11 2.6.0 VarBase print error
jiahy0825 08f99dc
fix pybind11 2.6.0 VarBase print error
jiahy0825 45bc3b8
handle PADDLE_ON_INFERENCE
jiahy0825 85c4abd
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jiahy0825 c193063
modify according to reviewer
jiahy0825 6a20c8f
fix cmake
jiahy0825 3063de9
cmake decouple pybind_util when not ON_INFER
jiahy0825 8609d90
cmake decouple pybind_util when not ON_INFER
jiahy0825 4b9126e
remove copy of inference_lib.cmake
jiahy0825 68e2e9f
change pybind.cc headerfile fluid->phi
jiahy0825 941d674
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jiahy0825 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
// Copyright (c) 2023 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/utils/pybind.h" | ||
#include "paddle/phi/core/enforce.h" | ||
|
||
namespace paddle { | ||
namespace pybind { | ||
|
||
PyTypeObject* p_tensor_type; | ||
PyTypeObject* p_string_tensor_type; | ||
|
||
bool PyCheckTensor(PyObject* obj) { | ||
if (!p_tensor_type) { | ||
return false; | ||
} | ||
return PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(p_tensor_type)); | ||
} | ||
|
||
paddle::experimental::Tensor CastPyArg2Tensor(PyObject* obj, ssize_t arg_pos) { | ||
if (PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(p_tensor_type)) || | ||
PyObject_IsInstance(obj, | ||
reinterpret_cast<PyObject*>(p_string_tensor_type))) { | ||
return reinterpret_cast<TensorObject*>(obj)->tensor; | ||
} else { | ||
PADDLE_THROW(phi::errors::InvalidArgument( | ||
"argument (position %d) must be " | ||
"Tensor, but got %s", | ||
arg_pos + 1, | ||
reinterpret_cast<PyTypeObject*>(obj->ob_type)->tp_name)); | ||
} | ||
} | ||
|
||
PyObject* ToPyObject(const paddle::experimental::Tensor& value, | ||
bool return_py_none_if_not_initialize) { | ||
if (return_py_none_if_not_initialize && !value.initialized()) { | ||
RETURN_PY_NONE | ||
} | ||
PyObject* obj = nullptr; | ||
if (value.initialized() && value.is_string_tensor()) { | ||
// In order to return the core.eager.StringTensor, there is need | ||
// to use p_string_tensor_type to create a python obj. | ||
obj = p_string_tensor_type->tp_alloc(p_string_tensor_type, 0); | ||
} else { | ||
obj = p_tensor_type->tp_alloc(p_tensor_type, 0); | ||
} | ||
if (obj) { | ||
auto v = reinterpret_cast<TensorObject*>(obj); | ||
new (&(v->tensor)) paddle::experimental::Tensor(); | ||
v->tensor = value; | ||
} else { | ||
PADDLE_THROW( | ||
phi::errors::Fatal("tp_alloc return null, can not new a PyObject.")); | ||
} | ||
return obj; | ||
} | ||
|
||
} // namespace pybind | ||
} // namespace paddle |
This file contains hidden or 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,76 @@ | ||
// Copyright (c) 2023 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. | ||
|
||
#pragma once | ||
|
||
#include "paddle/phi/api/include/tensor.h" | ||
#include "pybind11/pybind11.h" | ||
#include "pybind11/stl.h" | ||
|
||
namespace py = pybind11; | ||
|
||
namespace paddle { | ||
namespace pybind { | ||
|
||
extern PyTypeObject* p_tensor_type; | ||
|
||
typedef struct { | ||
PyObject_HEAD paddle::experimental::Tensor tensor; | ||
// Weak references | ||
PyObject* weakrefs; | ||
} TensorObject; | ||
|
||
#define RETURN_PY_NONE \ | ||
Py_INCREF(Py_None); \ | ||
return Py_None; | ||
|
||
// Internal use only, to expose the Tensor type to Python. | ||
bool PyCheckTensor(PyObject* obj); | ||
|
||
// Internal use only, to expose the Tensor type to Python. | ||
paddle::experimental::Tensor CastPyArg2Tensor(PyObject* obj, ssize_t arg_pos); | ||
|
||
// Internal use only, to expose the Tensor type to Python. | ||
PyObject* ToPyObject(const paddle::experimental::Tensor& value, | ||
bool return_py_none_if_not_initialize = false); | ||
|
||
} // namespace pybind | ||
} // namespace paddle | ||
|
||
namespace pybind11 { | ||
namespace detail { | ||
|
||
template <> | ||
struct type_caster<paddle::experimental::Tensor> { | ||
public: | ||
PYBIND11_TYPE_CASTER(paddle::experimental::Tensor, | ||
_("paddle::experimental::Tensor")); | ||
|
||
bool load(handle src, bool) { | ||
PyObject* obj = src.ptr(); | ||
if (paddle::pybind::PyCheckTensor(obj)) { | ||
value = paddle::pybind::CastPyArg2Tensor(obj, 0); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
static handle cast(const paddle::experimental::Tensor& src, | ||
return_value_policy /* policy */, | ||
handle /* parent */) { | ||
return handle(paddle::pybind::ToPyObject(src)); | ||
} | ||
}; | ||
} // namespace detail | ||
} // namespace pybind11 |
This file contains hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
py_test(test_cpp_extension_setup SRCS test_cpp_extension_setup.py) | ||
py_test(test_cpp_extension_jit SRCS test_cpp_extension_jit.py) | ||
|
||
set_tests_properties(test_cpp_extension_setup PROPERTIES TIMEOUT 120) | ||
set_tests_properties(test_cpp_extension_jit PROPERTIES TIMEOUT 120) |
38 changes: 38 additions & 0 deletions
38
python/paddle/fluid/tests/cpp_extension/cpp_extension_setup.py
This file contains hidden or 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,38 @@ | ||
# Copyright (c) 2023 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. | ||
|
||
import os | ||
from site import getsitepackages | ||
|
||
from paddle.utils.cpp_extension import CppExtension, setup | ||
|
||
paddle_includes = [] | ||
for site_packages_path in getsitepackages(): | ||
paddle_includes.append( | ||
os.path.join(site_packages_path, 'paddle', 'include') | ||
) | ||
paddle_includes.append( | ||
os.path.join(site_packages_path, 'paddle', 'include', 'third_party') | ||
) | ||
|
||
setup( | ||
name='custom_cpp_extension', | ||
ext_modules=CppExtension( | ||
sources=["custom_add.cc", "custom_sub.cc"], | ||
include_dirs=paddle_includes | ||
+ [os.path.dirname(os.path.abspath(__file__))], | ||
extra_compile_args={'cc': ['-w', '-g']}, | ||
verbose=True, | ||
), | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.