Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7150289b
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
7150289b
编写于
1月 17, 2018
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refine CPU kernel
1. Allocate memory for output before compute. 2. Rename 'ctc_decode' to 'ctc_align'
上级
adcfde3e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
28 addition
and
33 deletion
+28
-33
paddle/operators/ctc_align_op.cc
paddle/operators/ctc_align_op.cc
+10
-10
paddle/operators/ctc_align_op.cu
paddle/operators/ctc_align_op.cu
+4
-4
paddle/operators/ctc_align_op.h
paddle/operators/ctc_align_op.h
+7
-12
python/paddle/v2/fluid/tests/test_ctc_align.py
python/paddle/v2/fluid/tests/test_ctc_align.py
+7
-7
未找到文件。
paddle/operators/ctc_
decode
_op.cc
→
paddle/operators/ctc_
align
_op.cc
浏览文件 @
7150289b
...
...
@@ -12,20 +12,20 @@ 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/ctc_
decode
_op.h"
#include "paddle/operators/ctc_
align
_op.h"
namespace
paddle
{
namespace
operators
{
class
CTC
Decode
Op
:
public
framework
::
OperatorWithKernel
{
class
CTC
Align
Op
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Input"
),
"Input of CTC
Decode
Op should not be null."
);
"Input of CTC
Align
Op should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Output"
),
"Output of CTC
Decode
Op should not be null."
);
"Output of CTC
Align
Op should not be null."
);
auto
input_dims
=
ctx
->
GetInputDim
(
"Input"
);
...
...
@@ -42,14 +42,14 @@ class CTCDecodeOp : public framework::OperatorWithKernel {
}
};
class
CTC
Decode
OpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
CTC
Align
OpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
CTC
Decode
OpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
CTC
Align
OpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"Input"
,
"(LodTensor, default: LoDTensor<int>), Its shape is "
"[Lp, 1], where Lp is the sum of all input sequences' length."
);
AddOutput
(
"Output"
,
"(Tensor, default: Tensor<int>), The
decode
result."
);
AddOutput
(
"Output"
,
"(Tensor, default: Tensor<int>), The
align
result."
);
AddAttr
<
int
>
(
"blank"
,
"(int, default: 0), the blank label setted in Connectionist "
"Temporal Classification (CTC) op."
)
...
...
@@ -59,7 +59,7 @@ class CTCDecodeOpMaker : public framework::OpProtoAndCheckerMaker {
"merge repeated elements between two blanks. "
)
.
SetDefault
(
true
);
AddComment
(
R"DOC(
CTC
Decoder
is used to merge repeated elements between two blanks
CTC
Align op
is used to merge repeated elements between two blanks
and then delete all blanks in sequence.
Given:
...
...
@@ -86,7 +86,7 @@ Then:
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
ctc_
decode
,
ops
::
CTCDecodeOp
,
ops
::
CTCDecode
OpMaker
,
REGISTER_OPERATOR
(
ctc_
align
,
ops
::
CTCAlignOp
,
ops
::
CTCAlign
OpMaker
,
paddle
::
framework
::
EmptyGradOpMaker
);
REGISTER_OP_CPU_KERNEL
(
ctc_
decode
,
ops
::
CTCDecode
Kernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
);
ctc_
align
,
ops
::
CTCAlign
Kernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
);
paddle/operators/ctc_
decode
_op.cu
→
paddle/operators/ctc_
align
_op.cu
浏览文件 @
7150289b
...
...
@@ -15,7 +15,7 @@ limitations under the License. */
#include <stdio.h>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include "paddle/operators/ctc_
decode
_op.h"
#include "paddle/operators/ctc_
align
_op.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -42,7 +42,7 @@ __global__ void MergeAndDelCudaKernel(const int64_t num_token, const T* tokens,
}
template
<
typename
T
>
class
CTC
Decode
OpCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
class
CTC
Align
OpCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
ctx
.
GetPlace
()),
...
...
@@ -87,5 +87,5 @@ class CTCDecodeOpCUDAKernel : public framework::OpKernel<T> {
}
// namespace operators
}
// namespace paddle
REGISTER_OP_CUDA_KERNEL
(
ctc_
decode
,
paddle
::
operators
::
CTC
Decode
OpCUDAKernel
<
int
>
);
REGISTER_OP_CUDA_KERNEL
(
ctc_
align
,
paddle
::
operators
::
CTC
Align
OpCUDAKernel
<
int
>
);
paddle/operators/ctc_
decode
_op.h
→
paddle/operators/ctc_
align
_op.h
浏览文件 @
7150289b
...
...
@@ -23,7 +23,7 @@ using Tensor = framework::Tensor;
using
LoDTensor
=
framework
::
LoDTensor
;
template
<
typename
DeviceContext
,
typename
T
>
class
CTC
Decode
Kernel
:
public
framework
::
OpKernel
<
T
>
{
class
CTC
Align
Kernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
input
=
ctx
.
Input
<
LoDTensor
>
(
"Input"
);
...
...
@@ -43,7 +43,8 @@ class CTCDecodeKernel : public framework::OpKernel<T> {
bool
merge_repeated
=
ctx
.
Attr
<
bool
>
(
"merge_repeated"
);
// merge repeated tokens and delete blank
std
::
vector
<
std
::
vector
<
int
>>
pathes
(
num_sequences
);
T
*
output_data
=
output
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
size_t
output_idx
=
0
;
std
::
vector
<
size_t
>
output_lod0
(
1
,
0
);
const
T
*
input_data
=
input
->
data
<
T
>
();
for
(
size_t
seq_idx
=
0
;
seq_idx
<
num_sequences
;
++
seq_idx
)
{
...
...
@@ -52,11 +53,12 @@ class CTCDecodeKernel : public framework::OpKernel<T> {
i
<
input_lod
[
level
][
seq_idx
+
1
];
++
i
)
{
if
(
input_data
[
i
]
!=
blank
&&
!
(
merge_repeated
&&
input_data
[
i
]
==
prev_token
))
{
pathes
[
seq_idx
].
push_back
(
input_data
[
i
]);
output_data
[
output_idx
]
=
input_data
[
i
];
++
output_idx
;
}
prev_token
=
input_data
[
i
];
}
output_lod0
.
push_back
(
output_
lod0
.
back
()
+
pathes
[
seq_idx
].
size
()
);
output_lod0
.
push_back
(
output_
idx
);
}
// set output lod
...
...
@@ -65,14 +67,7 @@ class CTCDecodeKernel : public framework::OpKernel<T> {
output
->
set_lod
(
output_lod
);
// resize output dims
T
*
output_data
=
output
->
mutable_data
<
T
>
(
{
static_cast
<
int64_t
>
(
output_lod0
.
back
()),
1
},
ctx
.
GetPlace
());
// copy result to output
for
(
int
i
=
0
;
i
<
num_sequences
;
++
i
)
{
memcpy
(
output_data
+
output_lod0
[
i
],
pathes
[
i
].
data
(),
sizeof
(
int
)
*
pathes
[
i
].
size
());
}
output
->
Resize
({
static_cast
<
int64_t
>
(
output_lod0
.
back
()),
1
});
}
};
...
...
python/paddle/v2/fluid/tests/test_ctc_
decode
.py
→
python/paddle/v2/fluid/tests/test_ctc_
align
.py
浏览文件 @
7150289b
...
...
@@ -5,7 +5,7 @@ from op_test import OpTest
from
test_softmax_op
import
stable_softmax
def
CTC
Decode
(
input
,
lod
,
blank
,
merge_repeated
):
def
CTC
Align
(
input
,
lod
,
blank
,
merge_repeated
):
lod0
=
lod
[
0
]
result
=
[]
for
i
in
range
(
len
(
lod0
)
-
1
):
...
...
@@ -20,9 +20,9 @@ def CTCDecode(input, lod, blank, merge_repeated):
return
result
class
TestCTC
Decode
Op
(
OpTest
):
class
TestCTC
Align
Op
(
OpTest
):
def
config
(
self
):
self
.
op_type
=
"ctc_
decode
"
self
.
op_type
=
"ctc_
align
"
self
.
input_lod
=
[[
0
,
11
,
18
]]
self
.
blank
=
0
self
.
merge_repeated
=
False
...
...
@@ -32,8 +32,8 @@ class TestCTCDecodeOp(OpTest):
def
setUp
(
self
):
self
.
config
()
output
=
CTC
Decode
(
self
.
input
,
self
.
input_lod
,
self
.
blank
,
self
.
merge_repeated
)
output
=
CTC
Align
(
self
.
input
,
self
.
input_lod
,
self
.
blank
,
self
.
merge_repeated
)
self
.
inputs
=
{
"Input"
:
(
self
.
input
,
self
.
input_lod
),
}
self
.
outputs
=
{
"Output"
:
output
}
...
...
@@ -47,9 +47,9 @@ class TestCTCDecodeOp(OpTest):
pass
class
TestCTC
DecodeOpCase1
(
TestCTCDecode
Op
):
class
TestCTC
AlignOpCase1
(
TestCTCAlign
Op
):
def
config
(
self
):
self
.
op_type
=
"ctc_
decode
"
self
.
op_type
=
"ctc_
align
"
self
.
input_lod
=
[[
0
,
11
,
19
]]
self
.
blank
=
0
self
.
merge_repeated
=
True
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录