-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathtrain_2gpu.py
380 lines (294 loc) · 12.9 KB
/
train_2gpu.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
'''
Train AlexNet on ImageNet with 2 GPUs.
'''
import sys
import time
from multiprocessing import Process, Queue
import yaml
import numpy as np
import zmq
import pycuda.driver as drv
import pycuda.gpuarray as gpuarray
sys.path.append('./lib')
from tools import (save_weights, load_weights,
save_momentums, load_momentums)
from train_funcs import (unpack_configs, adjust_learning_rate,
get_val_error_loss, get_rand3d, train_model_wrap,
proc_configs)
def train_net(config, private_config):
# UNPACK CONFIGS
(flag_para_load, train_filenames, val_filenames,
train_labels, val_labels, img_mean) = \
unpack_configs(config, ext_data=private_config['ext_data'],
ext_label=private_config['ext_label'])
gpu_send_queue = private_config['queue_gpu_send']
gpu_recv_queue = private_config['queue_gpu_recv']
# pycuda and zmq set up
drv.init()
dev = drv.Device(int(private_config['gpu'][-1]))
ctx = dev.make_context()
sock_gpu = zmq.Context().socket(zmq.PAIR)
if private_config['flag_client']:
sock_gpu.connect('tcp://localhost:{0}'.format(config['sock_gpu']))
else:
sock_gpu.bind('tcp://*:{0}'.format(config['sock_gpu']))
if flag_para_load:
sock_data = zmq.Context().socket(zmq.PAIR)
sock_data.connect('tcp://localhost:{0}'.format(
private_config['sock_data']))
load_send_queue = private_config['queue_t2l']
load_recv_queue = private_config['queue_l2t']
else:
load_send_queue = None
load_recv_queue = None
import theano.sandbox.cuda
theano.sandbox.cuda.use(private_config['gpu'])
import theano
theano.config.on_unused_input = 'warn'
from layers import DropoutLayer
from alex_net import AlexNet, compile_models
import theano.misc.pycuda_init
import theano.misc.pycuda_utils
## BUILD NETWORK ##
model = AlexNet(config)
layers = model.layers
batch_size = model.batch_size
## COMPILE FUNCTIONS ##
(train_model, validate_model, train_error, learning_rate,
shared_x, shared_y, rand_arr, vels) = compile_models(model, config)
total_params = model.params + vels
# total_params = model.params
# initialize gpuarrays that points to the theano shared variable
# pass parameters and other stuff
param_ga_list = []
param_other_list = []
param_ga_other_list = []
h_list = []
shape_list = []
dtype_list = []
average_fun_list = []
for param in total_params:
param_other = theano.shared(param.get_value())
param_ga = \
theano.misc.pycuda_utils.to_gpuarray(param.container.value)
param_ga_other = \
theano.misc.pycuda_utils.to_gpuarray(
param_other.container.value)
h = drv.mem_get_ipc_handle(param_ga.ptr)
average_fun = \
theano.function([], updates=[(param,
(param + param_other) / 2.)])
param_other_list.append(param_other)
param_ga_list.append(param_ga)
param_ga_other_list.append(param_ga_other)
h_list.append(h)
shape_list.append(param_ga.shape)
dtype_list.append(param_ga.dtype)
average_fun_list.append(average_fun)
# pass shape, dtype and handles
sock_gpu.send_pyobj((shape_list, dtype_list, h_list))
shape_other_list, dtype_other_list, h_other_list = sock_gpu.recv_pyobj()
param_ga_remote_list = []
# create gpuarray point to the other gpu use the passed information
for shape_other, dtype_other, h_other in zip(shape_other_list,
dtype_other_list,
h_other_list):
param_ga_remote = \
gpuarray.GPUArray(shape_other, dtype_other,
gpudata=drv.IPCMemoryHandle(h_other))
param_ga_remote_list.append(param_ga_remote)
print "Information passed between 2 GPUs"
##########################################
######################### TRAIN MODEL ################################
print '... training'
if flag_para_load:
# pass ipc handle and related information
gpuarray_batch = theano.misc.pycuda_utils.to_gpuarray(
shared_x.container.value)
h = drv.mem_get_ipc_handle(gpuarray_batch.ptr)
sock_data.send_pyobj((gpuarray_batch.shape, gpuarray_batch.dtype, h))
load_send_queue.put(img_mean)
n_train_batches = len(train_filenames)
minibatch_range = range(n_train_batches)
# gpu sync before start
gpu_send_queue.put('before_start')
assert gpu_recv_queue.get() == 'before_start'
# Start Training Loop
epoch = 0
step_idx = 0
val_record = []
while epoch < config['n_epochs']:
epoch = epoch + 1
if config['shuffle']:
np.random.shuffle(minibatch_range)
if config['resume_train'] and epoch == 1:
load_epoch = config['load_epoch']
load_weights(layers, config['weights_dir'], load_epoch)
epoch = load_epoch + 1
lr_to_load = np.load(
config['weights_dir'] + 'lr_' + str(load_epoch) + '.npy')
val_record = list(
np.load(config['weights_dir'] + 'val_record.npy'))
learning_rate.set_value(lr_to_load)
load_momentums(vels, config['weights_dir'], epoch)
if flag_para_load:
# send the initial message to load data, before each epoch
load_send_queue.put(str(train_filenames[minibatch_range[0]]))
load_send_queue.put(get_rand3d())
# clear the sync before 1st calc
load_send_queue.put('calc_finished')
count = 0
for minibatch_index in minibatch_range:
num_iter = (epoch - 1) * n_train_batches + count
count = count + 1
if count%20 == 1:
s = time.time()
cost_ij = train_model_wrap(train_model, shared_x,
shared_y, rand_arr, img_mean,
count, minibatch_index,
minibatch_range, batch_size,
train_filenames, train_labels,
flag_para_load,
config['batch_crop_mirror'],
send_queue=load_send_queue,
recv_queue=load_recv_queue)
# gpu sync
gpu_send_queue.put('after_train')
assert gpu_recv_queue.get() == 'after_train'
# exchanging weights
for param_ga, param_ga_other, param_ga_remote in \
zip(param_ga_list, param_ga_other_list,
param_ga_remote_list):
drv.memcpy_peer(param_ga_other.ptr,
param_ga_remote.ptr,
param_ga_remote.dtype.itemsize *
param_ga_remote.size,
ctx, ctx)
ctx.synchronize()
# gpu sync
gpu_send_queue.put('after_ctx_sync')
assert gpu_recv_queue.get() == 'after_ctx_sync'
# do average
for average_fun in average_fun_list:
average_fun()
# report train stats
if num_iter % config['print_freq'] == 0:
gpu_send_queue.put(cost_ij)
that_cost = gpu_recv_queue.get()
cost_ij = (cost_ij + that_cost) / 2.
if private_config['flag_verbose']:
print 'training @ iter = ', num_iter
print 'training cost:', cost_ij
if config['print_train_error']:
error_ij = train_error()
gpu_send_queue.put(error_ij)
that_error = gpu_recv_queue.get()
error_ij = (error_ij + that_error) / 2.
if private_config['flag_verbose']:
print 'training error rate:', error_ij
if flag_para_load and (count < len(minibatch_range)):
load_send_queue.put('calc_finished')
if count%20 == 0:
e = time.time()
print "time per 20 iter:", (e - s)
############### Test on Validation Set ##################
DropoutLayer.SetDropoutOff()
this_val_error, this_val_loss = get_val_error_loss(
rand_arr, shared_x, shared_y,
val_filenames, val_labels,
flag_para_load, img_mean,
batch_size, validate_model,
send_queue=load_send_queue, recv_queue=load_recv_queue)
# report validation stats
gpu_send_queue.put(this_val_error)
that_val_error = gpu_recv_queue.get()
this_val_error = (this_val_error + that_val_error) / 2.
gpu_send_queue.put(this_val_loss)
that_val_loss = gpu_recv_queue.get()
this_val_loss = (this_val_loss + that_val_loss) / 2.
if private_config['flag_verbose']:
print('epoch %i: validation loss %f ' %
(epoch, this_val_loss))
print('epoch %i: validation error %f %%' %
(epoch, this_val_error * 100.))
val_record.append([this_val_error, this_val_loss])
if private_config['flag_save']:
np.save(config['weights_dir'] + 'val_record.npy', val_record)
DropoutLayer.SetDropoutOn()
############################################
# Adapt Learning Rate
step_idx = adjust_learning_rate(config, epoch, step_idx,
val_record, learning_rate)
# Save Weights, only one of them will do
if private_config['flag_save']:
if epoch % config['snapshot_freq'] == 0:
save_weights(layers, config['weights_dir'], epoch)
np.save(config['weights_dir'] + 'lr_' + str(epoch) + '.npy',
learning_rate.get_value())
save_momentums(vels, config['weights_dir'], epoch)
print('Optimization complete.')
if __name__ == '__main__':
with open('config.yaml', 'r') as f:
config = yaml.load(f)
with open('spec_2gpu.yaml', 'r') as f:
config = dict(config.items() + yaml.load(f).items())
config = proc_configs(config)
queue_gpu_0to1 = Queue(1)
queue_gpu_1to0 = Queue(1)
private_config_0 = {}
private_config_0['queue_gpu_send'] = queue_gpu_0to1
private_config_0['queue_gpu_recv'] = queue_gpu_1to0
private_config_0['sock_data'] = config['sock_data0']
private_config_0['gpu'] = config['gpu0']
private_config_0['ext_data'] = '.hkl'
private_config_0['ext_label'] = '.npy'
private_config_0['ext_data'] = '_0.hkl'
private_config_0['ext_label'] = '_0.npy'
private_config_0['flag_client'] = True
private_config_0['flag_verbose'] = True
private_config_0['flag_save'] = True
private_config_1 = {}
private_config_1['queue_gpu_send'] = queue_gpu_1to0
private_config_1['queue_gpu_recv'] = queue_gpu_0to1
private_config_1['sock_data'] = config['sock_data1']
private_config_1['gpu'] = config['gpu1']
private_config_1['ext_data'] = '_1.hkl'
private_config_1['ext_label'] = '_1.npy'
private_config_1['flag_client'] = False
private_config_1['flag_verbose'] = False
private_config_1['flag_save'] = False
if config['para_load']:
from proc_load import fun_load
private_config_0['queue_l2t'] = Queue(1)
private_config_0['queue_t2l'] = Queue(1)
train_proc_0 = Process(target=train_net,
args=(config, private_config_0))
load_proc_0 = Process(target=fun_load,
args=(dict(private_config_0.items() +
config.items()),
private_config_0['sock_data']))
private_config_1['queue_l2t'] = Queue(1)
private_config_1['queue_t2l'] = Queue(1)
train_proc_1 = Process(target=train_net,
args=(config, private_config_1))
load_proc_1 = Process(target=fun_load,
args=(dict(private_config_1.items() +
config.items()),
private_config_1['sock_data']))
train_proc_0.start()
load_proc_0.start()
train_proc_1.start()
load_proc_1.start()
train_proc_0.join()
load_proc_0.join()
train_proc_1.join()
load_proc_1.join()
else:
train_proc_0 = Process(target=train_net,
args=(config, private_config_0))
train_proc_1 = Process(target=train_net,
args=(config, private_config_1))
train_proc_0.start()
train_proc_1.start()
train_proc_0.join()
train_proc_1.join()