-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopencl_learner.py
217 lines (181 loc) · 7.41 KB
/
opencl_learner.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import pyopencl as cl
import numpy
import pyopencl.clrandom
import pyopencl.array
import mako
import math
from batch import *
class GaussNewton:
def __init__(self, opencl_context, network, batch_size, opencl_program_directory="", use_double=False, dropout=True):
self.ctx = opencl_context
self.network = network
self.queue = cl.CommandQueue(self.ctx)
self.batch_size = batch_size
self.program = None
self.dropout = dropout
self.memory_flags = cl.mem_flags.READ_WRITE# | cl.mem_flags.ALLOC_HOST_PTR
self.use_double = use_double
if False:
self.float_size = 8
self.float_type = numpy.float64
self.float_name = 'double'
else:
self.float_size = 4
self.float_type = numpy.float32
self.float_name = 'float'
self.states = cl.Buffer(self.ctx, self.memory_flags, self.float_size*self.network.layers_sum*batch_size )
self.batches_buf = cl.Buffer(self.ctx, self.memory_flags, self.float_size*(self.network.sizes[0]+self.network.sizes[-1])*batch_size )
self.R_states = cl.Buffer(self.ctx, self.memory_flags, self.float_size*self.network.layers_sum*batch_size )
self.LJ = cl.Buffer(self.ctx, self.memory_flags, self.float_size*self.network.sizes[-1]*batch_size )
self.error = cl.Buffer(self.ctx, self.memory_flags, self.float_size*self.network.sizes[-1]*batch_size )
self.targets = cl.Buffer(self.ctx, self.memory_flags, self.float_size*self.network.sizes[-1]*batch_size )
self.temp_vec1 = cl.Buffer(self.ctx, self.memory_flags, self.float_size*self.network.layer_max*batch_size*2 )
self.temp_vec2 = (self.temp_vec1,cl.Buffer(self.ctx, self.memory_flags, self.float_size*self.network.layer_max*batch_size*2 ))
self.loss_summer = cl.Buffer(self.ctx, self.memory_flags, self.float_size*batch_size )
self.weights = cl.Buffer(self.ctx, self.memory_flags, self.float_size*self.network.weights_sum)
self.R_weights = cl.Buffer(self.ctx, self.memory_flags, self.float_size*self.network.weights_sum)
self.result_weights = cl.Buffer(self.ctx, self.memory_flags, self.float_size*self.network.weights_sum)
self.current_batch = None
self.work_group_size = 1
self.random_state = cl.Buffer(self.ctx, self.memory_flags, self.network.layer_max*112 )
self.load_programs(opencl_program_directory)
self.update_weights(network)
def update_weights(self,network=None,gpu_net_array=None):
if gpu_net_array is None:
gpu_net_array = network.to_array(order='F')
cl.enqueue_copy(self.queue, self.weights, gpu_net_array,is_blocking=False)
def update_R_weights(self,network=None,gpu_net_array=None):
if gpu_net_array is None:
gpu_net_array = network.to_array(order='F')
cl.enqueue_copy(self.queue, self.R_weights, gpu_net_array,is_blocking=False)
def read_result_weights(self, gpu_net_array):
cl.enqueue_copy(self.queue, gpu_net_array, self.result_weights, is_blocking=True)
return gpu_net_array
def read_weights(self, gpu_net_array):
cl.enqueue_copy(self.queue, gpu_net_array, self.weights, is_blocking=True)
return gpu_net_array
def load_programs(self, dir):
from mako.template import Template
#Sigmoid
f = open(dir+"program.cl",'r')
program_tmp = f.read()
program_src = str(Template(program_tmp).render(
layer_count=len(self.network.sizes),
weight_count=len(self.network.sizes)-1,
layer_sizes=self.network.sizes,
weight_sizes=self.network.weights_sizes,
layer_offsets=self.network.layer_offsets,
weight_offsets=self.network.weights_offsets,
work_group_size=self.work_group_size,
vector_count = self.batch_size,
float_type = ['float','double'][self.use_double],
use_double = self.use_double,
dropout = self.dropout,
))
self.program = cl.Program(self.ctx, program_src).build()#("-Werror")
self.program.init_random(self.queue,(self.network.layer_max,),(1,),
numpy.uint32(123),
self.random_state,
)
def load_batch(self, batch):
cl.enqueue_copy(self.queue, self.batches_buf, batch.buffers,is_blocking=False)
self.program.copy_buffers(self.queue,(self.work_group_size*self.network.sizes[0],),(self.work_group_size,),
self.batches_buf,self.states,self.targets)
def forward_pass(self):
for x in xrange(len(self.network.weights_sizes)):
enable_dropout = 1
if not self.dropout or x == len(self.network.weights_sizes)-1:
enable_dropout = 0
self.program.forward_pass(self.queue,(self.work_group_size*self.network.layer_max,),(self.work_group_size,),
self.states,
self.weights,
numpy.uint32(x),
self.random_state,
numpy.uint32(enable_dropout),)
def R_forward_pass(self):
for x in xrange(len(self.network.weights_sizes)):
self.program.R_forward_pass(self.queue,(self.work_group_size*self.network.layer_max,),(self.work_group_size,),
self.states,
self.R_states,
self.weights,
self.R_weights,
self.LJ,
numpy.uint32(x))
def backward_pass(self,vector):
self.program.zero(self.queue,(self.network.weights_sum,),None,
self.result_weights
)
self.program.backward_pass(self.queue,(self.work_group_size*self.network.layer_max,),(self.work_group_size,),
self.states,
vector,
self.weights,
self.result_weights,
self.temp_vec2[(len(self.network.weights_sizes)) % 2],
numpy.uint32(len(self.network.weights_sizes)-1)
)
for x in reversed(xrange(len(self.network.weights_sizes)-1)):
self.program.backward_pass(self.queue,(self.work_group_size*self.network.layer_max,),(self.work_group_size,),
self.states,
self.temp_vec2[(x)%2],
self.weights,
self.result_weights,
self.temp_vec2[(x+1)%2],
numpy.uint32(x)
)
def gradient(self, batch):
self.load_batch(batch)
self.forward_pass()
self.program.load_error(self.queue,(self.work_group_size*self.network.layer_max,),(self.work_group_size,),
self.states.get_sub_region(self.float_size*self.batch_size*self.network.layer_offsets[-2],self.float_size*self.batch_size*self.network.sizes[-1]),
self.targets,
self.error
)
self.backward_pass(self.error)
self.program.calc_loss(self.queue,(self.work_group_size*self.network.layer_max,),(self.work_group_size,),
self.states,
self.weights,
self.targets,
self.loss_summer)
def read_back_loss(self):
self.program.sum(self.queue,(self.work_group_size*1,),(self.work_group_size,),
self.loss_summer,
numpy.uint32(self.batch_size),
numpy.uint32(self.batch_size))
loss_result = numpy.array([0.5]).astype(self.float_type)
cl.enqueue_copy(self.queue, loss_result,
self.loss_summer,
is_blocking=True)
return loss_result[0]
def load_forward_and_loss(self, batch):
self.load_batch(batch)
self.forward_pass()
self.program.calc_loss(self.queue,(self.work_group_size*self.network.layer_max,),(self.work_group_size,),
self.states,
self.weights,
self.targets,
self.loss_summer)
def gauss_product(self):
#self.load_batch(batch)
#self.forward_pass()
self.R_forward_pass()
self.backward_pass(self.LJ)
def contrastive_divergence(self, batch, rbm):
self.load_batch(batch)
self.forward_pass()
rnd_size = 2**int(math.ceil(math.log(self.network.weights_sum, 2)))
vec = pyopencl.clrandom.rand(self.queue, (rnd_size,), dtype=numpy.float32)
self.program.contrastive_divergence(self.queue,(self.work_group_size*self.network.layer_max,),(self.work_group_size,),
self.states,
self.temp_vec1,
self.weights,
self.result_weights,
vec.data,
numpy.uint32(rnd_size),
numpy.uint32(rbm),
numpy.uint32(3),
numpy.uint32(0.1)
)
def zero_results(self):
self.program.zero(self.queue,(self.network.weights_sum,),None,
self.result_weights
)