@@ -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,83 @@ setup(
14591459
14601460执行 ` python setup_cpu.py install ` 或者 ` python setup_cuda.py install ` 即可一键完成自定义算子的编译和安装。
14611461
1462- 以 ` python setup_cuda .py install ` 为例,执行日志如下:
1462+ 以 ` python setup_cpu .py install ` 为例,执行日志如下 (省略日志时间,以及 setuptools 版本相关警告等信息) :
14631463
14641464```
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
1465+ [ INFO] dist.py:1018 - running install
1466+ [ INFO] dist.py:1018 - running build
1467+ [ INFO] dist.py:1018 - running build_ext
14781468Compiling 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
1469+ [ INFO] build_ext.py:538 - building 'custom_setup_ops' extension
1470+ [ 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
1471+ [ 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
1472+ /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
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 -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
1474+ Received len(custom_op) = 1, using custom operator
1475+ Removed: build/custom_setup_ops/lib.linux-x86_64-cpython-39/build/custom_setup_ops/temp.linux-x86_64-cpython-39/relu_cpu.o
1476+ [ INFO] dist.py:1018 - running install_lib
1477+ [ 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
1478+ [ 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
1479+ [ 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
1480+ [ INFO] util.py:485 - byte-compiling /usr/local/lib/python3.9/dist-packages/custom_setup_ops.py to custom_setup_ops.cpython-39.pyc
1481+ [ INFO] dist.py:1018 - running install_egg_info
1482+ [ INFO] dist.py:1018 - running egg_info
1483+ [ INFO] dir_util.py:58 - creating custom_setup_ops.egg-info
1484+ [ INFO] egg_info.py:651 - writing custom_setup_ops.egg-info/PKG-INFO
1485+ [ INFO] egg_info.py:279 - writing dependency_links to custom_setup_ops.egg-info/dependency_links.txt
1486+ [ INFO] egg_info.py:279 - writing top-level names to custom_setup_ops.egg-info/top_level.txt
1487+ [ INFO] util.py:332 - writing manifest file 'custom_setup_ops.egg-info/SOURCES.txt'
1488+ [ INFO] sdist.py:203 - reading manifest file 'custom_setup_ops.egg-info/SOURCES.txt'
1489+ [ INFO] util.py:332 - writing manifest file 'custom_setup_ops.egg-info/SOURCES.txt'
1490+ [ 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
1491+ [ INFO] dist.py:1018 - running install_scripts
15041492```
15051493
1506- 执行成功后,如日志所示,自定义算子模块 ` custom_setup_ops ` 被安装至如下目录 :
1494+ 执行成功后,如日志所示,自定义算子模块 ` custom_setup_ops ` 的安装信息如下 :
15071495
1508- ` /usr/local/lib/python3.7/site-packages/custom_setup_ops-0.0.0-py3.7-linux-x86_64.egg `
1496+ ``` shell
15091497
1510- ` custom_setup_ops-0.0.0-py3.7-linux-x86_64.egg ` 目录中内容如下:
1498+ > pip show custom_setup_ops
1499+ Name: custom_setup_ops
1500+ Version: 0.0.0
1501+ Summary:
1502+ Home-page:
1503+ Author:
1504+ Author-email:
1505+ License:
1506+ Location: /usr/local/lib/python3.9/dist-packages
1507+ Requires:
1508+ Required-by:
15111509
15121510```
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
1511+
1512+ 其目录结构如下:
1513+
1514+ ``` shell
1515+
1516+ > tree /usr/local/lib/python3.9/dist-packages/custom_setup_ops*
1517+ /usr/local/lib/python3.9/dist-packages/custom_setup_ops
1518+ | -- __init__.py
1519+ ` -- custom_setup_ops_pd_.so
1520+ /usr/local/lib/python3.9/dist-packages/custom_setup_ops-0.0.0-py3.9.egg-info
1521+ | -- PKG-INFO
1522+ | -- SOURCES.txt
1523+ | -- dependency_links.txt
1524+ | -- not-zip-safe
1525+ ` -- top_level.txt
1526+
15151527```
15161528
1517- 其中 ` custom_setup_ops_pd_.so ` 为自定义算子编译生成的动态库,` custom_setup_ops.py ` 为根据 ` PaddlePaddle ` 接口的定义规则,自动生成的自定义算子 python 模块源码,其示例内容为(自动生成的代码后续可能会更新,生成结果可能与示例代码不一致):
1529+ 其中 ` custom_setup_ops_pd_.so ` 为自定义算子编译生成的动态库,` custom_setup_ops/__init__ .py ` 为根据 ` PaddlePaddle ` 接口的定义规则,自动生成的自定义算子 python 模块源码,其示例内容为(自动生成的代码后续可能会更新,生成结果可能与示例代码不一致):
15181530
15191531``` python
1532+
15201533from paddle import _C_ops
15211534from paddle.framework import in_dynamic_or_pir_mode
15221535from paddle.base.layer_helper import LayerHelper
1536+ from paddle.jit.marker import unified
15231537
1538+ @unified
15241539def custom_relu (x ):
15251540 # The output variable's dtype use default value 'float32',
15261541 # and the actual dtype of output variable will be inferred in runtime.
@@ -1586,11 +1601,24 @@ __bootstrap__()
15861601随后,可以直接在构建模型过程中导入使用,简单示例如下:
15871602
15881603``` python
1589- import paddle
1590- from custom_setup_ops import custom_relu
1591-
1592- x = paddle.randn([4 , 10 ], dtype = ' float32' )
1593- relu_out = custom_relu(x)
1604+ In [1 ]: import paddle
1605+ ... : from custom_setup_ops import custom_relu
1606+ ... : paddle.set_device(' cpu' )
1607+ ... :
1608+ ... : x = paddle.randn([4 , 10 ], dtype = ' float32' )
1609+ ... : relu_out = custom_relu(x)
1610+
1611+ In [2 ]: relu_out
1612+ Out[2 ]:
1613+ Tensor(shape = [4 , 10 ], dtype = float32, place = Place(cpu), stop_gradient = True ,
1614+ [[0.02314972 , 0.65578228 , 0 . , 0 . , 0.18305063 , 0 . ,
1615+ 0.67343038 , 0 . , 1.16782570 , 1.71236455 ],
1616+ [0 . , 0.60349381 , 0 . , 0 . , 0 . , 0 . ,
1617+ 0.14162211 , 0 . , 0 . , 0.33964530 ],
1618+ [0 . , 0 . , 0.12062856 , 0.18853758 , 0.54154527 , 0.73217475 ,
1619+ 0 . , 0 . , 0 . , 0 . ],
1620+ [0.04105225 , 0 . , 0.67857188 , 0.95838499 , 1.08346415 , 2.47209001 ,
1621+ 0 . , 0 . , 0.22969440 , 1.08237624 ]])
15941622```
15951623
15961624> 注:` setuptools ` 的封装是为了简化自定义算子编译和使用流程,即使不依赖于 ` setuptools ` ,也可以自行编译生成动态库,并封装相应的 python API,然后在基于 ` PaddlePaddle ` 实现的模型中使用
0 commit comments