Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5f1e372d
提交
5f1e372d
authored
4月 25, 2017
作者:
Alexander Matyasko
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix make node for gpu svd and add infer shape
上级
625e75cd
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
68 行增加
和
44 行删除
+68
-44
linalg.py
theano/gpuarray/linalg.py
+29
-10
magma_svd.c
theano/gpuarray/magma_svd.c
+37
-32
test_linalg.py
theano/gpuarray/tests/test_linalg.py
+2
-2
没有找到文件。
theano/gpuarray/linalg.py
浏览文件 @
5f1e372d
...
@@ -8,7 +8,7 @@ import numpy as np
...
@@ -8,7 +8,7 @@ import numpy as np
from
numpy.linalg.linalg
import
LinAlgError
from
numpy.linalg.linalg
import
LinAlgError
import
theano
import
theano
from
theano
import
Op
,
config
from
theano
import
Op
,
config
,
tensor
from
theano.gof
import
COp
from
theano.gof
import
COp
from
theano.gpuarray
import
GpuArrayType
from
theano.gpuarray
import
GpuArrayType
...
@@ -382,20 +382,39 @@ class GpuMagmaSVD(COp):
...
@@ -382,20 +382,39 @@ class GpuMagmaSVD(COp):
A
=
as_gpuarray_variable
(
A
,
ctx_name
)
A
=
as_gpuarray_variable
(
A
,
ctx_name
)
if
A
.
ndim
!=
2
:
if
A
.
ndim
!=
2
:
raise
LinAlgError
(
"Matrix rank error"
)
raise
LinAlgError
(
"Matrix rank error"
)
return
theano
.
Apply
(
self
,
[
A
],
if
self
.
compute_uv
:
[
A
.
type
(),
return
theano
.
Apply
(
self
,
[
A
],
GpuArrayType
(
A
.
dtype
,
broadcastable
=
[
False
],
[
A
.
type
(),
context_name
=
ctx_name
)(),
GpuArrayType
(
A
.
dtype
,
broadcastable
=
[
False
],
A
.
type
()])
context_name
=
ctx_name
)(),
A
.
type
()])
else
:
return
theano
.
Apply
(
self
,
[
A
],
[
GpuArrayType
(
A
.
dtype
,
broadcastable
=
[
False
],
context_name
=
ctx_name
)()])
def
get_params
(
self
,
node
):
def
get_params
(
self
,
node
):
return
node
.
inputs
[
0
]
.
type
.
context
return
node
.
inputs
[
0
]
.
type
.
context
def
get_op_params
(
self
):
def
get_op_params
(
self
):
compute_uv
=
int
(
self
.
compute_uv
)
params
=
[]
full_matrices
=
int
(
self
.
full_matrices
)
if
self
.
compute_uv
:
return
[(
'COMPUTE_UV'
,
compute_uv
),
params
.
append
((
'COMPUTE_UV'
,
'1'
))
(
'FULL_MATRICES'
,
full_matrices
)]
if
self
.
full_matrices
:
params
.
append
((
'FULL_MATRICES'
,
'1'
))
return
params
def
infer_shape
(
self
,
node
,
shapes
):
x_shape
,
=
shapes
M
,
N
=
x_shape
K
=
tensor
.
minimum
(
M
,
N
)
s_shape
=
(
K
,
)
if
self
.
compute_uv
:
u_shape
=
(
M
,
M
)
if
self
.
full_matrices
else
(
M
,
K
)
vt_shape
=
(
N
,
N
)
if
self
.
full_matrices
else
(
K
,
N
)
return
[
u_shape
,
s_shape
,
vt_shape
]
else
:
return
[
s_shape
]
def
gpu_svd
(
a
,
full_matrices
=
1
,
compute_uv
=
1
):
def
gpu_svd
(
a
,
full_matrices
=
1
,
compute_uv
=
1
):
...
...
theano/gpuarray/magma_svd.c
浏览文件 @
5f1e372d
...
@@ -4,8 +4,14 @@ setup_ext_cuda();
...
@@ -4,8 +4,14 @@ setup_ext_cuda();
#section support_code_struct
#section support_code_struct
int
APPLY_SPECIFIC
(
magma_svd
)(
PyGpuArrayObject
*
A
,
PyGpuArrayObject
**
U
,
int
APPLY_SPECIFIC
(
magma_svd
)(
PyGpuArrayObject
*
A
,
PyGpuArrayObject
**
S
,
PyGpuArrayObject
**
VT
,
#ifdef COMPUTE_UV
PyGpuArrayObject
**
U
,
#endif
PyGpuArrayObject
**
S
,
#ifdef COMPUTE_UV
PyGpuArrayObject
**
VT
,
#endif
PyGpuContextObject
*
c
)
{
PyGpuContextObject
*
c
)
{
magma_int_t
M
,
N
,
K
,
ldu
,
ldv
,
M_U
,
N_VT
,
info
;
magma_int_t
M
,
N
,
K
,
ldu
,
ldv
,
M_U
,
N_VT
,
info
;
magma_vec_t
jobu
,
jobv
;
magma_vec_t
jobu
,
jobv
;
...
@@ -56,37 +62,35 @@ int APPLY_SPECIFIC(magma_svd)(PyGpuArrayObject *A, PyGpuArrayObject **U,
...
@@ -56,37 +62,35 @@ int APPLY_SPECIFIC(magma_svd)(PyGpuArrayObject *A, PyGpuArrayObject **U,
goto
fail
;
goto
fail
;
}
}
if
(
COMPUTE_UV
)
{
#ifdef COMPUTE_UV
if
(
FULL_MATRICES
)
{
#ifdef FULL_MATRICES
jobu
=
MagmaAllVec
;
jobu
=
MagmaAllVec
;
jobv
=
MagmaAllVec
;
jobv
=
MagmaAllVec
;
}
#else
else
{
jobu
=
MagmaSomeVec
;
jobu
=
MagmaSomeVec
;
jobv
=
MagmaSomeVec
;
jobv
=
MagmaSomeVec
;
#endif
}
M_U
=
(
jobu
==
MagmaAllVec
?
M
:
K
);
M_U
=
(
jobu
==
MagmaAllVec
?
M
:
K
);
N_VT
=
(
jobv
==
MagmaAllVec
?
N
:
K
);
N_VT
=
(
jobv
==
MagmaAllVec
?
N
:
K
);
ldu
=
M
;
ldu
=
M
;
ldv
=
N_VT
;
ldv
=
N_VT
;
if
(
MAGMA_SUCCESS
!=
magma_smalloc_pinned
(
&
u_data
,
M_U
*
M
))
{
if
(
MAGMA_SUCCESS
!=
magma_smalloc_pinned
(
&
u_data
,
M_U
*
M
))
{
PyErr_SetString
(
PyExc_RuntimeError
,
PyErr_SetString
(
PyExc_RuntimeError
,
"GpuMagmaSVD: failed to allocate memory"
);
"GpuMagmaSVD: failed to allocate memory"
);
goto
fail
;
goto
fail
;
}
if
(
MAGMA_SUCCESS
!=
magma_smalloc_pinned
(
&
vt_data
,
N
*
N_VT
))
{
PyErr_SetString
(
PyExc_RuntimeError
,
"GpuMagmaSVD: failed to allocate memory"
);
goto
fail
;
}
}
}
else
{
if
(
MAGMA_SUCCESS
!=
magma_smalloc_pinned
(
&
vt_data
,
N
*
N_VT
))
{
jobu
=
MagmaNoVec
;
PyErr_SetString
(
PyExc_RuntimeError
,
jobv
=
MagmaNoVec
;
"GpuMagmaSVD: failed to allocate memory"
);
ldu
=
M
;
goto
fail
;
ldv
=
N
;
}
}
#else
jobu
=
MagmaNoVec
;
jobv
=
MagmaNoVec
;
ldu
=
M
;
ldv
=
N
;
#endif
// query for workspace size
// query for workspace size
magma_sgesvd
(
jobu
,
jobv
,
M
,
N
,
NULL
,
M
,
NULL
,
NULL
,
ldu
,
NULL
,
ldv
,
magma_sgesvd
(
jobu
,
jobv
,
M
,
N
,
NULL
,
M
,
NULL
,
NULL
,
ldu
,
NULL
,
ldv
,
...
@@ -124,6 +128,7 @@ int APPLY_SPECIFIC(magma_svd)(PyGpuArrayObject *A, PyGpuArrayObject **U,
...
@@ -124,6 +128,7 @@ int APPLY_SPECIFIC(magma_svd)(PyGpuArrayObject *A, PyGpuArrayObject **U,
cudaMemcpy
(
PyGpuArray_DEV_DATA
(
*
S
),
s_data
,
K
*
sizeof
(
float
),
cudaMemcpy
(
PyGpuArray_DEV_DATA
(
*
S
),
s_data
,
K
*
sizeof
(
float
),
cudaMemcpyDeviceToDevice
);
cudaMemcpyDeviceToDevice
);
#ifdef COMPUTE_UV
u_dims
[
0
]
=
N
;
u_dims
[
1
]
=
N_VT
;
u_dims
[
0
]
=
N
;
u_dims
[
1
]
=
N_VT
;
if
(
theano_prep_output
(
U
,
2
,
u_dims
,
A
->
ga
.
typecode
,
GA_C_ORDER
,
c
)
!=
0
){
if
(
theano_prep_output
(
U
,
2
,
u_dims
,
A
->
ga
.
typecode
,
GA_C_ORDER
,
c
)
!=
0
){
PyErr_SetString
(
PyExc_RuntimeError
,
PyErr_SetString
(
PyExc_RuntimeError
,
...
@@ -145,7 +150,7 @@ int APPLY_SPECIFIC(magma_svd)(PyGpuArrayObject *A, PyGpuArrayObject **U,
...
@@ -145,7 +150,7 @@ int APPLY_SPECIFIC(magma_svd)(PyGpuArrayObject *A, PyGpuArrayObject **U,
// to match numpy.linalg.svd output
// to match numpy.linalg.svd output
cudaMemcpy
(
PyGpuArray_DEV_DATA
(
*
VT
),
u_data
,
M_U
*
M
*
sizeof
(
float
),
cudaMemcpy
(
PyGpuArray_DEV_DATA
(
*
VT
),
u_data
,
M_U
*
M
*
sizeof
(
float
),
cudaMemcpyDeviceToDevice
);
cudaMemcpyDeviceToDevice
);
#endif
res
=
0
;
res
=
0
;
fail:
fail:
if
(
a_data
!=
NULL
)
if
(
a_data
!=
NULL
)
...
...
theano/gpuarray/tests/test_linalg.py
浏览文件 @
5f1e372d
...
@@ -292,7 +292,7 @@ class TestMagma(unittest.TestCase):
...
@@ -292,7 +292,7 @@ class TestMagma(unittest.TestCase):
mode
=
mode_with_gpu
.
including
(
'magma'
))
mode
=
mode_with_gpu
.
including
(
'magma'
))
A_val
=
rand
(
50
,
100
)
A_val
=
rand
(
50
,
100
)
utt
.
assert_allclose
(
f_cpu
(
A_val
),
f_gpu
(
A_val
)
[
1
]
)
utt
.
assert_allclose
(
f_cpu
(
A_val
),
f_gpu
(
A_val
))
A_val
=
rand
(
100
,
50
)
A_val
=
rand
(
100
,
50
)
utt
.
assert_allclose
(
f_cpu
(
A_val
),
f_gpu
(
A_val
)
[
1
]
)
utt
.
assert_allclose
(
f_cpu
(
A_val
),
f_gpu
(
A_val
))
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论