Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
39d39068
提交
39d39068
authored
3月 22, 2011
作者:
Ian Goodfellow
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added python c api version of cuda nd array dimshuffle
added python binding for cuda nd array dimshuffle added test of the above used the above to cover all four tranpose cases of test_cuda_ndarray.py:test_dot
上级
9415e78e
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
158 行增加
和
6 行删除
+158
-6
cuda_ndarray.cu
theano/sandbox/cuda/cuda_ndarray.cu
+118
-1
test_cuda_ndarray.py
theano/sandbox/cuda/tests/test_cuda_ndarray.py
+39
-4
test_conv3d.py
theano/tensor/nnet/tests/test_conv3d.py
+1
-1
没有找到文件。
theano/sandbox/cuda/cuda_ndarray.cu
浏览文件 @
39d39068
...
...
@@ -18,6 +18,10 @@
static int g_gpu_context_active = 0;
PyObject *
CudaNdarray_Dimshuffle(PyObject* _unused, PyObject* args);
/**
*
* In the test program I'm using, the _outstanding_mallocs decreases with every call.
...
...
@@ -332,7 +336,10 @@ PyObject * CudaNdarray_CreateArrayObj(CudaNdarray * self)
return rval;
}
// TODO-- we have two functions here, ZEROS and Zeros.
// ZEROS is meant to be called just from C code (you don't need to pass it PyObject * s)
// but this naming is very weird, makes it look like a macro
// we should figure out the correct convention and change to that
PyObject* CudaNdarray_ZEROS(int n, int * dims)
{
...
...
@@ -2010,6 +2017,7 @@ CudaNdarray_Dot(PyObject* _unused, PyObject* args)
PyObject *r=NULL;
PyObject * rval = NULL;
//args should consist of two python objects ("OO")
if (! PyArg_ParseTuple(args, "OO", &l, &r))
return NULL;
...
...
@@ -2155,7 +2163,10 @@ filter(PyObject* __unsed_self, PyObject *args) // args = (data, broadcastable, s
}
}
//TODO-- CudaNdarray_Dot and CudaNdarray_active_device_name are following different capitalization conventions.
// Pick one and standardize it, this file is already annoying enough to grep through
static PyMethodDef module_methods[] = {
{"dimshuffle", CudaNdarray_Dimshuffle, METH_VARARGS, "Returns the dimshuffle of a CudaNdarray."},
{"dot", CudaNdarray_Dot, METH_VARARGS, "Returns the matrix product of two CudaNdarray arguments."},
{"gpu_init", CudaNdarray_gpu_init, METH_VARARGS, "Select the gpu card to use; also usable to test whether CUDA is available."},
{"active_device_name", CudaNdarray_active_device_name, METH_VARARGS, "Get the name of the active device."},
...
...
@@ -2936,6 +2947,7 @@ CudaNdarray_reduce_max(CudaNdarray * self, const CudaNdarray * A)
return -1;
}
/**
*
* pattern is a permutation of [0, 1, ... self->nd-1] with the following twists:
...
...
@@ -3001,3 +3013,108 @@ CudaNdarray_dimshuffle(CudaNdarray * self, unsigned int len, const int * pattern
return 0;
}
PyObject *
CudaNdarray_Dimshuffle(PyObject* _unused, PyObject* args)
{
PyObject * self = NULL;
PyObject * pattern_object = NULL;
int * pattern = NULL;
PyObject * rval = NULL;
int success = -1;
//const int * dims = NULL;
//args should consist of two python objects ("OO")
if (! PyArg_ParseTuple(args, "OO", &self, &pattern_object))
return NULL;
if (!CudaNdarray_Check(self) )
{
PyErr_SetString(PyExc_TypeError, "First argument to cuda_ndarray.dimshuffle must be a CudaNdarray");
return NULL;
}
//parse pattern_object into int * pattern
Py_ssize_t pattern_dim = PyObject_Length(pattern_object);
if (pattern_dim < 0)
{
PyErr_SetString(PyExc_TypeError, "Couldn't get length of third argument to cuda_ndarray.dimshuffle");
return NULL;
}
pattern = (int *) malloc( pattern_dim * sizeof(int));
for (Py_ssize_t i = 0; i < pattern_dim; i++)
{
PyObject * idx = PyLong_FromLong(i);
if (idx == NULL)
{
PyErr_SetString(PyExc_Exception, "Couldn't make long object to loop over list/tuple");
goto CudaNdarray_dimshuffle_fail;
}
long elem_value = 0;
PyObject * elem = PyObject_GetItem(pattern_object, idx);
if (elem == NULL)
{
Py_XDECREF( elem);
PyErr_SetString(PyExc_ValueError, "Third argument to dimshuffle must be list or tuple of integers");
goto CudaNdarray_dimshuffle_fail;
}
elem_value = PyInt_AsLong(elem);
if (elem_value == -1 && PyErr_Occurred() )
{
Py_XDECREF(elem);
PyErr_SetString(PyExc_ValueError, "Third argument to dimshuffle must be list or tuple of integers");
goto CudaNdarray_dimshuffle_fail;
}
pattern[i] = elem_value;
Py_XDECREF( elem );
Py_XDECREF( idx );
}
//allocate rval
rval = (PyObject *) CudaNdarray_View((CudaNdarray *) self);
if (rval == NULL)
{
//CudaNdarray_New should have set the exception string
goto CudaNdarray_dimshuffle_fail;
}
//printf("pattern_dim: %d\n",pattern_dim);
//printf("pattern: %d %d\n",pattern[0],pattern[1]);
//dims = CudaNdarray_HOST_DIMS( (CudaNdarray *) self);
//printf("dims before: %d %d\n",dims[0],dims[1]);
success = CudaNdarray_dimshuffle((CudaNdarray *) rval, pattern_dim, pattern);
if (success != 0)
{
//Exception string should already be set by CudaNdarray_dimshuffle
goto CudaNdarray_dimshuffle_fail;
}
return rval;
CudaNdarray_dimshuffle_fail:
if (pattern != NULL)
free(pattern);
if (rval != NULL)
Py_XDECREF(rval);
return NULL;
}
theano/sandbox/cuda/tests/test_cuda_ndarray.py
浏览文件 @
39d39068
...
...
@@ -154,18 +154,53 @@ def test_nvcc_bug():
assert
numpy
.
allclose
(
a
+
a
,
numpy
.
asarray
(
c
))
assert
numpy
.
allclose
(
a
,
numpy
.
asarray
(
d
))
def
test_dimshuffle
():
utt
.
seed_rng
()
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
a
=
theano
.
_asarray
(
rng
.
randn
(
3
,
11
),
dtype
=
'float32'
)
b
=
cuda_ndarray
.
CudaNdarray
(
a
)
assert
numpy
.
allclose
(
numpy
.
transpose
(
a
),
cuda_ndarray
.
dimshuffle
(
b
,(
1
,
0
)))
def
test_dot
():
print
>>
sys
.
stdout
,
'starting test_dot'
a0
=
theano
.
_asarray
(
numpy
.
random
.
rand
(
4
,
7
),
dtype
=
'float32'
)
a1
=
theano
.
_asarray
(
numpy
.
random
.
rand
(
7
,
6
),
dtype
=
'float32'
)
utt
.
seed_rng
()
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
a0
=
theano
.
_asarray
(
rng
.
randn
(
4
,
7
),
dtype
=
'float32'
)
a1
=
theano
.
_asarray
(
rng
.
randn
(
7
,
6
),
dtype
=
'float32'
)
b0
=
cuda_ndarray
.
CudaNdarray
(
a0
)
b1
=
cuda_ndarray
.
CudaNdarray
(
a1
)
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
,
a1
),
cuda_ndarray
.
dot
(
b0
,
b1
))
##TODO: #617
#print >> sys.stderr, 'WARNING TODO test_dot: not testing all 8 transpose cases of dot'
a1
=
theano
.
_asarray
(
rng
.
randn
(
6
,
7
),
dtype
=
'float32'
)
b1
=
cuda_ndarray
.
CudaNdarray
(
a1
)
numpy_version
=
numpy
.
dot
(
a0
,
a1
.
T
)
transposed
=
cuda_ndarray
.
dimshuffle
(
b1
,(
1
,
0
))
cuda_version
=
cuda_ndarray
.
dot
(
b0
,
transposed
)
assert
numpy
.
allclose
(
numpy_version
,
cuda_version
)
a1
=
theano
.
_asarray
(
rng
.
randn
(
7
,
6
),
dtype
=
'float32'
)
b1
=
cuda_ndarray
.
CudaNdarray
(
a1
)
a0
=
theano
.
_asarray
(
rng
.
randn
(
7
,
4
),
dtype
=
'float32'
)
b0
=
cuda_ndarray
.
CudaNdarray
(
a0
)
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
.
T
,
a1
),
cuda_ndarray
.
dot
(
cuda_ndarray
.
dimshuffle
(
b0
,(
1
,
0
)),
b1
))
a1
=
theano
.
_asarray
(
rng
.
randn
(
6
,
7
),
dtype
=
'float32'
)
b1
=
cuda_ndarray
.
CudaNdarray
(
a1
)
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
.
T
,
a1
.
T
),
cuda_ndarray
.
dot
(
cuda_ndarray
.
dimshuffle
(
b0
,(
1
,
0
)),
cuda_ndarray
.
dimshuffle
(
b1
,(
1
,
0
)
)
)
)
def
test_sum
():
shape
=
(
2
,
3
)
...
...
theano/tensor/nnet/tests/test_conv3d.py
浏览文件 @
39d39068
...
...
@@ -410,7 +410,7 @@ class TestConv3D(unittest.TestCase):
W_mat_T
=
sparse
.
csr_matrix
(
W_mat
.
transpose
())
temp
=
W_mat_T
.
matmat
(
Hv_mat
)
temp
=
W_mat_T
*
Hv_mat
V_mat
=
(
temp
.
transpose
()
+
rbv
)
.
transpose
()
if
N
.
abs
(
V_mat
-
Vv_mat
)
.
max
()
>
1e-5
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论