forked from r00tman/kfac-theano
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcusolver.py
More file actions
164 lines (123 loc) · 4.75 KB
/
cusolver.py
File metadata and controls
164 lines (123 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from __future__ import absolute_import, division, print_function
import pkg_resources
import theano
from theano.sandbox.cuda import GpuOp, cuda_available
from theano.sandbox.cuda.basic_ops import as_cuda_ndarray_variable
from theano.sandbox.cuda.type import CudaNdarrayType
if cuda_available:
from theano.sandbox.cuda import CudaNdarray
try:
from theano.sandbox.cuda import cuda_ndarray
dimshuffle = cuda_ndarray.cuda_ndarray.dimshuffle
except ImportError:
pass
cusolver_available = False
try:
from scikits.cuda import cusolver
cusolver_available = True
except NotImplementedError: # (ImportError, OSError, RuntimeError, pkg_resources.DistributionNotFound):
pass
cusolver_handle = None
class GpuCusolverSolve(GpuOp):
"""
CUSOLVER GPU solver OP.
Parameters
----------
trans
Whether to take the transpose of the input matrix or not.
"""
__props__ = ('trans',)
def __init__(self, trans='N'):
self.trans = trans
super(GpuCusolverSolve, self).__init__()
def make_node(self, inp1, inp2):
inp1 = as_cuda_ndarray_variable(inp1)
inp2 = as_cuda_ndarray_variable(inp2)
assert inp1.ndim == 2
assert inp2.ndim == 2
return theano.Apply(
self, [inp1, inp2],
[CudaNdarrayType(broadcastable=[False] * inp1.type.ndim)()])
def make_thunk(self,
node,
storage_map, _,
no_recycling=[],
impl=None):
if not cusolver_available:
raise RuntimeError('CUSOLVER is not available and '
'GpuCusolverSolve Op can not be constructed.')
inputs = [storage_map[v] for v in node.inputs]
outputs = [storage_map[v] for v in node.outputs]
def thunk():
global cusolver_handle
# Size of the matrices to invert.
z = outputs[0]
# Matrix.
A = inputs[0][0]
# Solution vectors.
b = inputs[1][0]
# A is not explicitly converted between C and F order, instead we
# switch the "transpose" flag.
if self.trans in ('T', 'C'):
trans = 'N'
else:
trans = 'T'
# Convert b to F-order from C-order.
b_cpy = dimshuffle(b, (1, 0)).reshape((b.shape[0], b.shape[1]))
# This copy forces allocation of a new C-contiguous buffer
# and returns it.
A_cpy = A.copy()
b_cpy = b_cpy.copy()
assert(len(A.shape) == 2)
assert(len(b.shape) == 2)
if trans in ['T', 'C']:
trans = 1
l, n = A.shape
k, m = b.shape
if n != k:
raise ValueError('A and b must be aligned.')
elif trans in ['N']:
trans = 0
n, l = A.shape
k, m = b.shape
if l != m:
raise ValueError('A and b must be aligned.')
else:
raise ValueError('Invalid value for trans')
lda = max(1, n)
ldb = max(1, n, l)
A_ptr = A_cpy.gpudata
b_ptr = b_cpy.gpudata
if cusolver_handle is None:
cusolver_handle = cusolver.cusolverDnCreate()
print('cusolver handle', cusolver_handle)
workspace_size = cusolver.cusolverDnSgetrf_bufferSize(
cusolver_handle, m, n, A_ptr, lda)
if (thunk.workspace is None or
thunk.workspace.size != workspace_size):
thunk.workspace = CudaNdarray.zeros((workspace_size,))
if thunk.pivots is None or thunk.pivots.size != min(m, n):
thunk.pivots = CudaNdarray.zeros((min(m, n),))
if thunk.dev_info is None:
thunk.dev_info = CudaNdarray.zeros((1,))
workspace_ptr = thunk.workspace.gpudata
pivots_ptr = thunk.pivots.gpudata
dev_info_ptr = thunk.dev_info.gpudata
cusolver.cusolverDnSgetrf(
cusolver_handle, n, l, A_ptr, lda, workspace_ptr,
pivots_ptr, dev_info_ptr)
cusolver.cusolverDnSgetrs(
cusolver_handle, trans, n, m, A_ptr, lda,
pivots_ptr, b_ptr, ldb, dev_info_ptr)
# Convert b to F-order from C-order and assign it to output.
b_cpy = b_cpy.reshape(b.shape[::-1])
b_cpy = dimshuffle(b_cpy, (1, 0))
z[0] = b_cpy
thunk.inputs = inputs
thunk.outputs = outputs
thunk.lazy = False
thunk.workspace = None
thunk.pivots = None
thunk.dev_info = None
return thunk
gpu_solve = GpuCusolverSolve()