Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
305b11ae
提交
305b11ae
authored
6月 27, 2013
作者:
Vivek Kulkarni
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2 from nouiz/viveksck-try_nouiz
Bugfix and reuse old code when the new one isn't used
上级
32e5fa85
69267c88
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
169 行增加
和
24 行删除
+169
-24
basic_ops.py
theano/sandbox/cuda/basic_ops.py
+142
-15
opt.py
theano/sandbox/cuda/opt.py
+16
-1
test_basic_ops.py
theano/sandbox/cuda/tests/test_basic_ops.py
+11
-8
没有找到文件。
theano/sandbox/cuda/basic_ops.py
浏览文件 @
305b11ae
...
...
@@ -2308,6 +2308,7 @@ class GpuSubtensor(GpuOp, tensor.Subtensor):
return
()
return
(
3
,
hv
)
class
GpuAdvancedSubtensor1
(
tensor
.
AdvancedSubtensor1
,
GpuOp
):
"""
Implement AdvancedSubtensor1 on the gpu.
...
...
@@ -2392,14 +2393,6 @@ class GpuAdvancedIncSubtensor1(tensor.AdvancedIncSubtensor1, GpuOp):
y_
=
as_cuda_ndarray_variable
(
y
)
ilist_
=
tensor
.
as_tensor_variable
(
ilist
)
convert_map
=
{
8
:
tensor
.
basic
.
_convert_to_int8
,
16
:
tensor
.
basic
.
_convert_to_int16
,
32
:
tensor
.
basic
.
_convert_to_int32
,
64
:
tensor
.
basic
.
_convert_to_int64
}
intwidth
=
theano
.
gof
.
compiledir
.
python_int_bitwidth
()
ilist_
=
convert_map
[
intwidth
](
ilist_
)
assert
x_
.
type
.
dtype
==
y_
.
type
.
dtype
assert
x_
.
type
.
ndim
>=
y_
.
type
.
ndim
...
...
@@ -2451,14 +2444,11 @@ class GpuAdvancedIncSubtensor1(tensor.AdvancedIncSubtensor1, GpuOp):
out
[
0
]
=
x
def
c_code_cache_version
(
self
):
return
(
1
,)
return
(
3
,)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
active_device_no
=
theano
.
sandbox
.
cuda
.
active_device_number
()
compute_capability
=
theano
.
sandbox
.
cuda
.
device_properties
(
active_device_no
)[
'major'
]
if
(
self
.
set_instead_of_inc
)
or
\
(
node
.
inputs
[
0
]
.
ndim
!=
node
.
inputs
[
1
]
.
ndim
)
or
\
(
compute_capability
<
2
):
(
node
.
inputs
[
0
]
.
ndim
!=
node
.
inputs
[
1
]
.
ndim
):
raise
NotImplementedError
(
"This case does not have C code yet."
)
x
=
inputs
[
0
]
...
...
@@ -2469,6 +2459,19 @@ class GpuAdvancedIncSubtensor1(tensor.AdvancedIncSubtensor1, GpuOp):
inplace
=
int
(
self
.
inplace
)
return
"""
PyObject *x_obj, *y_obj, *row_x, *row_y;
PyObject *x_rowind_obj, *y_rowind_obj;
dtype_
%(ind)
s *p_index;
int num_indices, j;
int ret;
num_indices = PyArray_SIZE(
%(ind)
s);
if ((num_indices - 1) > LONG_MAX) {
PyErr_Format(PyExc_AssertionError,
"num_indices
%%
d exceeds LONG_MAX + 1", num_indices);
%(fail)
s;
}
Py_XDECREF(
%(out)
s);
if (!
%(inplace)
s) {
%(out)
s = (CudaNdarray*)CudaNdarray_Copy(
%(x)
s);
...
...
@@ -2477,12 +2480,136 @@ class GpuAdvancedIncSubtensor1(tensor.AdvancedIncSubtensor1, GpuOp):
Py_XINCREF(
%(out)
s);
}
CudaNdarray_vector_add_fast(
%(x)
s,
%(y)
s,
%(ind)
s);
x_obj = (PyObject*)CudaNdarray_View(
%(out)
s);
y_obj = (PyObject*)CudaNdarray_View(
%(y)
s);
for (j = 0;j < num_indices; j++) {
p_index = (dtype_
%(ind)
s *)PyArray_GETPTR1(
%(ind)
s, j);
x_rowind_obj = PyInt_FromLong(*p_index);
if (PyInt_AsLong(x_rowind_obj) != (*p_index)) {
PyErr_Format(PyExc_AssertionError,
"Error in converting row index to integer from long");
// Dec Ref what ever we have increfed or allocated so far
// We deallocate objects exactly in the reverse order they were allocated.
Py_XDECREF(x_rowind_obj);
Py_XDECREF(y_obj);
Py_XDECREF(x_obj);
%(fail)
s;
}
y_rowind_obj = PyInt_FromLong(j);
row_x = CudaNdarray_Subscript(x_obj, x_rowind_obj);
row_y = CudaNdarray_Subscript(y_obj, y_rowind_obj);
if ((row_x == NULL) || (row_y == NULL)) {
Py_XDECREF(row_y);
Py_XDECREF(row_x);
Py_XDECREF(y_rowind_obj);
Py_XDECREF(x_rowind_obj);
Py_XDECREF(y_obj);
Py_XDECREF(x_obj);
%(fail)
s;
}
ret = CudaNdarray_inplace_elemwise(row_x, row_y, IADD);
if (ret != 0) {
Py_XDECREF(row_y);
Py_XDECREF(row_x);
Py_XDECREF(y_rowind_obj);
Py_XDECREF(x_rowind_obj);
Py_XDECREF(y_obj);
Py_XDECREF(x_obj);
%(fail)
s;
}
Py_XDECREF(row_y);
Py_XDECREF(row_x);
Py_XDECREF(y_rowind_obj);
Py_XDECREF(x_rowind_obj);
}
Py_XDECREF(y_obj);
Py_XDECREF(x_obj);
if (!
%(out)
s) {
%(fail)
s
}
"""
%
locals
()
"""
%
locals
()
class
GpuAdvancedIncSubtensor1_dev20
(
GpuAdvancedIncSubtensor1
):
"""Implement AdvancedIncSubtensor1 on the gpu, but use function
only avail on compute capability 2.0 and more recent.
"""
def
make_node
(
self
,
x
,
y
,
ilist
):
"""It defer from GpuAdvancedIncSubtensor1 in that it make sure
the index are of type long.
"""
x_
=
as_cuda_ndarray_variable
(
x
)
y_
=
as_cuda_ndarray_variable
(
y
)
ilist_
=
tensor
.
as_tensor_variable
(
ilist
)
convert_map
=
{
8
:
tensor
.
basic
.
_convert_to_int8
,
16
:
tensor
.
basic
.
_convert_to_int16
,
32
:
tensor
.
basic
.
_convert_to_int32
,
64
:
tensor
.
basic
.
_convert_to_int64
}
intwidth
=
theano
.
gof
.
compiledir
.
python_int_bitwidth
()
ilist_
=
convert_map
[
intwidth
](
ilist_
)
assert
x_
.
type
.
dtype
==
y_
.
type
.
dtype
assert
x_
.
type
.
ndim
>=
y_
.
type
.
ndim
if
ilist_
.
type
.
dtype
[:
3
]
not
in
(
'int'
,
'uin'
):
raise
TypeError
(
'index must be integers'
)
if
ilist_
.
type
.
broadcastable
!=
(
False
,):
raise
TypeError
(
'index must be vector'
)
if
x_
.
type
.
ndim
==
0
:
raise
TypeError
(
'cannot index into a scalar'
)
if
x_
.
type
.
broadcastable
[
0
]:
# the caller should have made a copy of x len(ilist) times
raise
TypeError
(
'cannot index into a broadcastable dimension'
)
return
Apply
(
self
,
[
x_
,
y_
,
ilist_
],
[
x_
.
type
()])
def
c_code_cache_version
(
self
):
return
(
2
,)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
active_device_no
=
theano
.
sandbox
.
cuda
.
active_device_number
()
compute_capability
=
device_properties
(
active_device_no
)[
'major'
]
if
((
self
.
set_instead_of_inc
)
or
(
node
.
inputs
[
0
]
.
ndim
!=
node
.
inputs
[
1
]
.
ndim
)
or
(
node
.
inputs
[
0
]
.
ndim
!=
2
)
or
(
compute_capability
<
2
)):
raise
NotImplementedError
(
"This case does not have C code yet."
)
x
=
inputs
[
0
]
y
=
inputs
[
1
]
ind
=
inputs
[
2
]
out
=
outputs
[
0
]
fail
=
sub
[
'fail'
]
inplace
=
int
(
self
.
inplace
)
return
"""
Py_XDECREF(
%(out)
s);
if (!
%(inplace)
s) {
%(out)
s = (CudaNdarray*)CudaNdarray_Copy(
%(x)
s);
} else {
%(out)
s =
%(x)
s;
Py_XINCREF(
%(out)
s);
}
CudaNdarray_vector_add_fast(
%(out)
s,
%(y)
s,
%(ind)
s);
if (!
%(out)
s) {
%(fail)
s
}
"""
%
locals
()
def
c_support_code_apply
(
self
,
node
,
nodename
):
return
"""
...
...
theano/sandbox/cuda/opt.py
浏览文件 @
305b11ae
...
...
@@ -776,9 +776,16 @@ def local_gpu_advanced_incsubtensor1(node):
'either set the `warn.gpu_set_subtensor1` config '
'option to False, or `warn.ignore_bug_before` to at '
'least
\'
0.6
\'
.'
,
stacklevel
=
1
)
active_device_no
=
theano
.
sandbox
.
cuda
.
active_device_number
()
compute_capability
=
device_properties
(
active_device_no
)[
'major'
]
if
(
compute_capability
<
2
or
x
.
ndim
!=
2
or
y
.
ndim
!=
2
):
gpu_op
=
GpuAdvancedIncSubtensor1
(
set_instead_of_inc
=
set_instead_of_inc
)
else
:
gpu_op
=
GpuAdvancedIncSubtensor1_dev20
(
set_instead_of_inc
=
set_instead_of_inc
)
return
[
gpu_op
(
gpu_from_host
(
x
),
gpu_from_host
(
y
),
*
coords
)]
# Should not execute for GpuAdvancedIncSubtensor1
...
...
@@ -809,8 +816,16 @@ def local_gpu_advanced_incsubtensor1(node):
'option to False, or `warn.ignore_bug_before` to at '
'least
\'
0.6
\'
.'
,
stacklevel
=
1
)
active_device_no
=
theano
.
sandbox
.
cuda
.
active_device_number
()
compute_capability
=
device_properties
(
active_device_no
)[
'major'
]
if
(
compute_capability
<
2
or
x
.
ndim
!=
2
or
y
.
ndim
!=
2
):
gpu_op
=
GpuAdvancedIncSubtensor1
(
set_instead_of_inc
=
set_instead_of_inc
)
else
:
gpu_op
=
GpuAdvancedIncSubtensor1_dev20
(
set_instead_of_inc
=
set_instead_of_inc
)
return
[
host_from_gpu
(
gpu_op
(
gpu_x
,
gpu_y
,
*
coords
))]
return
False
...
...
theano/sandbox/cuda/tests/test_basic_ops.py
浏览文件 @
305b11ae
...
...
@@ -999,20 +999,23 @@ class T_subtensor(theano.tensor.tests.test_basic.T_subtensor):
def
test_advinc_subtensor1
():
""" Test the second case in the opt local_gpu_advanced_incsubtensor1 """
for
shp
in
[(
3
,
3
),
(
3
,
3
,
3
)]:
shared
=
cuda
.
shared_constructor
#shared = tensor.shared
xval
=
numpy
.
asarray
([[
1
,
2
,
3
],
[
4
,
5
,
6
],
[
7
,
8
,
9
]],
dtype
=
'float32'
)
yval
=
numpy
.
asarray
([[
10
,
10
,
10
],
[
10
,
10
,
10
]],
dtype
=
'float32'
)
xval
=
numpy
.
arange
(
numpy
.
prod
(
shp
),
dtype
=
'float32'
)
.
reshape
(
shp
)
+
1
yval
=
numpy
.
empty
((
2
,)
+
shp
[
1
:],
dtype
=
'float32'
)
yval
[:]
=
10
x
=
shared
(
xval
,
name
=
'x'
)
y
=
T
.
fmatrices
(
'y'
)
y
=
T
.
tensor
(
dtype
=
'float32'
,
broadcastable
=
(
False
,)
*
len
(
shp
),
name
=
'y'
)
expr
=
T
.
advanced_inc_subtensor1
(
x
,
y
,
[
0
,
2
])
f
=
theano
.
function
([
y
],
expr
,
mode
=
mode_with_gpu
)
assert
sum
([
isinstance
(
node
.
op
,
cuda
.
GpuAdvancedIncSubtensor1
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
==
1
assert
numpy
.
allclose
(
f
(
yval
),
[[
11.
,
12.
,
13.
],
[
4.
,
5.
,
6.
],
[
17.
,
18.
,
19.
]])
rval
=
f
(
yval
)
rep
=
xval
.
copy
()
rep
[[
0
,
2
]]
+=
yval
assert
numpy
.
allclose
(
rval
,
rep
)
def
test_inc_subtensor
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论