Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5fe10920
提交
5fe10920
authored
12月 01, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added sanity check mode
上级
27e259fb
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
24 行增加
和
187 行删除
+24
-187
function_module.py
theano/compile/function_module.py
+19
-0
mode.py
theano/compile/mode.py
+4
-1
opt.py
theano/tensor/opt.py
+1
-186
没有找到文件。
theano/compile/function_module.py
浏览文件 @
5fe10920
...
@@ -333,10 +333,28 @@ class SanityCheckFunction(Function):
...
@@ -333,10 +333,28 @@ class SanityCheckFunction(Function):
fn
[
item
]
=
value
fn
[
item
]
=
value
def
__call__
(
self
,
*
args
,
**
kwargs
):
def
__call__
(
self
,
*
args
,
**
kwargs
):
for
fn
in
self
.
others
:
for
stor1
,
stor2
in
zip
(
self
.
input_storage
,
fn
.
input_storage
):
stor2
.
value
=
copy
(
stor1
.
value
)
results
=
super
(
SanityCheckFunction
,
self
)
.
__call__
(
*
args
,
**
kwargs
)
results
=
super
(
SanityCheckFunction
,
self
)
.
__call__
(
*
args
,
**
kwargs
)
all_outputs
=
[
copy
(
c
.
value
)
for
c
in
self
.
output_storage
]
# we keep a copy to make sure it's not overwritten
all_outputs
=
[
copy
(
c
.
value
)
for
c
in
self
.
output_storage
]
# we keep a copy to make sure it's not overwritten
for
fn
in
self
.
others
:
for
fn
in
self
.
others
:
fn
(
*
args
,
**
kwargs
)
fn
(
*
args
,
**
kwargs
)
for
i
,
(
c1
,
c2
,
input
)
in
enumerate
(
zip
(
self
.
input_storage
,
fn
.
input_storage
,
self
.
maker
.
inputs
)):
if
not
input
.
mutable
:
if
not
self
.
check_equal
(
c1
.
value
,
c2
.
value
):
name
=
c2
.
name
raise
ValueError
(
"Input #
%
i
%
s using
%
s and
%
s differs."
%
(
i
,
" (
%
s)"
%
name
if
name
else
""
,
self
.
maker
.
mode
,
fn
.
maker
.
mode
),
c1
.
value
,
c2
.
value
)
# This checks all output storage (this includes state variables that we updated)
# This checks all output storage (this includes state variables that we updated)
# This is ok because the results of a call stick around in their storage
# This is ok because the results of a call stick around in their storage
for
i
,
(
r1
,
c2
)
in
enumerate
(
zip
(
all_outputs
,
fn
.
output_storage
)):
for
i
,
(
r1
,
c2
)
in
enumerate
(
zip
(
all_outputs
,
fn
.
output_storage
)):
...
@@ -689,6 +707,7 @@ def function(inputs, outputs, mode='FAST_RUN', accept_inplace = False):
...
@@ -689,6 +707,7 @@ def function(inputs, outputs, mode='FAST_RUN', accept_inplace = False):
defaults
=
[
getattr
(
input
,
'value'
,
None
)
for
input
in
inputs
]
defaults
=
[
getattr
(
input
,
'value'
,
None
)
for
input
in
inputs
]
mode
=
predefined_modes
.
get
(
mode
,
mode
)
if
isinstance
(
mode
,
(
list
,
tuple
)):
# "mode comparison" semantics
if
isinstance
(
mode
,
(
list
,
tuple
)):
# "mode comparison" semantics
if
not
mode
:
if
not
mode
:
raise
ValueError
(
"Please provide at least one mode."
)
raise
ValueError
(
"Please provide at least one mode."
)
...
...
theano/compile/mode.py
浏览文件 @
5fe10920
...
@@ -139,9 +139,12 @@ class Mode(object):
...
@@ -139,9 +139,12 @@ class Mode(object):
FAST_COMPILE
=
Mode
(
'py'
,
'fast_compile'
)
FAST_COMPILE
=
Mode
(
'py'
,
'fast_compile'
)
FAST_RUN
=
Mode
(
'c|py'
,
'fast_run'
)
FAST_RUN
=
Mode
(
'c|py'
,
'fast_run'
)
SANITY_CHECK
=
[
Mode
(
'c|py'
,
None
),
Mode
(
'c|py'
,
'fast_run'
)]
predefined_modes
=
{
'FAST_COMPILE'
:
FAST_COMPILE
,
predefined_modes
=
{
'FAST_COMPILE'
:
FAST_COMPILE
,
'FAST_RUN'
:
FAST_RUN
}
'FAST_RUN'
:
FAST_RUN
,
'SANITY_CHECK'
:
SANITY_CHECK
}
default_mode
=
'FAST_COMPILE'
default_mode
=
'FAST_COMPILE'
def
register_mode
(
name
,
mode
):
def
register_mode
(
name
,
mode
):
...
...
theano/tensor/opt.py
浏览文件 @
5fe10920
...
@@ -832,192 +832,7 @@ def constant_folding(node):
...
@@ -832,192 +832,7 @@ def constant_folding(node):
register_canonicalize
(
constant_folding
)
register_canonicalize
(
constant_folding
)
#################
from
blas
import
_dot22
# BLAS-related
#################
import
blas
class
_Dot22
(
gof
.
Op
):
"""Compute a matrix-matrix product.
This is a specialization of the more general Dot()
"""
def
make_node
(
self
,
x
,
y
):
assert
x
.
type
in
T
.
float_matrix_types
#makes sure x is a matrix
assert
y
.
type
==
x
.
type
#makes sure y is a matrix
bz
=
[
x
.
type
.
broadcastable
[
0
],
y
.
type
.
broadcastable
[
1
]]
outputs
=
[
T
.
tensor
(
x
.
type
.
dtype
,
bz
)]
return
gof
.
Apply
(
self
,
[
x
,
y
],
outputs
)
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,
)):
try
:
z
[
0
]
=
numpy
.
asarray
(
numpy
.
dot
(
x
,
y
))
except
ValueError
,
e
:
# The error raised by numpy has no shape information, we mean to add that
e
.
args
=
e
.
args
+
(
x
.
shape
,
y
.
shape
)
raise
def
__str__
(
self
):
return
"_dot22"
def
c_support_code
(
self
):
#return blas.cblas_header_text()
mod_str
=
"""
#ifndef MOD
#define MOD
%
#endif
"""
return
blas
.
blas_proto
()
+
mod_str
def
c_headers
(
self
):
return
[
'<iostream>'
]
def
c_libraries
(
self
):
return
blas
.
ldflags
()
def
c_code
(
self
,
node
,
name
,
(
_x
,
_y
),
(
_z
,
),
sub
):
return
"""
int unit = 0;
int type_num =
%(_x)
s->descr->type_num;
int type_size =
%(_x)
s->descr->elsize; // in bytes
npy_intp* Nx =
%(_x)
s->dimensions;
npy_intp* Ny =
%(_y)
s->dimensions;
npy_intp* Nz = 0; //
%(_z)
s->dimensions;
npy_intp* Sx =
%(_x)
s->strides;
npy_intp* Sy =
%(_y)
s->strides;
npy_intp* Sz = 0;//
%(_z)
s->strides;
//strides for x, y, z in dimensions 0, 1
int sx_0, sx_1, sy_0, sy_1, sz_0, sz_1;
if ((NULL ==
%(_z)
s)
|| (
%(_z)
s->dimensions[0] !=
%(_x)
s->dimensions[0])
|| (
%(_z)
s->dimensions[1] !=
%(_y)
s->dimensions[1]))
{
if (NULL !=
%(_z)
s) Py_XDECREF(
%(_z)
s);
npy_intp dims[2];
dims[0] =
%(_x)
s->dimensions[0];
dims[1] =
%(_y)
s->dimensions[1];
%(_z)
s = (PyArrayObject*)PyArray_SimpleNew(2, dims, type_num_
%(_x)
s);
if(!
%(_z)
s) {
PyErr_SetString(PyExc_MemoryError, "failed to alloc dot22 output");
%(fail)
s
}
}
Nz =
%(_z)
s->dimensions;
Sz =
%(_z)
s->strides;
if (
%(_x)
s->nd != 2) {PyErr_SetString(PyExc_NotImplementedError, "rank(x) != 2");
%(fail)
s;}
if (
%(_y)
s->nd != 2) {PyErr_SetString(PyExc_NotImplementedError, "rank(y) != 2");
%(fail)
s;}
if (
%(_z)
s->nd != 2) {PyErr_SetString(PyExc_NotImplementedError, "rank(z) != 2");
%(fail)
s;}
if ((
%(_x)
s->descr->type_num != PyArray_DOUBLE)
&& (
%(_x)
s->descr->type_num != PyArray_FLOAT))
{PyErr_SetString(PyExc_NotImplementedError, "type(x) is not double or float");
%(fail)
s;}
if ((
%(_y)
s->descr->type_num != PyArray_DOUBLE)
&& (
%(_y)
s->descr->type_num != PyArray_FLOAT))
{PyErr_SetString(PyExc_NotImplementedError, "type(y) is not double or float");
%(fail)
s;}
if ((
%(_z)
s->descr->type_num != PyArray_DOUBLE)
&& (
%(_z)
s->descr->type_num != PyArray_FLOAT))
{PyErr_SetString(PyExc_NotImplementedError, "type(z) is not double or float");
%(fail)
s;}
if ((
%(_x)
s->descr->type_num !=
%(_y)
s->descr->type_num)
||(
%(_x)
s->descr->type_num !=
%(_z)
s->descr->type_num))
{ PyErr_SetString(PyExc_NotImplementedError, "type(z), type(y), type(z) are not all the same");
%(fail)
s; }
if ((Nx[0] != Nz[0]) || (Nx[1] != Ny[0]) || (Ny[1] != Nz[1]))
{
PyErr_SetString(PyExc_ValueError, "Input dimensions do not agree");
%(fail)
s;
}
if ((Sx[0] < 1) || (Sx[1] < 1) || (Sx[0] MOD type_size) || (Sx[1] MOD type_size)
|| (Sy[0] < 1) || (Sy[1] < 1) || (Sy[0] MOD type_size) || (Sy[1] MOD type_size)
|| (Sz[0] < 1) || (Sz[1] < 1) || (Sz[0] MOD type_size) || (Sz[1] MOD type_size))
{
PyErr_SetString(PyExc_ValueError, "stride is not multiple of element size");
%(fail)
s;
}
/*
encode the stride structure of _x,_y,_z into a single integer
*/
unit |= ((Sx[1] == type_size) ? 0x0 : (Sx[0] == type_size) ? 0x1 : 0x2) << 8;
unit |= ((Sy[1] == type_size) ? 0x0 : (Sy[0] == type_size) ? 0x1 : 0x2) << 4;
unit |= ((Sz[1] == type_size) ? 0x0 : (Sz[0] == type_size) ? 0x1 : 0x2) << 0;
/* create appropriate strides for malformed matrices that are row or column
* vectors
*/
sx_0 = (Nx[0] > 1) ? Sx[0]/type_size : Nx[1];
sx_1 = (Nx[1] > 1) ? Sx[1]/type_size : Nx[0];
sy_0 = (Ny[0] > 1) ? Sy[0]/type_size : Ny[1];
sy_1 = (Ny[1] > 1) ? Sy[1]/type_size : Ny[0];
sz_0 = (Nz[0] > 1) ? Sz[0]/type_size : Nz[1];
sz_1 = (Nz[1] > 1) ? Sz[1]/type_size : Nz[0];
switch (type_num)
{
case PyArray_FLOAT:
{
float a = 1.0;
float b = 0.0;
float* x = (float*)PyArray_DATA(
%(_x)
s);
float* y = (float*)PyArray_DATA(
%(_y)
s);
float* z = (float*)PyArray_DATA(
%(_z)
s);
char N = 'N';
char T = 'T';
int Nz0 = Nz[0], Nz1 = Nz[1], Nx1 = Nx[1];
//std::cerr << (unit/256) MOD 16 << (unit / 16) MOD 16 << unit MOD 16<< '
\\
n';
switch(unit)
{
case 0x000: sgemm_(&N, &N, &Nz1, &Nz0, &Nx1, &a, y, &sy_0, x, &sx_0, &b, z, &sz_0); break;
case 0x100: sgemm_(&N, &T, &Nz1, &Nz0, &Nx1, &a, y, &sy_0, x, &sx_1, &b, z, &sz_0); break;
case 0x010: sgemm_(&T, &N, &Nz1, &Nz0, &Nx1, &a, y, &sy_1, x, &sx_0, &b, z, &sz_0); break;
case 0x110: sgemm_(&T, &T, &Nz1, &Nz0, &Nx1, &a, y, &sy_1, x, &sx_1, &b, z, &sz_0); break;
case 0x001: sgemm_(&T, &T, &Nz0, &Nz1, &Nx1, &a, x, &sx_0, y, &sy_0, &b, z, &sz_1); break;
case 0x101: sgemm_(&N, &T, &Nz0, &Nz1, &Nx1, &a, x, &sx_1, y, &sy_0, &b, z, &sz_1); break;
case 0x011: sgemm_(&T, &N, &Nz0, &Nz1, &Nx1, &a, x, &sx_0, y, &sy_1, &b, z, &sz_1); break;
case 0x111: sgemm_(&N, &N, &Nz0, &Nz1, &Nx1, &a, x, &sx_1, y, &sy_1, &b, z, &sz_1); break;
default: PyErr_SetString(PyExc_ValueError, "some matrix has no unit stride");
%(fail)
s;
};
#undef REAL
}
break;
case PyArray_DOUBLE:
{
double a = 1.0;
double b = 0.0;
double* x = (double*)PyArray_DATA(
%(_x)
s);
double* y = (double*)PyArray_DATA(
%(_y)
s);
double* z = (double*)PyArray_DATA(
%(_z)
s);
char N = 'N';
char T = 'T';
int Nz0 = Nz[0], Nz1 = Nz[1], Nx1 = Nx[1];
//std::cerr << (unit/256) MOD 16 << (unit / 16) MOD 16 << unit MOD 16<< '
\\
n';
switch(unit)
{
case 0x000: dgemm_(&N, &N, &Nz1, &Nz0, &Nx1, &a, y, &sy_0, x, &sx_0, &b, z, &sz_0); break;
case 0x100: dgemm_(&N, &T, &Nz1, &Nz0, &Nx1, &a, y, &sy_0, x, &sx_1, &b, z, &sz_0); break;
case 0x010: dgemm_(&T, &N, &Nz1, &Nz0, &Nx1, &a, y, &sy_1, x, &sx_0, &b, z, &sz_0); break;
case 0x110: dgemm_(&T, &T, &Nz1, &Nz0, &Nx1, &a, y, &sy_1, x, &sx_1, &b, z, &sz_0); break;
case 0x001: dgemm_(&T, &T, &Nz0, &Nz1, &Nx1, &a, x, &sx_0, y, &sy_0, &b, z, &sz_1); break;
case 0x101: dgemm_(&N, &T, &Nz0, &Nz1, &Nx1, &a, x, &sx_1, y, &sy_0, &b, z, &sz_1); break;
case 0x011: dgemm_(&T, &N, &Nz0, &Nz1, &Nx1, &a, x, &sx_0, y, &sy_1, &b, z, &sz_1); break;
case 0x111: dgemm_(&N, &N, &Nz0, &Nz1, &Nx1, &a, x, &sx_1, y, &sy_1, &b, z, &sz_1); break;
default: PyErr_SetString(PyExc_ValueError, "some matrix has no unit stride");
%(fail)
s;
};
#undef REAL
}
break;
}
"""
%
dict
(
locals
(),
**
sub
)
_dot22
=
_Dot22
()
@gof.local_optimizer
([
T
.
dot
])
def
local_dot_to_dot22
(
node
):
if
node
.
op
==
T
.
dot
:
return
[
_dot22
(
*
node
.
inputs
)]
else
:
return
False
register_specialize
(
local_dot_to_dot22
)
@gof.local_optimizer
([
T
.
sub
])
@gof.local_optimizer
([
T
.
sub
])
def
local_sub_to_gemm
(
node
):
def
local_sub_to_gemm
(
node
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论