Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5f2fde89
提交
5f2fde89
authored
6月 01, 2017
作者:
João Victor Tozatti Risso
提交者:
João Victor Tozatti Risso
7月 20, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add basic support code for CTC context in C GPU wrapper
Signed-off-by:
João Victor Tozatti Risso
<
joaovictortr@gmail.com
>
上级
a35e0c11
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
131 行增加
和
3 行删除
+131
-3
ctc_wrapper.c
theano/gpuarray/ctc_wrapper.c
+131
-3
没有找到文件。
theano/gpuarray/ctc_wrapper.c
浏览文件 @
5f2fde89
#section support_code
struct
ctcOptions
;
typedef
struct
ctc_context
{
struct
ctcOptions
options
;
void
*
workspace
;
int
*
input_lengths
;
int
*
flat_labels
;
int
*
label_lengths
;
}
ctc_context_t
;
void
ctc_context_init
(
ctc_context_t
*
context
)
{
memset
(
&
(
context
->
options
),
0
,
sizeof
(
struct
ctcOptions
));
options
->
loc
=
CTC_GPU
;
options
->
stream
=
NULL
;
context
->
workspace
=
NULL
;
context
->
input_lengths
=
NULL
;
context
->
flat_labels
=
NULL
;
context
->
label_lengths
=
NULL
;
}
void
ctc_context_destroy
(
ctc_context_t
*
context
)
{
if
(
NULL
!=
context
->
workspace
)
free
(
context
->
workspace
);
if
(
NULL
!=
context
->
input_lengths
)
free
(
context
->
input_lengths
);
if
(
NULL
!=
context
->
flat_labels
)
free
(
context
->
flat_labels
);
if
(
NULL
!=
context
->
label_lengths
)
free
(
context
->
label_lengths
);
}
int
ctc_check_result
(
ctcStatus_t
retcode
,
const
char
*
msg
)
{
if
(
CTC_STATUS_SUCCESS
!=
retcode
)
{
// Get error message from underlying library
const
char
*
ctc_msg
=
ctcGetStatusString
(
retcode
);
PyErr_Format
(
PyExc_RuntimeError
,
"%s | CTC library error message: %s"
,
msg
,
ctc_msg
);
return
1
;
}
return
0
;
}
#section support_code_struct
#section support_code_struct
int
APPLY_SPECIFIC
(
ctc_cost_gpu
)(
PyGpuArrayObject
*
in_activations
,
int
APPLY_SPECIFIC
(
ctc_cost_gpu
)(
PyGpuArrayObject
*
in_activations
,
...
@@ -7,7 +62,81 @@ int APPLY_SPECIFIC(ctc_cost_gpu)(PyGpuArrayObject * in_activations,
...
@@ -7,7 +62,81 @@ int APPLY_SPECIFIC(ctc_cost_gpu)(PyGpuArrayObject * in_activations,
PyGpuArrayObject
**
out_gradients
,
PyGpuArrayObject
**
out_gradients
,
PyGpuContextObject
*
ctx
)
PyGpuContextObject
*
ctx
)
{
{
return
0
;
ctc_context_t
ctc_ctx
;
ctc_context_init
(
&
ctc_ctx
);
if
(
!
PyArray_IS_C_CONTIGUOUS
(
in_activations
)
)
{
PyErr_SetString
(
PyExc_RuntimeError
,
"activations array must be C-contiguous."
);
return
1
;
}
npy_float32
*
activations
=
(
npy_float32
*
)
PyArray_DATA
(
in_activations
);
// TODO: flatten input_lengths to conform with underlying library memory layout
// TODO: flatten labels to conform with underlying library memory layout
const
npy_int
minibatch_size
=
PyArray_DIMS
(
in_activations
)[
1
];
const
npy_int
alphabet_size
=
PyArray_DIMS
(
in_activations
)[
2
];
npy_float32
*
costs
=
NULL
;
const
npy_intp
cost_size
=
minibatch_size
;
if
(
NULL
==
*
out_costs
||
// symbolic variable has no real backing
PyArray_NDIM
(
*
out_costs
)
!=
1
||
PyArray_DIMS
(
*
out_costs
)[
0
]
!=
cost_size
)
{
PY_XDECREF
(
*
out_costs
);
*
out_costs
=
pygpu_zeros
(
1
,
cost_size
,
GA_FLOAT
,
GA_C_ORDER
,
ctx
,
Py_None
);
if
(
NULL
==
*
out_costs
)
{
// Destroy previous CTC context before returning exception
ctc_context_destroy
(
&
ctc_ctx
);
PyErr_Format
(
PyExc_MemoryError
,
"Could not allocate storage for CTC costs"
);
return
1
;
}
}
costs
=
(
npy_float32
*
)
PyArray_DATA
(
*
out_costs
);
npy_float32
*
gradients
=
NULL
;
if
(
NULL
!=
out_gradients
)
// if gradient computation is not disabled
{
if
(
NULL
==
*
out_gradients
||
PyArray_NDIM
(
*
out_gradients
)
!=
3
||
PyArray_DIMS
(
*
out_gradients
)[
0
]
!=
PyArray_DIMS
(
in_activations
)[
0
]
||
PyArray_DIMS
(
*
out_gradients
)[
1
]
!=
PyArray_DIMS
(
in_activations
)[
1
]
||
PyArray_DIMS
(
*
out_gradients
)[
2
]
!=
PyArray_DIMS
(
in_activations
)[
2
]
)
{
Py_XDECREF
(
*
out_gradients
);
*
out_gradients
=
pygpu_zeros
(
3
,
PyArray_DIMS
(
in_activations
),
NPY_FLOAT32
,
0
);
if
(
NULL
==
*
out_gradients
)
{
ctc_context_destroy
(
&
ctc_ctx
);
PyErr_Format
(
PyExc_MemoryError
,
"Could not allocate storage for CTC gradients!"
);
return
1
;
}
}
gradients
=
(
npy_float32
*
)
PyArray_DATA
(
*
out_gradients
);
}
ctc_context_destroy
(
&
ctc_ctx
);
return
0
;
}
}
int
APPLY_SPECIFIC
(
ctc_cost_gpu_no_grad
)(
PyGpuArrayObject
*
in_activations
,
int
APPLY_SPECIFIC
(
ctc_cost_gpu_no_grad
)(
PyGpuArrayObject
*
in_activations
,
...
@@ -22,4 +151,4 @@ int APPLY_SPECIFIC(ctc_cost_gpu_no_grad)(PyGpuArrayObject * in_activations,
...
@@ -22,4 +151,4 @@ int APPLY_SPECIFIC(ctc_cost_gpu_no_grad)(PyGpuArrayObject * in_activations,
out_costs
,
out_costs
,
NULL
,
NULL
,
ctx
);
ctx
);
}
}
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论