-
Couldn't load subscription status.
- Fork 869
[CustomOp] Add cpp_extension zh doc #3292
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
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4ba7c37
add cpp_extension doc
Aurelius84 9610a75
add ignore in api_white_list.txt
Aurelius84 7e2a6cc
add get_build_directory
Aurelius84 2f6f0cf
add get_build_directory in overview
Aurelius84 2cd068c
fix end line
Aurelius84 a928e67
modify according with review
Aurelius84 72d246e
update doc
Aurelius84 e83d342
update
Aurelius84 e11152f
add optional
Aurelius84 74973a7
add link of ABI
Aurelius84 86369c5
add defalut value
Aurelius84 2fe6efc
split custom op table
Aurelius84 324d77a
rm some words
Aurelius84 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
40 changes: 40 additions & 0 deletions
40
doc/paddle/api/paddle/utils/cpp_extension/CUDAExtension_cn.rst
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,40 @@ | ||
| .. _cn_api_paddle_utils_cpp_extension_CUDAExtension: | ||
|
|
||
| CUDAExtension | ||
| ------------------------------- | ||
|
|
||
| .. py:function:: paddle.utils.cpp_extension.CUDAExtension(sources, *args, **kwargs) | ||
|
|
||
| 此接口用于配置自定义 OP 的源文件信息,编译生成同时支持 CPU 和 GPU 设备上执行的算子。若要编译仅支持 CPU 设备的算子,请使用 :ref:`cn_api_paddle_utils_cpp_extension_CppExtension` 。 | ||
|
|
||
| 此接口是对 Python 内建库 ``setuptools.Extension`` 的进一步封装。除了不需要显式地指定 ``name`` 参数,其他参数以及使用方式上,与原生内建库接口保持一致。 | ||
|
|
||
| **使用样例如下:** | ||
|
|
||
| .. code-block:: text | ||
|
|
||
| # setup.py | ||
|
|
||
| # 编译支持 CPU/GPU 的算子 | ||
| from paddle.utils.cpp_extension import CUDAExtension, setup | ||
|
|
||
| setup( | ||
| name='custom_op', | ||
| ext_modules=CUDAExtension( | ||
| sources=['relu_op.cc', 'relu_op.cu'] | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| .. note:: | ||
|
|
||
| 搭配 ``setup`` 接口使用,编译生成的动态库名称与 :ref:`cn_api_paddle_utils_cpp_extension_setup` 接口中的 ``name`` 一致。 | ||
|
|
||
|
|
||
|
|
||
| 参数: | ||
| - **sources** (list[str]): 用于指定自定义 OP 对应的源码文件。cpp 源文件支持 .cc、.cpp等后缀;cuda 源文件以 .cu 为后缀。 | ||
| - **\*args, \*\*kwargs** (可选): 用于指定 Extension 的其他参数,支持的参数与 ``setuptools.Extension`` 一致。 | ||
|
|
||
| 返回: ``setuptools.Extension`` 对象 |
38 changes: 38 additions & 0 deletions
38
doc/paddle/api/paddle/utils/cpp_extension/CppExtension_cn.rst
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 @@ | ||
| .. _cn_api_paddle_utils_cpp_extension_CppExtension: | ||
|
|
||
| CppExtension | ||
| ------------------------------- | ||
|
|
||
| .. py:function:: paddle.utils.cpp_extension.CppExtension(sources, *args, **kwargs) | ||
|
|
||
| 此接口用于配置自定义 OP 的源文件信息,编译生成仅支持 CPU 设备上执行的算子。若要编译同时支持 GPU 设备的算子,请使用 :ref:`cn_api_paddle_utils_cpp_extension_CUDAExtension` 。 | ||
|
|
||
| 此接口是对 Python 内建库 ``setuptools.Extension`` 的进一步封装。除了不需要显式地指定 ``name`` 参数,其他参数以及使用方式上,与原生内建库接口保持一致。 | ||
|
|
||
| **使用样例如下:** | ||
|
|
||
| .. code-block:: text | ||
|
|
||
| # setup.py | ||
|
|
||
| # 编译仅支持 CPU 的算子 | ||
| from paddle.utils.cpp_extension import CppExtension, setup | ||
|
|
||
| setup( | ||
| name='custom_op', | ||
| ext_modules=CppExtension(sources=['relu_op.cc']) | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| .. note:: | ||
|
|
||
| 搭配 ``setup`` 接口使用,编译生成的动态库名称与 :ref:`cn_api_paddle_utils_cpp_extension_setup` 接口中的 ``name`` 一致。 | ||
|
|
||
|
|
||
|
|
||
| 参数: | ||
| - **sources** (list[str]): 用于指定自定义 OP 对应的源码文件。cpp 源文件支持 .cc、.cpp等后缀 | ||
| - **\*args, \*\*kwargs** (可选): 用于指定 Extension 的其他参数,支持的参数与 ``setuptools.Extension`` 一致。 | ||
|
|
||
| 返回: ``setuptools.Extension`` 对象 |
20 changes: 20 additions & 0 deletions
20
doc/paddle/api/paddle/utils/cpp_extension/get_build_directory_cn.rst
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,20 @@ | ||
| .. _cn_api_paddle_utils_cpp_extension_get_build_directory: | ||
|
|
||
| get_build_directory | ||
| ------------------------------- | ||
|
|
||
| .. py:function:: paddle.utils.cpp_extension.get_build_directory() | ||
|
|
||
| 此接口返回编译自定义 OP 时生成动态链接库所在的 build 目录路径。此目录可以通过 ``export PADDLE_EXTENSION_DIR=XXX`` 来设置。若未设定,则默认使用 ``~/.cache/paddle_extension`` 作为 build 目录。 | ||
|
|
||
|
|
||
| 返回:编译自定义 OP 的 build 目录路径。 | ||
|
|
||
| **代码示例**: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from paddle.utils.cpp_extension import get_build_directory | ||
|
|
||
| build_dir = get_build_directory() | ||
| print(build_dir) |
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,54 @@ | ||
| .. _cn_api_paddle_utils_cpp_extension_load: | ||
|
|
||
| load | ||
| ------------------------------- | ||
|
|
||
| .. py:function:: paddle.utils.cpp_extension.load(name, sources, extra_cxx_cflags=None, extra_cuda_cflags=None, extra_ldflags=None, extra_include_paths=None, build_directory=None, interpreter=None, verbose=False) | ||
|
|
||
| 此接口将即时编译(Just-In-Time)传入的自定义 OP 对应的 cpp 和 cuda 源码文件,返回一个包含自定义算子 API 的 ``Module`` 对象。 | ||
|
|
||
| 其通过子进程的方式,在后台隐式地执行源码文件编译、符号链接、动态库生成、组网 API 接口生成等一系列过程。不需要本地预装 CMake 或者 Ninja 等工具命令,仅需必要的编译器命令环境,如 Linux 下需安装版本不低于 5.4 的 GCC,并软链到 ``/usr/bin/cc`` ;若编译支持 GPU 设备的算子,则需要预装 ``nvcc`` 编译环境。 | ||
|
|
||
| 同时,编译前会执行 `ABI 兼容性检查 <https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html>`_ ,即检查 ``cc`` 命令对应的 GCC 版本是否与编译本地安装的 Paddle 时的 GCC 版本一致。如对于 CUDA 10.1 以上的 Paddle 默认使用 GCC 8.2 编译,则本地 ``cc`` 对应的编译器版本也需为 8.2 ,否则可能由于 ABI 兼容性原因引发自定义 OP 执行期报错。 | ||
|
|
||
| 相对于 :ref:`cn_api_paddle_utils_cpp_extension_setup` 的方式,此接口不需要额外的 ``setup.py`` 文件和 ``python setup.py install`` 命令,``load`` 接口包含了一键执行自定义 OP 的编译和加载的全部流程。 | ||
|
|
||
| .. note:: | ||
|
|
||
| 1. 编译器的 ABI 兼容性是向前兼容的,Linux 下推荐使用 GCC 8.2 高版本作为 ``/usr/bin/cc`` 命令的软链对象。 | ||
| 2. Linux 下可通过 ``which cc`` 查看 ``cc`` 命令的位置;使用 ``cc --version`` 查看对应的 GCC 版本。 | ||
| 3. 目前支持 Linux 和 Windows 平台, MacOS 平台正在支持中。 | ||
|
|
||
|
|
||
| **使用样例如下:** | ||
|
|
||
| .. code-block:: text | ||
|
|
||
| import paddle | ||
| from paddle.utils.cpp_extension import load | ||
|
|
||
| custom_op_module = load( | ||
| name="op_shared_libary_name", # 生成动态链接库的名称 | ||
| sources=['relu_op.cc', 'relu_op.cu'], # 自定义 OP 的源码文件列表 | ||
| extra_cxx_cflags=['-DPADDLE_WITH_MKLDNN'], # 如预装的 Paddle 支持 MKLDNN,需指定此 flag | ||
| extra_cuda_cflags=['-DPADDLE_WITH_MKLDNN'], # 如预装的 Paddle 支持 MKLDNN,需指定此 flag | ||
| interpreter='python3.7', # 可指定使用其他 python 解释器路径 | ||
| verbose=True # 打印编译过程中的日志信息 | ||
| ) | ||
|
|
||
| x = paddle.randn([4, 10], dtype='float32') | ||
| out = custom_op_module.relu(x) | ||
|
|
||
|
|
||
| 参数: | ||
| - **name** (str): 用于指定编译自定义 OP 时,生成的动态链接库的名字,不包括后缀如 .so 或者 .dll | ||
| - **sources** (list[str]): 用于指定自定义 OP 对应的源码文件。cpp 源文件支持 .cc、.cpp 等后缀;cuda 源文件以 .cu 为后缀。 | ||
| - **extra_cxx_cflags** (list[str], 可选): 用于指定编译 cpp 源文件时额外的编译选项。默认情况下,Paddle 框架相关的必要选项均已被隐式地包含;若预装的 Paddle 是支持 MKLDNN 的,则需要在此参数中额外指定 ``-DPADDLE_WITH_MKLDNN`` 。默认值为 None 。 | ||
| - **extra_cuda_cflags** (list[str], 可选): 用于指定编译 cuda 源文件时额外的编译选项。默认情况下,Paddle 框架相关的必要选项均已被隐式地包含;若预装的 Paddle 是支持 MKLDNN 的,则需要在此参数中额外指定 ``-DPADDLE_WITH_MKLDNN`` 。 ``nvcc`` 相关的编译选项请参考: `CUDA Compiler Driver NVCC <https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html>`_ 。 默认值为 None 。 | ||
| - **extra_ldflags** (list[str], 可选): 用于指定编译自定义 OP 时额外的链接选项。GCC 支持的链接选项请参考: `GCC Link Options <https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html>`_ 。 默认值为 None 。 | ||
| - **extra_include_paths** (list[str], 可选): 用于指定编译 cpp 或 cuda 源文件时,额外的头文件搜索目录。默认情况下,Paddle 框架相关头文件所在目录 ``site-packages/paddle/include`` 已被隐式地包含。默认值为 None 。 | ||
| - **build_directory** (str, 可选): 用于指定存放生成动态链接库的目录。若为 None, 则会使用环境变量 ``PADDLE_EXTENSION_DIR`` 的值作为默认的存放目录。可使用 :ref:`cn_api_paddle_utils_cpp_extension_get_build_directory` 接口查看当前的目录设置。默认值为 None 。 | ||
| - **interpreter** (str, 可选): 用于指定执行即时编译所需要的解释器的路径,支持别名和完整路径,默认值为 ``python`` 。若用户本地包含多个版本的 python 环境,需要确保默认的 ``python`` 命令与当前解释器一致,否则需要指定此参数。如当前为 python3.7 环境,可设置此参数为 ``'python3.7'`` 。默认值为 None 。 | ||
| - **verbose** (str, 可选): 用于指定是否需要输出编译过程中的日志信息,默认为 False。 | ||
|
|
||
| 返回: 包含自定义 OP 的可调用 Module 对象。 |
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,69 @@ | ||
| .. _cn_api_paddle_utils_cpp_extension_setup: | ||
|
|
||
| setup | ||
| ------------------------------- | ||
|
|
||
| .. py:function:: paddle.utils.cpp_extension.setup(**attr) | ||
|
|
||
| 此接口用于配置如何编译自定义 OP 源文件,包括编译动态库,自动地生成 Python API 并以 Module 的形式安装到 site-packages 目录等过程。编译完成后,支持通过 ``import`` 语句导入使用。 | ||
|
|
||
| 此接口是对 Python 内建库中的 ``setuptools.setup`` 接口的进一步封装,支持的参数类型,以及使用方式均与原生接口保持一致。接口隐藏了 Paddle 框架内部概念,如默认需要指定的编译选项,头文件搜索目录,链接选项等;此接口会自动搜索和检查本地的 ``cc`` 和 ``nvcc`` 编译命令和版本环境,根据用户指定的 ``Extension`` 类型,完成支持 CPU 或 GPU 设备的算子编译。 | ||
|
|
||
| 同时,编译前会执行 `ABI 兼容性检查 <https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html>`_ ,即检查 ``cc`` 命令对应的 GCC 版本是否与编译本地安装的 Paddle 时的 GCC 版本一致。如对于 CUDA 10.1 以上的 Paddle 默认使用 GCC 8.2 编译,则本地 ``cc`` 对应的编译器版本也需为 8.2 ,否则可能由于 ABI 兼容性原因引发自定义 OP 执行期报错。 | ||
|
|
||
| 相对于即时编译的 :ref:`cn_api_paddle_utils_cpp_extension_load` 接口,此接口仅需执行一次 ``python setup.py install`` 命令,即可像其他 python 库一样 import 导入使用。如下是一个 ``setup.py`` 文件的简单样例: | ||
|
|
||
|
|
||
| .. note:: | ||
|
|
||
| 1. 编译器的 ABI 兼容性是向前兼容的,Linux 下推荐使用 GCC 8.2 高版本作为 ``/usr/bin/cc`` 命令的软链对象。 | ||
| 2. Linux 下可通过 ``which cc`` 查看 ``cc`` 命令的位置;使用 ``cc --version`` 查看对应的 GCC 版本。 | ||
| 3. 目前支持 Linux 和 Windows 平台, MacOS 平台正在支持中。 | ||
|
|
||
| .. code-block:: text | ||
|
|
||
| # setup.py | ||
|
|
||
| # 方式一:编译支持 CPU 和 GPU 的算子 | ||
| from paddle.utils.cpp_extension import CUDAExtension, setup | ||
|
|
||
| setup( | ||
| name='custom_op', # package 的名称,用于 import | ||
| ext_modules=CUDAExtension( | ||
| sources=['relu_op.cc', 'relu_op.cu', 'tanh_op.cc', 'tanh_op.cu'] # 支持同时编译多个 OP | ||
Aurelius84 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ) | ||
|
|
||
| # 方式二:编译支持仅 CPU 的算子 | ||
| from paddle.utils.cpp_extension import CppExtension, setup | ||
|
|
||
| setup( | ||
| name='custom_op', # package 的名称,用于 import | ||
| ext_modules=CppExtension( | ||
| sources=['relu_op.cc', 'tanh_op.cc'] # 支持同时编译多个 OP | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| 在源文件所在目录下执行 ``python setup.py install`` 即可完成自定义 OP 编译和 ``custom_op`` 库的安装。在组网时,可以通过如下方式使用: | ||
|
|
||
| .. code-block:: text | ||
|
|
||
| import paddle | ||
| from custom_op import relu, tanh | ||
|
|
||
| x = paddle.randn([4, 10], dtype='float32') | ||
| relu_out = relu(x) | ||
| tanh_out = tanh(x) | ||
|
|
||
|
|
||
|
|
||
| 参数: | ||
| - **name** (string) - 用于指定生成的动态链接库的名称,以及安装到 site-packages 的 ``Module`` 名字 | ||
| - **ext_modules** (Extension): 用于指定包含自定义 OP 必要源文件、编译选项等信息的 ``Extension`` 。若只编译运行在 CPU 设备上的 OP,请使用 :ref:`cn_api_paddle_utils_cpp_extension_CppExtension` ; 若编译同时支持 GPU 设备上的 OP, 请使用 :ref:`cn_api_paddle_utils_cpp_extension_CUDAExtension` 。 | ||
| - **include_dirs** (list[str], 可选): 用于指定编译自定义 OP 时额外的头文件搜索目录。此接口默认会自动添加 ``site-packages/paddle/include`` 目录。若自定义 OP 源码引用了其他三方库文件,可以通过此参数指定三方库的搜索目录。默认值为 None 。 | ||
| - **extra_compile_args** (list[str] | dict, 可选): 用于指定编译自定义 OP 时额外的编译选项,如 ``-O3`` 等。若为 ``list[str]`` 类型,则表示这些编译选项会同时应用到 ``cc`` 和 ``nvcc`` 编译过程;可以通过 ``{'cxx': [...], 'nvcc': [...]}`` 字典的形式单独指定额外的 ``cc`` 或 ``nvcc`` 的编译选项。默认值为 None 。 | ||
| - **\*\*attr** (dict, 可选) - 其他参数与 ``setuptools.setup`` 一致。 | ||
|
|
||
| 返回:None | ||
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.
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.
在第8行前加一列吧 叫
自定义OP相关API,然后把这5个API归为一类,这样会好一点,写法可以参考工具类相关APIThere 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, Done