-
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
[Cpp Extension] Support Cpp Extension #49893
Conversation
… support_C++_extension
… support_C++_extension
你的PR提交成功,感谢你对开源项目的贡献! |
… support_C++_extension
cmake/third_party.cmake
Outdated
if(WITH_PYTHON) | ||
include(external/python) # find python and python_module | ||
include(external/pybind11) # download pybind11 | ||
list(APPEND third_party_deps extern_pybind) | ||
endif() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里去掉WITH_PYTHON可开关会不会对推理的编译产生影响?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks,计划将 if(WITH_PYTHON)
改为 if(NOT ON_INFER)
Maybe change if(WITH_PYTHON)
to if(NOT ON_INFER)
would be better.
if os.name == 'nt' or sys.platform.startswith('darwin'): | ||
# Cpp Extension only support Linux now | ||
mod = types.ModuleType(__name__) | ||
else: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的平台限制问题后续可以解决吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Windows 和 Mac 平台和 Linux 平台编译的差别较大,需要线下评估这项需求和排期。
There are differences between Windows, Mac, and Linux, we need to assess this requirement offline.
if os.name == 'nt' or sys.platform.startswith('darwin'): | ||
# only support Linux now | ||
exit() | ||
|
||
# Because Windows don't use docker, the shared lib already exists in the | ||
# cache dir, it will not be compiled again unless the shared lib is removed. | ||
file = '{}\\custom_cpp_extension\\custom_cpp_extension.pyd'.format( | ||
get_build_directory() | ||
) | ||
if os.name == 'nt' and os.path.isfile(file): | ||
cmd = 'del {}'.format(file) | ||
run_cmd(cmd, True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这部分代码可以封装起来吗?用户在参考示例代码的时候这里看上去可能会有一些干扰
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感谢,原本此处代码是留作扩展 Windows 的,可以先删除这部分代码,保持单测的整洁
Thanks, these codes are kept for Windows, I can delete this part temporarily to keep unit test clean.
paddle/utils/pybind.cc
Outdated
// limitations under the License. | ||
|
||
#include "paddle/utils/pybind.h" | ||
#include "paddle/fluid/platform/enforce.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里能否用phi的,utils下的文件是对外放出的,但是platform下的文件没有放出,可能会编译错误
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thx~
4dc1572
to
941d674
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM for
set_tests_properties(test_cpp_extension_setup PROPERTIES TIMEOUT 120) set_tests_properties(test_cpp_extension_jit PROPERTIES TIMEOUT 120)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR types
New features
PR changes
Others
Describe
飞桨框架提供了丰富的算子库和易用的自定义算子机制,能够满足绝大多数场景的使用需求。
但是在以下场景下,用户可能希望定制化 C++ 实现,从而满足特定需求:
现有的自定义算子机制在下列场景下无法满足用户需求:
为此,我们需要提供一种更为灵活的 C++ 扩展机制,以此机制实现的自定义扩展,能够 即插即用 ,不需要重新编译安装飞桨框架。
主要改动:
1. 支持 Cpp Extension 机制
2. 支持 setup 编译和 JIT 编译
3. 添加 Cpp Extension 单测
4. 公开 pybind11 相关头文件
[Update pybind11] update pybind11 TAG, 2.4.3->2.6.0 #50068
目前本 PR 内容相对较多,且作为整体功能比较难拆分。
为便于 PR review 和并行推进剩余工作,后续会通过一些小 PR 继续完善 TODO 项:
1. 添加多个单测,充分验证与测试 Cpp Extension 机制:
2. 支持 Mac 和 Windows 平台
PaddlePaddle has provided a large operator library and easy-to-use custom operator mechanism, which can satisfy most scenario requirements. However, in the following scenario, users may hope to customize Cpp implementation and thus meet specific needs:
The existing custom operator mechanism can not satisfy users' requirements in the following scenarios:
Therefore, we need to provide a more flexible Cpp Extension mechanism and this mechanism can plug in the PaddlePaddle framework without recompiling the whole project.
Main Change:
1. Support Cpp Extension mechanism
2. Support setup compile and Just-In-Time (JIT) compile
3. Add Cpp Extension unit test
4. Disclose pybind11 header files
[Update pybind11] update pybind11 TAG, 2.4.3->2.6.0 #50068
This PR is relatively large and hard to split. To simplify the review process and finish remained work parallelly, some small PRs will be created to finish the TODO list.
1. Add multiple unit tests to fully verify the Cpp Extension mechanism
2. Support Mac and Windows platform