Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5fe90b3d
提交
5fe90b3d
authored
10月 02, 2012
作者:
Ian Goodfellow
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
renamed xview to zview since it is a view of z, not x
上级
cf8fd44e
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
95 行增加
和
80 行删除
+95
-80
basic_ops.py
theano/sandbox/cuda/basic_ops.py
+9
-9
cuda_ndarray.cu
theano/sandbox/cuda/cuda_ndarray.cu
+30
-21
basic.py
theano/tensor/basic.py
+56
-50
没有找到文件。
theano/sandbox/cuda/basic_ops.py
浏览文件 @
5fe90b3d
...
...
@@ -2457,7 +2457,7 @@ class GpuIncSubtensor(tensor.IncSubtensor, GpuOp):
This doesn't need to actually set up the view with the
right indexing; we'll do that manually later.
"""
return
"""CudaNdarray*
x
view = (CudaNdarray*)
return
"""CudaNdarray*
z
view = (CudaNdarray*)
CudaNdarray_New(
%(view_ndim)
s)"""
%
locals
()
def
get_helper_c_code_args
(
self
):
...
...
@@ -2487,41 +2487,41 @@ class GpuIncSubtensor(tensor.IncSubtensor, GpuOp):
def
link_view_array
(
self
,
x
,
fail
):
return
"""
if (CudaNdarray_set_device_data(
x
view, CudaNdarray_DEV_DATA(
%(x)
s),
if (CudaNdarray_set_device_data(
z
view, CudaNdarray_DEV_DATA(
%(x)
s),
(PyObject*) NULL))
{
PyErr_Format(PyExc_RuntimeError,
"GpuSubtensor is not able to set the"
" devdata field of the view");
Py_XDECREF(
x
view);
Py_XDECREF(
z
view);
%(fail)
s;
}
cnda_mark_dev_structure_dirty(
x
view);
cnda_mark_dev_structure_dirty(
z
view);
"""
%
locals
()
def
set_view_base
(
self
,
x
,
fail
):
return
"""
//Set the base only now
if(CudaNdarray_set_device_data(
xview, CudaNdarray_DEV_DATA(x
view),
if(CudaNdarray_set_device_data(
zview, CudaNdarray_DEV_DATA(z
view),
%(x)
s)){
PyErr_Format(PyExc_RuntimeError,
"GpuSubtensor is not able to set"
" the base of the view array");
Py_XDECREF(
x
view);
Py_XDECREF(
z
view);
%(fail)
s;
}"""
%
locals
()
def
add_to_
x
view
(
self
,
x
,
fail
):
def
add_to_
z
view
(
self
,
x
,
fail
):
return
"""
PyObject * add_result = CudaNdarray_inplace_add((PyObject *)
x
view,
PyObject * add_result = CudaNdarray_inplace_add((PyObject *)
z
view,
(PyObject *) py_
%(x)
s);
if (! add_result )
{
Py_DECREF(
x
view);
Py_DECREF(
z
view);
%(fail)
s;
}
else
...
...
theano/sandbox/cuda/cuda_ndarray.cu
浏览文件 @
5fe90b3d
...
...
@@ -101,33 +101,42 @@ int device_free(void *ptr)
// it returns something else I still don't see why we should ignore
// it. All we want to do here is reset the flag.
cudaGetLastError
();
#if COMPUTE_GPU_MEM_USED
fprintf
(
stderr
,
"Error freeing device pointer %p (%s).%d byte already allocated
\n
"
,
ptr
,
cudaGetErrorString
(
err
),
_allocated_size
);
#else
fprintf
(
stderr
,
"Error freeing device pointer %p (%s).
\n
"
,
ptr
,
cudaGetErrorString
(
err
));
#endif
PyErr_Format
(
PyExc_MemoryError
,
"error freeing device pointer %p (%s)"
,
ptr
,
cudaGetErrorString
(
err
));
#if COMPUTE_GPU_MEM_USED
fprintf
(
stderr
,
"Error freeing device pointer %p (%s).%d byte already allocated
\n
"
,
ptr
,
cudaGetErrorString
(
err
),
_allocated_size
);
#else
fprintf
(
stderr
,
"Error freeing device pointer %p (%s).
\n
"
,
ptr
,
cudaGetErrorString
(
err
));
#endif
PyErr_Format
(
PyExc_MemoryError
,
"error freeing device pointer %p (%s)"
,
ptr
,
cudaGetErrorString
(
err
));
return
-
1
;
}
_outstanding_mallocs
[
0
]
-=
(
ptr
!=
NULL
);
#if COMPUTE_GPU_MEM_USED
int
i
=
0
;
size_t
total_freed
=
0
;
for
(;
i
<
TABLE_SIZE
;
i
++
)
if
(
_alloc_size_table
[
i
].
ptr
==
ptr
){
_allocated_size
-=
_alloc_size_table
[
i
].
size
;
total_freed
+=
_alloc_size_table
[
i
].
size
;
_alloc_size_table
[
i
].
ptr
=
0
;
_alloc_size_table
[
i
].
size
=
0
;
#if COMPUTE_GPU_MEM_USED
int
i
=
0
;
size_t
total_freed
=
0
;
for
(;
i
<
TABLE_SIZE
;
i
++
)
if
(
_alloc_size_table
[
i
].
ptr
==
ptr
){
_allocated_size
-=
_alloc_size_table
[
i
].
size
;
total_freed
+=
_alloc_size_table
[
i
].
size
;
_alloc_size_table
[
i
].
ptr
=
0
;
_alloc_size_table
[
i
].
size
=
0
;
break
;
}
//if(i==TABLE_SIZE)
// printf("Unallocated unknow size!\n");
//fprintf(stderr, "freed %li bytes of device memory (%s). %d already allocated, ptr=%p\n", (long)total_freed, cudaGetErrorString(err),_allocated_size,ptr);
#endif
break
;
}
//if(i==TABLE_SIZE)
// printf("Unallocated unknow size!\n");
//fprintf(stderr, "freed %li bytes of device memory (%s). %d already allocated, ptr=%p\n", (long)total_freed, cudaGetErrorString(err),_allocated_size,ptr);
#endif
return
0
;
}
static
PyObject
*
outstanding_mallocs
(
PyObject
*
self
,
PyObject
*
args
)
{
...
...
theano/tensor/basic.py
浏览文件 @
5fe90b3d
...
...
@@ -3972,13 +3972,14 @@ class Subtensor(Op):
return
{
"c_prefix"
:
"PyArray"
,
"update_flags"
:
(
"PyArray_UpdateFlags(
xview
,"
"update_flags"
:
(
"PyArray_UpdateFlags(
%(view_name)
s
,"
" 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
}
"strides_mul"
:
1
,
"view_name"
:
"xview"
}
@staticmethod
...
...
@@ -3989,6 +3990,7 @@ class Subtensor(Op):
set_dim
=
None
,
set_stride
=
None
,
strides_mul
=
None
,
view_name
=
None
):
"""
The parameters c_prefix, update_flags, set_data, set_dim,
...
...
@@ -4016,6 +4018,11 @@ class Subtensor(Op):
if
c_prefix
is
None
:
c_prefix
=
default_args
[
'c_prefix'
]
if
view_name
is
None
:
view_name
=
default_args
[
'view_name'
]
#update_flags may depend on view_name
update_flags
=
update_flags
%
locals
()
#
# two arrays are created in C code:
...
...
@@ -4090,6 +4097,8 @@ class Subtensor(Op):
x
,
=
inputs
[:
1
]
z
,
=
outputs
xview
=
view_name
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
...
...
@@ -4105,30 +4114,30 @@ class Subtensor(Op):
int inner_ii = 0; // the current dimension of zview
int outer_ii = 0; // current dimension of z
char* ptr = (char*)
%(c_prefix)
s_BYTES(
xview
);
char* ptr = (char*)
%(c_prefix)
s_BYTES(
%(xview)
s
);
if ((
%(c_prefix)
s_DIMS(
xview
) ==
%(c_prefix)
s_DIMS(
%(x)
s))
if ((
%(c_prefix)
s_DIMS(
%(xview)
s
) ==
%(c_prefix)
s_DIMS(
%(x)
s))
&& (
%(c_prefix)
s_DIMS(
%(x)
s) != NULL))
{
PyErr_Format(PyExc_ValueError, "x and
xview
"
PyErr_Format(PyExc_ValueError, "x and
%(xview)
s
"
"(with
%%
d dims) have the same dimensions"
" pointers:
%%
p and
%%
p",
%(c_prefix)
s_NDIM(
%(x)
s),
%(c_prefix)
s_DIMS(
xview
),
%(c_prefix)
s_DIMS(
%(xview)
s
),
%(c_prefix)
s_DIMS(
%(x)
s));
Py_XDECREF(
xview
);
Py_XDECREF(
%(xview)
s
);
%(fail)
s;
}
if (
%(c_prefix)
s_STRIDES(
xview
) ==
%(c_prefix)
s_STRIDES(
%(x)
s)
if (
%(c_prefix)
s_STRIDES(
%(xview)
s
) ==
%(c_prefix)
s_STRIDES(
%(x)
s)
&& (
%(c_prefix)
s_DIMS(
%(x)
s) != NULL))
{
PyErr_Format(PyExc_ValueError, "x and
xview
"
PyErr_Format(PyExc_ValueError, "x and
%(xview)
s
"
"(with
%%
d dims) have the same strides"
" pointers:
%%
p and
%%
p",
%(c_prefix)
s_NDIM(
%(x)
s),
%(c_prefix)
s_STRIDES(
xview
),
%(c_prefix)
s_STRIDES(
%(xview)
s
),
%(c_prefix)
s_STRIDES(
%(x)
s));
Py_XDECREF(
xview
);
Py_XDECREF(
%(xview)
s
);
%(fail)
s;
}
...
...
@@ -4150,10 +4159,10 @@ class Subtensor(Op):
// PySlice_GetIndicesEx in python source
if (!step)
{
Py_DECREF(
xview
);
Py_DECREF(
%(xview)
s
);
PyErr_Format(PyExc_ValueError,
"slice step cannot be zero");
Py_XDECREF(
xview
);
Py_XDECREF(
%(xview)
s
);
%(fail)
s;
}
...
...
@@ -4204,8 +4213,8 @@ class Subtensor(Op):
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,
%(set_dim)
s(
%(xview)
s
, inner_ii, slicelength);
%(set_stride)
s(
%(xview)
s
, inner_ii,
%(c_prefix)
s_STRIDES(
%(x)
s)[outer_ii] * step);
inner_ii += 1;
...
...
@@ -4225,27 +4234,27 @@ class Subtensor(Op):
else
{
PyErr_Format(PyExc_IndexError,"index out of bounds");
Py_XDECREF(
xview
);
Py_XDECREF(
%(xview)
s
);
%(fail)
s;
}
}
else
{
PyErr_Format(PyExc_IndexError,"index out of bounds");
Py_XDECREF(
xview
);
Py_XDECREF(
%(xview)
s
);
%(fail)
s;
}
spec_pos += 1;
}
}
%(set_data)
s(
xview
, ptr, (PyObject*)NULL);
assert (inner_ii <=
%(c_prefix)
s_NDIM(
xview
));
while (inner_ii <
%(c_prefix)
s_NDIM(
xview
))
%(set_data)
s(
%(xview)
s
, ptr, (PyObject*)NULL);
assert (inner_ii <=
%(c_prefix)
s_NDIM(
%(xview)
s
));
while (inner_ii <
%(c_prefix)
s_NDIM(
%(xview)
s
))
{
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]);
%(set_dim)
s(
%(xview)
s
, inner_ii,
%(c_prefix)
s_DIMS(
%(x)
s)[outer_ii]);
%(set_stride)
s(
%(xview)
s
, inner_ii,
%(c_prefix)
s_STRIDES(
%(x)
s)[outer_ii]);
inner_ii += 1;
outer_ii += 1;
}
...
...
@@ -4601,7 +4610,7 @@ class IncSubtensor(Op):
{
if (
%(x)
s !=
%(z)
s)
{
Py_
x
DECREF(
%(z)
s);
Py_
X
DECREF(
%(z)
s);
Py_INCREF(
%(x)
s);
%(z)
s =
%(x)
s;
}
...
...
@@ -4613,28 +4622,25 @@ class IncSubtensor(Op):
}
"""
%
locals
()
# IG: Note: this makes a variable called "xview"
# even though it is a view of z.
# I assume this is because IncSubtensor was written
# by copy-pasting Subtensor and in Subtensor you make
# a view of x.
alloc_view_of_z
=
self
.
make_view_array
(
z
,
view_ndim
)
alloc_zview
=
self
.
make_view_array
(
z
,
view_ndim
)
# On GPU, it takes two steps to make a view
link_
view_of_z
=
self
.
link_view_array
(
z
,
fail
);
link_
zview
=
self
.
link_view_array
(
z
,
fail
);
#Make a first view on the output, as we will write into it.
build_view
=
"""
//TODO: give this Op a second output so that this view can be cached
//TODO: alternatively, fix the memory leak on failure
%(alloc_
view_of_z
)
s;
if (!
x
view)
%(alloc_
zview
)
s;
if (!
z
view)
{
%(fail)
s;
}
%(link_
view_of_z
)
s;
%(link_
zview
)
s;
"""
%
locals
()
# make xview actually a view of %(z)s
get_xview
=
self
.
define_set_data
()
+
\
# make zview actually a view of %(z)s
helper_args
=
self
.
get_helper_c_code_args
()
helper_args
[
'view_name'
]
=
'zview'
get_zview
=
self
.
define_set_data
()
+
\
Subtensor
.
helper_c_code
(
node
=
node
,
name
=
name
,
...
...
@@ -4642,33 +4648,33 @@ class IncSubtensor(Op):
outputs
=
outputs
,
sub
=
sub
,
idx_list
=
self
.
idx_list
,
**
self
.
get_helper_c_code_args
()
**
helper_args
)
copy_into
=
self
.
copy_into
(
"
x
view"
,
y
)
copy_into
=
self
.
copy_into
(
"
z
view"
,
y
)
add_to_
xview
=
self
.
add_to_x
view
(
y
,
fail
)
add_to_
zview
=
self
.
add_to_z
view
(
y
,
fail
)
make_modification
=
"""
if (
%(op_is_set)
s)
{
if (
%(copy_into)
s) // does broadcasting
{
Py_DECREF(
x
view);
Py_DECREF(
z
view);
%(fail)
s;
}
}
else
{
%(add_to_
x
view)
s
%(add_to_
z
view)
s
}
"""
%
locals
()
return
(
copy_input_if_necessary
+
build_view
+
"{"
+
get_
x
view
+
"}"
+
"{"
+
get_
z
view
+
"}"
+
make_modification
+
"Py_DECREF(
x
view);"
+
"Py_DECREF(
z
view);"
)
def
do_type_checking
(
self
,
node
):
...
...
@@ -4719,7 +4725,7 @@ class IncSubtensor(Op):
"""
return
"""Py_INCREF(PyArray_DESCR(
%(x)
s));
PyArrayObject *
x
view =
PyArrayObject *
z
view =
(PyArrayObject*)PyArray_NewFromDescr(
&PyArray_Type,
PyArray_DESCR(
%(x)
s),
...
...
@@ -4750,34 +4756,34 @@ class IncSubtensor(Op):
return
""
def
link_view_array
(
self
,
x
,
fail
):
""" Returns code to complete making
x
view a view of x"""
""" Returns code to complete making
z
view a view of x"""
# On CPU there is nothing to do, make_view_array already did this
return
""
def
set_view_base
(
self
,
x
,
fail
):
""" Returns code to make
x
view be a correct view of x,
""" Returns code to make
z
view be a correct view of x,
after helper_c_code is done messing with x"""
# On CPU there is nothing to do
return
""
def
add_to_
x
view
(
self
,
x
,
fail
):
""" Return C code to add x to
xview. Should DECREF x
view if the
def
add_to_
z
view
(
self
,
x
,
fail
):
""" Return C code to add x to
zview. Should DECREF z
view if the
add fails."""
return
"""
PyArrayObject * add_rval = (PyArrayObject*)PyNumber_InPlaceAdd(
(PyObject*)
x
view, py_
%(x)
s);
(PyObject*)
z
view, py_
%(x)
s);
if (add_rval)
{
assert (PyArray_Check((PyObject*)add_rval));
assert (PyArray_DATA(add_rval) == PyArray_DATA(
x
view));
assert (PyArray_DATA(add_rval) == PyArray_DATA(
z
view));
Py_DECREF(add_rval);
}
else
{
Py_DECREF(
x
view);
Py_DECREF(
z
view);
%(fail)
s;
}"""
%
locals
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论