Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1568241f
提交
1568241f
authored
6月 05, 2017
作者:
João Victor Tozatti Risso
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add local optimization to disable gradient computation in CTC GPU wrapper
Signed-off-by:
João Victor Tozatti Risso
<
joaovictor.risso@gmail.com
>
上级
d9c5f4fe
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
33 行增加
和
20 行删除
+33
-20
ctc.py
theano/gpuarray/ctc.py
+33
-20
没有找到文件。
theano/gpuarray/ctc.py
浏览文件 @
1568241f
...
@@ -5,12 +5,17 @@ import theano
...
@@ -5,12 +5,17 @@ import theano
from
theano
import
Op
from
theano
import
Op
from
theano
import
config
from
theano
import
config
import
theano.tensor
as
T
import
theano.tensor
as
T
from
theano.tensor.extra_ops
import
cpu_contiguous
from
.basic_ops
import
(
gpu_contiguous
,
as_gpuarray_variable
,
from
.basic_ops
import
(
gpu_contiguous
,
as_gpuarray_variable
,
infer_context_name
,
CGpuKernelBase
)
infer_context_name
,
CGpuKernelBase
)
import
theano.tensor.nnet.ctc
import
theano.tensor.nnet.ctc
from
.type
import
GpuArrayType
from
.type
import
GpuArrayType
from
.opt
import
register_opt
,
op_lifter
,
register_opt2
from
.opt
import
register_opt
,
op_lifter
,
register_opt2
from
theano.gradient
import
grad_undefined
from
theano.gradient
import
grad_undefined
from
theano
import
gof
from
theano.gof
import
local_optimizer
from
theano.tensor.opt
import
register_canonicalize
from
theano.tensor.opt
import
register_stabilize
import
os
import
os
import
pygpu
import
pygpu
...
@@ -36,14 +41,14 @@ class GpuConnectionistTemporalClassification(CGpuKernelBase, Op):
...
@@ -36,14 +41,14 @@ class GpuConnectionistTemporalClassification(CGpuKernelBase, Op):
Op
.
__init__
(
self
)
Op
.
__init__
(
self
)
CGpuKernelBase
.
__init__
(
self
,
self
.
func_file
,
self
.
func_name
)
CGpuKernelBase
.
__init__
(
self
,
self
.
func_file
,
self
.
func_name
)
self
.
costs
_type
=
GpuArrayType
(
dtype
=
'float32'
,
self
.
costs
=
GpuArrayType
(
dtype
=
'float32'
,
broadcastable
=
(
False
,),
broadcastable
=
(
False
,),
context_name
=
self
.
context_name
)
context_name
=
self
.
context_name
)
if
self
.
compute_grad
:
if
self
.
compute_grad
:
self
.
grad
s_type
=
GpuArrayType
(
dtype
=
'float32'
,
self
.
grad
ients
=
GpuArrayType
(
dtype
=
'float32'
,
broadcastable
=
(
False
,
False
,
False
,),
broadcastable
=
(
False
,
False
,
False
,),
context_name
=
self
.
context_name
)
context_name
=
self
.
context_name
)
if
config
.
ctc
.
root
==
""
:
if
config
.
ctc
.
root
==
""
:
raise
ValueError
(
'ctc.root variable is not set, please set it '
raise
ValueError
(
'ctc.root variable is not set, please set it '
...
@@ -67,15 +72,10 @@ class GpuConnectionistTemporalClassification(CGpuKernelBase, Op):
...
@@ -67,15 +72,10 @@ class GpuConnectionistTemporalClassification(CGpuKernelBase, Op):
# We assume here that the header is available at the include directory
# We assume here that the header is available at the include directory
# of the CTC root directory.
# of the CTC root directory.
dirs
.
append
(
os
.
path
.
join
(
config
.
ctc
.
root
,
"include"
))
dirs
.
append
(
os
.
path
.
join
(
config
.
ctc
.
root
,
"include"
))
dirs
=
dirs
+
list
(
pygpu
.
get_include
())
dirs
=
dirs
+
list
(
super
(
CGpuKernelBase
,
self
)
.
c_header_dirs
())
return
dirs
return
dirs
def
c_headers
(
self
):
def
c_headers
(
self
):
headers
=
[
'ctc.h'
]
return
[
'ctc.h'
]
headers
=
headers
+
super
(
CGpuKernelBase
,
self
)
.
c_headers
()
headers
=
headers
+
[
'<numpy_compat.h>'
,
'<gpuarray_helper.h>'
]
return
headers
def
make_node
(
self
,
activations
,
labels
,
input_lengths
):
def
make_node
(
self
,
activations
,
labels
,
input_lengths
):
if
not
ctc_enabled
:
if
not
ctc_enabled
:
...
@@ -91,9 +91,9 @@ class GpuConnectionistTemporalClassification(CGpuKernelBase, Op):
...
@@ -91,9 +91,9 @@ class GpuConnectionistTemporalClassification(CGpuKernelBase, Op):
# Ensure activations array is C-contiguous
# Ensure activations array is C-contiguous
t_activations
=
gpu_contiguous
(
t_activations
)
t_activations
=
gpu_contiguous
(
t_activations
)
t_labels
=
as_gpuarray_variable
(
labels
,
context_name
=
self
.
context_name
)
# Labels and input lengths are always on the CPU
t_
input_lengths
=
as_gpuarray_variable
(
input_lengths
,
t_
labels
=
T
.
as_tensor_variable
(
labels
)
context_name
=
self
.
context_name
)
t_input_lengths
=
T
.
as_tensor_variable
(
input_lengths
)
if
t_activations
.
type
.
dtype
!=
'float32'
:
if
t_activations
.
type
.
dtype
!=
'float32'
:
raise
TypeError
(
'Activations must use the float32 type!'
)
raise
TypeError
(
'Activations must use the float32 type!'
)
...
@@ -107,18 +107,30 @@ class GpuConnectionistTemporalClassification(CGpuKernelBase, Op):
...
@@ -107,18 +107,30 @@ class GpuConnectionistTemporalClassification(CGpuKernelBase, Op):
# Return only the cost. Gradient will be returned by grad()
# Return only the cost. Gradient will be returned by grad()
self
.
default_output
=
0
self
.
default_output
=
0
out_params
=
[
self
.
costs_type
()]
out_params
=
[
as_gpuarray_variable
(
self
.
costs
(),
context_name
=
self
.
context_name
)]
if
self
.
grads_type
is
not
None
:
if
self
.
gradients
is
not
None
:
out_params
.
append
(
self
.
grads_type
())
out_params
.
append
(
as_gpuarray_variable
(
self
.
gradients
(),
context_name
=
self
.
context_name
))
return
theano
.
Apply
(
self
,
inputs
=
[
t_activations
,
t_labels
,
t_input_lengths
],
return
theano
.
Apply
(
self
,
inputs
=
[
t_activations
,
t_labels
,
t_input_lengths
],
outputs
=
out_params
)
outputs
=
out_params
)
def
grad
(
self
,
inputs
,
output_
grads
):
def
grad
(
self
,
inputs
,
grads
):
return
[
self
.
grads_type
(
),
return
[
as_gpuarray_variable
(
self
.
gradients
(),
context_name
=
self
.
context_name
),
grad_undefined
(
self
,
1
,
inputs
[
1
]),
grad_undefined
(
self
,
1
,
inputs
[
1
]),
grad_undefined
(
self
,
2
,
inputs
[
2
])]
grad_undefined
(
self
,
2
,
inputs
[
2
])]
def
ctc
(
activations
,
labels
,
input_lengths
):
def
ctc
(
activations
,
labels
,
input_lengths
):
return
GpuConnectionistTemporalClassification
()(
activations
,
labels
,
return
GpuConnectionistTemporalClassification
()(
activations
,
labels
,
input_lengths
)
input_lengths
)
# Disable gradient computation if not needed
@register_canonicalize
@register_stabilize
@local_optimizer
([
GpuConnectionistTemporalClassification
])
def
local_GpuConnectionistTemporalClassification_no_grad
(
node
):
if
isinstance
(
node
.
op
,
GpuConnectionistTemporalClassification
):
if
len
(
node
.
outputs
)
>
1
:
if
len
(
node
.
outputs
[
1
]
.
clients
)
==
0
:
# gradient is not used
node
.
op
=
GpuConnectionistTemporalClassification
(
compute_grad
=
False
)
node
.
outputs
=
node
.
outputs
[:
1
]
# costs only
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论