Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
3e2c6a56
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看板
未验证
提交
3e2c6a56
编写于
7月 28, 2023
作者:
Y
Yuang Liu
提交者:
GitHub
7月 28, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[bug fix] fix scatter 0d index grad error (#55738)
上级
9c101490
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
45 addition
and
8 deletion
+45
-8
paddle/phi/infermeta/ternary.cc
paddle/phi/infermeta/ternary.cc
+12
-1
paddle/phi/kernels/funcs/scatter.cu.h
paddle/phi/kernels/funcs/scatter.cu.h
+2
-6
paddle/phi/kernels/funcs/scatter.h
paddle/phi/kernels/funcs/scatter.h
+1
-1
test/legacy_test/test_zero_dim_tensor.py
test/legacy_test/test_zero_dim_tensor.py
+30
-0
未找到文件。
paddle/phi/infermeta/ternary.cc
浏览文件 @
3e2c6a56
...
...
@@ -1060,7 +1060,7 @@ void ScatterInferMeta(const MetaTensor& x,
(
ref_dims
.
size
()
==
updates_dims
.
size
()),
true
,
phi
::
errors
::
InvalidArgument
(
"When the Input(
Updates
) is not a 0D tensor, the "
"When the Input(
Index
) is not a 0D tensor, the "
"Input(X) and Input(Updates) should have the same shape size, "
"but received the size of Input(x)'s shape is %d, the size of "
"Input(Updates)'s shape is %d."
,
...
...
@@ -1075,6 +1075,17 @@ void ScatterInferMeta(const MetaTensor& x,
"batch-size is %d."
,
updates_dims
[
0
],
index_dims
[
0
]));
}
else
{
PADDLE_ENFORCE_EQ
(
(
ref_dims
.
size
()
-
1
==
updates_dims
.
size
()),
true
,
phi
::
errors
::
InvalidArgument
(
"When the Input(Index) is a 0D tensor, the "
"Input(Updates) should have the shape size as Input(X)'s "
"shape size - 1. But received the size of Input(x)'s shape is %d, "
" the size of Input(Updates)'s shape is %d."
,
ref_dims
.
size
(),
updates_dims
.
size
()));
}
out
->
set_dims
(
ref_dims
);
out
->
share_lod
(
x
);
...
...
paddle/phi/kernels/funcs/scatter.cu.h
浏览文件 @
3e2c6a56
...
...
@@ -195,12 +195,8 @@ void GPUScatterGradForX(const phi::GPUContext& ctx,
int64_t
index_size
=
index
.
dims
().
size
()
==
0
?
1
:
index
.
dims
()[
0
];
auto
dst_dims
=
output
->
dims
();
// slice size
int64_t
slice_size
=
1
;
// slice size
if
(
index
.
dims
().
size
()
!=
0
)
{
for
(
int
i
=
1
;
i
<
dst_dims
.
size
();
++
i
)
slice_size
*=
dst_dims
[
i
];
}
else
{
for
(
int
i
=
0
;
i
<
dst_dims
.
size
();
++
i
)
slice_size
*=
dst_dims
[
i
];
}
int64_t
slice_size
=
1
;
for
(
int
i
=
1
;
i
<
dst_dims
.
size
();
++
i
)
slice_size
*=
dst_dims
[
i
];
const
IndexT
*
p_index
=
index
.
data
<
IndexT
>
();
T
*
p_output
=
output
->
data
<
T
>
();
const
size_t
&
slice_bytes
=
slice_size
*
sizeof
(
T
);
...
...
paddle/phi/kernels/funcs/scatter.h
浏览文件 @
3e2c6a56
...
...
@@ -244,7 +244,7 @@ template <typename T, typename IndexT = int>
void
CPUScatterGradForX
(
const
phi
::
CPUContext
&
ctx
UNUSED
,
const
DenseTensor
&
index
,
DenseTensor
*
output
)
{
int64_t
index_size
=
index
.
dims
()[
0
];
int64_t
index_size
=
index
.
dims
()
.
size
()
==
0
?
1
:
index
.
dims
()
[
0
];
auto
dst_dims
=
output
->
dims
();
const
IndexT
*
p_index
=
index
.
data
<
IndexT
>
();
T
*
p_output
=
output
->
data
<
T
>
();
...
...
test/legacy_test/test_zero_dim_tensor.py
浏览文件 @
3e2c6a56
...
...
@@ -1916,6 +1916,36 @@ class TestSundryAPI(unittest.TestCase):
np
.
testing
.
assert_array_equal
(
out
.
numpy
()[
1
],
[
1.0
,
2.0
,
3.0
])
self
.
assertEqual
(
out
.
grad
.
shape
,
[
2
,
3
])
def
test_scatter_shape_check
(
self
):
x
=
paddle
.
to_tensor
([
1.0
,
2.0
,
3.0
])
index
=
paddle
.
to_tensor
(
1
)
updates
=
paddle
.
to_tensor
([
3.0
])
with
self
.
assertRaises
(
ValueError
):
out
=
paddle
.
scatter
(
x
,
index
,
updates
)
x
=
paddle
.
to_tensor
([[
1.0
,
1.0
],
[
2.0
,
2.0
],
[
3.0
,
3.0
]])
index
=
paddle
.
to_tensor
(
1
)
updates
=
paddle
.
to_tensor
([[
5.0
,
5.0
]])
with
self
.
assertRaises
(
ValueError
):
out
=
paddle
.
scatter
(
x
,
index
,
updates
)
def
test_scatter_0D_index
(
self
):
x
=
paddle
.
to_tensor
([
1.0
,
2.0
,
3.0
],
stop_gradient
=
False
)
index
=
paddle
.
to_tensor
(
1
)
updates
=
paddle
.
to_tensor
(
3.0
)
out
=
paddle
.
scatter
(
x
,
index
,
updates
)
out
.
backward
()
np
.
testing
.
assert_array_equal
(
x
.
grad
.
numpy
()[
1
],
0.0
)
x
=
paddle
.
to_tensor
(
[[
1.0
,
1.0
],
[
2.0
,
2.0
],
[
3.0
,
3.0
]],
stop_gradient
=
False
)
index
=
paddle
.
to_tensor
(
1
)
updates
=
paddle
.
to_tensor
([
5.0
,
5.0
])
out
=
paddle
.
scatter
(
x
,
index
,
updates
)
out
.
backward
()
np
.
testing
.
assert_array_equal
(
x
.
grad
.
numpy
()[
1
],
[
0.0
,
0.0
])
def
test_diagflat
(
self
):
x1
=
paddle
.
rand
([])
x2
=
paddle
.
rand
([])
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录