Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
43f97ea3
提交
43f97ea3
authored
7月 23, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test_elemwise{0,1,2} pass in debugmode
上级
602e87d8
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
73 行增加
和
45 行删除
+73
-45
Makefile
Makefile
+2
-2
basic_ops.py
basic_ops.py
+12
-12
test_basic_ops.py
tests/test_basic_ops.py
+21
-5
type.py
type.py
+16
-4
type_support.cu
type_support.cu
+22
-22
没有找到文件。
Makefile
浏览文件 @
43f97ea3
type_support.so
:
type_support.cu
type_support.so
:
type_support.cu
$(HOME)/cvs/lgcm/cuda_ndarray/cuda_ndarray.so
nvcc
-O3
-shared
-I
$(HOME)
/cvs/lgcm/cuda_ndarray
-I
$(CUDA_ROOT)
/include
-I
/usr/include/python2.6
-o
type_support.so
-Xcompiler
-fPIC
type_support.cu
-L
$(CUDA_ROOT)
/lib
$(HOME)
/cvs/lgcm/cuda_ndarray/cuda_ndarray.so
nvcc
-g
-shared
-I
$(HOME)
/cvs/lgcm/cuda_ndarray
-I
$(CUDA_ROOT)
/include
-I
/usr/include/python2.6
-o
type_support.so
-Xcompiler
-fPIC
type_support.cu
-L
$(CUDA_ROOT)
/lib
-L
$(HOME)
/cvs/lgcm/cuda_ndarray
-lcuda_ndarray
clean
:
clean
:
rm
type_support.so
rm
type_support.so
basic_ops.py
浏览文件 @
43f97ea3
...
@@ -275,7 +275,7 @@ class GpuElemwise(Op):
...
@@ -275,7 +275,7 @@ class GpuElemwise(Op):
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
opname
=
str
(
self
.
scalar_op
)
opname
=
str
(
self
.
scalar_op
)
print
>>
sio
,
"""
print
>>
sio
,
"""
std::cerr << "C_CODE
%(opname)
s START
\\
n";
//
std::cerr << "C_CODE
%(opname)
s START
\\
n";
//standard elemwise size checks
//standard elemwise size checks
const int * dims = NULL;
const int * dims = NULL;
"""
%
locals
()
"""
%
locals
()
...
@@ -310,7 +310,7 @@ class GpuElemwise(Op):
...
@@ -310,7 +310,7 @@ class GpuElemwise(Op):
print
>>
sio
,
"""
print
>>
sio
,
"""
if (cnda_
%(oname)
s){
if (cnda_
%(oname)
s){
//TODO: check if we can maybe use existing storage
//TODO: check if we can maybe use existing storage
Py_
X
DECREF(cnda_
%(oname)
s);
Py_DECREF(cnda_
%(oname)
s);
cnda_
%(oname)
s = NULL;
cnda_
%(oname)
s = NULL;
}
}
if (NULL == cnda_
%(oname)
s)
if (NULL == cnda_
%(oname)
s)
...
@@ -324,13 +324,13 @@ class GpuElemwise(Op):
...
@@ -324,13 +324,13 @@ class GpuElemwise(Op):
if (CudaNdarray_alloc_contiguous(cnda_
%(oname)
s,
%(nd)
s, dims))
if (CudaNdarray_alloc_contiguous(cnda_
%(oname)
s,
%(nd)
s, dims))
{
{
//error string already set
//error string already set
Py_
X
DECREF(cnda_
%(oname)
s);
Py_DECREF(cnda_
%(oname)
s);
cnda_
%(oname)
s = NULL;
cnda_
%(oname)
s = NULL;
%(fail)
s;
%(fail)
s;
}
}
}
}
std::cerr << "ELEMWISE NEW
%(oname)
s nd" << cnda_
%(oname)
s->nd << "
\\
n";
//
std::cerr << "ELEMWISE NEW
%(oname)
s nd" << cnda_
%(oname)
s->nd << "
\\
n";
std::cerr << "ELEMWISE NEW
%(oname)
s data" << cnda_
%(oname)
s->devdata << "
\\
n";
//
std::cerr << "ELEMWISE NEW
%(oname)
s data" << cnda_
%(oname)
s->devdata << "
\\
n";
"""
%
locals
()
"""
%
locals
()
print
>>
sio
,
"""
print
>>
sio
,
"""
{
{
...
@@ -357,14 +357,14 @@ class GpuElemwise(Op):
...
@@ -357,14 +357,14 @@ class GpuElemwise(Op):
"""
%
locals
()
"""
%
locals
()
for
oname
in
outputs
:
for
oname
in
outputs
:
print
>>
sio
,
"""
print
>>
sio
,
"""
Py_
X
DECREF(cnda_
%(oname)
s);
Py_DECREF(cnda_
%(oname)
s);
cnda_
%(oname)
s = NULL;
cnda_
%(oname)
s = NULL;
"""
%
locals
()
"""
%
locals
()
print
>>
sio
,
"""
print
>>
sio
,
"""
%(fail)
s;
%(fail)
s;
}
}
}
}
std::cerr << "C_CODE
%(opname)
s END
\\
n";
//
std::cerr << "C_CODE
%(opname)
s END
\\
n";
"""
%
locals
()
"""
%
locals
()
return
sio
.
getvalue
()
return
sio
.
getvalue
()
...
@@ -477,14 +477,14 @@ class GpuDimShuffle(Op):
...
@@ -477,14 +477,14 @@ class GpuDimShuffle(Op):
if (CudaNdarray_set_nd(cnda_
%(res)
s,
%(nd_out)
s))
if (CudaNdarray_set_nd(cnda_
%(res)
s,
%(nd_out)
s))
{
{
// err message set
// err message set
Py_
X
DECREF(cnda_
%(res)
s);
Py_DECREF(cnda_
%(res)
s);
cnda_
%(res)
s = NULL;
cnda_
%(res)
s = NULL;
%(fail)
s;
%(fail)
s;
}
}
if (CudaNdarray_set_device_data(cnda_
%(res)
s, CudaNdarray_DEV_DATA(cnda_
%(input)
s)))
if (CudaNdarray_set_device_data(cnda_
%(res)
s, CudaNdarray_DEV_DATA(cnda_
%(input)
s)
, cnda_
%(input)
s
))
{
{
// err message set
// err message set
Py_
X
DECREF(cnda_
%(res)
s);
Py_DECREF(cnda_
%(res)
s);
cnda_
%(res)
s = NULL;
cnda_
%(res)
s = NULL;
%(fail)
s;
%(fail)
s;
}
}
...
@@ -508,13 +508,13 @@ class GpuDimShuffle(Op):
...
@@ -508,13 +508,13 @@ class GpuDimShuffle(Op):
if (CudaNdarray_copy_structure_to_device(cnda_
%(res)
s))
if (CudaNdarray_copy_structure_to_device(cnda_
%(res)
s))
{
{
//err msg set
//err msg set
Py_
X
DECREF(cnda_
%(res)
s);
Py_DECREF(cnda_
%(res)
s);
cnda_
%(res)
s = NULL;
cnda_
%(res)
s = NULL;
%(fail)
s;
%(fail)
s;
}
}
"""
%
locals
()
"""
%
locals
()
if
1
:
if
0
:
print
'--------------------------------------'
print
'--------------------------------------'
print
'C_CODE'
print
'C_CODE'
print
''
print
''
...
...
tests/test_basic_ops.py
浏览文件 @
43f97ea3
import
sys
import
sys
,
time
from
theano.compile.sandbox.sharedvalue
import
shared
from
theano.compile.sandbox.sharedvalue
import
shared
from
theano.compile.sandbox.pfunc
import
pfunc
from
theano.compile.sandbox.pfunc
import
pfunc
from
theano
import
tensor
from
theano
import
tensor
...
@@ -18,8 +18,9 @@ def test_elemwise0():
...
@@ -18,8 +18,9 @@ def test_elemwise0():
a0
=
a
.
value
*
1.0
a0
=
a
.
value
*
1.0
print
'BEFORE ADD'
,
a
.
value
print
'BEFORE ADD'
,
a
.
value
for
i
,
node
in
enumerate
(
f
.
maker
.
env
.
toposort
()):
print
i
,
node
f
(
numpy
.
ones
((
4
,
4
)))
f
(
numpy
.
ones
((
4
,
4
)))
print
f
.
maker
.
env
.
toposort
()
print
'AFTER ADD'
,
a
.
value
print
'AFTER ADD'
,
a
.
value
assert
numpy
.
all
(
a0
+
1.0
==
a
.
value
)
assert
numpy
.
all
(
a0
+
1.0
==
a
.
value
)
...
@@ -52,9 +53,24 @@ def test_elemwise1():
...
@@ -52,9 +53,24 @@ def test_elemwise1():
def
test_elemwise2
():
def
test_elemwise2
():
""" Several kinds of elemwise expressions with dimension permutations """
""" Several kinds of elemwise expressions with dimension permutations """
rng
=
numpy
.
random
.
RandomState
(
int
(
time
.
time
()))
print
'random?'
,
rng
.
rand
(
3
)
shape
=
(
3
,
5
)
for
pattern
in
[(
0
,
1
),
(
1
,
0
)]:
a
=
tcn
.
shared_constructor
(
rng
.
rand
(
*
shape
),
name
=
None
)
b
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
[
0
]
*
len
(
shape
))()
f
=
pfunc
([
b
],
[],
updates
=
[(
a
,
(
a
+
b
)
.
dimshuffle
(
pattern
))])
has_elemwise
=
False
for
i
,
node
in
enumerate
(
f
.
maker
.
env
.
toposort
()):
print
>>
sys
.
stderr
,
i
,
node
has_elemwise
=
has_elemwise
or
isinstance
(
node
.
op
,
tensor
.
Elemwise
)
assert
not
has_elemwise
#let debugmode catch errors
print
>>
sys
.
stderr
,
'pattern'
,
pattern
f
(
rng
.
rand
(
*
shape
)
*.
3
)
shape
=
(
3
,
4
,
5
,
6
)
shape
=
(
3
,
4
,
5
,
6
)
a
=
tcn
.
shared_constructor
(
numpy
.
random
.
rand
(
*
shape
),
'a'
)
a
=
tcn
.
shared_constructor
(
rng
.
rand
(
*
shape
),
'a'
)
b
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
[
0
]
*
len
(
shape
))()
b
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
[
0
]
*
len
(
shape
))()
f
=
pfunc
([
b
],
[],
updates
=
[(
a
,
(
a
+
b
)
.
dimshuffle
([
2
,
0
,
3
,
1
])
*
f
=
pfunc
([
b
],
[],
updates
=
[(
a
,
(
a
+
b
)
.
dimshuffle
([
2
,
0
,
3
,
1
])
*
tensor
.
exp
(
b
**
a
)
.
dimshuffle
([
2
,
0
,
3
,
1
]))])
tensor
.
exp
(
b
**
a
)
.
dimshuffle
([
2
,
0
,
3
,
1
]))])
...
@@ -64,7 +80,7 @@ def test_elemwise2():
...
@@ -64,7 +80,7 @@ def test_elemwise2():
has_elemwise
=
has_elemwise
or
isinstance
(
node
.
op
,
tensor
.
Elemwise
)
has_elemwise
=
has_elemwise
or
isinstance
(
node
.
op
,
tensor
.
Elemwise
)
assert
not
has_elemwise
assert
not
has_elemwise
#let debugmode catch errors
#let debugmode catch errors
f
(
numpy
.
ones
(
shape
))
f
(
rng
.
rand
(
*
shape
))
def
test_elemwise3
():
def
test_elemwise3
():
""" Several kinds of elemwise expressions with dimension permutations and broadcasting"""
""" Several kinds of elemwise expressions with dimension permutations and broadcasting"""
...
@@ -75,5 +91,5 @@ def test_elemwise3():
...
@@ -75,5 +91,5 @@ def test_elemwise3():
f
=
pfunc
([
b
],
[],
updates
=
[(
a
,
(
a
+
b
)
.
dimshuffle
([
2
,
0
,
3
,
1
])
*
tensor
.
exp
(
1
+
f
=
pfunc
([
b
],
[],
updates
=
[(
a
,
(
a
+
b
)
.
dimshuffle
([
2
,
0
,
3
,
1
])
*
tensor
.
exp
(
1
+
b
**
a
)
.
dimshuffle
([
2
,
0
,
3
,
1
]))])
b
**
a
)
.
dimshuffle
([
2
,
0
,
3
,
1
]))])
#let debugmode catch errors
#let debugmode catch errors
f
(
numpy
.
ones
(
6
))
f
(
numpy
.
random
.
rand
(
6
))
type.py
浏览文件 @
43f97ea3
...
@@ -50,6 +50,10 @@ class CudaNdarrayType(Type):
...
@@ -50,6 +50,10 @@ class CudaNdarrayType(Type):
def
filter
(
self
,
data
,
strict
=
False
):
def
filter
(
self
,
data
,
strict
=
False
):
return
type_support_filter
(
data
,
self
.
broadcastable
,
strict
)
return
type_support_filter
(
data
,
self
.
broadcastable
,
strict
)
@staticmethod
def
values_eq_approx
(
a
,
b
):
return
tensor
.
TensorType
.
values_eq_approx
(
numpy
.
asarray
(
a
),
numpy
.
asarray
(
b
))
def
dtype_specs
(
self
):
def
dtype_specs
(
self
):
"""Return a tuple (python type, c type, numpy typenum) that corresponds to
"""Return a tuple (python type, c type, numpy typenum) that corresponds to
self.dtype.
self.dtype.
...
@@ -130,6 +134,7 @@ class CudaNdarrayType(Type):
...
@@ -130,6 +134,7 @@ class CudaNdarrayType(Type):
if (CudaNdarray_Check(py_
%(name)
s))
if (CudaNdarray_Check(py_
%(name)
s))
{
{
cnda_
%(name)
s = (CudaNdarray*)py_
%(name)
s;
cnda_
%(name)
s = (CudaNdarray*)py_
%(name)
s;
Py_INCREF(py_
%(name)
s);
}
}
else
else
{
{
...
@@ -141,22 +146,29 @@ class CudaNdarrayType(Type):
...
@@ -141,22 +146,29 @@ class CudaNdarrayType(Type):
def
c_cleanup
(
self
,
name
,
sub
):
def
c_cleanup
(
self
,
name
,
sub
):
return
"""
return
"""
std::cerr << "cleanup " << py_
%(name)
s << "
\\
n";
//std::cerr << "cleanup " << py_
%(name)
s << "
\\
n";
Py_XDECREF(py_
%(name)
s);
"""
%
locals
()
"""
%
locals
()
def
c_sync
(
self
,
name
,
sub
):
def
c_sync
(
self
,
name
,
sub
):
"""Override `CLinkerOp.c_sync` """
"""Override `CLinkerOp.c_sync` """
return
"""
return
"""
std::cerr << "sync
\\
n";
//
std::cerr << "sync
\\
n";
if (NULL == cnda_
%(name)
s) {
if (NULL == cnda_
%(name)
s) {
// failure: sync None to storage
// failure: sync None to storage
Py_XDECREF(py_
%(name)
s);
Py_XDECREF(py_
%(name)
s);
py_
%(name)
s = Py_None;
py_
%(name)
s = Py_None;
Py_
X
INCREF(py_
%(name)
s);
Py_INCREF(py_
%(name)
s);
}
}
else
else
{
{
py_
%(name)
s = (PyObject*)cnda_
%(name)
s;
if (py_
%(name)
s != (PyObject*)cnda_
%(name)
s)
{
Py_XDECREF(py_
%(name)
s);
py_
%(name)
s = (PyObject*)cnda_
%(name)
s;
Py_INCREF(py_
%(name)
s);
}
assert(py_
%(name)
s->ob_refcnt);
}
}
"""
%
locals
()
"""
%
locals
()
...
...
type_support.cu
浏览文件 @
43f97ea3
...
@@ -4,10 +4,8 @@
...
@@ -4,10 +4,8 @@
#include "cuda_ndarray.cuh"
#include "cuda_ndarray.cuh"
#define DECL(s) static PyObject * s(PyObject * self, PyObject *args)
static PyObject *
static PyObject *
filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
filter(PyObject*
__unsed_
self, PyObject *args) // args = (data, broadcastable, strict)
{
{
PyObject *py_data=NULL;
PyObject *py_data=NULL;
PyArrayObject * data = NULL;
PyArrayObject * data = NULL;
...
@@ -20,8 +18,8 @@ filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
...
@@ -20,8 +18,8 @@ filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
PyErr_SetString(PyExc_TypeError, "broadcastable arg should be a tuple of int.");
PyErr_SetString(PyExc_TypeError, "broadcastable arg should be a tuple of int.");
return NULL;
return NULL;
}
}
Py_
X
INCREF(py_data);
Py_INCREF(py_data);
Py_
X
INCREF(broadcastable);
Py_INCREF(broadcastable);
CudaNdarray * cnda = (CudaNdarray*)py_data;
CudaNdarray * cnda = (CudaNdarray*)py_data;
...
@@ -30,15 +28,17 @@ filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
...
@@ -30,15 +28,17 @@ filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
//TODO: support non-strict "casting" from a vt to the broadcastable/type/size that we need.
//TODO: support non-strict "casting" from a vt to the broadcastable/type/size that we need.
if (!CudaNdarray_Check(py_data))
if (!CudaNdarray_Check(py_data))
{
{
Py_XDECREF(py_data);
Py_DECREF(py_data);
Py_XDECREF(broadcastable);
Py_DECREF(broadcastable);
std::cerr << "strict mode requires CudaNdarray\n";
PyErr_SetString(PyExc_TypeError, "strict mode requires CudaNdarray");
PyErr_SetString(PyExc_TypeError, "strict mode requires CudaNdarray");
return NULL;
return NULL;
}
}
if (cnda->nd != PyTuple_Size(broadcastable))
if (cnda->nd != PyTuple_Size(broadcastable))
{
{
Py_XDECREF(py_data);
Py_DECREF(py_data);
Py_XDECREF(broadcastable);
Py_DECREF(broadcastable);
std::cerr << "Wrong rank: "<< cnda->nd << " " << PyTuple_Size(broadcastable) << "\n";
PyErr_Format(PyExc_TypeError, "Wrong rank: %i vs %li", cnda->nd, (long)PyTuple_Size(broadcastable));
PyErr_Format(PyExc_TypeError, "Wrong rank: %i vs %li", cnda->nd, (long)PyTuple_Size(broadcastable));
return NULL;
return NULL;
}
}
...
@@ -46,13 +46,14 @@ filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
...
@@ -46,13 +46,14 @@ filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
{
{
if ((cnda->dim[i] > 1) and PyInt_AsLong(PyTuple_GetItem(broadcastable, Py_ssize_t(i))))
if ((cnda->dim[i] > 1) and PyInt_AsLong(PyTuple_GetItem(broadcastable, Py_ssize_t(i))))
{
{
std::cerr << "Non-unit size in bcastable dim:\n";
PyErr_Format(PyExc_TypeError, "Non-unit size in broadcastable vt dimension %i", i);
PyErr_Format(PyExc_TypeError, "Non-unit size in broadcastable vt dimension %i", i);
Py_
X
DECREF(py_data);
Py_DECREF(py_data);
Py_
X
DECREF(broadcastable);
Py_DECREF(broadcastable);
return NULL;
return NULL;
}
}
}
}
Py_
X
DECREF(broadcastable);
Py_DECREF(broadcastable);
return py_data;
return py_data;
}
}
else
else
...
@@ -61,8 +62,8 @@ filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
...
@@ -61,8 +62,8 @@ filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
if (!data)
if (!data)
{
{
//err message already defined
//err message already defined
Py_
X
DECREF(py_data);
Py_DECREF(py_data);
Py_
X
DECREF(broadcastable);
Py_DECREF(broadcastable);
return NULL;
return NULL;
}
}
for (int i = 0; i < data->nd; ++i)
for (int i = 0; i < data->nd; ++i)
...
@@ -70,22 +71,21 @@ filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
...
@@ -70,22 +71,21 @@ filter(PyObject* self, PyObject *args) // args = (data, broadcastable, strict)
if ((data->dimensions[i] > 1) and PyInt_AsLong(PyTuple_GetItem(broadcastable, Py_ssize_t(i))))
if ((data->dimensions[i] > 1) and PyInt_AsLong(PyTuple_GetItem(broadcastable, Py_ssize_t(i))))
{
{
PyErr_Format(PyExc_TypeError, "Non-unit size in broadcastable dimension %i", i);
PyErr_Format(PyExc_TypeError, "Non-unit size in broadcastable dimension %i", i);
Py_
X
DECREF(data);
Py_DECREF(data);
Py_
X
DECREF(py_data);
Py_DECREF(py_data);
Py_
X
DECREF(broadcastable);
Py_DECREF(broadcastable);
return NULL;
return NULL;
}
}
}
}
CudaNdarray * rval = (CudaNdarray*) CudaNdarray_new_null();
CudaNdarray * rval = (CudaNdarray*) CudaNdarray_new_null();
if (CudaNdarray_CopyFromArray(rval, data))
if (CudaNdarray_CopyFromArray(rval, data))
{
{
Py_
X
DECREF(rval);
Py_DECREF(rval);
rval = NULL;
rval = NULL;
}
}
Py_
X
DECREF(data);
Py_DECREF(data);
Py_
X
DECREF(py_data);
Py_DECREF(py_data);
Py_
X
DECREF(broadcastable);
Py_DECREF(broadcastable);
return (PyObject*)rval;
return (PyObject*)rval;
}
}
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论