-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpserver.lua
303 lines (275 loc) · 11.7 KB
/
pserver.lua
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
-- Parameter server
-- Author: Sixin Zhang ([email protected])
-- Author: Minwei Feng ([email protected])
-- Important change:
-- init the parameter from the first local worker (once and only once)
-- before other service gets started. From the pclient side,
-- it's also the first local pclient (worker) who sends its parameter
-- to the psever.
require 'mpiT'
local pServer = torch.class('pServer')
function pServer:__init(conf,state)
self.state = state or {}
self.rank = conf.rank or -1
self.cranks = conf.cranks or {} -- client ranks
self.mtype = mpiT.FLOAT
self.mworld = conf.world or mpiT.COMM_WORLD
self.offset = -1 -- offset of param grad
self.size = -1 -- size of param grad
self.tensor = {} -- tensor from storage
self.storage = {} -- param gradient storage
self.emptys = torch.Storage()
self.state.on = false
self.state.io = false
self.state.iostop = 0
self.coq = Queue() -- coroutine queue
self.conf = conf
end
local function pServer_recvinit(self,crank)
coroutine.yield(mpiT.signal_INIT)
-- get meta info
local cinfo = torch.LongStorage(2)
mpiT.aio_recv(cinfo,2,mpiT.LONG,
crank,mpiT.tag_ps_recv_init,self.mworld,self.state)
-- init storage
if self.offset == -1 then
self.offset = cinfo[1]
self.size = cinfo[2]
self.storage.p = torch.Storage(self.size)
self.storage.g = {}
self.storage.g[crank] = torch.Storage(self.size)
self.tensor.p = torch.Tensor(self.storage.p)
self.tensor.g = {}
self.tensor.g[crank] = torch.Tensor(self.storage.g[crank])
if self.conf.opt.optimization == 'rmsprop' then
self.gradAccum = torch.Tensor():resizeAs(self.tensor.p):zero()
self.gradSqAccum = torch.Tensor():resizeAs(self.tensor.p):zero()
self.update = torch.Tensor():resizeAs(self.tensor.p):zero()
self.gradRms = torch.Tensor():resizeAs(self.tensor.p):zero()
end
if self.conf.opt.optimization == 'adam' then
self.adam_t = 0
self.adam_m = torch.Tensor():resizeAs(self.tensor.p):zero()
self.adam_v = torch.Tensor():resizeAs(self.tensor.p):zero()
self.adam_d = torch.Tensor():resizeAs(self.tensor.p):zero()
end
if self.conf.opt.optimization == 'adamax' then
self.adamax_t = 0
self.adamax_m = torch.Tensor():resizeAs(self.tensor.p):zero()
self.adamax_u = torch.Tensor():resizeAs(self.tensor.p):zero()
self.adamax_max = self.tensor.p.new(2, unpack(self.tensor.p:size():totable())):zero()
end
if self.conf.opt.optimization == 'adagrad' then
self.pversion = 0
self.paramVariance = torch.Tensor():typeAs(self.tensor.p):resizeAs(self.tensor.p):zero()
self.paramStd = torch.Tensor():typeAs(self.tensor.p):resizeAs(self.tensor.p)
end
if self.conf.opt.optimization == 'adadelta' then
self.pversion = 0
self.paramVariance = torch.Tensor():typeAs(self.tensor.p):resizeAs(self.tensor.p):zero()
self.paramStd = torch.Tensor():typeAs(self.tensor.p):resizeAs(self.tensor.p):zero()
self.delta = torch.Tensor():typeAs(self.tensor.p):resizeAs(self.tensor.p):zero()
self.accDelta = torch.Tensor():typeAs(self.tensor.p):resizeAs(self.tensor.p):zero()
end
else
assert(self.offset == cinfo[1])
assert(self.size == cinfo[2])
self.storage.g[crank] = torch.Storage(self.size)
self.tensor.g[crank] = torch.Tensor(self.storage.g[crank])
end
--print('pServer:recvinit',self.rank,crank,self.offset,self.size)
coroutine.yield(mpiT.signal_DONE)
end
local function pServer_sendparam(self,crank)
coroutine.yield(mpiT.signal_INIT)
while (self.state.on) do
--print('pServer_sendparam to recv',crank,self.size)
mpiT.aio_recv(self.emptys,0,self.mtype,
crank,mpiT.tag_ps_recv_header,self.mworld,self.state)
--print('ps ' .. self.rank .. ' send param to ' .. crank)
if self.state.io then
mpiT.aio_send(self.storage.p,self.size,self.mtype,
crank,mpiT.tag_ps_send_param,self.mworld,self.state)
end
-- if self.rank == 1 then
-- print("rank " .. self.rank .. " " .. collectgarbage("count"))
-- end
end
coroutine.yield(mpiT.signal_DONE)
end
-- Warning: no lock on self.tensor.p during this recvgrad, expect inconsistent read
local function pServer_recvgrad(self,crank)
coroutine.yield(mpiT.signal_INIT)
while (self.state.on) do
-- recv
mpiT.aio_recv(self.storage.g[crank],self.size,self.mtype,
crank,mpiT.tag_ps_recv_grad,self.mworld,self.state)
--print('ps ' .. self.rank .. ' recv grad from ' .. crank)
-- apply
if self.conf.opt.optimization == 'rmsprop' then
if self.conf.opt.modeRMSProp == 'global' then
local decay = self.conf.opt.decayRMSProp
local lr = self.conf.opt.lrRMSProp
local momentum = self.conf.opt.momentumRMSProp
local epsilon = self.conf.opt.epsilonRMSProp
self.gradAccum:mul(decay):add(1 - decay, self.tensor.g[crank])
self.gradSqAccum:mul(decay):add(1 - decay, torch.cmul(self.tensor.g[crank], self.tensor.g[crank]))
self.gradRms:copy(self.gradSqAccum)
:add(-1, torch.cmul(self.gradAccum, self.gradAccum))
:add(epsilon):sqrt()
self.update:mul(momentum):add(-lr, torch.cdiv(self.tensor.g[crank], self.gradRms))
self.tensor.p:add(self.update)
elseif self.conf.opt.modeRMSProp == 'local' then
self.tensor.p:add(self.tensor.g[crank])
end
elseif self.conf.opt.optimization == 'adam' then
if self.conf.opt.modeAdam == 'global' then
local lr = self.conf.opt.lrAdam
local beta1 = self.conf.opt.beta1Adam
local beta2 = self.conf.opt.beta2Adam
local epsilon = self.conf.opt.epsilonAdam
self.adam_t = self.adam_t + 1
self.adam_m:mul(beta1):add(1-beta1, self.tensor.g[crank])
self.adam_v:mul(beta2):addcmul(1-beta2, self.tensor.g[crank], self.tensor.g[crank])
self.adam_d:copy(self.adam_v):sqrt():add(epsilon)
local beta1_t = 1 - math.pow(beta1, math.floor(self.adam_t/self.conf.opt.stepDivAdam) + 1 )
local beta2_t = 1 - math.pow(beta2, math.floor(self.adam_t/self.conf.opt.stepDivAdam) + 1 )
local lr_t = lr * math.sqrt(beta2_t)/beta1_t
self.tensor.p:addcdiv(-lr_t, self.adam_m, self.adam_d)
end
elseif self.conf.opt.optimization == 'adamax' then
if self.conf.opt.modeAdam == 'global' then
local lr = self.conf.opt.lrAdam
local beta1 = self.conf.opt.beta1Adam
local beta2 = self.conf.opt.beta2Adam
local epsilon = self.conf.opt.epsilonAdam
self.adamax_t = self.adamax_t + 1
self.adamax_m:mul(beta1):add(1-beta1, self.tensor.g[crank])
self.adamax_max[1]:copy(self.adamax_u):mul(beta2)
self.adamax_max[2]:copy(self.tensor.g[crank]):abs():add(epsilon)
self.adamax_u:max(self.adamax_max, 1)
local beta1_t = 1 - math.pow(beta1, self.adamax_t )
local lr_t = lr /beta1_t
self.tensor.p:addcdiv(-lr_t, self.adamax_m, self.adamax_u)
end
elseif self.conf.opt.optimization == 'adagrad' then
if self.conf.opt.modeAdagrad == 'global' then
local lr = self.conf.opt.lrAdagrad
local lrd = self.conf.opt.lrDecayAdagrad
local epsilon = self.conf.opt.epsilonAdagrad
local clr = lr / (1 + self.pversion*lrd)
self.paramVariance:addcmul(1, self.tensor.g[crank], self.tensor.g[crank])
self.paramStd:resizeAs(self.paramVariance):copy(self.paramVariance):sqrt()
self.tensor.p:addcdiv(-clr, self.tensor.g[crank], self.paramStd:add(epsilon))
self.pversion = self.pversion + 1
end
elseif self.conf.opt.optimization == 'adadelta' then
if self.conf.opt.modeAdagrad == 'global' then
local rho = self.conf.opt.rhoAdadelta
local epsilon = self.conf.opt.epsilonAdadelta
local lr = self.conf.opt.lrAdadelta
self.paramVariance:mul(rho):addcmul(1-rho, self.tensor.g[crank], self.tensor.g[crank])
self.paramStd:resizeAs(self.paramVariance):copy(self.paramVariance):add(epsilon):sqrt()
self.delta:resizeAs(self.paramVariance):copy(self.accDelta):add(epsilon):sqrt():cdiv(self.paramStd):cmul(self.tensor.g[crank])
self.tensor.p:add(-lr, self.delta)
self.accDelta:mul(rho):addcmul(1-rho, self.delta, self.delta)
self.pversion = self.pversion + 1
end
else
self.tensor.p:add(self.tensor.g[crank])
end
if self.state.on then
mpiT.aio_send(self.emptys,0,self.mtype,
crank,mpiT.tag_ps_recv_grad_tail,self.mworld,self.state)
end
end
coroutine.yield(mpiT.signal_DONE)
end
local function pServer_recvparam(self,crank)
coroutine.yield(mpiT.signal_INIT)
mpiT.aio_recv(self.storage.p,self.size,self.mtype,
crank,mpiT.tag_ps_recv_param,self.mworld,self.state)
if self.state.on then
mpiT.aio_send(self.emptys,0,self.mtype,
crank,mpiT.tag_ps_recv_param_tail,self.mworld,self.state)
end
--print('ps ' .. self.rank .. ' recv param from ' .. crank)
coroutine.yield(mpiT.signal_DONE)
end
local function pServer_recvparam_always(self,crank)
coroutine.yield(mpiT.signal_INIT)
while (self.state.on) do
mpiT.aio_recv(self.storage.p,self.size,self.mtype,
crank,mpiT.tag_ps_recv_param,self.mworld,self.state)
if self.state.on then
mpiT.aio_send(self.emptys,0,self.mtype,
crank,mpiT.tag_ps_recv_param_tail,self.mworld,self.state)
end
--print('ps ' .. self.rank .. ' recv param from ' .. crank)
end
coroutine.yield(mpiT.signal_DONE)
end
-- stop
function table.len(tbl)
local l=0
if tbl then
for k,v in pairs(tbl) do
l=l+1
end
end
return l
end
local function pServer_recvstop(self,crank)
coroutine.yield(mpiT.signal_INIT)
local tostop = torch.ByteStorage(1)
mpiT.aio_recv(tostop,1,mpiT.BYTE,
crank,mpiT.tag_ps_recv_stop,self.mworld,self.state)
if tostop[1] then
self.state.iostop = self.state.iostop + 1
if self.state.iostop == table.len(self.cranks) then
self.state.on = false
self.state.io = false
-- print('server', self.rank, 'finally stoped by', crank)
end
end
coroutine.yield(mpiT.signal_DONE)
end
function pServer:start()
self.state.on = true
self.state.io = true
-- init
self.coq:clear()
for i,crank in pairs(self.cranks) do
local co = mpiT.co_execute(pServer_recvinit,{self,crank})
self.coq:push(co)
end
mpiT.co_wait(self.coq)
for i,crank in pairs(self.cranks) do
if i == 1 then
-- init the parameter from the first local worker
local co3 = mpiT.co_execute(pServer_recvparam,{self,crank})
self.coq:push(co3)
mpiT.co_wait(self.coq)
end
if self.conf.opt.singlemode then
local co0 = mpiT.co_execute(pServer_recvstop,{self,crank})
if crank == 0 then
local co2 = mpiT.co_execute(pServer_recvparam_always,{self,crank})
self.coq:push(co2)
end
if crank == 2 then
local co2 = mpiT.co_execute(pServer_sendparam,{self,crank})
self.coq:push(co2)
end
else
-- on request
local co0 = mpiT.co_execute(pServer_recvstop,{self,crank})
local co1 = mpiT.co_execute(pServer_recvgrad,{self,crank})
local co2 = mpiT.co_execute(pServer_sendparam,{self,crank})
self.coq:push(co0)
self.coq:push(co1)
self.coq:push(co2)
end
end
mpiT.co_wait(self.coq)
end