Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
50da7310
提交
50da7310
authored
2月 01, 2008
作者:
bergstrj@iro.umontreal.ca
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added more error checking to gemm, changed names from iadd to add_inplace and…
added more error checking to gemm, changed names from iadd to add_inplace and similar for other inplace ops.
上级
6db0342b
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
56 行增加
和
41 行删除
+56
-41
core.py
core.py
+56
-41
没有找到文件。
core.py
浏览文件 @
50da7310
...
@@ -623,23 +623,23 @@ class NumpyR(gof.PythonR):
...
@@ -623,23 +623,23 @@ class NumpyR(gof.PythonR):
def
__add__
(
self
,
y
):
return
add
(
self
,
y
)
def
__add__
(
self
,
y
):
return
add
(
self
,
y
)
def
__radd__
(
self
,
x
):
return
add
(
x
,
self
)
def
__radd__
(
self
,
x
):
return
add
(
x
,
self
)
def
__iadd__
(
self
,
y
):
return
iadd
(
self
,
y
)
def
__iadd__
(
self
,
y
):
return
add_inplace
(
self
,
y
)
def
__sub__
(
self
,
y
):
return
sub
(
self
,
y
)
def
__sub__
(
self
,
y
):
return
sub
(
self
,
y
)
def
__rsub__
(
self
,
x
):
return
sub
(
x
,
self
)
def
__rsub__
(
self
,
x
):
return
sub
(
x
,
self
)
def
__isub__
(
self
,
y
):
return
isub
(
self
,
y
)
def
__isub__
(
self
,
y
):
return
sub_inplace
(
self
,
y
)
def
__mul__
(
self
,
y
):
return
mul
(
self
,
y
)
def
__mul__
(
self
,
y
):
return
mul
(
self
,
y
)
def
__rmul__
(
self
,
x
):
return
mul
(
x
,
self
)
def
__rmul__
(
self
,
x
):
return
mul
(
x
,
self
)
def
__imul__
(
self
,
y
):
return
imul
(
self
,
y
)
def
__imul__
(
self
,
y
):
return
mul_inplace
(
self
,
y
)
def
__div__
(
self
,
y
):
return
div
(
self
,
y
)
def
__div__
(
self
,
y
):
return
div
(
self
,
y
)
def
__rdiv__
(
self
,
x
):
return
div
(
x
,
self
)
def
__rdiv__
(
self
,
x
):
return
div
(
x
,
self
)
def
__idiv__
(
self
,
y
):
return
idiv
(
self
,
y
)
def
__idiv__
(
self
,
y
):
return
div_inplace
(
self
,
y
)
def
__pow__
(
self
,
y
):
return
pow
(
self
,
y
)
def
__pow__
(
self
,
y
):
return
pow
(
self
,
y
)
def
__rpow__
(
self
,
x
):
return
pow
(
x
,
self
)
def
__rpow__
(
self
,
x
):
return
pow
(
x
,
self
)
def
__ipow__
(
self
,
y
):
return
ipow
(
self
,
y
)
def
__ipow__
(
self
,
y
):
return
pow_inplace
(
self
,
y
)
def
__neg__
(
self
):
return
neg
(
self
)
def
__neg__
(
self
):
return
neg
(
self
)
...
@@ -704,8 +704,8 @@ class add_elemwise(elemwise):
...
@@ -704,8 +704,8 @@ class add_elemwise(elemwise):
def
c_foreach
((
x_i
,
y_i
),
(
z_i
,
)):
def
c_foreach
((
x_i
,
y_i
),
(
z_i
,
)):
return
"z_i = x_i + y_i;"
return
"z_i = x_i + y_i;"
iadd_elemwis
e
=
add_elemwise
.
inplace_version
()
add_elemwise_inplac
e
=
add_elemwise
.
inplace_version
()
iadd_elemwis
e
.
set_impl
(
assert_same_shapes
(
numpy
.
ndarray
.
__iadd__
))
add_elemwise_inplac
e
.
set_impl
(
assert_same_shapes
(
numpy
.
ndarray
.
__iadd__
))
class
add_scalar
(
tensor_scalar_op
):
class
add_scalar
(
tensor_scalar_op
):
...
@@ -714,8 +714,8 @@ class add_scalar(tensor_scalar_op):
...
@@ -714,8 +714,8 @@ class add_scalar(tensor_scalar_op):
return
gz
,
sum
(
gz
)
return
gz
,
sum
(
gz
)
c_expr
=
"x_i + a"
c_expr
=
"x_i + a"
iadd_scalar
=
add_scalar
.
inplace_version
()
add_scalar_inplace
=
add_scalar
.
inplace_version
()
iadd_scalar
.
set_impl
(
tensor_scalar_impl
(
numpy
.
ndarray
.
__iadd__
))
add_scalar_inplace
.
set_impl
(
tensor_scalar_impl
(
numpy
.
ndarray
.
__iadd__
))
class
twice
(
elemwise
):
class
twice
(
elemwise
):
def
grad
(
x
,
gz
):
def
grad
(
x
,
gz
):
...
@@ -737,8 +737,8 @@ class sub_elemwise(elemwise):
...
@@ -737,8 +737,8 @@ class sub_elemwise(elemwise):
def
c_foreach
((
x_i
,
y_i
),
(
z_i
,
)):
def
c_foreach
((
x_i
,
y_i
),
(
z_i
,
)):
return
"z_i = x_i - y_i;"
return
"z_i = x_i - y_i;"
isub_elemwis
e
=
sub_elemwise
.
inplace_version
()
sub_elemwise_inplac
e
=
sub_elemwise
.
inplace_version
()
isub_elemwis
e
.
set_impl
(
assert_same_shapes
(
numpy
.
ndarray
.
__isub__
))
sub_elemwise_inplac
e
.
set_impl
(
assert_same_shapes
(
numpy
.
ndarray
.
__isub__
))
def
sub_scalar_r
(
x
,
a
):
def
sub_scalar_r
(
x
,
a
):
return
add_scalar
(
x
,
-
a
)
return
add_scalar
(
x
,
-
a
)
...
@@ -746,8 +746,8 @@ def sub_scalar_r(x, a):
...
@@ -746,8 +746,8 @@ def sub_scalar_r(x, a):
def
sub_scalar_l
(
x
,
a
):
def
sub_scalar_l
(
x
,
a
):
return
add_scalar
(
-
x
,
a
)
return
add_scalar
(
-
x
,
a
)
def
isub_scalar_r
(
x
,
a
):
def
sub_scalar_r_inplace
(
x
,
a
):
return
iadd_scalar
(
x
,
-
a
)
return
add_scalar_inplace
(
x
,
-
a
)
## Element-wise multiplication ##
## Element-wise multiplication ##
...
@@ -759,8 +759,8 @@ class mul_elemwise(elemwise):
...
@@ -759,8 +759,8 @@ class mul_elemwise(elemwise):
def
c_foreach
((
x_i
,
y_i
),
(
z_i
,
)):
def
c_foreach
((
x_i
,
y_i
),
(
z_i
,
)):
return
"z_i = x_i * y_i;"
return
"z_i = x_i * y_i;"
imul_elemwis
e
=
mul_elemwise
.
inplace_version
()
mul_elemwise_inplac
e
=
mul_elemwise
.
inplace_version
()
imul_elemwis
e
.
set_impl
(
assert_same_shapes
(
numpy
.
ndarray
.
__imul__
))
mul_elemwise_inplac
e
.
set_impl
(
assert_same_shapes
(
numpy
.
ndarray
.
__imul__
))
class
scale
(
tensor_scalar_op
):
class
scale
(
tensor_scalar_op
):
...
@@ -769,8 +769,8 @@ class scale(tensor_scalar_op):
...
@@ -769,8 +769,8 @@ class scale(tensor_scalar_op):
return
scale
(
a
,
gz
),
sum
(
mul_elemwise
(
x
,
gz
))
return
scale
(
a
,
gz
),
sum
(
mul_elemwise
(
x
,
gz
))
c_expr
=
"x_i * a"
c_expr
=
"x_i * a"
iscal
e
=
scale
.
inplace_version
()
scale_inplac
e
=
scale
.
inplace_version
()
iscal
e
.
set_impl
(
tensor_scalar_impl
(
numpy
.
ndarray
.
__imul__
))
scale_inplac
e
.
set_impl
(
tensor_scalar_impl
(
numpy
.
ndarray
.
__imul__
))
class
sqr
(
elemwise
):
class
sqr
(
elemwise
):
...
@@ -815,8 +815,8 @@ class div_elemwise(elemwise):
...
@@ -815,8 +815,8 @@ class div_elemwise(elemwise):
def
c_foreach
((
x_i
,
y_i
),
(
z_i
,
)):
def
c_foreach
((
x_i
,
y_i
),
(
z_i
,
)):
return
"z_i = x_i / y_i;"
return
"z_i = x_i / y_i;"
idiv_elemwis
e
=
div_elemwise
.
inplace_version
()
div_elemwise_inplac
e
=
div_elemwise
.
inplace_version
()
idiv_elemwis
e
.
set_impl
(
assert_same_shapes
(
numpy
.
ndarray
.
__idiv__
))
div_elemwise_inplac
e
.
set_impl
(
assert_same_shapes
(
numpy
.
ndarray
.
__idiv__
))
def
div_scalar_r
(
x
,
a
):
def
div_scalar_r
(
x
,
a
):
return
scale
(
x
,
inv_elemwise
(
a
))
return
scale
(
x
,
inv_elemwise
(
a
))
...
@@ -824,8 +824,8 @@ def div_scalar_r(x, a):
...
@@ -824,8 +824,8 @@ def div_scalar_r(x, a):
def
div_scalar_l
(
x
,
a
):
def
div_scalar_l
(
x
,
a
):
return
scale
(
inv_elemwise
(
x
),
a
)
return
scale
(
inv_elemwise
(
x
),
a
)
def
idiv_scalar_r
(
x
,
a
):
def
div_scalar_r_inplace
(
x
,
a
):
return
iscal
e
(
x
,
inv_elemwise
(
a
))
return
scale_inplac
e
(
x
,
inv_elemwise
(
a
))
...
@@ -856,7 +856,7 @@ iinv_elemwise = inv_elemwise.inplace_version()
...
@@ -856,7 +856,7 @@ iinv_elemwise = inv_elemwise.inplace_version()
class
blas_code
:
class
blas_code
:
@staticmethod
@staticmethod
def
gemm_xyz
(
a_init
,
b_init
):
def
gemm_xyz
(
check_ab
,
a_init
,
b_init
):
mod
=
'
%
'
mod
=
'
%
'
return
"""
return
"""
const char * error_string = NULL;
const char * error_string = NULL;
...
@@ -880,6 +880,8 @@ class blas_code :
...
@@ -880,6 +880,8 @@ class blas_code :
if (_y->nd != 2) goto _dot_execute_fallback;
if (_y->nd != 2) goto _dot_execute_fallback;
if (_z->nd != 2) goto _dot_execute_fallback;
if (_z->nd != 2) goto _dot_execute_fallback;
%(check_ab)
s
if ((_x->descr->type_num != PyArray_DOUBLE)
if ((_x->descr->type_num != PyArray_DOUBLE)
&& (_x->descr->type_num != PyArray_FLOAT))
&& (_x->descr->type_num != PyArray_FLOAT))
goto _dot_execute_fallback;
goto _dot_execute_fallback;
...
@@ -930,6 +932,7 @@ class blas_code :
...
@@ -930,6 +932,7 @@ class blas_code :
{
{
case PyArray_FLOAT:
case PyArray_FLOAT:
{
{
#define REAL float
float a =
%(a_init)
s;
float a =
%(a_init)
s;
float b =
%(b_init)
s;
float b =
%(b_init)
s;
...
@@ -949,10 +952,12 @@ class blas_code :
...
@@ -949,10 +952,12 @@ class blas_code :
case 0x111: cblas_sgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, Nz[0], Nz[1], Nx[1], a, x, sx_1, y, sy_1, b, z, sz_1); break;
case 0x111: cblas_sgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, Nz[0], Nz[1], Nx[1], a, x, sx_1, y, sy_1, b, z, sz_1); break;
default: goto _dot_execute_fallback;
default: goto _dot_execute_fallback;
};
};
#undef REAL
}
}
break;
break;
case PyArray_DOUBLE:
case PyArray_DOUBLE:
{
{
#define REAL double
double a =
%(a_init)
s;
double a =
%(a_init)
s;
double b =
%(b_init)
s;
double b =
%(b_init)
s;
...
@@ -971,6 +976,7 @@ class blas_code :
...
@@ -971,6 +976,7 @@ class blas_code :
case 0x111: cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, Nz[0], Nz[1], Nx[1], a, x, sx_1, y, sy_1, b, z, sz_1); break;
case 0x111: cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, Nz[0], Nz[1], Nx[1], a, x, sx_1, y, sy_1, b, z, sz_1); break;
default: goto _dot_execute_fallback;
default: goto _dot_execute_fallback;
};
};
#undef REAL
}
}
break;
break;
}
}
...
@@ -1019,10 +1025,8 @@ class blas_code :
...
@@ -1019,10 +1025,8 @@ class blas_code :
"""
"""
class
dot
(
omega_op
):
class
dot
(
omega_op
):
def
impl
(
x
,
y
):
z
=
numpy
.
dot
(
x
,
y
)
impl
=
numpy
.
dot
#print z.dtype
return
z
def
grad
(
x
,
y
,
gz
):
def
grad
(
x
,
y
,
gz
):
return
dot
(
gz
,
transpose
(
y
)),
dot
(
transpose
(
x
),
gz
)
return
dot
(
gz
,
transpose
(
y
)),
dot
(
transpose
(
x
),
gz
)
def
specs
(
x
,
y
):
def
specs
(
x
,
y
):
...
@@ -1030,11 +1034,11 @@ class dot(omega_op):
...
@@ -1030,11 +1034,11 @@ class dot(omega_op):
shape
=
(
x
[
2
][
0
],
y
[
2
][
1
])
shape
=
(
x
[
2
][
0
],
y
[
2
][
1
])
return
(
numpy
.
ndarray
,
upcast
(
x
[
1
],
y
[
1
]),
shape
)
return
(
numpy
.
ndarray
,
upcast
(
x
[
1
],
y
[
1
]),
shape
)
def
c_headers
(
self
):
def
c_headers
(
self
):
return
[
"<gsl/gsl_cblas.h>"
]
return
_blas_headers
def
c_libs
(
self
):
def
c_libs
(
self
):
return
[
"cblas"
,
"atlas"
,
"g2c"
]
return
_blas_libs
def
c_impl
((
_x
,
_y
),
(
_z
,
)):
def
c_impl
((
_x
,
_y
),
(
_z
,
)):
return
blas_code
.
gemm_xyz
(
'1.0'
,
'0.0'
)
return
blas_code
.
gemm_xyz
(
'
'
,
'
1.0'
,
'0.0'
)
class
gemm
(
omega_op
,
inplace
):
class
gemm
(
omega_op
,
inplace
):
...
@@ -1070,9 +1074,20 @@ class gemm(omega_op, inplace):
...
@@ -1070,9 +1074,20 @@ class gemm(omega_op, inplace):
def
c_libs
(
self
):
def
c_libs
(
self
):
return
_blas_libs
return
_blas_libs
def
c_impl
((
_zin
,
_a
,
_x
,
_y
,
_b
),
(
_z
,)):
def
c_impl
((
_zin
,
_a
,
_x
,
_y
,
_b
),
(
_z
,)):
return
blas_code
.
gemm_xyz
(
check_ab
=
"""
'((_a->descr->type_num == PyArray_FLOAT) ? (float*)_a->data : (double*)_a->data)[0]'
,
{
'((_b->descr->type_num == PyArray_FLOAT) ? (float*)_b->data : (double*)_b->data)[0]'
)
if ((_a->descr->type_num != PyArray_DOUBLE)
&& (_a->descr->type_num != PyArray_FLOAT))
goto _dot_execute_fallback;
if ((_b->descr->type_num != PyArray_DOUBLE)
&& (_b->descr->type_num != PyArray_FLOAT))
goto _dot_execute_fallback;
}
"""
return
blas_code
.
gemm_xyz
(
check_ab
,
'(_a->descr->type_num == PyArray_FLOAT) ? (REAL)(((float*)_a->data)[0]) : (REAL)(((double*)_a->data)[0])'
,
'(_b->descr->type_num == PyArray_FLOAT) ? (REAL)(((float*)_b->data)[0]) : (REAL)(((double*)_b->data)[0])'
)
## Transposition ##
## Transposition ##
...
@@ -1186,8 +1201,8 @@ class pow_elemwise(elemwise):
...
@@ -1186,8 +1201,8 @@ class pow_elemwise(elemwise):
def
c_foreach
((
x_i
,
s_i
),
(
z_i
,
)):
def
c_foreach
((
x_i
,
s_i
),
(
z_i
,
)):
return
"z_i = pow(x_i, s_i)"
return
"z_i = pow(x_i, s_i)"
ipow_elemwis
e
=
pow_elemwise
.
inplace_version
()
pow_elemwise_inplac
e
=
pow_elemwise
.
inplace_version
()
ipow_elemwis
e
.
set_impl
(
assert_same_shapes
(
numpy
.
ndarray
.
__ipow__
))
pow_elemwise_inplac
e
.
set_impl
(
assert_same_shapes
(
numpy
.
ndarray
.
__ipow__
))
class
pow_scalar_l
(
tensor_scalar_op
):
class
pow_scalar_l
(
tensor_scalar_op
):
...
@@ -1202,8 +1217,8 @@ class pow_scalar_r(tensor_scalar_op):
...
@@ -1202,8 +1217,8 @@ class pow_scalar_r(tensor_scalar_op):
return
gz
*
s
*
(
pow_scalar_r
(
x
,
s
-
1.0
))
return
gz
*
s
*
(
pow_scalar_r
(
x
,
s
-
1.0
))
c_expr
=
"pow(x_i, a)"
c_expr
=
"pow(x_i, a)"
ipow_scalar_r
=
pow_scalar_r
.
inplace_version
()
pow_scalar_r_inplace
=
pow_scalar_r
.
inplace_version
()
ipow_scalar_r
.
set_impl
(
tensor_scalar_impl
(
numpy
.
ndarray
.
__ipow__
))
pow_scalar_r_inplace
.
set_impl
(
tensor_scalar_impl
(
numpy
.
ndarray
.
__ipow__
))
...
@@ -1273,19 +1288,19 @@ class get_slice(omega_op, view):
...
@@ -1273,19 +1288,19 @@ class get_slice(omega_op, view):
add
=
scalar_switch
(
add_elemwise
,
add_scalar
,
add_scalar
)
add
=
scalar_switch
(
add_elemwise
,
add_scalar
,
add_scalar
)
iadd
=
scalar_switch
(
iadd_elemwise
,
iadd_scalar
)
add_inplace
=
scalar_switch
(
add_elemwise_inplace
,
add_scalar_inplace
)
sub
=
scalar_switch
(
sub_elemwise
,
sub_scalar_r
,
sub_scalar_l
)
sub
=
scalar_switch
(
sub_elemwise
,
sub_scalar_r
,
sub_scalar_l
)
isub
=
scalar_switch
(
isub_elemwise
,
isub_scalar_r
)
sub_inplace
=
scalar_switch
(
sub_elemwise_inplace
,
sub_scalar_r_inplace
)
mul
=
scalar_switch
(
mul_elemwise
,
scale
,
scale
)
mul
=
scalar_switch
(
mul_elemwise
,
scale
,
scale
)
imul
=
scalar_switch
(
imul_elemwise
,
iscal
e
)
mul_inplace
=
scalar_switch
(
mul_elemwise_inplace
,
scale_inplac
e
)
div
=
scalar_switch
(
div_elemwise
,
div_scalar_r
,
div_scalar_l
)
div
=
scalar_switch
(
div_elemwise
,
div_scalar_r
,
div_scalar_l
)
idiv
=
scalar_switch
(
idiv_elemwise
,
idiv_scalar_r
)
div_inplace
=
scalar_switch
(
div_elemwise_inplace
,
div_scalar_r_inplace
)
pow
=
scalar_switch
(
pow_elemwise
,
pow_scalar_r
,
pow_scalar_l
)
pow
=
scalar_switch
(
pow_elemwise
,
pow_scalar_r
,
pow_scalar_l
)
ipow
=
scalar_switch
(
ipow_elemwise
,
ipow_scalar_r
)
pow_inplace
=
scalar_switch
(
pow_elemwise_inplace
,
pow_scalar_r_inplace
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论