Skip to content

Commit 19639e3

Browse files
authored
Merge pull request #12254 from JiayiFeng/fix_lr_decay
Fix learning rate scheduler performance issue
2 parents 7c046ae + 4cba550 commit 19639e3

File tree

2 files changed

+59
-65
lines changed

2 files changed

+59
-65
lines changed

python/paddle/fluid/layers/learning_rate_scheduler.py

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ def noam_decay(d_model, warmup_steps):
6262
The decayed learning rate.
6363
"""
6464
global_step = _decay_step_counter(1)
65-
with init_on_cpu():
66-
a = global_step**-0.5
67-
b = (warmup_steps**-1.5) * global_step
68-
lr_value = (d_model**-0.5) * ops.elementwise_min(a, b)
65+
66+
a = global_step**-0.5
67+
b = (warmup_steps**-1.5) * global_step
68+
lr_value = (d_model**-0.5) * ops.elementwise_min(a, b)
6969

7070
return lr_value
7171

@@ -108,12 +108,10 @@ def exponential_decay(learning_rate, decay_steps, decay_rate, staircase=False):
108108
"""
109109
global_step = _decay_step_counter()
110110

111-
with init_on_cpu():
112-
# update learning_rate
113-
div_res = global_step / decay_steps
114-
if staircase:
115-
div_res = ops.floor(div_res)
116-
decayed_lr = learning_rate * (decay_rate**div_res)
111+
div_res = global_step / decay_steps
112+
if staircase:
113+
div_res = ops.floor(div_res)
114+
decayed_lr = learning_rate * (decay_rate**div_res)
117115

118116
return decayed_lr
119117

@@ -138,11 +136,10 @@ def natural_exp_decay(learning_rate, decay_steps, decay_rate, staircase=False):
138136
"""
139137
global_step = _decay_step_counter()
140138

141-
with init_on_cpu():
142-
div_res = global_step / decay_steps
143-
if staircase:
144-
div_res = ops.floor(div_res)
145-
decayed_lr = learning_rate * ops.exp(-1 * decay_rate * div_res)
139+
div_res = global_step / decay_steps
140+
if staircase:
141+
div_res = ops.floor(div_res)
142+
decayed_lr = learning_rate * ops.exp(-1 * decay_rate * div_res)
146143

147144
return decayed_lr
148145

@@ -184,12 +181,11 @@ def inverse_time_decay(learning_rate, decay_steps, decay_rate, staircase=False):
184181
"""
185182
global_step = _decay_step_counter()
186183

187-
with init_on_cpu():
188-
div_res = global_step / decay_steps
189-
if staircase:
190-
div_res = ops.floor(div_res)
184+
div_res = global_step / decay_steps
185+
if staircase:
186+
div_res = ops.floor(div_res)
191187

192-
decayed_lr = learning_rate / (1 + decay_rate * div_res)
188+
decayed_lr = learning_rate / (1 + decay_rate * div_res)
193189

194190
return decayed_lr
195191

@@ -224,25 +220,22 @@ def polynomial_decay(learning_rate,
224220
"""
225221
global_step = _decay_step_counter()
226222

227-
with init_on_cpu():
228-
if cycle:
229-
div_res = ops.ceil(global_step / decay_steps)
230-
zero_var = tensor.fill_constant(
231-
shape=[1], dtype='float32', value=0.0)
232-
one_var = tensor.fill_constant(
233-
shape=[1], dtype='float32', value=1.0)
234-
235-
with control_flow.Switch() as switch:
236-
with switch.case(global_step == zero_var):
237-
tensor.assign(input=one_var, output=div_res)
238-
decay_steps = decay_steps * div_res
239-
else:
240-
decay_steps_var = tensor.fill_constant(
241-
shape=[1], dtype='float32', value=float(decay_steps))
242-
global_step = ops.elementwise_min(x=global_step, y=decay_steps_var)
223+
if cycle:
224+
div_res = ops.ceil(global_step / decay_steps)
225+
zero_var = tensor.fill_constant(shape=[1], dtype='float32', value=0.0)
226+
one_var = tensor.fill_constant(shape=[1], dtype='float32', value=1.0)
243227

244-
decayed_lr = (learning_rate - end_learning_rate) * \
245-
((1 - global_step / decay_steps) ** power) + end_learning_rate
228+
with control_flow.Switch() as switch:
229+
with switch.case(global_step == zero_var):
230+
tensor.assign(input=one_var, output=div_res)
231+
decay_steps = decay_steps * div_res
232+
else:
233+
decay_steps_var = tensor.fill_constant(
234+
shape=[1], dtype='float32', value=float(decay_steps))
235+
global_step = ops.elementwise_min(x=global_step, y=decay_steps_var)
236+
237+
decayed_lr = (learning_rate - end_learning_rate) * \
238+
((1 - global_step / decay_steps) ** power) + end_learning_rate
246239
return decayed_lr
247240

248241

@@ -277,28 +270,28 @@ def piecewise_decay(boundaries, values):
277270

278271
global_step = _decay_step_counter()
279272

280-
with init_on_cpu():
281-
lr = tensor.create_global_var(
282-
shape=[1],
283-
value=0.0,
284-
dtype='float32',
285-
persistable=True,
286-
name="learning_rate")
273+
lr = tensor.create_global_var(
274+
shape=[1],
275+
value=0.0,
276+
dtype='float32',
277+
persistable=True,
278+
name="learning_rate")
287279

288-
with control_flow.Switch() as switch:
289-
for i in range(len(boundaries)):
290-
boundary_val = tensor.fill_constant(
291-
shape=[1], dtype='float32', value=float(boundaries[i]))
292-
value_var = tensor.fill_constant(
293-
shape=[1], dtype='float32', value=float(values[i]))
294-
with switch.case(global_step < boundary_val):
295-
tensor.assign(value_var, lr)
296-
last_value_var = tensor.fill_constant(
280+
with control_flow.Switch() as switch:
281+
for i in range(len(boundaries)):
282+
boundary_val = tensor.fill_constant(
297283
shape=[1],
298284
dtype='float32',
299-
value=float(values[len(values) - 1]))
300-
with switch.default():
301-
tensor.assign(last_value_var, lr)
285+
value=float(boundaries[i]),
286+
force_cpu=True)
287+
value_var = tensor.fill_constant(
288+
shape=[1], dtype='float32', value=float(values[i]))
289+
with switch.case(global_step < boundary_val):
290+
tensor.assign(value_var, lr)
291+
last_value_var = tensor.fill_constant(
292+
shape=[1], dtype='float32', value=float(values[len(values) - 1]))
293+
with switch.default():
294+
tensor.assign(last_value_var, lr)
302295

303296
return lr
304297

@@ -333,9 +326,9 @@ def _balanced_weight(param_norm, grad_norm):
333326
grad_norm = ops.sqrt(nn.reduce_sum(input=ops.square(grad)))
334327
if type(param_lr) == float and param_lr == 1.0:
335328
decayed_lr = learning_rate * param_norm \
336-
/ _balanced_weight(param_norm, grad_norm)
329+
/ _balanced_weight(param_norm, grad_norm)
337330
else:
338331
decayed_lr = learning_rate * param_lr * param_norm \
339-
/ _balanced_weight(param_norm, grad_norm)
332+
/ _balanced_weight(param_norm, grad_norm)
340333
# set back param local learning rate
341334
param.optimize_attr['learning_rate'] = decayed_lr

python/paddle/fluid/tests/unittests/test_learning_rate_scheduler.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,21 @@ def check_decay(self, python_decay_fn, fluid_decay_fn, kwargs):
9191

9292
def check_decay_with_place(self, place, python_decay_fn, fluid_decay_fn,
9393
kwargs):
94+
main_prog = fluid.Program()
95+
startup_prog = fluid.Program()
9496

95-
decayed_lr = fluid_decay_fn(**kwargs)
97+
with fluid.program_guard(main_prog, startup_prog):
98+
decayed_lr = fluid_decay_fn(**kwargs)
9699

97100
place = fluid.CPUPlace()
98101
exe = fluid.Executor(place)
99102

100-
exe.run(fluid.default_startup_program())
103+
exe.run(startup_prog)
101104

102-
fluid.memory_optimize(fluid.default_main_program())
105+
fluid.memory_optimize(main_prog)
103106

104107
for step in range(10):
105-
lr_val, = exe.run(fluid.default_main_program(),
106-
feed={},
107-
fetch_list=[decayed_lr])
108+
lr_val, = exe.run(main_prog, feed={}, fetch_list=[decayed_lr])
108109
python_decayed_lr = python_decay_fn(
109110
global_step=float(step), **kwargs)
110111
self.assertAlmostEqual(

0 commit comments

Comments
 (0)