Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
b06ec0c7
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
b06ec0c7
编写于
4月 24, 2023
作者:
H
HongyuJia
提交者:
GitHub
4月 24, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[CppExtension Unittest] Add unit test of vector<Tensor> (#53040)
上级
81fb7df2
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
43 addition
and
0 deletion
+43
-0
python/paddle/fluid/tests/cpp_extension/custom_extension.cc
python/paddle/fluid/tests/cpp_extension/custom_extension.cc
+11
-0
python/paddle/fluid/tests/cpp_extension/test_cpp_extension_jit.py
...addle/fluid/tests/cpp_extension/test_cpp_extension_jit.py
+15
-0
python/paddle/fluid/tests/cpp_extension/test_cpp_extension_setup.py
...dle/fluid/tests/cpp_extension/test_cpp_extension_setup.py
+17
-0
未找到文件。
python/paddle/fluid/tests/cpp_extension/custom_extension.cc
浏览文件 @
b06ec0c7
...
...
@@ -26,6 +26,16 @@ paddle::Tensor custom_add(const paddle::Tensor& x, const paddle::Tensor& y) {
return
x
.
exp
()
+
y
.
exp
();
}
std
::
vector
<
paddle
::
Tensor
>
custom_tensor
(
const
std
::
vector
<
paddle
::
Tensor
>&
inputs
)
{
std
::
vector
<
paddle
::
Tensor
>
out
;
out
.
reserve
(
inputs
.
size
());
for
(
const
auto
&
input
:
inputs
)
{
out
.
push_back
(
input
+
1.0
);
}
return
out
;
}
paddle
::
Tensor
nullable_tensor
(
bool
return_none
=
false
)
{
paddle
::
Tensor
t
;
if
(
!
return_none
)
{
...
...
@@ -45,6 +55,7 @@ paddle::optional<paddle::Tensor> optional_tensor(bool return_option = false) {
PYBIND11_MODULE
(
custom_cpp_extension
,
m
)
{
m
.
def
(
"custom_add"
,
&
custom_add
,
"exp(x) + exp(y)"
);
m
.
def
(
"custom_sub"
,
&
custom_sub
,
"exp(x) - exp(y)"
);
m
.
def
(
"custom_tensor"
,
&
custom_tensor
,
"x + 1"
);
m
.
def
(
"nullable_tensor"
,
&
nullable_tensor
,
"returned Tensor might be None"
);
m
.
def
(
"optional_tensor"
,
&
optional_tensor
,
"returned Tensor might be optional"
);
...
...
python/paddle/fluid/tests/cpp_extension/test_cpp_extension_jit.py
浏览文件 @
b06ec0c7
...
...
@@ -68,6 +68,7 @@ class TestCppExtensionJITInstall(unittest.TestCase):
def
test_cpp_extension
(
self
):
self
.
_test_extension_function
()
self
.
_test_extension_class
()
self
.
_test_vector_tensor
()
self
.
_test_nullable_tensor
()
self
.
_test_optional_tensor
()
if
paddle
.
is_compiled_with_cuda
():
...
...
@@ -109,6 +110,20 @@ class TestCppExtensionJITInstall(unittest.TestCase):
atol
=
1e-5
,
)
def
_test_vector_tensor
(
self
):
for
dtype
in
self
.
dtypes
:
np_inputs
=
[
np
.
random
.
uniform
(
-
1
,
1
,
[
4
,
8
]).
astype
(
dtype
)
for
_
in
range
(
3
)
]
inputs
=
[
paddle
.
to_tensor
(
np_x
,
dtype
=
dtype
)
for
np_x
in
np_inputs
]
out
=
custom_cpp_extension
.
custom_tensor
(
inputs
)
target_out
=
[
x
+
1.0
for
x
in
inputs
]
for
i
in
range
(
3
):
np
.
testing
.
assert_allclose
(
out
[
i
].
numpy
(),
target_out
[
i
].
numpy
(),
atol
=
1e-5
)
def
_test_nullable_tensor
(
self
):
x
=
custom_cpp_extension
.
nullable_tensor
(
True
)
assert
x
is
None
,
"Return None when input parameter return_none = True"
...
...
python/paddle/fluid/tests/cpp_extension/test_cpp_extension_setup.py
浏览文件 @
b06ec0c7
...
...
@@ -148,6 +148,7 @@ class TestCppExtensionSetupInstall(unittest.TestCase):
# Extension
self
.
_test_extension_function_plain
()
self
.
_test_extension_function_mixed
()
self
.
_test_vector_tensor
()
self
.
_test_extension_class
()
self
.
_test_nullable_tensor
()
self
.
_test_optional_tensor
()
...
...
@@ -218,6 +219,22 @@ class TestCppExtensionSetupInstall(unittest.TestCase):
atol
=
1e-5
,
)
def
_test_vector_tensor
(
self
):
import
custom_cpp_extension
for
dtype
in
self
.
dtypes
:
np_inputs
=
[
np
.
random
.
uniform
(
-
1
,
1
,
[
4
,
8
]).
astype
(
dtype
)
for
_
in
range
(
3
)
]
inputs
=
[
paddle
.
to_tensor
(
np_x
,
dtype
=
dtype
)
for
np_x
in
np_inputs
]
out
=
custom_cpp_extension
.
custom_tensor
(
inputs
)
target_out
=
[
x
+
1
for
x
in
inputs
]
for
i
in
range
(
3
):
np
.
testing
.
assert_allclose
(
out
[
i
].
numpy
(),
target_out
[
i
].
numpy
(),
atol
=
1e-5
)
def
_test_nullable_tensor
(
self
):
import
custom_cpp_extension
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录