Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
33c4b2cc
提交
33c4b2cc
authored
9月 19, 2012
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
parametrise the helper fct in Subtensor to allow reusing it in GpuSubtensor.
Also add missing decref in error handling code.
上级
ae13b006
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
61 行增加
和
24 行删除
+61
-24
basic.py
theano/tensor/basic.py
+61
-24
没有找到文件。
theano/tensor/basic.py
浏览文件 @
33c4b2cc
...
...
@@ -3952,9 +3952,21 @@ class Subtensor(Op):
return
"
%
s{
%
s}"
%
(
self
.
__class__
.
__name__
,
", "
.
join
(
indices
))
@staticmethod
def
helper_c_code
(
node
,
name
,
inputs
,
outputs
,
sub
,
idx_list
):
if
not
isinstance
(
node
.
inputs
[
0
]
.
type
,
TensorType
):
raise
NotImplementedError
()
def
helper_c_code
(
node
,
name
,
inputs
,
outputs
,
sub
,
idx_list
,
c_prefix
=
"PyArray"
,
update_flags
=
(
"PyArray_UpdateFlags(xview,"
" NPY_ARRAY_C_CONTIGUOUS|"
"NPY_ARRAY_F_CONTIGUOUS);"
),
set_data
=
'PyArray_set_data'
,
set_dim
=
'PyArray_set_dim'
,
set_stride
=
'PyArray_set_stride'
,
strides_mul
=
1
,
):
"""The parameters c_prefix, update_flags, set_data, set_dim,
set_stride and strides_mul are there to allow reusing this
function on PyArray and CudaNdarray object.
"""
#
# two arrays are created in C code:
# is_slice: len == ndim, 0 means int, 1 means slice
...
...
@@ -4029,6 +4041,10 @@ class Subtensor(Op):
z
,
=
outputs
rval
=
"""
#define PyArray_set_dim(obj, idx, d) PyArray_DIMS(obj)[idx]=d
#define PyArray_set_stride(obj, idx, d) PyArray_STRIDES(obj)[idx]=d
#define PyArray_set_data(obj, ptr, base) PyArray_BYTES(obj)=ptr
// The subtensor is created by iterating over the dimensions
// and updating stride, shape, and data pointers
...
...
@@ -4039,22 +4055,30 @@ class Subtensor(Op):
int inner_ii = 0; // the current dimension of zview
int outer_ii = 0; // current dimension of z
if ((PyArray_DIMS(xview) == PyArray_DIMS(
%(x)
s))
&& (PyArray_DIMS(
%(x)
s) != NULL))
char* ptr = (char*)
%(c_prefix)
s_BYTES(xview);
if ((
%(c_prefix)
s_DIMS(xview) ==
%(c_prefix)
s_DIMS(
%(x)
s))
&& (
%(c_prefix)
s_DIMS(
%(x)
s) != NULL))
{
PyErr_Format(PyExc_ValueError, "x and xview"
"(with
%%
d dims) have the same dimensions"
" pointers:
%%
p and
%%
p",
PyArray_NDIM(
%(x)
s), PyArray_DIMS(xview), PyArray_DIMS(
%(x)
s));
%(c_prefix)
s_NDIM(
%(x)
s),
%(c_prefix)
s_DIMS(xview),
%(c_prefix)
s_DIMS(
%(x)
s));
Py_XDECREF(xview);
%(fail)
s;
}
if (
PyArray_STRIDES(xview) == PyArray
_STRIDES(
%(x)
s)
&& (
PyArray
_DIMS(
%(x)
s) != NULL))
if (
%(c_prefix)
s_STRIDES(xview) ==
%(c_prefix)
s
_STRIDES(
%(x)
s)
&& (
%(c_prefix)
s
_DIMS(
%(x)
s) != NULL))
{
PyErr_Format(PyExc_ValueError, "x and xview"
"(with
%%
d dims) have the same strides"
" pointers:
%%
p and
%%
p",
PyArray_NDIM(
%(x)
s), PyArray_STRIDES(xview), PyArray_STRIDES(
%(x)
s));
%(c_prefix)
s_NDIM(
%(x)
s),
%(c_prefix)
s_STRIDES(xview),
%(c_prefix)
s_STRIDES(
%(x)
s));
Py_XDECREF(xview);
%(fail)
s;
}
...
...
@@ -4062,7 +4086,7 @@ class Subtensor(Op):
{
if (is_slice[outer_ii])
{
npy_intp length =
PyArray
_DIMS(
%(x)
s)[outer_ii];
npy_intp length =
%(c_prefix)
s
_DIMS(
%(x)
s)[outer_ii];
npy_intp slicelength;
npy_intp start = subtensor_spec[spec_pos+0];
npy_intp stop = subtensor_spec[spec_pos+1];
...
...
@@ -4079,6 +4103,7 @@ class Subtensor(Op):
Py_DECREF(xview);
PyErr_Format(PyExc_ValueError,
"slice step cannot be zero");
Py_XDECREF(xview);
%(fail)
s;
}
...
...
@@ -4126,9 +4151,12 @@ class Subtensor(Op):
}
assert (slicelength <= length);
xview->data += PyArray_STRIDES(
%(x)
s)[outer_ii] * start;
PyArray_DIMS(xview)[inner_ii] = slicelength;
PyArray_STRIDES(xview)[inner_ii] = PyArray_STRIDES(
%(x)
s)[outer_ii] * step;
ptr +=
%(c_prefix)
s_STRIDES(
%(x)
s)[outer_ii] * start *
%(strides_mul)
s;
%(set_dim)
s(xview, inner_ii, slicelength);
%(set_stride)
s(xview, inner_ii,
%(c_prefix)
s_STRIDES(
%(x)
s)[outer_ii] * step);
inner_ii += 1;
spec_pos += 3;
...
...
@@ -4136,47 +4164,54 @@ class Subtensor(Op):
else // tuple coord `outer_ii` is an int
{
int idx = subtensor_spec[spec_pos];
if (idx < 0) idx +=
PyArray
_DIMS(
%(x)
s)[outer_ii];
if (idx < 0) idx +=
%(c_prefix)
s
_DIMS(
%(x)
s)[outer_ii];
if (idx >= 0)
{
if (idx <
PyArray
_DIMS(
%(x)
s)[outer_ii])
if (idx <
%(c_prefix)
s
_DIMS(
%(x)
s)[outer_ii])
{
xview->data += PyArray_STRIDES(
%(x)
s)[outer_ii] * idx;
ptr +=
%(c_prefix)
s_STRIDES(
%(x)
s)[outer_ii] * idx *
%(strides_mul)
s;
}
else
{
PyErr_Format(PyExc_IndexError,"index out of bounds");
Py_XDECREF(xview);
%(fail)
s;
}
}
else
{
PyErr_Format(PyExc_IndexError,"index out of bounds");
Py_XDECREF(xview);
%(fail)
s;
}
spec_pos += 1;
}
}
assert (inner_ii <= PyArray_NDIM(xview));
while (inner_ii < PyArray_NDIM(xview))
%(set_data)
s(xview, ptr, (PyObject*)NULL);
assert (inner_ii <=
%(c_prefix)
s_NDIM(xview));
while (inner_ii <
%(c_prefix)
s_NDIM(xview))
{
assert (outer_ii <
PyArray
_NDIM(
%(x)
s));
PyArray_DIMS(xview)[inner_ii] = PyArray_DIMS(
%(x)
s)[outer_ii]
;
PyArray_STRIDES(xview)[inner_ii] = PyArray_STRIDES(
%(x)
s)[outer_ii]
;
assert (outer_ii <
%(c_prefix)
s
_NDIM(
%(x)
s));
%(set_dim)
s(xview, inner_ii,
%(c_prefix)
s_DIMS(
%(x)
s)[outer_ii])
;
%(set_stride)
s(xview, inner_ii,
%(c_prefix)
s_STRIDES(
%(x)
s)[outer_ii])
;
inner_ii += 1;
outer_ii += 1;
}
PyArray_UpdateFlags(xview, NPY_ARRAY_C_CONTIGUOUS|NPY_F_CONTIGUOUS);
%(update_flags)
s
"""
%
locals
()
# print rval
return
rval
@staticmethod
def
helper_c_code_cache_version
():
return
(
4
,)
return
(
5
,)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
# DEBUG
if
not
isinstance
(
node
.
inputs
[
0
]
.
type
,
TensorType
):
raise
NotImplementedError
()
x
=
inputs
[
0
]
z
,
=
outputs
view_ndim
=
node
.
outputs
[
0
]
.
ndim
...
...
@@ -4219,7 +4254,7 @@ class Subtensor(Op):
# have a versioned version of this op's C code.
if
len
(
hv
)
==
0
:
return
()
return
(
1
,
hv
)
return
(
2
,
hv
)
def
R_op
(
self
,
inputs
,
eval_points
):
# Subtensor is not differentiable wrt to its indices, therefore we
...
...
@@ -4479,6 +4514,8 @@ class IncSubtensor(Op):
out
[
0
]
=
x
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
# DEBUG
if
not
isinstance
(
node
.
inputs
[
0
]
.
type
,
TensorType
):
raise
NotImplementedError
()
if
self
.
inplace
:
# convert bool to int
inplace
=
1
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论