Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e566d38e
提交
e566d38e
authored
6月 23, 2014
作者:
Dustin Webb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added to test to determine the behavior of the BLAS and intialize the memory as…
Added to test to determine the behavior of the BLAS and intialize the memory as needed to avoid unexpected NaN's.
上级
cdc32336
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
67 行增加
和
6 行删除
+67
-6
blas_c.py
theano/tensor/blas_c.py
+67
-6
没有找到文件。
theano/tensor/blas_c.py
浏览文件 @
e566d38e
...
...
@@ -5,6 +5,9 @@ from theano.tensor.blas import ldflags, blas_header_text, blas_header_version
from
theano.tensor.blas
import
blas_optdb
,
optdb
,
local_optimizer
,
EquilibriumOptimizer
from
theano.tensor.blas
import
Ger
,
ger
,
ger_destructive
from
theano.tensor.blas
import
Gemv
,
gemv_inplace
,
gemv_no_inplace
from
theano.tensor
import
basic
as
T
import
numpy
import
theano.compile
class
BaseBLAS
(
object
):
...
...
@@ -280,13 +283,13 @@ def make_c_ger_destructive(node):
####### ####### #######
def
gemv_c_code
(
aa
,
xx
,
yy
,
zz
,
alpha
,
beta
,
destructive
,
fail
):
def
gemv_c_code
(
aa
,
xx
,
yy
,
zz
,
alpha
,
beta
,
destructive
,
fail
,
force_init_beta
=
False
):
"""
zz <- beta * aa + alpha * dot(xx, yy)
where xx is a matrix, yy and aa are vectors (ergo zz is vector)
"""
return
"""
code
=
"""
int elemsize ;
float fbeta;
...
...
@@ -365,7 +368,7 @@ def gemv_c_code(aa, xx, yy, zz, alpha, beta, destructive, fail):
PyErr_SetString(PyExc_AssertionError, "
%(zz)
s !=
%(aa)
s");
%(fail)
s
}
if (dbeta != 0)
if (dbeta != 0
||
%(force_init_beta)
d
)
{
if (PyArray_DESCR(
%(zz)
s)->type_num == NPY_FLOAT)
{
...
...
@@ -566,17 +569,26 @@ def gemv_c_code(aa, xx, yy, zz, alpha, beta, destructive, fail):
}
}
"""
%
locals
()
"""
return
code
%
locals
()
class
CGemv
(
BaseBLAS
,
Gemv
):
def
__init__
(
self
,
force_init_beta
=
False
,
**
kwargs
):
super
(
BaseBLAS
,
self
)
.
__init__
(
**
kwargs
)
super
(
Gemv
,
self
)
.
__init__
()
self
.
force_init_beta
=
force_init_beta
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
aa
,
alpha
,
xx
,
yy
,
beta
=
inp
zz
,
=
out
code
=
gemv_c_code
(
aa
,
xx
,
yy
,
zz
,
alpha
,
beta
,
destructive
=
int
(
self
.
inplace
),
fail
=
sub
[
'fail'
])
fail
=
sub
[
'fail'
],
force_init_beta
=
self
.
force_init_beta
)
return
code
def
c_code_cache_version
(
self
):
...
...
@@ -592,7 +604,56 @@ def use_c_gemv(node):
# Only float32 and float64 are supported for now.
if
(
node
.
op
==
gemv_no_inplace
and
node
.
outputs
[
0
]
.
dtype
in
[
'float32'
,
'float64'
]):
return
[
CGemv
(
inplace
=
False
)(
*
node
.
inputs
)]
"""
Test issue 1569.
Namely when evaulating
beta*aa + alpha*dot(xx, yy)
where we set z = b = zeros of the correct dimensions we do not actually
set z = zeros and instead let the BLAS perform b*z with uninitialized
memory for speed. Occasionally the memory contains values that are
equivalent to NaN in which case the product b*z contains NaN's for
correctly implemented BLAS libraries. In this situation, since we are
introducing the NaN's, we need to test whether the BLAS performs
correctly. If it *does*, i.e. it actually performs the multiplication
b*z which will result in NaN's in the result, then we need intialize
the memory to zeros.
Note: We perform this check here, as opposed to in the global scope,
because the environment is not completely setup at the point in which
we would perform this check in global scope.
"""
aa
=
T
.
vector
(
'aa'
)
yy
=
T
.
vector
(
'yy'
)
xx
=
T
.
matrix
(
'xx'
)
f
=
theano
.
function
(
[
aa
,
yy
,
xx
],
gemv_no_inplace
(
aa
,
0.
,
xx
,
yy
,
0.
),
theano
.
compile
.
Mode
(
optimizer
=
'fast_compile'
)
)
# Here we introduce NaNs into the data, if they are returned by the BLAS
# then we want gemv_c_code to initiliaze the memory to 0 so that we
# inadvertantly introduce NaNs to the users data.
aa_data
=
numpy
.
array
(
float
(
'NaN'
)
*
numpy
.
ones
((
2
,)),
dtype
=
theano
.
config
.
floatX
)
yy_data
=
numpy
.
array
(
numpy
.
zeros
((
2
,)),
dtype
=
theano
.
config
.
floatX
)
xx_data
=
numpy
.
array
(
float
(
'NaN'
)
*
numpy
.
ones
((
2
,
2
)),
dtype
=
theano
.
config
.
floatX
)
zz
=
f
(
aa_data
,
yy_data
,
xx_data
)
force_init_beta
=
numpy
.
isnan
(
zz
)
.
any
()
return
[
CGemv
(
inplace
=
False
,
force_init_beta
=
force_init_beta
)(
*
node
.
inputs
)]
if
(
node
.
op
==
gemv_inplace
and
node
.
outputs
[
0
]
.
dtype
in
[
'float32'
,
'float64'
]):
return
[
CGemv
(
inplace
=
True
)(
*
node
.
inputs
)]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论