Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
3388e52d
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
3388e52d
编写于
1月 18, 2018
作者:
Y
Yan Chunwei
提交者:
GitHub
1月 18, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Bugfix/beamsearch op (#7611)
上级
f086ebb8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
120 addition
and
13 deletion
+120
-13
paddle/operators/CMakeLists.txt
paddle/operators/CMakeLists.txt
+1
-2
paddle/operators/beam_search_op.cc
paddle/operators/beam_search_op.cc
+16
-7
paddle/operators/beam_search_op.h
paddle/operators/beam_search_op.h
+17
-4
paddle/operators/beam_search_op_test.cc
paddle/operators/beam_search_op_test.cc
+86
-0
未找到文件。
paddle/operators/CMakeLists.txt
浏览文件 @
3388e52d
...
...
@@ -178,14 +178,13 @@ foreach(src ${GENERAL_OPS})
endforeach
()
file
(
APPEND
${
pybind_file
}
"USE_OP(less_than);
\n
USE_OP(logical_and);
\n
USE_NO_KERNEL_OP(read_from_array);
\n
"
)
set
(
GLOB_OP_LIB
${
OP_LIBRARY
}
CACHE INTERNAL
"Global OP library"
)
cc_test
(
gather_test SRCS gather_test.cc DEPS tensor
)
cc_test
(
net_op_test SRCS net_op_test.cc DEPS net_op
)
cc_test
(
scatter_test SRCS scatter_test.cc DEPS tensor
)
cc_test
(
beam_search_decode_op_test SRCS beam_search_decode_op_test.cc DEPS lod_tensor
)
cc_test
(
beam_search_op_test SRCS beam_search_op_test.cc DEPS lod_tensor beam_search_op
)
cc_test
(
strided_memcpy_test SRCS strided_memcpy_test.cc DEPS tensor paddle_memory
)
if
(
WITH_GPU
)
cc_test
(
nccl_op_test SRCS nccl_op_test.cu.cc DEPS nccl_op gpu_info device_context
)
...
...
paddle/operators/beam_search_op.cc
浏览文件 @
3388e52d
...
...
@@ -29,7 +29,7 @@ void BeamSearch::operator()(const framework::LoDTensor &pre_ids,
PruneEndidCandidates
(
pre_ids
,
&
selected_items
);
// calculate the output tensor's height
size_t
num_instances
=
std
::
accumulate
(
std
::
begin
(
items
),
std
::
end
(
items
),
0
,
std
::
begin
(
selected_items
),
std
::
end
(
selected_
items
),
0
,
[](
size_t
a
,
std
::
vector
<
Item
>
&
b
)
{
return
a
+
b
.
size
();
});
// the output tensor shape should be [num_instances, 1]
auto
dims
=
framework
::
make_ddim
(
...
...
@@ -48,12 +48,20 @@ void BeamSearch::operator()(const framework::LoDTensor &pre_ids,
size_t
low_offset
=
0
;
for
(
auto
&
items
:
selected_items
)
{
low_level
.
push_back
(
low_offset
);
sort
(
items
.
begin
(),
items
.
end
(),
[](
const
Item
&
a
,
const
Item
&
b
)
{
if
(
a
.
offset
<
b
.
offset
)
{
return
true
;
}
return
a
.
id
<
b
.
id
;
});
for
(
auto
&
item
:
items
)
{
ids_data
[
low_offset
]
=
item
.
id
;
scores_data
[
low_offset
]
=
item
.
score
;
low_offset
++
;
}
}
low_level
.
push_back
(
low_offset
);
// fill lod
auto
abs_lod
=
framework
::
ToAbsOffset
(
ids_
->
lod
());
auto
&
high_level
=
abs_lod
[
lod_level_
];
...
...
@@ -64,16 +72,21 @@ void BeamSearch::operator()(const framework::LoDTensor &pre_ids,
selected_scores
->
set_lod
(
lod
);
}
void
BeamSearch
::
PruneEndidCandidates
(
const
framework
::
LoDTensor
&
pre_ids
,
std
::
vector
<
std
::
vector
<
Item
>>
*
items
)
{
int
BeamSearch
::
PruneEndidCandidates
(
const
framework
::
LoDTensor
&
pre_ids
,
std
::
vector
<
std
::
vector
<
Item
>>
*
items
)
{
auto
*
pre_ids_data
=
pre_ids
.
data
<
int64_t
>
();
int
res
=
0
;
for
(
size_t
offset
=
0
;
offset
<
items
->
size
();
offset
++
)
{
auto
prefix_id
=
pre_ids_data
[
offset
];
if
(
prefix_id
==
end_id_
)
{
items
->
at
(
offset
).
clear
();
}
else
{
res
++
;
}
}
return
res
;
}
std
::
vector
<
std
::
vector
<
BeamSearch
::
Item
>>
BeamSearch
::
ToMap
(
...
...
@@ -121,11 +134,7 @@ bool BeamSearch::NextItemSet(std::vector<BeamSearch::Item> *items) {
auto
ids
=
*
ids_
;
auto
scores
=
*
scores_
;
auto
source_abs_two_level_lod
=
framework
::
SliceInLevel
(
ids
.
lod
(),
lod_level_
,
sent_offset_
,
sent_offset_
+
1
);
source_abs_two_level_lod
=
framework
::
ToAbsOffset
(
source_abs_two_level_lod
);
auto
abs_lod
=
framework
::
ToAbsOffset
(
ids
.
lod
());
PADDLE_ENFORCE_GE
(
source_abs_two_level_lod
.
size
(),
2UL
);
auto
*
ids_data
=
ids
.
data
<
int64_t
>
();
auto
*
scores_data
=
scores
.
data
<
float
>
();
...
...
paddle/operators/beam_search_op.h
浏览文件 @
3388e52d
...
...
@@ -73,7 +73,15 @@ namespace operators {
* second level:
* [0, 2, 4]
*
* tensor's data
* id tensor's data
* [[
* 4,
* 1,
* 3,
* 8,
* ]]
*
* score tensor's data
* [[
* 0.5,
* 0.3,
...
...
@@ -137,16 +145,21 @@ class BeamSearch {
Item
()
{}
Item
(
size_t
offset
,
size_t
id
,
float
score
)
:
offset
(
offset
),
id
(
id
),
score
(
score
)
{}
// offset in the
lod_level_+1
// offset in the
higher lod level.
size_t
offset
;
// // prefix id in the lower lod level.
// size_t prefix;
// the candidate id
id_t
id
;
// the corresponding score
score_t
score
;
};
void
PruneEndidCandidates
(
const
framework
::
LoDTensor
&
pre_ids
,
std
::
vector
<
std
::
vector
<
Item
>>*
items
);
/*
* Delete all the records that follows the end token.
*/
int
PruneEndidCandidates
(
const
framework
::
LoDTensor
&
pre_ids
,
std
::
vector
<
std
::
vector
<
Item
>>*
items
);
/*
* Transform the items into a map whose key is offset, value is the items.
...
...
paddle/operators/beam_search_op_test.cc
0 → 100644
浏览文件 @
3388e52d
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/beam_search_op.h"
#include <gtest/gtest.h>
#include <vector>
namespace
paddle
{
namespace
test
{
using
std
::
vector
;
using
framework
::
LoDTensor
;
using
framework
::
LoD
;
using
operators
::
BeamSearch
;
using
paddle
::
platform
::
CPUPlace
;
using
std
::
cout
;
using
std
::
endl
;
void
CreateInput
(
LoDTensor
*
ids
,
LoDTensor
*
scores
)
{
LoD
lod
;
vector
<
size_t
>
level0
({
0
,
1
,
4
});
vector
<
size_t
>
level1
({
0
,
1
,
2
,
3
,
4
});
lod
.
push_back
(
level0
);
lod
.
push_back
(
level1
);
ids
->
set_lod
(
lod
);
scores
->
set_lod
(
lod
);
auto
dims
=
framework
::
make_ddim
(
vector
<
int64_t
>
({
4
,
3
}));
ids
->
Resize
(
dims
);
scores
->
Resize
(
dims
);
CPUPlace
place
;
auto
*
ids_data
=
ids
->
mutable_data
<
int64_t
>
(
place
);
auto
*
scores_data
=
scores
->
mutable_data
<
float
>
(
place
);
vector
<
int64_t
>
_ids
({
4
,
2
,
5
,
2
,
1
,
3
,
3
,
5
,
2
,
8
,
2
,
1
});
vector
<
float
>
_scores
(
{
0.5
,
0.3
,
0.2
,
0.6
,
0.3
,
0.1
,
0.9
,
0.5
,
0.1
,
0.7
,
0.5
,
0.1
});
for
(
int
i
=
0
;
i
<
12
;
i
++
)
{
ids_data
[
i
]
=
_ids
[
i
];
scores_data
[
i
]
=
_scores
[
i
];
}
}
TEST
(
beam_search_op
,
run
)
{
CPUPlace
place
;
LoDTensor
ids
,
scores
;
CreateInput
(
&
ids
,
&
scores
);
LoDTensor
pre_ids
;
pre_ids
.
Resize
(
framework
::
make_ddim
(
vector
<
int64_t
>
(
4
,
1
)));
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
pre_ids
.
mutable_data
<
int64_t
>
(
place
)[
i
]
=
i
+
1
;
}
BeamSearch
beamsearch
(
ids
,
scores
,
(
int64_t
)
0
,
(
int64_t
)
2
,
0
);
LoDTensor
sids
,
sscores
;
beamsearch
(
pre_ids
,
&
sids
,
&
sscores
);
LOG
(
INFO
)
<<
"score: "
<<
sscores
<<
endl
;
ASSERT_EQ
(
sids
.
lod
(),
sscores
.
lod
());
vector
<
int
>
tids
({
2
,
4
,
3
,
8
});
vector
<
float
>
tscores
({
0.3
,
0.5
,
0.9
,
0.7
});
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
ASSERT_EQ
(
tids
[
i
],
sids
.
data
<
int64_t
>
()[
i
]);
ASSERT_EQ
(
tscores
[
i
],
sscores
.
data
<
float
>
()[
i
]);
}
}
}
// namespace test
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录