Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
800bf55c
提交
800bf55c
authored
9月 12, 2013
作者:
Arnaud Bergeron
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add GpuDimShuffle (both the C and python versions).
上级
68ab8d91
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
126 行增加
和
10 行删除
+126
-10
elemwise.py
theano/sandbox/gpuarray/elemwise.py
+113
-1
test_elemwise.py
theano/sandbox/gpuarray/tests/test_elemwise.py
+5
-2
opt.py
theano/tensor/opt.py
+1
-1
test_elemwise.py
theano/tensor/tests/test_elemwise.py
+7
-6
没有找到文件。
theano/sandbox/gpuarray/elemwise.py
浏览文件 @
800bf55c
...
@@ -3,7 +3,7 @@ from itertools import izip
...
@@ -3,7 +3,7 @@ from itertools import izip
import
numpy
import
numpy
from
theano
import
Op
,
Apply
,
scalar
from
theano
import
Op
,
Apply
,
scalar
from
theano.tensor.elemwise
import
Elemwise
from
theano.tensor.elemwise
import
Elemwise
,
DimShuffle
try
:
try
:
import
pygpu
import
pygpu
...
@@ -173,3 +173,115 @@ class SupportCodeError(Exception):
...
@@ -173,3 +173,115 @@ class SupportCodeError(Exception):
"""
"""
We do not support certain things (such as the C++ complex struct)
We do not support certain things (such as the C++ complex struct)
"""
"""
class
GpuDimShuffle
(
DimShuffle
):
def
make_node
(
self
,
input
):
res
=
DimShuffle
.
make_node
(
self
,
input
)
otype
=
GpuArrayType
(
dtype
=
res
.
outputs
[
0
]
.
type
.
dtype
,
broadcastable
=
res
.
outputs
[
0
]
.
type
.
broadcastable
)
input
=
as_gpuarray_variable
(
input
)
return
Apply
(
self
,
[
input
],
[
otype
()])
def
__str__
(
self
):
if
self
.
inplace
:
s
=
"InplaceGpuDimShuffle{
%
s}"
else
:
s
=
"GpuDimShuffle{
%
s}"
return
s
%
(
','
.
join
(
str
(
x
)
for
x
in
self
.
new_order
))
def
perform
(
self
,
node
,
inp
,
out
):
input
,
=
inp
storage
,
=
out
res
=
input
res
=
res
.
transpose
(
self
.
shuffle
+
self
.
drop
)
shape
=
list
(
res
.
shape
[:
len
(
self
.
shuffle
)])
for
augm
in
self
.
augment
:
shape
.
insert
(
augm
,
1
)
res
=
res
.
reshape
(
shape
)
if
not
self
.
inplace
:
res
=
res
.
copy
()
storage
[
0
]
=
res
def
c_support_code_apply
(
self
,
node
,
name
):
def
copy_shape
(
nd_out
):
stmts
=
[]
e
=
0
for
d
in
range
(
nd_out
):
if
d
in
self
.
augment
:
stmts
.
append
(
"sh[
%
s] = 1;"
%
(
d
,))
else
:
stmts
.
append
(
"sh[
%
s] = tmp.dimensions[
%
s];"
%
(
d
,
e
))
e
+=
1
return
'
\n
'
.
join
(
stmts
)
return
"""
static const unsigned int
%(name)
s_ax[] = {
%(shuffle)
s};
static int
%(name)
s_f(GpuArrayObject *res, GpuArrayObject *a) {
GpuArray tmp;
size_t sh[
%(nd_out)
s];
unsigned int i;
int err;
err = GpuArray_transpose(&tmp, &a->ga,
%(name)
s_ax);
if (err != GA_NO_ERROR) {
PyErr_SetString(PyExc_RuntimeError, "error in _transpose call");
return -1;
}
%(copy_shape)
s
err = GpuArray_reshape(&res->ga, &tmp,
%(nd_out)
s, sh,
GA_ANY_ORDER, 1);
if (err != GA_NO_ERROR) {
PyErr_SetString(PyExc_RuntimeError, "error in _reshape call");
return -1;
}
GpuArray_clear(&tmp);
return 0;
}
"""
%
dict
(
shuffle
=
', '
.
join
(
str
(
a
)
for
a
in
(
self
.
shuffle
+
self
.
drop
)),
name
=
name
,
nd_out
=
len
(
self
.
new_order
),
copy_shape
=
copy_shape
(
len
(
self
.
new_order
)))
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
d
=
dict
(
name
=
name
,
fail
=
sub
[
'fail'
],
inp
=
inputs
[
0
],
out
=
outputs
[
0
],
nd
=
len
(
self
.
input_broadcastable
))
process
=
"""
if (
%(inp)
s->ga.nd !=
%(nd)
s) {
PyErr_SetString(PyExc_TypeError, "input nd");
%(fail)
s
}
Py_XDECREF(
%(out)
s);
%(out)
s = new_GpuArray((PyObject *)&GpuArrayType, GpuArray_default_context());
if (
%(out)
s == NULL) {
%(fail)
s}
if (
%(name)
s_f(
%(out)
s,
%(inp)
s)) {
%(fail)
s
}
"""
%
d
if
not
self
.
inplace
:
process
+=
"""
if (
%(out)
s->ga.data ==
%(inp)
s->ga.data) {
PyObject *
%(name)
s_tmp;
%(name)
s_tmp = PyObject_CallMethod((PyObject *)
%(out)
s, "copy", NULL);
if (
%(name)
s_tmp == NULL) {
%(fail)
s }
if (!PyObject_IsInstance(
%(name)
s_tmp, (PyObject *)&GpuArrayType)) {
PyErr_SetString(PyExc_TypeError, "not a GpuArray out of the copy");
%(fail)
s
}
Py_DECREF(
%(out)
s);
%(out)
s = (GpuArrayObject *)
%(name)
s_tmp;
}
"""
%
d
return
process
def
c_code_cache_version
(
self
):
return
(
0
,)
theano/sandbox/gpuarray/tests/test_elemwise.py
浏览文件 @
800bf55c
...
@@ -4,10 +4,10 @@ from theano import scalar
...
@@ -4,10 +4,10 @@ from theano import scalar
from
theano.gof
import
FunctionGraph
from
theano.gof
import
FunctionGraph
from
theano.gof.python25
import
all
,
any
from
theano.gof.python25
import
all
,
any
from
theano.tensor.tests.test_elemwise
import
test_Broadcast
from
theano.tensor.tests.test_elemwise
import
test_Broadcast
,
test_DimShuffle
from
theano.sandbox.gpuarray.tests.test_basic_ops
import
rand_gpuarray
from
theano.sandbox.gpuarray.tests.test_basic_ops
import
rand_gpuarray
from
theano.sandbox.gpuarray.elemwise
import
GpuElemwise
from
theano.sandbox.gpuarray.elemwise
import
GpuElemwise
,
GpuDimShuffle
from
theano.sandbox.gpuarray.type
import
GpuArrayType
from
theano.sandbox.gpuarray.type
import
GpuArrayType
from
pygpu.array
import
gpuarray
from
pygpu.array
import
gpuarray
...
@@ -26,3 +26,6 @@ class test_gpu_Broadcast(test_Broadcast):
...
@@ -26,3 +26,6 @@ class test_gpu_Broadcast(test_Broadcast):
#def rand_cval(self, shp):
#def rand_cval(self, shp):
# return rand_gpuarray(*shp, **dict(cls=gpuarray))
# return rand_gpuarray(*shp, **dict(cls=gpuarray))
class
test_GpuDimShuffle
(
test_DimShuffle
):
op
=
GpuDimShuffle
theano/tensor/opt.py
浏览文件 @
800bf55c
...
@@ -460,7 +460,7 @@ def dimshuffle_as_view(node):
...
@@ -460,7 +460,7 @@ def dimshuffle_as_view(node):
op
=
node
.
op
op
=
node
.
op
if
not
isinstance
(
op
,
DimShuffle
)
or
op
.
inplace
:
if
not
isinstance
(
op
,
DimShuffle
)
or
op
.
inplace
:
return
False
return
False
new_op
=
DimShuffle
(
op
.
input_broadcastable
,
op
.
new_order
,
inplace
=
True
)
new_op
=
op
.
__class__
(
op
.
input_broadcastable
,
op
.
new_order
,
inplace
=
True
)
return
[
new_op
(
*
node
.
inputs
)]
return
[
new_op
(
*
node
.
inputs
)]
#Step 60 is the inplace optimization stage.
#Step 60 is the inplace optimization stage.
...
...
theano/tensor/tests/test_elemwise.py
浏览文件 @
800bf55c
...
@@ -24,6 +24,7 @@ def FunctionGraph(i, o):
...
@@ -24,6 +24,7 @@ def FunctionGraph(i, o):
class
test_DimShuffle
(
unittest_tools
.
InferShapeTester
):
class
test_DimShuffle
(
unittest_tools
.
InferShapeTester
):
op
=
DimShuffle
def
with_linker
(
self
,
linker
):
def
with_linker
(
self
,
linker
):
for
xsh
,
shuffle
,
zsh
in
[((
2
,
3
),
(
1
,
'x'
,
0
),
(
3
,
1
,
2
)),
for
xsh
,
shuffle
,
zsh
in
[((
2
,
3
),
(
1
,
'x'
,
0
),
(
3
,
1
,
2
)),
...
@@ -38,12 +39,12 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
...
@@ -38,12 +39,12 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
((
1
,),
(
'x'
,
'x'
),
(
1
,
1
))]:
((
1
,),
(
'x'
,
'x'
),
(
1
,
1
))]:
ib
=
[(
entry
==
1
)
for
entry
in
xsh
]
ib
=
[(
entry
==
1
)
for
entry
in
xsh
]
x
=
TensorType
(
'float64'
,
ib
)(
'x'
)
x
=
TensorType
(
'float64'
,
ib
)(
'x'
)
e
=
DimShuffle
(
ib
,
shuffle
)(
x
)
e
=
self
.
op
(
ib
,
shuffle
)(
x
)
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
],
[
e
]))
.
make_function
()
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
],
[
e
]))
.
make_function
()
assert
f
(
numpy
.
ones
(
xsh
))
.
shape
==
zsh
assert
f
(
numpy
.
ones
(
xsh
))
.
shape
==
zsh
#test that DimShuffle.infer_shape work correctly
#test that DimShuffle.infer_shape work correctly
x
=
TensorType
(
'float64'
,
ib
)(
'x'
)
x
=
TensorType
(
'float64'
,
ib
)(
'x'
)
e
=
DimShuffle
(
ib
,
shuffle
)(
x
)
e
=
self
.
op
(
ib
,
shuffle
)(
x
)
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
],
[
e
.
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
],
[
e
.
shape
]))
.
make_function
()
shape
]))
.
make_function
()
assert
all
(
f
(
numpy
.
ones
(
xsh
)))
==
all
(
zsh
)
assert
all
(
f
(
numpy
.
ones
(
xsh
)))
==
all
(
zsh
)
...
@@ -51,12 +52,12 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
...
@@ -51,12 +52,12 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
# Test when we drop a axis that is not broadcastable
# Test when we drop a axis that is not broadcastable
ib
=
[
False
,
True
,
False
]
ib
=
[
False
,
True
,
False
]
x
=
TensorType
(
'float64'
,
ib
)(
'x'
)
x
=
TensorType
(
'float64'
,
ib
)(
'x'
)
self
.
assertRaises
(
ValueError
,
DimShuffle
,
ib
,
shuffle
)
self
.
assertRaises
(
ValueError
,
self
.
op
,
ib
,
shuffle
)
# Test when we drop a axis that don't have shape 1
# Test when we drop a axis that don't have shape 1
ib
=
[
True
,
True
,
False
]
ib
=
[
True
,
True
,
False
]
x
=
TensorType
(
'float64'
,
ib
)(
'x'
)
x
=
TensorType
(
'float64'
,
ib
)(
'x'
)
e
=
DimShuffle
(
ib
,
(
1
,
2
))(
x
)
e
=
self
.
op
(
ib
,
(
1
,
2
))(
x
)
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
],
[
e
.
shape
]))
.
make_function
()
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
],
[
e
.
shape
]))
.
make_function
()
self
.
assertRaises
(
TypeError
,
f
,
numpy
.
ones
((
2
,
1
,
4
)))
self
.
assertRaises
(
TypeError
,
f
,
numpy
.
ones
((
2
,
1
,
4
)))
...
@@ -89,8 +90,8 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
...
@@ -89,8 +90,8 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
adtens
=
TensorType
(
'float64'
,
ib
)(
'x'
)
adtens
=
TensorType
(
'float64'
,
ib
)(
'x'
)
adtens_val
=
numpy
.
ones
(
xsh
)
adtens_val
=
numpy
.
ones
(
xsh
)
self
.
_compile_and_check
([
adtens
],
self
.
_compile_and_check
([
adtens
],
[
DimShuffle
(
ib
,
shuffle
)(
adtens
)],
[
self
.
op
(
ib
,
shuffle
)(
adtens
)],
[
adtens_val
],
DimShuffle
,
[
adtens_val
],
self
.
op
,
warn
=
False
)
warn
=
False
)
def
test_too_big_rank
(
self
):
def
test_too_big_rank
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论