Skip to content

Commit fafceed

Browse files
committed
[CppExtension] 更新自定义算子文档,优化代码示例格式并添加现代构建方式说明
1 parent cb01cb3 commit fafceed

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

docs/guides/custom_op/new_cpp_op_cn.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,8 @@ setup(
14591459

14601460
执行 `python setup_cpu.py install` 或者 `python setup_cuda.py install` 即可一键完成自定义算子的编译和安装。
14611461

1462+
> 注:在当前目录只有一个 `setup.py` 文件时,可以使用 `pip install . --no-build-isolation` 这种现代化的方式进行构建。
1463+
14621464
`python setup_cpu.py install` 为例,执行日志如下 (省略日志时间,以及 setuptools 版本相关警告等信息):
14631465

14641466
```
@@ -1601,24 +1603,23 @@ __bootstrap__()
16011603
随后,可以直接在构建模型过程中导入使用,简单示例如下:
16021604

16031605
```python
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]])
1606+
import paddle
1607+
from custom_setup_ops import custom_relu
1608+
paddle.set_device('cpu')
1609+
1610+
x = paddle.randn([4, 10], dtype='float32')
1611+
relu_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]])
16221623
```
16231624

16241625
> 注:`setuptools` 的封装是为了简化自定义算子编译和使用流程,即使不依赖于 `setuptools`,也可以自行编译生成动态库,并封装相应的 python API,然后在基于 `PaddlePaddle` 实现的模型中使用

0 commit comments

Comments
 (0)