Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
621065de
提交
621065de
authored
7月 05, 2017
作者:
João Victor Tozatti Risso
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add ctc_available function to auto-detect warp-ctc availability
Signed-off-by:
João Victor Tozatti Risso
<
joaovictor.risso@gmail.com
>
上级
d131cf93
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
95 行增加
和
62 行删除
+95
-62
ctc.py
theano/gpuarray/ctc.py
+6
-28
test_ctc.py
theano/gpuarray/tests/test_ctc.py
+3
-3
ctc.py
theano/tensor/nnet/ctc.py
+84
-29
test_ctc.py
theano/tensor/nnet/tests/test_ctc.py
+2
-2
没有找到文件。
theano/gpuarray/ctc.py
浏览文件 @
621065de
...
...
@@ -11,13 +11,12 @@ from theano.gradient import grad_undefined
from
theano.gof
import
local_optimizer
from
theano.tensor.opt
import
register_canonicalize
from
theano.tensor.opt
import
register_stabilize
from
theano.tensor.nnet.ctc
import
ctc_available
import
os
import
os.path
from
.
import
pygpu
ctc_enabled
=
config
.
ctc
.
enabled
class
GpuConnectionistTemporalClassification
(
gof
.
COp
):
"""
...
...
@@ -39,14 +38,10 @@ class GpuConnectionistTemporalClassification(gof.COp):
params_type
=
gpu_context_type
def
__init__
(
self
,
compute_grad
=
True
):
if
not
ctc_
enabled
:
raise
RuntimeError
(
'Baidu CTC is not
enabled
and '
if
not
ctc_
available
()
:
raise
RuntimeError
(
'Baidu CTC is not
available
and '
'GpuConnectionistTemporalClassification Op '
'can not be constructed.'
)
elif
config
.
ctc
.
root
==
""
:
raise
ValueError
(
'ctc.root variable is not set, please set it '
'to the root directory of the CTC library in '
'your system.'
)
self
.
compute_grad
=
compute_grad
# Return only the cost. Gradient will be returned by grad()
...
...
@@ -55,32 +50,15 @@ class GpuConnectionistTemporalClassification(gof.COp):
gof
.
COp
.
__init__
(
self
,
self
.
func_file
,
self
.
func_name
)
def
c_lib_dirs
(
self
):
dirs
=
[]
if
ctc_enabled
:
# Find the directory that contains libwarpctc.so
lib_found
=
False
for
lib_dir
in
[
"build"
,
"lib"
,
"lib64"
]:
lib_path
=
os
.
path
.
join
(
config
.
ctc
.
root
,
lib_dir
)
if
os
.
path
.
isdir
(
lib_path
)
and
os
.
path
.
exists
(
lib_path
):
lib_found
=
os
.
path
.
exists
(
os
.
path
.
join
(
lib_path
,
"libwarpctc.so"
))
if
lib_found
:
dirs
.
append
(
lib_path
)
break
if
not
lib_found
:
raise
RuntimeError
(
'libwarpctc.so could not be found. '
,
'Please check the config.ctc.root variable.'
)
return
dirs
assert
ctc_available
.
path
is
not
None
return
[
ctc_available
.
path
]
def
c_libraries
(
self
):
return
[
"warpctc"
,
"gpuarray"
]
def
c_header_dirs
(
self
):
dirs
=
[
os
.
path
.
dirname
(
__file__
),
pygpu
.
get_include
()]
if
ctc_enabled
:
# We assume here that the header is available at the include directory
# of the CTC root directory.
dirs
.
append
(
os
.
path
.
join
(
config
.
ctc
.
root
,
"include"
))
dirs
.
append
(
os
.
path
.
join
(
config
.
ctc
.
root
,
"include"
))
return
dirs
def
c_headers
(
self
):
...
...
theano/gpuarray/tests/test_ctc.py
浏览文件 @
621065de
...
...
@@ -7,14 +7,14 @@ import theano
import
theano.tensor
as
T
from
theano.tests
import
unittest_tools
as
utt
import
theano.gpuarray
from
theano.gpuarray.ctc
import
(
ctc_enabled
,
gpu_ctc
,
GpuConnectionistTemporalClassification
)
from
theano.tensor.nnet.ctc
import
(
ctc
,
ConnectionistTemporalClassification
)
from
theano.gpuarray.ctc
import
(
gpu_ctc
,
GpuConnectionistTemporalClassification
)
from
theano.tensor.nnet.ctc
import
(
ctc
,
ctc_available
,
ConnectionistTemporalClassification
)
from
.config
import
(
mode_with_gpu
,
mode_without_gpu
)
class
TestCTC
(
unittest
.
TestCase
):
def
setUp
(
self
):
if
not
ctc_
enabled
:
if
not
ctc_
available
()
:
self
.
skipTest
(
'Optional library warp-ctc not available'
)
def
check_ctc
(
self
,
activations
,
labels
,
input_length
,
expected_costs
,
expected_grads
):
...
...
theano/tensor/nnet/ctc.py
浏览文件 @
621065de
...
...
@@ -5,12 +5,88 @@ import theano.tensor as T
from
theano
import
config
from
theano
import
gof
from
theano.gof
import
local_optimizer
from
theano.gof.cmodule
import
GCC_compiler
from
theano.tensor.opt
import
register_canonicalize
from
theano.tensor.opt
import
register_stabilize
from
theano.tensor.extra_ops
import
cpu_contiguous
from
theano.gradient
import
grad_undefined
ctc_enabled
=
config
.
ctc
.
enabled
def
_ctc_find_lib
():
"""
Find the directory that contains libwarpctc.so
"""
lib_found
=
False
for
lib_dir
in
[
"build"
,
"lib"
,
"lib64"
]:
lib_path
=
os
.
path
.
join
(
config
.
ctc
.
root
,
lib_dir
)
if
os
.
path
.
isdir
(
lib_path
)
and
os
.
path
.
exists
(
lib_path
):
lib_found
=
os
.
path
.
exists
(
os
.
path
.
join
(
lib_path
,
"libwarpctc.so"
))
if
lib_found
:
return
True
,
lib_path
return
False
,
None
def
_ctc_check_compile
(
ctc_lib_path
):
preambule
=
"""
#include <string.h>
#include "ctc.h"
"""
body
=
"""
ctcOptions options;
memset(&options, 0, sizeof(ctcOptions));
options.loc = CTC_CPU;
options.num_threads = 1;
"""
params
=
[
"-I
%
s"
%
(
os
.
path
.
join
(
config
.
ctc
.
root
,
"include"
))]
params
.
extend
([
'-I
%
s'
%
(
os
.
path
.
dirname
(
__file__
))])
params
.
extend
([
"-L
%
s"
%
(
ctc_lib_path
)])
params
.
extend
([
"-l"
,
"warpctc"
])
compiler_res
=
GCC_compiler
.
try_flags
(
params
,
preambule
=
preambule
,
body
=
body
,
try_run
=
False
,
output
=
True
)
avail
,
out
,
err
=
compiler_res
if
isinstance
(
compiler_res
,
tuple
)
else
(
compiler_res
,
None
,
None
)
if
not
avail
:
return
False
,
(
"cannot compile with warp-ctc. "
"We got this error:
\n
"
+
str
(
err
))
return
True
,
None
def
ctc_present
():
if
ctc_present
.
avail
is
not
None
:
return
ctc_present
.
avail
ctc_present
.
avail
,
ctc_lib_path
=
_ctc_find_lib
()
if
ctc_lib_path
is
None
:
ctc_present
.
msg
=
'libwarpctc.so could not be found. '
,
'Please check your config.ctc.root variable.'
else
:
ctc_present
.
path
=
ctc_lib_path
ctc_present
.
avail
,
ctc_present
.
msg
=
_ctc_check_compile
(
ctc_present
.
path
)
return
ctc_present
.
avail
ctc_present
.
avail
=
None
ctc_present
.
msg
=
None
ctc_present
.
path
=
None
def
ctc_available
():
if
config
.
ctc
.
root
==
''
:
ctc_available
.
msg
=
'ctc.root variable is not set, please set it '
,
'to the root directory of the CTC library in '
,
'your system.'
return
False
elif
os
.
name
==
'nt'
:
ctc_available
.
msg
=
'Windows platforms are currently not supported '
,
'by underlying CTC library (warp-ctc).'
return
False
elif
not
ctc_present
():
ctc_available
.
msg
=
ctc_present
.
msg
return
False
ctc_available
.
path
=
ctc_present
.
path
return
True
ctc_available
.
msg
=
None
ctc_available
.
path
=
None
class
ConnectionistTemporalClassification
(
gof
.
COp
,
gof
.
OpenMPOp
):
...
...
@@ -37,14 +113,10 @@ class ConnectionistTemporalClassification(gof.COp, gof.OpenMPOp):
func_name
=
"APPLY_SPECIFIC(ctc_cost_cpu)"
def
__init__
(
self
,
compute_grad
=
True
):
if
not
ctc_
enabled
:
raise
RuntimeError
(
'Baidu CTC is not
enabled
and '
if
not
ctc_
available
()
:
raise
RuntimeError
(
'Baidu CTC is not
available
and '
'ConnectionistTemporalClassification Op '
'can not be constructed.'
)
elif
config
.
ctc
.
root
==
""
:
raise
ValueError
(
'ctc.root variable is not set, please set it '
'to the root directory of the CTC library in '
'your system.'
)
gof
.
COp
.
__init__
(
self
,
self
.
func_file
,
self
.
func_name
)
gof
.
OpenMPOp
.
__init__
(
self
)
...
...
@@ -54,33 +126,16 @@ class ConnectionistTemporalClassification(gof.COp, gof.OpenMPOp):
self
.
default_output
=
0
def
c_lib_dirs
(
self
):
dirs
=
[]
if
ctc_enabled
:
# Find the directory that contains libwarpctc.so
lib_found
=
False
for
lib_dir
in
[
"build"
,
"lib"
,
"lib64"
]:
lib_path
=
os
.
path
.
join
(
config
.
ctc
.
root
,
lib_dir
)
if
os
.
path
.
isdir
(
lib_path
)
and
os
.
path
.
exists
(
lib_path
):
lib_found
=
os
.
path
.
exists
(
os
.
path
.
join
(
lib_path
,
"libwarpctc.so"
))
if
lib_found
:
dirs
.
append
(
lib_path
)
break
if
not
lib_found
:
raise
RuntimeError
(
'libwarpctc.so could not be found. '
,
'Please check the config.ctc.root variable.'
)
return
dirs
assert
ctc_available
.
path
is
not
None
return
[
ctc_available
.
path
]
def
c_libraries
(
self
):
return
[
"warpctc"
]
def
c_header_dirs
(
self
):
dirs
=
[]
if
ctc_enabled
:
# We assume here that the header is available at the include directory
# of the CTC root directory.
dirs
.
append
(
os
.
path
.
join
(
config
.
ctc
.
root
,
"include"
))
return
dirs
# We assume here that the header is available at the include directory
# of the CTC root directory.
return
[
os
.
path
.
join
(
config
.
ctc
.
root
,
"include"
)]
def
c_compile_args
(
self
):
return
gof
.
OpenMPOp
.
c_compile_args
(
self
)
...
...
theano/tensor/nnet/tests/test_ctc.py
浏览文件 @
621065de
...
...
@@ -6,7 +6,7 @@ import numpy as np
import
theano
import
theano.tensor
as
T
from
theano.tests
import
unittest_tools
as
utt
from
theano.tensor.nnet.ctc
import
(
ctc_
enabled
,
ctc
,
ConnectionistTemporalClassification
)
from
theano.tensor.nnet.ctc
import
(
ctc_
available
,
ctc
,
ConnectionistTemporalClassification
)
class
TestCTC
(
unittest
.
TestCase
):
...
...
@@ -18,7 +18,7 @@ class TestCTC(unittest.TestCase):
"""
def
setUp
(
self
):
if
not
ctc_
enabled
:
if
not
ctc_
available
()
:
self
.
skipTest
(
'Optional library warp-ctc not available'
)
def
run_ctc
(
self
,
activations
,
labels
,
input_length
,
expected_costs
,
expected_grads
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论