Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
7b9d5b32
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,发现更多精彩内容 >>
提交
7b9d5b32
编写于
1月 08, 2018
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add document for sequence_erase_op
上级
37f933b8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
42 addition
and
34 deletion
+42
-34
paddle/operators/sequence_erase_op.cc
paddle/operators/sequence_erase_op.cc
+35
-7
paddle/operators/sequence_erase_op.cu
paddle/operators/sequence_erase_op.cu
+3
-8
paddle/operators/sequence_erase_op.h
paddle/operators/sequence_erase_op.h
+1
-16
python/paddle/v2/fluid/tests/test_sequence_erase_op.py
python/paddle/v2/fluid/tests/test_sequence_erase_op.py
+3
-3
未找到文件。
paddle/operators/sequence_erase_op.cc
浏览文件 @
7b9d5b32
...
...
@@ -26,7 +26,11 @@ class SequenceEraseOp : public framework::OperatorWithKernel {
"Input(X) of SequenceEraseOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of SequenceEraseOp should not be null."
);
ctx
->
SetOutputDim
(
"Out"
,
ctx
->
GetInputDim
(
"X"
));
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE
(
x_dims
.
size
()
==
2
&&
x_dims
[
1
]
==
1
,
"Input(X) of SequenceEraseOp should be a 2-D LoDTensor "
"with the 2nd dimension equal to 1."
);
ctx
->
SetOutputDim
(
"Out"
,
x_dims
);
}
};
...
...
@@ -35,17 +39,41 @@ class SequenceEraseOpMaker : public framework::OpProtoAndCheckerMaker {
SequenceEraseOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"(
LoDTensor) 2-D input LoDTensor with the 2-nd dimension
"
"
of length 1
."
);
"(
2-D LoDTensor with the 2nd dim. equal to 1)
"
"
Input LoDTensor of SequenceEraseOp
."
);
AddOutput
(
"Out"
,
"(
LoDTensor) 2-D output LoDTensor with the 2-nd dimension
"
"
of length 1
."
);
"(
2-D LoDTensor with the 2nd dim. equal to 1)
"
"
Output LoDTensor of SequenceEraseOp
."
);
AddAttr
<
std
::
vector
<
int
>>
(
"tokens"
,
"(vector<int>) "
"
Tokens to be removed from input
."
);
"(vector<int>)
Tokens need to be erased from
"
"
input sequences
."
);
AddComment
(
R"DOC(
Sequence Erase Operator.
Sequence erase operator erases tokens specified by Attr(tokens) in the input
sequences Input(X), and outputs the remaining data and modifies the LoD
information at the same time. For example, given a 2-D LoDTensor
X = [[2, 2, 6, 1, 3, 9, 6, 1, 0, 1]]^T
with lod = [[0, 3, 6, 10]], there are three sequences in the input:
X1 = [[2, 2, 6]]^T, X2 = [[1, 3, 9]]^T and X3 = [[6, 1, 0, 1]]^T.
If the tokens to be erased are Attr(tokens) = [2, 3, 5], after the erasing
operation, the three sequences become
X1' = [[6]]^T, X2' = [[1, 9]]^T and X3' = [[6, 1, 0, 1]]^T.
Hence the LoDTensor Output(Out) should be
Out = [[6, 1, 9, 6, 1, 0, 1]]^T,
with lod = [[0, 1, 3, 7]].
An example usage for this operator is to remove the special tokens when
computing the edit distance between two strings, such as blank, start token,
and end token.
)DOC"
);
}
};
...
...
paddle/operators/sequence_erase_op.cu
浏览文件 @
7b9d5b32
...
...
@@ -13,17 +13,13 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
#include <thrust/host_vector.h>
#include <thrust/reduce.h>
#include "paddle/operators/sequence_erase_op.h"
#include "paddle/platform/cuda_helper.h"
#include "paddle/platform/gpu_info.h"
namespace
paddle
{
namespace
operators
{
using
platform
::
PADDLE_CUDA_NUM_THREADS
;
using
Tensor
=
framework
::
Tensor
;
using
LoDTensor
=
framework
::
LoDTensor
;
template
<
typename
T
>
...
...
@@ -97,7 +93,7 @@ class SequenceEraseOpCUDAKernel : public framework::OpKernel<T> {
thrust
::
inclusive_scan
(
num_erased
.
begin
()
+
1
,
num_erased
.
end
(),
num_erased
.
begin
()
+
1
);
//
Reset
LoD
//
Calc
LoD
auto
lod_len
=
lod0
.
size
();
thrust
::
host_vector
<
int
>
host_lod
(
lod_len
);
for
(
size_t
i
=
0
;
i
<
lod_len
;
++
i
)
{
...
...
@@ -117,15 +113,14 @@ class SequenceEraseOpCUDAKernel : public framework::OpKernel<T> {
}
framework
::
LoD
out_lod
;
out_lod
.
push_back
(
out_lod0
);
out
->
set_lod
(
out_lod
);
out
->
Resize
({
out_lod0
.
back
(),
1
});
// Set output
out
->
Resize
({
out_lod0
.
back
(),
1
});
auto
out_dat
=
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
SetOutput
<<<
(
in_len
-
1
)
/
PADDLE_CUDA_NUM_THREADS
+
1
,
PADDLE_CUDA_NUM_THREADS
,
0
,
stream
>>>
(
in_dat
,
in_len
,
num_erased_ptr
,
out_dat
);
// Set LoD
out
->
set_lod
(
out_lod
);
}
};
...
...
paddle/operators/sequence_erase_op.h
浏览文件 @
7b9d5b32
...
...
@@ -15,14 +15,10 @@ limitations under the License. */
#pragma once
#include "paddle/framework/op_registry.h"
#include "paddle/operators/math/softmax.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
using
LoDTensor
=
framework
::
LoDTensor
;
template
<
typename
DeviceContext
,
typename
T
>
class
SequenceEraseKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
...
...
@@ -32,17 +28,6 @@ class SequenceEraseKernel : public framework::OpKernel<T> {
auto
lod
=
in
->
lod
();
PADDLE_ENFORCE_EQ
(
lod
.
size
(),
1UL
,
"Only support one level sequence now."
);
// auto dims = x->dims();
/*
const size_t level = lod.size() - 1;
PADDLE_ENFORCE_EQ(dims[0], static_cast<int64_t>(lod[level].back()),
"The first dimension of Input(X) should be equal to the "
"sum of all sequences' lengths.");
PADDLE_ENFORCE_EQ(dims[0], x->numel(),
"The width of each timestep in Input(X) of "
"SequenceEraseOp should be 1.");
out->mutable_data<T>(ctx.GetPlace());
*/
auto
tokens
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"tokens"
);
auto
in_len
=
in
->
numel
();
auto
in_dat
=
in
->
data
<
T
>
();
...
...
@@ -65,7 +50,7 @@ class SequenceEraseKernel : public framework::OpKernel<T> {
out
->
Resize
({
static_cast
<
int64_t
>
(
out_len
),
1
});
auto
out_dat
=
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
for
(
size
_t
i
=
0
;
i
<
in_len
;
++
i
)
{
for
(
int64
_t
i
=
0
;
i
<
in_len
;
++
i
)
{
if
(
num_erased
[
i
]
==
num_erased
[
i
+
1
])
{
out_dat
[
i
-
num_erased
[
i
]]
=
in_dat
[
i
];
}
...
...
python/paddle/v2/fluid/tests/test_sequence_erase_op.py
浏览文件 @
7b9d5b32
...
...
@@ -28,9 +28,9 @@ def sequence_erase(in_seq, lod0, tokens):
class
TestSequenceEraseOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"sequence_erase"
in_seq
=
np
.
random
.
randint
(
0
,
10
,
(
3
0
,
1
)).
astype
(
"int32"
)
lod
=
[[
0
,
5
,
15
,
3
0
]]
tokens
=
[
2
,
5
]
in_seq
=
np
.
random
.
randint
(
0
,
10
,
(
1
0
,
1
)).
astype
(
"int32"
)
lod
=
[[
0
,
3
,
6
,
1
0
]]
tokens
=
[
2
,
3
,
5
]
out_seq
,
new_lod0
=
sequence_erase
(
in_seq
,
lod
[
0
],
tokens
)
self
.
attrs
=
{
'tokens'
:
tokens
}
self
.
inputs
=
{
'X'
:
(
in_seq
,
lod
)}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录