Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b9b760e8
提交
b9b760e8
authored
2月 09, 2012
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make GpuGemm work on all kinds of memory layouts
It should now support matrices with negative strides, and non-unit strides in general, by making a copy in the appropriate cases.
上级
6c6b9e7a
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
123 行增加
和
55 行删除
+123
-55
blas.py
theano/sandbox/cuda/blas.py
+38
-26
cuda_ndarray.cu
theano/sandbox/cuda/cuda_ndarray.cu
+85
-29
没有找到文件。
theano/sandbox/cuda/blas.py
浏览文件 @
b9b760e8
...
@@ -191,7 +191,7 @@ class GpuGemm(Op):
...
@@ -191,7 +191,7 @@ class GpuGemm(Op):
return
Apply
(
self
,
[
z
,
a
,
x
,
y
,
b
],
[
z
.
type
()])
return
Apply
(
self
,
[
z
,
a
,
x
,
y
,
b
],
[
z
.
type
()])
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
3
,)
return
(
4
,)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
#z_out = alpha * dot(x,y) + beta * z_in
#z_out = alpha * dot(x,y) + beta * z_in
...
@@ -199,6 +199,7 @@ class GpuGemm(Op):
...
@@ -199,6 +199,7 @@ class GpuGemm(Op):
#not inplace version, we copy z_in to z_out.
#not inplace version, we copy z_in to z_out.
z_in
,
a
,
x
,
y
,
b
=
inputs
z_in
,
a
,
x
,
y
,
b
=
inputs
z_out
,
=
outputs
z_out
,
=
outputs
inplace
=
int
(
self
.
inplace
)
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
sio
=
StringIO
.
StringIO
()
sio
=
StringIO
.
StringIO
()
...
@@ -214,39 +215,50 @@ class GpuGemm(Op):
...
@@ -214,39 +215,50 @@ class GpuGemm(Op):
: (REAL)(((double*)
%(b)
s->data)[0]);
: (REAL)(((double*)
%(b)
s->data)[0]);
#undef REAL
#undef REAL
"""
if (
%(inplace)
s
if
self
.
inplace
:
&& (CudaNdarray_HOST_STRIDES(
%(z_in)
s)[0] >= 0)
print
>>
sio
,
"""
&& (CudaNdarray_HOST_STRIDES(
%(z_in)
s)[1] >= 0)
&& ((CudaNdarray_HOST_DIMS(
%(z_in)
s)[0] <= 1)
|| (CudaNdarray_HOST_STRIDES(
%(z_in)
s)[0] == 1)
|| (CudaNdarray_HOST_DIMS(
%(z_in)
s)[1] <= 1)
|| (CudaNdarray_HOST_STRIDES(
%(z_in)
s)[1] == 1)))
{
// The input has an appropriate layout, we work inplace
Py_XDECREF(
%(z_out)
s);
Py_XDECREF(
%(z_out)
s);
%(z_out)
s =
%(z_in)
s;
%(z_out)
s =
%(z_in)
s;
Py_INCREF(
%(z_out)
s);
Py_INCREF(
%(z_out)
s);
"""
}
else
:
else if (
%(z_out)
s
print
>>
sio
,
"""
&& (
%(z_out)
s->nd == 2)
if (!
%(z_out)
s
&& (CudaNdarray_HOST_DIMS(
%(z_out)
s)[0]
|| (
%(z_out)
s->nd != 2)
== CudaNdarray_HOST_DIMS(
%(z_in)
s)[0])
|| (CudaNdarray_HOST_DIMS(
%(z_out)
s)[0] != CudaNdarray_HOST_DIMS(
%(z_in)
s)[0])
&& (CudaNdarray_HOST_DIMS(
%(z_out)
s)[1]
|| (CudaNdarray_HOST_DIMS(
%(z_out)
s)[1] != CudaNdarray_HOST_DIMS(
%(z_in)
s)[1])
== CudaNdarray_HOST_DIMS(
%(z_in)
s)[1])
)
&& (CudaNdarray_HOST_STRIDES(
%(z_out)
s)[0] >= 0)
&& (CudaNdarray_HOST_STRIDES(
%(z_out)
s)[1] >= 0)
&& ((CudaNdarray_HOST_DIMS(
%(z_out)
s)[0] <= 1)
|| (CudaNdarray_HOST_STRIDES(
%(z_out)
s)[0] == 1)
|| (CudaNdarray_HOST_DIMS(
%(z_out)
s)[1] <= 1)
|| (CudaNdarray_HOST_STRIDES(
%(z_out)
s)[1] == 1)))
{
// The existing output has an appropriate layout,
// copy the input data into it, then work inplace
if (CudaNdarray_CopyFromCudaNdarray(
%(z_out)
s,
%(z_in)
s))
{
{
Py_XDECREF(
%(z_out)
s);
%(fail)
s;
%(z_out)
s = (CudaNdarray*)CudaNdarray_Copy(
%(z_in)
s);
if (!
%(z_out)
s)
{
%(fail)
s;
}
}
}
else
}
else
{
// Copy the input, use the copy as output
Py_XDECREF(
%(z_out)
s);
%(z_out)
s = (CudaNdarray*)CudaNdarray_Copy(
%(z_in)
s);
if (!
%(z_out)
s)
{
{
if (CudaNdarray_CopyFromCudaNdarray(
%(z_out)
s,
%(z_in)
s))
%(fail)
s;
{
%(fail)
s;
}
}
}
"""
}
print
>>
sio
,
"""
if (CudaNdarray_gemm(
%(name)
s_a,
%(x)
s,
%(y)
s,
%(name)
s_b,
%(z_out)
s))
if (CudaNdarray_gemm(
%(name)
s_a,
%(x)
s,
%(y)
s,
%(name)
s_b,
%(z_out)
s))
{
{
%(fail)
s;
%(fail)
s;
...
...
theano/sandbox/cuda/cuda_ndarray.cu
浏览文件 @
b9b760e8
...
@@ -2882,9 +2882,21 @@ int CudaNdarray_CopyFromCudaNdarray(CudaNdarray * self, const CudaNdarray * othe
...
@@ -2882,9 +2882,21 @@ int CudaNdarray_CopyFromCudaNdarray(CudaNdarray * self, const CudaNdarray * othe
int
CudaNdarray_gemm
(
float
alpha
,
const
CudaNdarray
*
A
,
const
CudaNdarray
*
B
,
float
beta
,
CudaNdarray
*
C
)
int
CudaNdarray_gemm
(
float
alpha
,
const
CudaNdarray
*
A
,
const
CudaNdarray
*
B
,
float
beta
,
CudaNdarray
*
C
)
{
{
if
(
A
->
nd
!=
2
)
{
PyErr_SetString
(
PyExc_ValueError
,
"non-matrix arg to gemm"
);
return
-
1
;
}
if
(
A
->
nd
!=
2
)
if
(
B
->
nd
!=
2
)
{
PyErr_SetString
(
PyExc_ValueError
,
"non-matrix arg to gemm"
);
return
-
1
;
}
{
if
(
C
->
nd
!=
2
)
{
PyErr_SetString
(
PyExc_ValueError
,
"non-matrix arg to gemm"
);
return
-
1
;
}
PyErr_SetString
(
PyExc_ValueError
,
"non-matrix arg A_ to gemm"
);
return
-
1
;
}
if
(
B
->
nd
!=
2
)
{
PyErr_SetString
(
PyExc_ValueError
,
"non-matrix arg B_ to gemm"
);
return
-
1
;
}
if
(
C
->
nd
!=
2
)
{
PyErr_SetString
(
PyExc_ValueError
,
"non-matrix arg C to gemm"
);
return
-
1
;
}
// We must allow dimensions to be zeros.
// We must allow dimensions to be zeros.
if
((
CudaNdarray_HOST_DIMS
(
A
)[
1
]
!=
CudaNdarray_HOST_DIMS
(
B
)[
0
])
if
((
CudaNdarray_HOST_DIMS
(
A
)[
1
]
!=
CudaNdarray_HOST_DIMS
(
B
)[
0
])
...
@@ -2901,14 +2913,72 @@ int CudaNdarray_gemm(float alpha, const CudaNdarray * A, const CudaNdarray * B,
...
@@ -2901,14 +2913,72 @@ int CudaNdarray_gemm(float alpha, const CudaNdarray * A, const CudaNdarray * B,
return
-
1
;
return
-
1
;
}
}
// a matrix has non-unit size and non-unit stride in both directions, we can't operate in-place
// If matrix A_ or B_ has non-unit size and non-unit stride in both
// TODO: make a copy instead of returning in error
// dimensions, we can make a copy.
if
(((
CudaNdarray_HOST_DIMS
(
A
)[
0
]
>
1
)
&&
(
CudaNdarray_HOST_STRIDES
(
A
)[
0
]
!=
1
))
&&
((
CudaNdarray_HOST_DIMS
(
A
)[
1
]
>
1
)
&&
(
CudaNdarray_HOST_STRIDES
(
A
)[
1
]
!=
1
)))
if
(((
CudaNdarray_HOST_DIMS
(
A
)[
0
]
>
1
)
{
PyErr_SetString
(
PyExc_NotImplementedError
,
"non-unit stride in gemm arg"
);
return
-
1
;
}
&&
(
CudaNdarray_HOST_STRIDES
(
A
)[
0
]
!=
1
)
if
(((
CudaNdarray_HOST_DIMS
(
B
)[
0
]
>
1
)
&&
(
CudaNdarray_HOST_STRIDES
(
B
)[
0
]
!=
1
))
&&
((
CudaNdarray_HOST_DIMS
(
B
)[
1
]
>
1
)
&&
(
CudaNdarray_HOST_STRIDES
(
B
)[
1
]
!=
1
)))
&&
(
CudaNdarray_HOST_DIMS
(
A
)[
1
]
>
1
)
{
PyErr_SetString
(
PyExc_NotImplementedError
,
"non-unit stride in gemm arg"
);
return
-
1
;
}
&&
(
CudaNdarray_HOST_STRIDES
(
A
)[
1
]
!=
1
))
if
(((
CudaNdarray_HOST_DIMS
(
C
)[
0
]
>
1
)
&&
(
CudaNdarray_HOST_STRIDES
(
C
)[
0
]
!=
1
))
&&
((
CudaNdarray_HOST_DIMS
(
C
)[
1
]
>
1
)
&&
(
CudaNdarray_HOST_STRIDES
(
C
)[
1
]
!=
1
)))
||
(
CudaNdarray_HOST_STRIDES
(
A
)[
0
]
<
0
)
{
PyErr_SetString
(
PyExc_NotImplementedError
,
"non-unit stride in gemm arg"
);
return
-
1
;
}
||
(
CudaNdarray_HOST_STRIDES
(
A
)[
1
]
<
0
))
{
const
CudaNdarray
*
A_new
=
(
CudaNdarray
*
)
CudaNdarray_Copy
(
A
);
if
(
!
A_new
)
return
-
1
;
A
=
A_new
;
}
else
{
// In the case above, we will need to decref A_new at the end.
// To make things simpler, we incref A here, so we can always
// decref A.
Py_INCREF
(
A
);
}
if
(((
CudaNdarray_HOST_DIMS
(
B
)[
0
]
>
1
)
&&
(
CudaNdarray_HOST_STRIDES
(
B
)[
0
]
!=
1
)
&&
(
CudaNdarray_HOST_DIMS
(
B
)[
1
]
>
1
)
&&
(
CudaNdarray_HOST_STRIDES
(
B
)[
1
]
!=
1
))
||
(
CudaNdarray_HOST_STRIDES
(
B
)[
0
]
<
0
)
||
(
CudaNdarray_HOST_STRIDES
(
B
)[
1
]
<
0
))
{
const
CudaNdarray
*
B_new
=
(
CudaNdarray
*
)
CudaNdarray_Copy
(
B
);
if
(
!
B_new
)
{
Py_XDECREF
(
A
);
return
-
1
;
}
B
=
B_new
;
}
else
{
// In the case above, we will need to decref B_new at the end.
// To make things simpler, we incref B here, so we can always
// decref B.
Py_INCREF
(
B
);
}
// If matrix C has non-unit size and non-unit stride in both
// dimensions, or negative strides, we can't operate. We cannot copy
// C either, because the calling code will expect the result to be
// in the original C container.
if
(((
CudaNdarray_HOST_DIMS
(
C
)[
0
]
>
1
)
&&
(
CudaNdarray_HOST_STRIDES
(
C
)[
0
]
!=
1
)
&&
(
CudaNdarray_HOST_DIMS
(
C
)[
1
]
>
1
)
&&
(
CudaNdarray_HOST_STRIDES
(
C
)[
1
]
!=
1
))
||
(
CudaNdarray_HOST_STRIDES
(
C
)[
0
]
<
0
)
||
(
CudaNdarray_HOST_STRIDES
(
C
)[
1
]
<
0
))
{
PyErr_Format
(
PyExc_AssertionError
,
"non-unit or negative stride in gemm arg C (%i,%i) of shape (%i,%i)"
,
CudaNdarray_HOST_STRIDES
(
C
)[
0
],
CudaNdarray_HOST_STRIDES
(
C
)[
1
],
CudaNdarray_HOST_DIMS
(
C
)[
0
],
CudaNdarray_HOST_DIMS
(
C
)[
1
]);
Py_XDECREF
(
A
);
Py_XDECREF
(
B
);
return
-
1
;
}
// the unit integer is divided logically into three fields of 4 bits
// the unit integer is divided logically into three fields of 4 bits
// the lowermost 4 bits encode the stride pattern of the output
// the lowermost 4 bits encode the stride pattern of the output
...
@@ -2943,24 +3013,6 @@ int CudaNdarray_gemm(float alpha, const CudaNdarray * A, const CudaNdarray * B,
...
@@ -2943,24 +3013,6 @@ int CudaNdarray_gemm(float alpha, const CudaNdarray * A, const CudaNdarray * B,
unit
|=
(
0x2
<<
0
);
unit
|=
(
0x2
<<
0
);
}
}
// I don't know if cudablas handles negative strides
if
(
(
CudaNdarray_HOST_STRIDES
(
A
)[
0
]
<
0
)
||
(
CudaNdarray_HOST_STRIDES
(
A
)[
1
]
<
0
)
||
(
CudaNdarray_HOST_STRIDES
(
B
)[
0
]
<
0
)
||
(
CudaNdarray_HOST_STRIDES
(
B
)[
1
]
<
0
)
||
(
CudaNdarray_HOST_STRIDES
(
C
)[
0
]
<
0
)
||
(
CudaNdarray_HOST_STRIDES
(
C
)[
1
]
<
0
))
{
PyErr_Format
(
PyExc_ValueError
,
"illegal strides in args to gemm (%i,%i)x(%i,%i)->(%i,%i)"
,
CudaNdarray_HOST_STRIDES
(
A
)[
0
],
CudaNdarray_HOST_STRIDES
(
A
)[
1
],
CudaNdarray_HOST_STRIDES
(
B
)[
0
],
CudaNdarray_HOST_STRIDES
(
B
)[
1
],
CudaNdarray_HOST_STRIDES
(
C
)[
0
],
CudaNdarray_HOST_STRIDES
(
C
)[
1
]);
return
-
1
;
}
/* create appropriate strides for malformed matrices that are row or column
/* create appropriate strides for malformed matrices that are row or column
* vectors
* vectors
*/
*/
...
@@ -2987,6 +3039,8 @@ int CudaNdarray_gemm(float alpha, const CudaNdarray * A, const CudaNdarray * B,
...
@@ -2987,6 +3039,8 @@ int CudaNdarray_gemm(float alpha, const CudaNdarray * A, const CudaNdarray * B,
cublasSgemm(T0, T1, D0, D1, D2, a, x, sx, y, sy, b, z, sz); \
cublasSgemm(T0, T1, D0, D1, D2, a, x, sx, y, sy, b, z, sz); \
} else { \
} else { \
PyErr_SetString(PyExc_NotImplementedError, "negative stride to sGemm");\
PyErr_SetString(PyExc_NotImplementedError, "negative stride to sGemm");\
Py_XDECREF(A);\
Py_XDECREF(B);\
return -1; \
return -1; \
}
}
...
@@ -3004,6 +3058,8 @@ int CudaNdarray_gemm(float alpha, const CudaNdarray * A, const CudaNdarray * B,
...
@@ -3004,6 +3058,8 @@ int CudaNdarray_gemm(float alpha, const CudaNdarray * A, const CudaNdarray * B,
return
-
1
;
return
-
1
;
};
};
CNDA_THREAD_SYNC
;
CNDA_THREAD_SYNC
;
Py_XDECREF
(
A
);
Py_XDECREF
(
B
);
cudaError_t
err
=
cudaGetLastError
();
cudaError_t
err
=
cudaGetLastError
();
if
(
CUBLAS_STATUS_SUCCESS
!=
err
)
if
(
CUBLAS_STATUS_SUCCESS
!=
err
)
{
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论