-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseboost_parallel.lua
More file actions
273 lines (210 loc) · 7.76 KB
/
seboost_parallel.lua
File metadata and controls
273 lines (210 loc) · 7.76 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
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
local function copy2(obj)
if type(obj) ~= 'table' then return obj end
local res = setmetatable({}, getmetatable(obj))
for k, v in pairs(obj) do res[copy2(k)] = copy2(v) end
return res
end
local ipc = require 'libipc'
local sys = require 'sys'
require 'cunn'
do --define server
local Master = torch.class('Master')
--n is the number of workers.
function Master:__init(x, n, port)
print('before ipc.server '..port)
self.server = ipc.server('127.0.0.1', port)
print('after ipc.server')
self.remote_models = {}
self.next_free = 0
self.n = n
for i = 0, n - 1 do
self.remote_models[i] = x:clone()
end
--connect to clients. This blocks untill all clients connect.
self.server:clients(n, function(client)
print('connected to client')
end)
end
--wait to get results from n workers.
function Master:block_on_workers()
self.next_free = 0
self.server:clients(self.n, function(client)
--will this run on paralel?? BUG!!!
local msg = client:recv(self.remote_models[self.next_free])
self.next_free = self.next_free + 1
end)
end
function Master:broadcast_to_workers(x)
self.server:clients(self.n, function(client)
client:send(x)
end)
end
function Master:close()
self.server:close()
end
--define worker
local Worker = torch.class('Worker')
function Worker:__init(id, port)
print('before ipc.client '..port)
self.client = ipc.client('127.0.0.1', port)
print('after ipc.client')
self.id = id
end
function Worker:send_to_master(x)
self.client:send(x)
end
--blocks
function Worker:recv_from_master(x)
return self.client:recv(x)
end
function Worker:close()
self.client:close()
end
end
function optim.seboost(opfunc, x, config, state)
-- get/update state
local state = state or config
local isCuda = config.isCuda or false
local sesopData = config.sesopData
local sesopLabels = config.sesopLabels
local sesopBatchSize = config.sesopBatchSize or 100
config.nodeIters = config.nodeIters or 100
config.merger = config.merger or 'sesop'
state.itr = state.itr or 0
config.numNodes = config.numNodes or 2
state.sesopIteration = state.sesopIteration or 0
state.itr = state.itr + 1
if (state.itr % config.nodeIters ~= 0) then
--print ('SEBOOST PARALLEL')
--print(fx)
x,fx = config.optMethod(opfunc, x, config.optConfig)
return x,fx
end
if (config.master == nil) then
--WORKER--
config.worker:send_to_master(x)
config.worker:recv_from_master(x)
local fHist = {}
fHist = config.worker:recv_from_master(fHist)
return x, fHist
end
if (config.worker == nil) then
--MASTER--
config.histSize = config.histSize or 0
if config.histSize ~=0 then
state.histspace = state.histspace or torch.zeros(x:size(1),config.histSize):cuda()
end
state.splitPoint = state.splitPoint or x:clone() --the first split point is the first point
config.master:block_on_workers()
--Do SESOP on master.remote_models:
if (state.dirs == nil) then
--if it is the first time
state.dirs = torch.zeros(x:size(1), config.numNodes)
state.aOpt = torch.zeros(config.numNodes + config.histSize)
--state.aOpt[1] = 1 --we start from taking the first node direction (maybe start from avrage?).
if (isCuda) then
state.dirs = state.dirs:cuda()
state.aOpt = state.aOpt:cuda()
end
end
--state.aOpt:copy(torch.ones(config.numNodes + config.histSize)*(1/(config.numNodes + config.histSize))) --avrage
state.aOpt:copy(torch.zeros(config.numNodes + config.histSize))
state.dirs[{ {}, 1 }]:copy(x - state.splitPoint)
--SV, build directions matrix
for i = 1, config.numNodes - 1 do
--[{ {}, i }] means: all of the first dim, slice in the second dim at i = get i col.
state.dirs[{ {}, i + 1 }]:copy(config.master.remote_models[i - 1] - state.splitPoint)
end
--Tao Code
local temp_dir = nil
if config.histSize ~= 0 then
temp_dir = torch.cat(state.dirs, state.histspace, 2)
else
temp_dir = state.dirs
end
--now optimize!
local xInit = state.splitPoint
-- create mini batch
local subT = (state.sesopIteration) * sesopBatchSize + 1
subT = subT % (sesopData:size(1) - sesopBatchSize) --Calculate the next batch index
local sesopInputs = sesopData:narrow(1, subT, sesopBatchSize)
local sesopTargets = sesopLabels:narrow(1, subT, sesopBatchSize)
-- Create inner opfunc for finding a*
local feval = function(a)
--A function of the coefficients
local dirMat = temp_dir
--Note that opfunc also gets the batch
local afx, adfdx = opfunc(xInit + dirMat*a, sesopInputs, sesopTargets)
return afx, (dirMat:t()*adfdx)
end
--x,f(x)
--config.maxIter = config.numNodes + config.histSize
--config.maxIter = config.histSize + config.numNodes + 20
config.maxIter = 200
local _ = nil
local fHist = nil
if config.merger == 'avrage' then
state.aOpt:copy(torch.ones(config.numNodes + config.histSize)*(1/(config.numNodes + config.histSize))) --avrage
fHist, _ = feval(state.aOpt)
elseif config.merger == 'min' then
state.aOpt:copy(torch.zeros(config.numNodes + config.histSize))
local bestIdx = 1
state.aOpt[1] = 1
local bestF, _ = feval(state.aOpt)
state.aOpt[1] = 0
for i = 2, config.numNodes + config.histSize do
state.aOpt[i] = 1
local f, _ = feval(state.aOpt)
state.aOpt[i] = 0
if f < bestF then
bestIdx = i
bestF = f
end
end
fHist = bestF
state.aOpt[bestIdx] = 1
else
state.starting_norms = state.starting_norms or torch.zeros(1)
state.starting_norms = torch.cat(state.starting_norms, config.model:forward(sesopInputs):norm()*torch.ones(1), 1)
torch.save(config.save..'/starting_norms.txt', state.starting_norms)
--state.aOpt:copy(torch.zeros(config.numNodes + config.histSize))
state.aOpt:copy(torch.ones(config.numNodes + config.histSize)*(1/(config.numNodes + config.histSize))) --avrage
for k,v in pairs(config.model:findModules('nn.FixableBatchNormalization')) do
--v.fix = true
v.train = false
end
--x,fx,i,dfx
_, fHist = optim.cg(feval, state.aOpt, config, state) --Apply optimization using inner function
for k,v in pairs(config.model:findModules('nn.FixableBatchNormalization')) do
--v.fix = false
v.train = true
end
end
--updating model weights!
x:copy(xInit)
local sesopDir = temp_dir*state.aOpt
x:add(sesopDir)
--Tao code update the history direction here
if config.histSize ~= 0 then
if config.histSize == 1 then
state.histspace = sesopDir
else
--we throw out the vector in column state.histSize
--we insert instead a new vector in column 1.
state.histspace = torch.cat(sesopDir, state.histspace:narrow(2, 1, config.histSize - 1), 2)
--for i = 1, config.numNodes - 1 do
--[{ {}, i }] means: all of the first dim, slice in the second dim at i = get i col.
-- state.dirs[{ {}, i + 1 }]:copy(config.master.remote_models[i - 1] - state.splitPoint)
--end
end
end
--the new split point is 'x'.
--The next time this function is called will be with 'x'.
--The next time we will change a node, it will get this 'x'.
state.splitPoint:copy(x)
state.sesopIteration = state.sesopIteration + 1
config.master:broadcast_to_workers(x)
config.master:broadcast_to_workers(fHist)
return x,fHist
end
end