Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
6bc6ccd1
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
6bc6ccd1
编写于
11月 29, 2017
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add gpu kernel for ctc_edit_distance_op
上级
f5681f15
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
135 addition
and
3 deletion
+135
-3
paddle/operators/ctc_edit_distance_op.cu
paddle/operators/ctc_edit_distance_op.cu
+130
-0
python/paddle/v2/fluid/tests/test_ctc_edit_distance_op.py
python/paddle/v2/fluid/tests/test_ctc_edit_distance_op.py
+5
-3
未找到文件。
paddle/operators/ctc_edit_distance_op.cu
0 → 100644
浏览文件 @
6bc6ccd1
/* 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 <algorithm>
#include "paddle/framework/op_registry.h"
#include "paddle/platform/cuda_helper.h"
#include "paddle/platform/gpu_info.h"
namespace
paddle
{
namespace
operators
{
using
platform
::
PADDLE_CUDA_NUM_THREADS
;
template
<
typename
T
>
__global__
void
FillFirstRow
(
T
*
dist
,
const
int
N
)
{
int
idx
=
blockDim
.
x
*
blockIdx
.
x
+
threadIdx
.
x
;
if
(
idx
<
N
+
1
)
{
dist
[
idx
]
=
idx
;
}
}
template
<
typename
T
>
__global__
void
FillFirstColumn
(
T
*
dist
,
const
int
M
,
const
int
N
)
{
int
idx
=
blockDim
.
x
*
blockIdx
.
x
+
threadIdx
.
x
;
if
(
idx
<
M
+
1
)
{
dist
[
idx
*
(
N
+
1
)]
=
idx
;
}
}
template
<
typename
T
>
__global__
void
Levenshtein
(
T
*
dist
,
const
T
*
x1
,
const
T
*
x2
,
const
int
M
,
const
int
N
,
const
int
start
)
{
int
idx
=
blockDim
.
x
*
blockIdx
.
x
+
threadIdx
.
x
;
int
offset
=
N
;
int
index
=
start
+
idx
*
offset
;
int
row
=
index
/
(
N
+
1
);
int
col
=
index
%
(
N
+
1
);
if
(
row
>
0
&&
col
>
0
&&
row
<
M
+
1
&&
col
<
N
+
1
)
{
int
cost
=
x1
[
row
-
1
]
==
x2
[
col
-
1
]
?
0
:
1
;
int
dels
=
dist
[(
row
-
1
)
*
(
N
+
1
)
+
col
]
+
1
;
int
ins
=
dist
[
row
*
(
N
+
1
)
+
col
-
1
]
+
1
;
int
subs
=
dist
[(
row
-
1
)
*
(
N
+
1
)
+
(
col
-
1
)]
+
cost
;
dist
[
index
]
=
min
(
dels
,
min
(
ins
,
subs
));
}
}
template
<
typename
Place
,
typename
T
>
class
CTCEditDistanceGPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
*
out_t
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
*
x1_t
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X1"
);
auto
*
x2_t
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X2"
);
out_t
->
mutable_data
<
float
>
(
ctx
.
GetPlace
());
auto
normalized
=
ctx
.
Attr
<
bool
>
(
"normalized"
);
auto
stream
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
ctx
.
device_context
())
.
stream
();
auto
m
=
x1_t
->
numel
();
auto
n
=
x2_t
->
numel
();
T
distance
=
0
;
if
(
m
==
0
)
{
distance
=
n
;
}
else
if
(
n
==
0
)
{
distance
=
m
;
}
else
{
framework
::
Tensor
dist_t
;
dist_t
.
Resize
({
m
+
1
,
n
+
1
});
dist_t
.
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
dist
=
dist_t
.
data
<
T
>
();
auto
x1
=
x1_t
->
data
<
T
>
();
auto
x2
=
x2_t
->
data
<
T
>
();
FillFirstColumn
<
T
><<<
1
+
m
/
PADDLE_CUDA_NUM_THREADS
,
PADDLE_CUDA_NUM_THREADS
,
0
,
stream
>>>
(
dist
,
m
,
n
);
FillFirstRow
<
T
><<<
1
+
n
/
PADDLE_CUDA_NUM_THREADS
,
PADDLE_CUDA_NUM_THREADS
,
0
,
stream
>>>
(
dist
,
n
);
// compute the elements of distance matrix in the anti-diagonal diretion
for
(
size_t
slice
=
2
;
slice
<
m
+
n
+
1
;
++
slice
)
{
int
z_m
=
slice
<
m
+
1
?
0
:
slice
-
m
;
int
z_n
=
slice
<
n
+
1
?
0
:
slice
-
n
;
// number of elments in the same anti-diagonal line
int
size
=
slice
-
(
z_m
+
z_n
)
+
1
;
int
start
=
slice
<
n
+
1
?
slice
:
z_n
*
(
n
+
1
)
-
1
;
Levenshtein
<
T
><<<
1
+
(
size
-
1
)
/
PADDLE_CUDA_NUM_THREADS
,
PADDLE_CUDA_NUM_THREADS
,
0
,
stream
>>>
(
dist
,
x1
,
x2
,
m
,
n
,
start
);
}
Place
gpu_place
=
boost
::
get
<
Place
>
(
ctx
.
GetPlace
());
memory
::
Copy
(
platform
::
CPUPlace
(),
&
distance
,
gpu_place
,
dist
+
m
*
(
n
+
1
)
+
n
,
sizeof
(
T
),
stream
);
}
if
(
normalized
)
{
distance
=
distance
/
n
;
}
auto
out
=
out_t
->
data
<
float
>
();
Place
gpu_place
=
boost
::
get
<
Place
>
(
ctx
.
GetPlace
());
float
dist_f
=
distance
;
memory
::
Copy
(
gpu_place
,
out
,
platform
::
CPUPlace
(),
&
dist_f
,
sizeof
(
float
),
stream
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
ctc_edit_distance
,
ops
::
CTCEditDistanceGPUKernel
<
paddle
::
platform
::
GPUPlace
,
int
>
,
ops
::
CTCEditDistanceGPUKernel
<
paddle
::
platform
::
GPUPlace
,
int64_t
>
);
python/paddle/v2/f
ramework/tests/test_ctc_edit_distance
.py
→
python/paddle/v2/f
luid/tests/test_ctc_edit_distance_op
.py
浏览文件 @
6bc6ccd1
...
...
@@ -37,9 +37,11 @@ def Levenshtein(hyp, ref):
class
TestCTCEditDistanceOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"ctc_edit_distance"
normalized
=
True
x1
=
np
.
array
([
0
,
12
,
3
,
5
]).
astype
(
"int64"
)
x2
=
np
.
array
([
0
,
12
,
4
,
7
,
8
]).
astype
(
"int64"
)
normalized
=
False
#x1 = np.array([0, 12, 3, 5]).astype("int64")
#x2 = np.array([0, 12, 4, 7, 8]).astype("int64")
x1
=
np
.
array
([
0
,
12
,
5
]).
astype
(
"int64"
)
x2
=
np
.
array
([
0
,
12
,
4
]).
astype
(
"int64"
)
distance
=
Levenshtein
(
hyp
=
x1
,
ref
=
x2
)
if
normalized
is
True
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录