Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
65b0181e
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看板
未验证
提交
65b0181e
编写于
1月 16, 2023
作者:
W
wawltor
提交者:
GitHub
1月 16, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add add_n for the 0d tensor (#49854)
上级
8fdb9087
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
94 addition
and
2 deletion
+94
-2
paddle/phi/infermeta/multiary.cc
paddle/phi/infermeta/multiary.cc
+7
-2
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
+65
-0
python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
...dle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
+22
-0
未找到文件。
paddle/phi/infermeta/multiary.cc
浏览文件 @
65b0181e
...
...
@@ -297,7 +297,7 @@ void AddNInferMeta(const std::vector<const MetaTensor*>& x,
if
(
N
==
1
)
{
VLOG
(
3
)
<<
"Warning: SumOp have only one input, may waste memory"
;
}
bool
is_all_0d_tensor
=
true
;
phi
::
DDim
in_dim
({
0
});
for
(
size_t
i
=
0
;
i
<
x
.
size
();
++
i
)
{
auto
x_dim
=
x
[
i
]
->
dims
();
...
...
@@ -313,6 +313,7 @@ void AddNInferMeta(const std::vector<const MetaTensor*>& x,
if
(
x_dim
.
size
()
==
0
)
{
continue
;
}
is_all_0d_tensor
=
false
;
if
(
phi
::
product
(
in_dim
)
==
0
)
{
in_dim
=
x_dim
;
}
else
{
...
...
@@ -360,7 +361,11 @@ void AddNInferMeta(const std::vector<const MetaTensor*>& x,
}
}
}
out
->
set_dims
(
in_dim
);
if
(
is_all_0d_tensor
)
{
out
->
set_dims
(
make_ddim
({}));
}
else
{
out
->
set_dims
(
in_dim
);
}
out
->
share_lod
(
*
x
[
0
]);
}
...
...
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
浏览文件 @
65b0181e
...
...
@@ -900,6 +900,31 @@ class TestSundryAPI(unittest.TestCase):
np
.
testing
.
assert_array_equal
(
out3_1
.
numpy
(),
out3_2
.
numpy
())
np
.
testing
.
assert_array_equal
(
out3_2
.
numpy
(),
np
.
asarray
(
1
))
def
test_add_n
(
self
):
x1
=
paddle
.
rand
([])
x1
.
stop_gradient
=
False
x2
=
paddle
.
rand
([])
x2
.
stop_gradient
=
False
x3
=
paddle
.
rand
([])
x3
.
stop_gradient
=
False
out1
=
paddle
.
add_n
(
x1
)
out2
=
paddle
.
add_n
([
x2
,
x3
])
out1
.
backward
()
out2
.
backward
()
self
.
assertEqual
(
x1
.
grad
.
shape
,
[])
self
.
assertTrue
(
x1
.
grad
.
numpy
()
==
1
)
self
.
assertEqual
(
x2
.
grad
.
shape
,
[])
self
.
assertTrue
(
x2
.
grad
.
numpy
()
==
1
)
self
.
assertEqual
(
x3
.
grad
.
shape
,
[])
self
.
assertTrue
(
x3
.
grad
.
numpy
()
==
1
)
self
.
assertEqual
(
out1
.
shape
,
[])
self
.
assertEqual
(
out1
.
grad
.
shape
,
[])
self
.
assertEqual
(
out2
.
shape
,
[])
self
.
assertEqual
(
out2
.
grad
.
shape
,
[])
def
test_reshape_list
(
self
):
x
=
paddle
.
rand
([])
x
.
stop_gradient
=
False
...
...
@@ -1534,6 +1559,46 @@ class TestSundryAPIStatic(unittest.TestCase):
np
.
testing
.
assert_array_equal
(
out3_1
,
out3_2
)
np
.
testing
.
assert_array_equal
(
out3_2
,
np
.
asarray
(
1
))
@
prog_scope
()
def
test_add_n
(
self
):
x1
=
paddle
.
rand
([])
x1
.
stop_gradient
=
False
x2
=
paddle
.
rand
([])
x2
.
stop_gradient
=
False
x3
=
paddle
.
rand
([])
x3
.
stop_gradient
=
False
out1
=
paddle
.
add_n
(
x1
)
out2
=
paddle
.
add_n
([
x2
,
x3
])
paddle
.
static
.
append_backward
(
out1
.
sum
())
paddle
.
static
.
append_backward
(
out2
.
sum
())
prog
=
paddle
.
static
.
default_main_program
()
block
=
prog
.
global_block
()
res
=
self
.
exe
.
run
(
prog
,
fetch_list
=
[
out1
,
out2
,
x1
.
grad_name
,
x2
.
grad_name
,
x3
.
grad_name
,
out1
.
grad_name
,
out2
.
grad_name
,
],
)
self
.
assertEqual
(
res
[
0
].
shape
,
())
self
.
assertEqual
(
res
[
1
].
shape
,
())
self
.
assertEqual
(
res
[
2
].
shape
,
())
self
.
assertEqual
(
res
[
2
],
1
)
self
.
assertEqual
(
res
[
3
].
shape
,
())
self
.
assertEqual
(
res
[
3
],
1
)
self
.
assertEqual
(
res
[
4
].
shape
,
())
self
.
assertEqual
(
res
[
4
],
1
)
self
.
assertEqual
(
res
[
5
].
shape
,
())
self
.
assertEqual
(
res
[
6
].
shape
,
())
@
prog_scope
()
def
test_reshape_list
(
self
):
x1
=
paddle
.
rand
([])
...
...
python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
浏览文件 @
65b0181e
...
...
@@ -592,6 +592,28 @@ class TestSundryAPI(unittest.TestCase):
np
.
testing
.
assert_array_equal
(
out3_1
.
numpy
(),
out3_2
.
numpy
())
np
.
testing
.
assert_array_equal
(
out3_2
.
numpy
(),
np
.
asarray
(
1
))
def
test_add_n
(
self
):
x1
=
paddle
.
rand
([])
x1
.
stop_gradient
=
False
x2
=
paddle
.
rand
([])
x2
.
stop_gradient
=
False
x3
=
paddle
.
rand
([])
x3
.
stop_gradient
=
False
out1
=
paddle
.
add_n
(
x1
)
out2
=
paddle
.
add_n
([
x2
,
x3
])
out1
.
retain_grads
()
out2
.
retain_grads
()
out1
.
backward
()
out2
.
backward
()
self
.
assertEqual
(
out1
.
shape
,
[])
self
.
assertEqual
(
out1
.
grad
.
shape
,
[])
self
.
assertEqual
(
out2
.
shape
,
[])
self
.
assertEqual
(
out2
.
grad
.
shape
,
[])
def
test_reshape_list
(
self
):
x
=
paddle
.
rand
([])
x
.
stop_gradient
=
False
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录