Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
e5e3d5cf
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
e5e3d5cf
编写于
10月 18, 2022
作者:
L
liu zhengxi
提交者:
GitHub
10月 18, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add value check & error message for gather_tree (#47051)
上级
5e9f491e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
43 addition
and
0 deletion
+43
-0
paddle/phi/kernels/cpu/gather_tree_kernel.cc
paddle/phi/kernels/cpu/gather_tree_kernel.cc
+10
-0
paddle/phi/kernels/gpu/gather_tree_kernel.cu
paddle/phi/kernels/gpu/gather_tree_kernel.cu
+7
-0
python/paddle/fluid/tests/unittests/test_gather_tree_op.py
python/paddle/fluid/tests/unittests/test_gather_tree_op.py
+19
-0
python/paddle/nn/functional/extension.py
python/paddle/nn/functional/extension.py
+7
-0
未找到文件。
paddle/phi/kernels/cpu/gather_tree_kernel.cc
浏览文件 @
e5e3d5cf
...
...
@@ -14,6 +14,7 @@
#include "paddle/phi/kernels/gather_tree_kernel.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"
namespace
phi
{
...
...
@@ -49,6 +50,15 @@ void GatherTreeKernel(const Context &dev_ctx,
out_data
[
idx
]
=
ids_data
[
idx
];
auto
parent
=
parents_data
[
idx
];
for
(
int
step
=
max_length
-
2
;
step
>=
0
;
step
--
)
{
PADDLE_ENFORCE_LT
(
parent
,
beam_size
,
phi
::
errors
::
InvalidArgument
(
"The parents must be less than beam size, but recieved"
"parents %d is greater than or equal to beam size %d. "
,
parent
,
beam_size
));
idx
=
step
*
batch_size
*
beam_size
+
batch
*
beam_size
;
out_data
[
idx
+
beam
]
=
ids_data
[
idx
+
parent
];
parent
=
parents_data
[
idx
+
parent
];
...
...
paddle/phi/kernels/gpu/gather_tree_kernel.cu
浏览文件 @
e5e3d5cf
...
...
@@ -16,6 +16,7 @@
#include <algorithm>
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"
namespace
phi
{
...
...
@@ -35,6 +36,12 @@ __global__ void GatherTree(const T *ids_data,
out_data
[
idx
]
=
ids_data
[
idx
];
auto
parent
=
parents_data
[
idx
];
for
(
int
step
=
max_length
-
2
;
step
>=
0
;
step
--
)
{
PADDLE_ENFORCE
((
parent
<
beam_size
),
"The parents must be less than beam size, but recieved"
"parents %ld is greater than or equal to beam size %ld. "
,
parent
,
beam_size
);
idx
=
step
*
batch_size
*
beam_size
+
batch
*
beam_size
;
out_data
[
idx
+
beam
]
=
ids_data
[
idx
+
parent
];
parent
=
parents_data
[
idx
+
parent
];
...
...
python/paddle/fluid/tests/unittests/test_gather_tree_op.py
浏览文件 @
e5e3d5cf
...
...
@@ -123,6 +123,25 @@ class TestGatherTreeOpError(unittest.TestCase):
fluid
.
layers
.
gather_tree
(
ids
,
bad_parents
)
self
.
assertRaises
(
TypeError
,
test_type_parents
)
def
test_ids_ndim
():
bad_ids
=
fluid
.
layers
.
data
(
name
=
'bad_test_ids'
,
shape
=
[
5
,
2
],
dtype
=
'int64'
,
append_batch_size
=
False
)
paddle
.
nn
.
functional
.
gather_tree
(
bad_ids
,
parents
)
self
.
assertRaises
(
ValueError
,
test_ids_ndim
)
def
test_parents_ndim
():
bad_parents
=
fluid
.
layers
.
data
(
name
=
'bad_test_parents'
,
shape
=
[
5
,
2
],
dtype
=
'int64'
,
append_batch_size
=
False
)
paddle
.
nn
.
functional
.
gather_tree
(
ids
,
bad_parents
)
self
.
assertRaises
(
ValueError
,
test_parents_ndim
)
paddle
.
disable_static
()
...
...
python/paddle/nn/functional/extension.py
浏览文件 @
e5e3d5cf
...
...
@@ -303,6 +303,13 @@ def gather_tree(ids, parents):
# [[[2, 2], [1, 6]], [[3, 3], [6, 1]], [[0, 1], [9, 0]]]
"""
if
ids
.
ndim
!=
3
:
raise
ValueError
(
"The input ids must be a 3D tensor with shape [length, batch_size, beam_size]"
)
if
ids
.
ndim
!=
parents
.
ndim
:
raise
ValueError
(
"The ids's shape must be the same as parents' shape. "
)
if
in_dygraph_mode
():
return
_C_ops
.
gather_tree
(
ids
,
parents
)
else
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录