Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
e79867b6
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
e79867b6
编写于
2月 06, 2023
作者:
zhouweiwei2014
提交者:
GitHub
2月 06, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Zero-Dim]Add some 0D Tensor UT (#50169)
上级
b3e5b0c4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
226 addition
and
30 deletion
+226
-30
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
+150
-17
python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
...dle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
+76
-13
未找到文件。
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
浏览文件 @
e79867b6
...
...
@@ -83,7 +83,6 @@ unary_api_list = [
paddle
.
lgamma
,
paddle
.
poisson
,
paddle
.
bernoulli
,
paddle
.
median
,
paddle
.
nn
.
functional
.
softmax
,
paddle
.
nn
.
functional
.
log_softmax
,
paddle
.
nn
.
functional
.
gumbel_softmax
,
...
...
@@ -193,8 +192,6 @@ reduce_api_list = [
paddle
.
logsumexp
,
paddle
.
all
,
paddle
.
any
,
paddle
.
argmax
,
paddle
.
argmin
,
]
...
...
@@ -216,10 +213,10 @@ class TestReduceAPI(unittest.TestCase):
self
.
assertEqual
(
x
.
shape
,
[])
self
.
assertEqual
(
out
.
shape
,
[])
if
api
not
in
[
paddle
.
argmax
,
paddle
.
argmin
]:
np
.
testing
.
assert_allclose
(
out
.
numpy
(),
x
.
numpy
())
out_empty_list
=
api
(
x
,
[])
self
.
assertEqual
(
out_empty_list
,
out
)
np
.
testing
.
assert_allclose
(
out
.
numpy
(),
x
.
numpy
())
out_empty_list
=
api
(
x
,
[])
self
.
assertEqual
(
out_empty_list
,
out
)
if
x
.
grad
is
not
None
:
self
.
assertEqual
(
x
.
grad
.
shape
,
[])
...
...
@@ -257,8 +254,7 @@ class TestReduceAPI(unittest.TestCase):
res
=
exe
.
run
(
main_prog
,
fetch_list
=
fetch_list
)
self
.
assertEqual
(
res
[
0
].
shape
,
())
self
.
assertEqual
(
res
[
1
].
shape
,
())
if
api
not
in
[
paddle
.
argmax
,
paddle
.
argmin
]:
np
.
testing
.
assert_allclose
(
res
[
0
],
res
[
1
])
np
.
testing
.
assert_allclose
(
res
[
0
],
res
[
1
])
if
len
(
res
)
>
2
:
self
.
assertEqual
(
res
[
2
].
shape
,
())
...
...
@@ -393,22 +389,40 @@ class TestBinaryAPI(unittest.TestCase):
for
api
in
binary_int_api_list
:
# 1) x is 0D, y is 0D
x
=
paddle
.
randint
(
-
10
,
10
,
[])
y
=
paddle
.
randint
(
-
10
,
10
,
[])
x_np
=
np
.
random
.
randint
(
-
10
,
10
,
[])
y_np
=
np
.
random
.
randint
(
-
10
,
10
,
[])
out_np
=
eval
(
'np.%s(x_np, y_np)'
%
api
.
__name__
)
x
=
paddle
.
to_tensor
(
x_np
)
y
=
paddle
.
to_tensor
(
y_np
)
out
=
api
(
x
,
y
)
self
.
assertEqual
(
out
.
shape
,
[])
np
.
testing
.
assert_array_equal
(
out
.
numpy
(),
out_np
)
# 2) x is ND, y is 0D
x
=
paddle
.
randint
(
-
10
,
10
,
[
3
,
5
])
y
=
paddle
.
randint
(
-
10
,
10
,
[])
x_np
=
np
.
random
.
randint
(
-
10
,
10
,
[
3
,
5
])
y_np
=
np
.
random
.
randint
(
-
10
,
10
,
[])
out_np
=
eval
(
'np.%s(x_np, y_np)'
%
api
.
__name__
)
x
=
paddle
.
to_tensor
(
x_np
)
y
=
paddle
.
to_tensor
(
y_np
)
out
=
api
(
x
,
y
)
self
.
assertEqual
(
out
.
shape
,
[
3
,
5
])
np
.
testing
.
assert_array_equal
(
out
.
numpy
(),
out_np
)
# 3) x is 0D , y is ND
x
=
paddle
.
randint
(
-
10
,
10
,
[])
y
=
paddle
.
randint
(
-
10
,
10
,
[
3
,
5
])
x_np
=
np
.
random
.
randint
(
-
10
,
10
,
[])
y_np
=
np
.
random
.
randint
(
-
10
,
10
,
[
3
,
5
])
out_np
=
eval
(
'np.%s(x_np, y_np)'
%
api
.
__name__
)
x
=
paddle
.
to_tensor
(
x_np
)
y
=
paddle
.
to_tensor
(
y_np
)
out
=
api
(
x
,
y
)
self
.
assertEqual
(
out
.
shape
,
[
3
,
5
])
np
.
testing
.
assert_array_equal
(
out
.
numpy
(),
out_np
)
paddle
.
enable_static
()
...
...
@@ -557,6 +571,57 @@ class TestSundryAPI(unittest.TestCase):
paddle
.
disable_static
()
self
.
x
=
paddle
.
rand
([])
def
_test_argmin
(
self
):
x
=
paddle
.
rand
([])
out1
=
paddle
.
argmin
(
x
,
0
)
out2
=
paddle
.
argmin
(
x
,
-
1
)
out3
=
paddle
.
argmin
(
x
,
None
)
self
.
assertEqual
(
out1
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out1
,
0.0
)
self
.
assertEqual
(
out2
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out2
,
0.0
)
self
.
assertEqual
(
out3
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out3
,
0.0
)
def
_test_argmax
(
self
):
x
=
paddle
.
rand
([])
out1
=
paddle
.
argmax
(
x
,
0
)
out2
=
paddle
.
argmax
(
x
,
-
1
)
out3
=
paddle
.
argmax
(
x
,
None
)
self
.
assertEqual
(
out1
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out1
,
0.0
)
self
.
assertEqual
(
out2
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out2
,
0.0
)
self
.
assertEqual
(
out3
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out3
,
0.0
)
def
test_median
(
self
):
x
=
paddle
.
rand
([])
x
.
stop_gradient
=
False
out1
=
paddle
.
median
(
x
,
0
)
out2
=
paddle
.
median
(
x
,
-
1
)
out3
=
paddle
.
median
(
x
,
None
)
out1
.
backward
()
out2
.
backward
()
out3
.
backward
()
self
.
assertEqual
(
out1
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out1
,
x
)
self
.
assertEqual
(
out2
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out2
,
x
)
self
.
assertEqual
(
out3
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out3
,
x
)
self
.
assertEqual
(
x
.
grad
.
shape
,
[])
np
.
testing
.
assert_allclose
(
x
.
grad
,
3.0
)
def
test_quantile
(
self
):
# 1) x is 0D
x
=
paddle
.
rand
([])
...
...
@@ -1531,6 +1596,74 @@ class TestSundryAPIStatic(unittest.TestCase):
paddle
.
enable_static
()
self
.
exe
=
paddle
.
static
.
Executor
()
@
prog_scope
()
def
_test_argmin
(
self
):
x
=
paddle
.
rand
([])
out1
=
paddle
.
argmin
(
x
,
0
)
out2
=
paddle
.
argmin
(
x
,
-
1
)
out3
=
paddle
.
argmin
(
x
,
None
)
prog
=
paddle
.
static
.
default_main_program
()
res
=
self
.
exe
.
run
(
prog
,
fetch_list
=
[
out1
,
out2
,
out3
,
],
)
self
.
assertEqual
(
res
[
0
].
shape
,
())
np
.
testing
.
assert_allclose
(
res
[
0
],
0.0
)
self
.
assertEqual
(
res
[
1
].
shape
,
())
np
.
testing
.
assert_allclose
(
res
[
1
],
0.0
)
self
.
assertEqual
(
res
[
2
].
shape
,
())
np
.
testing
.
assert_allclose
(
res
[
2
],
0.0
)
@
prog_scope
()
def
_test_argmax
(
self
):
x
=
paddle
.
rand
([])
out1
=
paddle
.
argmax
(
x
,
0
)
out2
=
paddle
.
argmax
(
x
,
-
1
)
out3
=
paddle
.
argmax
(
x
,
None
)
prog
=
paddle
.
static
.
default_main_program
()
res
=
self
.
exe
.
run
(
prog
,
fetch_list
=
[
out1
,
out2
,
out3
,
],
)
self
.
assertEqual
(
res
[
0
].
shape
,
())
np
.
testing
.
assert_allclose
(
res
[
0
],
0.0
)
self
.
assertEqual
(
res
[
1
].
shape
,
())
np
.
testing
.
assert_allclose
(
res
[
1
],
0.0
)
self
.
assertEqual
(
res
[
2
].
shape
,
())
np
.
testing
.
assert_allclose
(
res
[
2
],
0.0
)
@
prog_scope
()
def
test_median
(
self
):
x
=
paddle
.
rand
([])
x
.
stop_gradient
=
False
out
=
paddle
.
median
(
x
)
paddle
.
static
.
append_backward
(
out
.
sum
())
prog
=
paddle
.
static
.
default_main_program
()
res
=
self
.
exe
.
run
(
prog
,
fetch_list
=
[
x
,
out
,
x
.
grad_name
,
],
)
self
.
assertEqual
(
res
[
1
].
shape
,
())
np
.
testing
.
assert_allclose
(
res
[
1
],
res
[
0
])
self
.
assertEqual
(
res
[
2
].
shape
,
())
np
.
testing
.
assert_allclose
(
res
[
2
],
1.0
)
@
prog_scope
()
def
test_quantile
(
self
):
x1
=
paddle
.
rand
([])
...
...
@@ -1781,7 +1914,7 @@ class TestSundryAPIStatic(unittest.TestCase):
self
.
assertEqual
(
res
[
2
].
shape
,
(
2
,))
@
prog_scope
()
def
test_scatter_1D
(
self
):
def
_
test_scatter_1D
(
self
):
x
=
paddle
.
full
([
10
],
1.0
,
'float32'
)
x
.
stop_gradient
=
False
index
=
paddle
.
full
([],
2
,
'int64'
)
...
...
@@ -1797,7 +1930,7 @@ class TestSundryAPIStatic(unittest.TestCase):
self
.
assertEqual
(
res
[
2
].
shape
,
(
10
,))
@
prog_scope
()
def
test_scatter_XD
(
self
):
def
_
test_scatter_XD
(
self
):
x
=
paddle
.
full
([
2
,
3
],
1.0
,
'float32'
)
x
.
stop_gradient
=
False
index
=
paddle
.
full
([],
1
,
'int64'
)
...
...
python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
浏览文件 @
e79867b6
...
...
@@ -84,7 +84,6 @@ unary_api_list = [
paddle
.
lgamma
,
paddle
.
poisson
,
paddle
.
bernoulli
,
paddle
.
median
,
]
inplace_api_list
=
[
...
...
@@ -132,8 +131,6 @@ reduce_api_list = [
paddle
.
logsumexp
,
paddle
.
all
,
paddle
.
any
,
paddle
.
argmax
,
paddle
.
argmin
,
]
...
...
@@ -155,8 +152,7 @@ class TestReduceAPI(unittest.TestCase):
self
.
assertEqual
(
x
.
shape
,
[])
self
.
assertEqual
(
out
.
shape
,
[])
if
api
not
in
[
paddle
.
argmax
,
paddle
.
argmin
]:
np
.
testing
.
assert_allclose
(
out
.
numpy
(),
x
.
numpy
())
np
.
testing
.
assert_allclose
(
out
.
numpy
(),
x
.
numpy
())
if
x
.
grad
is
not
None
:
self
.
assertEqual
(
x
.
grad
.
shape
,
[])
self
.
assertEqual
(
out
.
grad
.
shape
,
[])
...
...
@@ -287,22 +283,40 @@ class TestBinaryAPI(unittest.TestCase):
for
api
in
binary_int_api_list
:
# 1) x is 0D, y is 0D
x
=
paddle
.
randint
(
-
10
,
10
,
[])
y
=
paddle
.
randint
(
-
10
,
10
,
[])
x_np
=
np
.
random
.
randint
(
-
10
,
10
,
[])
y_np
=
np
.
random
.
randint
(
-
10
,
10
,
[])
out_np
=
eval
(
'np.%s(x_np, y_np)'
%
api
.
__name__
)
x
=
paddle
.
to_tensor
(
x_np
)
y
=
paddle
.
to_tensor
(
y_np
)
out
=
api
(
x
,
y
)
self
.
assertEqual
(
out
.
shape
,
[])
np
.
testing
.
assert_array_equal
(
out
.
numpy
(),
out_np
)
# 2) x is ND, y is 0D
x
=
paddle
.
randint
(
-
10
,
10
,
[
3
,
5
])
y
=
paddle
.
randint
(
-
10
,
10
,
[])
x_np
=
np
.
random
.
randint
(
-
10
,
10
,
[
3
,
5
])
y_np
=
np
.
random
.
randint
(
-
10
,
10
,
[])
out_np
=
eval
(
'np.%s(x_np, y_np)'
%
api
.
__name__
)
x
=
paddle
.
to_tensor
(
x_np
)
y
=
paddle
.
to_tensor
(
y_np
)
out
=
api
(
x
,
y
)
self
.
assertEqual
(
out
.
shape
,
[
3
,
5
])
np
.
testing
.
assert_array_equal
(
out
.
numpy
(),
out_np
)
# 3) x is 0D , y is ND
x
=
paddle
.
randint
(
-
10
,
10
,
[])
y
=
paddle
.
randint
(
-
10
,
10
,
[
3
,
5
])
x_np
=
np
.
random
.
randint
(
-
10
,
10
,
[])
y_np
=
np
.
random
.
randint
(
-
10
,
10
,
[
3
,
5
])
out_np
=
eval
(
'np.%s(x_np, y_np)'
%
api
.
__name__
)
x
=
paddle
.
to_tensor
(
x_np
)
y
=
paddle
.
to_tensor
(
y_np
)
out
=
api
(
x
,
y
)
self
.
assertEqual
(
out
.
shape
,
[
3
,
5
])
np
.
testing
.
assert_array_equal
(
out
.
numpy
(),
out_np
)
paddle
.
enable_static
()
...
...
@@ -314,6 +328,57 @@ class TestSundryAPI(unittest.TestCase):
paddle
.
disable_static
()
self
.
x
=
paddle
.
rand
([])
def
_test_argmin
(
self
):
x
=
paddle
.
rand
([])
out1
=
paddle
.
argmin
(
x
,
0
)
out2
=
paddle
.
argmin
(
x
,
-
1
)
out3
=
paddle
.
argmin
(
x
,
None
)
self
.
assertEqual
(
out1
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out1
,
0.0
)
self
.
assertEqual
(
out2
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out2
,
0.0
)
self
.
assertEqual
(
out3
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out3
,
0.0
)
def
_test_argmax
(
self
):
x
=
paddle
.
rand
([])
out1
=
paddle
.
argmax
(
x
,
0
)
out2
=
paddle
.
argmax
(
x
,
-
1
)
out3
=
paddle
.
argmax
(
x
,
None
)
self
.
assertEqual
(
out1
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out1
,
0.0
)
self
.
assertEqual
(
out2
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out2
,
0.0
)
self
.
assertEqual
(
out3
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out3
,
0.0
)
def
test_median
(
self
):
x
=
paddle
.
rand
([])
x
.
stop_gradient
=
False
out1
=
paddle
.
median
(
x
,
0
)
out2
=
paddle
.
median
(
x
,
-
1
)
out3
=
paddle
.
median
(
x
,
None
)
out1
.
backward
()
out2
.
backward
()
out3
.
backward
()
self
.
assertEqual
(
out1
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out1
,
x
)
self
.
assertEqual
(
out2
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out2
,
x
)
self
.
assertEqual
(
out3
.
shape
,
[])
np
.
testing
.
assert_allclose
(
out3
,
x
)
self
.
assertEqual
(
x
.
grad
.
shape
,
[])
np
.
testing
.
assert_allclose
(
x
.
grad
,
3.0
)
def
test_linear
(
self
):
x
=
paddle
.
randn
([
3
,
2
])
w
=
paddle
.
full
(
shape
=
[
2
,
4
],
fill_value
=
0.5
)
...
...
@@ -977,8 +1042,6 @@ class TestSundryAPI(unittest.TestCase):
# Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.
class
TestNoBackwardAPI
(
unittest
.
TestCase
):
def
setUp
(
self
):
paddle
.
disable_static
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录