@@ -1423,7 +1423,7 @@ PD_BUILD_GRAD_OP(custom_add)
14231423
14241424### 使用 ` setuptools ` 编译
14251425
1426- 该方式是对 ` python ` 内建库中的 ` setuptools.setup ` 接口的进一步封装,能够自动地生成 Python API 并以 Module 的形式安装到 ` site-packages ` 目录。编译完成后,支持通过 ` import ` 语句导入使用。
1426+ 该方式是对 ` python ` 内建库中的 ` setuptools.setup ` 接口的进一步封装,能够自动地生成 Python API 并以 Module 的形式安装到 ` site-packages ` 或者 ` dist-packages ` 目录。编译完成后,支持通过 ` import ` 语句导入使用。
14271427
14281428您需要编写 ` setup.py ` 文件, 配置自定义算子的编译规则。
14291429
@@ -1459,68 +1459,85 @@ setup(
14591459
14601460执行 ` python setup_cpu.py install ` 或者 ` python setup_cuda.py install ` 即可一键完成自定义算子的编译和安装。
14611461
1462- 以 ` python setup_cuda.py install ` 为例,执行日志如下:
1462+ > 注:在当前目录只有一个 ` setup.py ` 文件时,可以使用 ` pip install . --no-build-isolation ` 这种现代化的方式进行构建。
1463+
1464+ 以 ` python setup_cpu.py install ` 为例,执行日志如下 (省略日志时间,以及 setuptools 版本相关警告等信息):
14631465
14641466```
1465- running install
1466- running bdist_egg
1467- running egg_info
1468- writing custom_setup_ops.egg-info/PKG-INFO
1469- writing dependency_links to custom_setup_ops.egg-info/dependency_links.txt
1470- writing top-level names to custom_setup_ops.egg-info/top_level.txt
1471- reading manifest file 'custom_setup_ops.egg-info/SOURCES.txt'
1472- writing manifest file 'custom_setup_ops.egg-info/SOURCES.txt'
1473- installing library code to build/custom_setup_ops/bdist.linux-x86_64/egg
1474- running install_lib
1475- running build_ext
1476- /usr/local/lib/python3.7/site-packages/paddle/fluid/layers/utils.py:77: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
1477- return (isinstance(seq, collections.Sequence) and
1467+ [ INFO] dist.py:1018 - running install
1468+ [ INFO] dist.py:1018 - running build
1469+ [ INFO] dist.py:1018 - running build_ext
14781470Compiling user custom op, it will cost a few seconds.....
1479- creating build/custom_setup_ops/bdist.linux-x86_64/egg
1480- copying build/custom_setup_ops/lib.linux-x86_64-3.7/version.txt -> build/custom_setup_ops/bdist.linux-x86_64/egg
1481- copying build/custom_setup_ops/lib.linux-x86_64-3.7/relu_cpu.o -> build/custom_setup_ops/bdist.linux-x86_64/egg
1482- copying build/custom_setup_ops/lib.linux-x86_64-3.7/relu_cuda.o -> build/custom_setup_ops/bdist.linux-x86_64/egg
1483- copying build/custom_setup_ops/lib.linux-x86_64-3.7/relu_cuda.cu.o -> build/custom_setup_ops/bdist.linux-x86_64/egg
1484- copying build/custom_setup_ops/lib.linux-x86_64-3.7/custom_setup_ops.so -> build/custom_setup_ops/bdist.linux-x86_64/egg
1485- creating stub loader for custom_setup_ops.so
1486- byte-compiling build/custom_setup_ops/bdist.linux-x86_64/egg/custom_setup_ops.py to custom_setup_ops.cpython-37.pyc
1487- creating build/custom_setup_ops/bdist.linux-x86_64/egg/EGG-INFO
1488- copying custom_setup_ops.egg-info/PKG-INFO -> build/custom_setup_ops/bdist.linux-x86_64/egg/EGG-INFO
1489- copying custom_setup_ops.egg-info/SOURCES.txt -> build/custom_setup_ops/bdist.linux-x86_64/egg/EGG-INFO
1490- copying custom_setup_ops.egg-info/dependency_links.txt -> build/custom_setup_ops/bdist.linux-x86_64/egg/EGG-INFO
1491- copying custom_setup_ops.egg-info/not-zip-safe -> build/custom_setup_ops/bdist.linux-x86_64/egg/EGG-INFO
1492- copying custom_setup_ops.egg-info/top_level.txt -> build/custom_setup_ops/bdist.linux-x86_64/egg/EGG-INFO
1493- writing build/custom_setup_ops/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
1494- creating 'dist/custom_setup_ops-0.0.0-py3.7-linux-x86_64.egg' and adding 'build/custom_setup_ops/bdist.linux-x86_64/egg' to it
1495- removing 'build/custom_setup_ops/bdist.linux-x86_64/egg' (and everything under it)
1496- Processing custom_setup_ops-0.0.0-py3.7-linux-x86_64.egg
1497- creating /usr/local/lib/python3.7/site-packages/custom_setup_ops-0.0.0-py3.7-linux-x86_64.egg
1498- Extracting custom_setup_ops-0.0.0-py3.7-linux-x86_64.egg to /usr/local/lib/python3.7/site-packages
1499- Adding custom-setup-ops 0.0.0 to easy-install.pth file
1500-
1501- Installed /usr/local/lib/python3.7/site-packages/custom_setup_ops-0.0.0-py3.7-linux-x86_64.egg
1502- Processing dependencies for custom-setup-ops==0.0.0
1503- Finished processing dependencies for custom-setup-ops==0.0.0
1471+ [ INFO] build_ext.py:538 - building 'custom_setup_ops' extension
1472+ [ INFO] dir_util.py:58 - creating /paddle/Paddle/build/tmp_setuptools/build/custom_setup_ops/lib.linux-x86_64-cpython-39/build/custom_setup_ops/temp.linux-x86_64-cpython-39
1473+ [ INFO] spawn.py:77 - x86_64-linux-gnu-g++ -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -fPIC -I/usr/local/lib/python3.9/dist-packages/paddle/include -I/usr/local/lib/python3.9/dist-packages/paddle/include/third_party -I/usr/local/lib/python3.9/dist-packages/paddle/include/paddle/phi/api/include/compat -I/usr/local/lib/python3.9/dist-packages/paddle/include/paddle/phi/api/include/compat/torch/csrc/api/include -I/usr/include/python3.9 -I/usr/include/python3.9 -c /paddle/Paddle/build/tmp_setuptools/relu_cpu.cc -o /paddle/Paddle/build/tmp_setuptools/build/custom_setup_ops/lib.linux-x86_64-cpython-39/build/custom_setup_ops/temp.linux-x86_64-cpython-39/relu_cpu.o -w -DPADDLE_WITH_CUSTOM_KERNEL -DPADDLE_EXTENSION_NAME=custom_setup_ops -D_GLIBCXX_USE_CXX11_ABI=1 -std=c++17
1474+ /paddle/Paddle/build/tmp_setuptools/build/custom_setup_ops/lib.linux-x86_64-cpython-39/build/custom_setup_ops/temp.linux-x86_64-cpython-39/relu_cpu.o is compiled
1475+ [ INFO] spawn.py:77 - x86_64-linux-gnu-g++ -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -shared -Wl,-O1 -Wl,-Bsymbolic-functions /paddle/Paddle/build/tmp_setuptools/build/custom_setup_ops/lib.linux-x86_64-cpython-39/build/custom_setup_ops/temp.linux-x86_64-cpython-39/relu_cpu.o -L/usr/local/lib/python3.9/dist-packages/paddle/libs -L/usr/local/lib/python3.9/dist-packages/paddle/base -L/usr/lib/x86_64-linux-gnu -Wl,--enable-new-dtags,-rpath,/usr/local/lib/python3.9/dist-packages/paddle/libs -Wl,--enable-new-dtags,-rpath,/usr/local/lib/python3.9/dist-packages/paddle/base -o build/custom_setup_ops/lib.linux-x86_64-cpython-39/custom_setup_ops.so -l:libpaddle.so
1476+ Received len(custom_op) = 1, using custom operator
1477+ Removed: build/custom_setup_ops/lib.linux-x86_64-cpython-39/build/custom_setup_ops/temp.linux-x86_64-cpython-39/relu_cpu.o
1478+ [ INFO] dist.py:1018 - running install_lib
1479+ [ INFO] file_util.py:130 - copying build/custom_setup_ops/lib.linux-x86_64-cpython-39/version.txt -> /usr/local/lib/python3.9/dist-packages
1480+ [ INFO] file_util.py:130 - copying build/custom_setup_ops/lib.linux-x86_64-cpython-39/custom_setup_ops.py -> /usr/local/lib/python3.9/dist-packages
1481+ [ INFO] file_util.py:130 - copying build/custom_setup_ops/lib.linux-x86_64-cpython-39/custom_setup_ops.so -> /usr/local/lib/python3.9/dist-packages
1482+ [ INFO] util.py:485 - byte-compiling /usr/local/lib/python3.9/dist-packages/custom_setup_ops.py to custom_setup_ops.cpython-39.pyc
1483+ [ INFO] dist.py:1018 - running install_egg_info
1484+ [ INFO] dist.py:1018 - running egg_info
1485+ [ INFO] dir_util.py:58 - creating custom_setup_ops.egg-info
1486+ [ INFO] egg_info.py:651 - writing custom_setup_ops.egg-info/PKG-INFO
1487+ [ INFO] egg_info.py:279 - writing dependency_links to custom_setup_ops.egg-info/dependency_links.txt
1488+ [ INFO] egg_info.py:279 - writing top-level names to custom_setup_ops.egg-info/top_level.txt
1489+ [ INFO] util.py:332 - writing manifest file 'custom_setup_ops.egg-info/SOURCES.txt'
1490+ [ INFO] sdist.py:203 - reading manifest file 'custom_setup_ops.egg-info/SOURCES.txt'
1491+ [ INFO] util.py:332 - writing manifest file 'custom_setup_ops.egg-info/SOURCES.txt'
1492+ [ INFO] util.py:332 - Copying custom_setup_ops.egg-info to /usr/local/lib/python3.9/dist-packages/custom_setup_ops-0.0.0-py3.9.egg-info
1493+ [ INFO] dist.py:1018 - running install_scripts
15041494```
15051495
1506- 执行成功后,如日志所示,自定义算子模块 ` custom_setup_ops ` 被安装至如下目录 :
1496+ 执行成功后,如日志所示,自定义算子模块 ` custom_setup_ops ` 的安装信息如下 :
15071497
1508- ` /usr/local/lib/python3.7/site-packages/custom_setup_ops-0.0.0-py3.7-linux-x86_64.egg `
1498+ ``` shell
15091499
1510- ` custom_setup_ops-0.0.0-py3.7-linux-x86_64.egg ` 目录中内容如下:
1500+ > pip show custom_setup_ops
1501+ Name: custom_setup_ops
1502+ Version: 0.0.0
1503+ Summary:
1504+ Home-page:
1505+ Author:
1506+ Author-email:
1507+ License:
1508+ Location: /usr/local/lib/python3.9/dist-packages
1509+ Requires:
1510+ Required-by:
15111511
15121512```
1513- custom_setup_ops_pd_.so EGG-INFO/ relu_cpu.o relu_cuda.o
1514- custom_setup_ops.py __pycache__/ relu_cuda.cu.o version.txt
1513+
1514+ 其目录结构如下:
1515+
1516+ ``` shell
1517+
1518+ > tree /usr/local/lib/python3.9/dist-packages/custom_setup_ops*
1519+ /usr/local/lib/python3.9/dist-packages/custom_setup_ops
1520+ | -- __init__.py
1521+ ` -- custom_setup_ops_pd_.so
1522+ /usr/local/lib/python3.9/dist-packages/custom_setup_ops-0.0.0-py3.9.egg-info
1523+ | -- PKG-INFO
1524+ | -- SOURCES.txt
1525+ | -- dependency_links.txt
1526+ | -- not-zip-safe
1527+ ` -- top_level.txt
1528+
15151529```
15161530
1517- 其中 ` custom_setup_ops_pd_.so ` 为自定义算子编译生成的动态库,` custom_setup_ops.py ` 为根据 ` PaddlePaddle ` 接口的定义规则,自动生成的自定义算子 python 模块源码,其示例内容为(自动生成的代码后续可能会更新,生成结果可能与示例代码不一致):
1531+ 其中 ` custom_setup_ops_pd_.so ` 为自定义算子编译生成的动态库,` custom_setup_ops/__init__ .py ` 为根据 ` PaddlePaddle ` 接口的定义规则,自动生成的自定义算子 python 模块源码,其示例内容为(自动生成的代码后续可能会更新,生成结果可能与示例代码不一致):
15181532
15191533``` python
1534+
15201535from paddle import _C_ops
15211536from paddle.framework import in_dynamic_or_pir_mode
15221537from paddle.base.layer_helper import LayerHelper
1538+ from paddle.jit.marker import unified
15231539
1540+ @unified
15241541def custom_relu (x ):
15251542 # The output variable's dtype use default value 'float32',
15261543 # and the actual dtype of output variable will be inferred in runtime.
@@ -1588,9 +1605,21 @@ __bootstrap__()
15881605``` python
15891606import paddle
15901607from custom_setup_ops import custom_relu
1608+ paddle.set_device(' cpu' )
15911609
15921610x = paddle.randn([4 , 10 ], dtype = ' float32' )
15931611relu_out = custom_relu(x)
1612+
1613+ # `relu_out` should be like:
1614+ # Tensor(shape=[4, 10], dtype=float32, place=Place(cpu), stop_gradient=True,
1615+ # [[0.02314972, 0.65578228, 0. , 0. , 0.18305063, 0. ,
1616+ # 0.67343038, 0. , 1.16782570, 1.71236455],
1617+ # [0. , 0.60349381, 0. , 0. , 0. , 0. ,
1618+ # 0.14162211, 0. , 0. , 0.33964530],
1619+ # [0. , 0. , 0.12062856, 0.18853758, 0.54154527, 0.73217475,
1620+ # 0. , 0. , 0. , 0. ],
1621+ # [0.04105225, 0. , 0.67857188, 0.95838499, 1.08346415, 2.47209001,
1622+ # 0. , 0. , 0.22969440, 1.08237624]])
15941623```
15951624
15961625> 注:` setuptools ` 的封装是为了简化自定义算子编译和使用流程,即使不依赖于 ` setuptools ` ,也可以自行编译生成动态库,并封装相应的 python API,然后在基于 ` PaddlePaddle ` 实现的模型中使用
0 commit comments