Skip to content

Commit a2820b9

Browse files
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into accelerate_embedding_grad
2 parents a61879a + 18be725 commit a2820b9

File tree

10 files changed

+43
-24
lines changed

10 files changed

+43
-24
lines changed

paddle/fluid/framework/details/multi_devices_graph_pass.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,9 @@ std::vector<ir::Node *> SortOpsAndDelayOptimizeOp(const ir::Graph &graph) {
252252
std::vector<ir::Node *> sorted_ret;
253253
for (size_t i = 0; i < ret.size(); ++i) {
254254
if (i < last_backward) {
255-
if (boost::get<int>(ret[i]->Op()->GetAttr(
256-
OpProtoAndCheckerMaker::OpRoleAttrName())) ==
257-
static_cast<int>(OpRole::kOptimize)) {
255+
if (static_cast<bool>(boost::get<int>(ret[i]->Op()->GetAttr(
256+
OpProtoAndCheckerMaker::OpRoleAttrName())) &
257+
static_cast<int>(OpRole::kOptimize))) {
258258
optimize_ops.push_back(ret[i]);
259259
} else {
260260
sorted_ret.push_back(ret[i]);

paddle/fluid/framework/op_proto_maker.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ void OpProtoAndCheckerMaker::operator()(proto::OpProto* proto,
7171
static_cast<int>(OpRole::kLoss) | static_cast<int>(OpRole::kForward),
7272
static_cast<int>(OpRole::kLoss) |
7373
static_cast<int>(OpRole::kBackward),
74+
static_cast<int>(OpRole::kOptimize) |
75+
static_cast<int>(OpRole::kLRSched),
7476
static_cast<int>(OpRole::kNotSpecified)})
7577
.SetDefault(static_cast<int>(OpRole::kNotSpecified));
7678
AddAttr<std::vector<std::string>>(OpRoleVarAttrName(),

paddle/fluid/framework/op_proto_maker.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ limitations under the License. */
2020
namespace paddle {
2121
namespace framework {
2222

23+
//////////////////////////
24+
// Don't add more roles to make this too complicated!
25+
//////////////////////////
2326
enum class OpRole {
2427
kForward = 0x0000,
2528
kBackward = 0x0001,

paddle/fluid/framework/parallel_executor.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,6 @@ ParallelExecutor::ParallelExecutor(
156156
params, member_->local_scopes_, member_->use_cuda_);
157157
#endif
158158

159-
// If the loss_var_name is given, the number of graph should be only one.
160-
if (loss_var_name.size()) {
161-
PADDLE_ENFORCE_EQ(ir::GraphNum(*graph), 1,
162-
"The number of graph should be only one");
163-
}
164-
165159
if (exec_strategy.type_ == ExecutionStrategy::kDefault) {
166160
member_->executor_.reset(new details::ThreadedSSAGraphExecutor(
167161
exec_strategy, member_->local_scopes_, places, std::move(graph)));

python/paddle/fluid/clip.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ def append_gradient_clip_ops(param_grads):
333333
for p, g in param_grads:
334334
if g is None:
335335
continue
336-
with p.block.program._optimized_guard([p, g]):
336+
with p.block.program._optimized_guard(
337+
[p, g]), framework.name_scope('append_clip'):
337338
clip_attr = getattr(p, 'gradient_clip_attr', NullGradientClipAttr())
338339
if clip_attr is None:
339340
clip_attr = NullGradientClipAttr()
@@ -348,7 +349,8 @@ def append_gradient_clip_ops(param_grads):
348349
for p, g in param_grads:
349350
if g is None:
350351
continue
351-
with p.block.program._optimized_guard([p, g]):
352+
with p.block.program._optimized_guard(
353+
[p, g]), framework.name_scope('append_graident_clip'):
352354
res.append(clip_attr._create_operators(param=p, grad=g))
353355

354356
return res

python/paddle/fluid/framework.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,25 +1496,32 @@ def _optimized_guard(self, param_and_grads):
14961496
>>> with program._optimized_guard([p,g]):
14971497
>>> p = p - 0.001 * g
14981498
"""
1499+
tmp_role = self._current_role
1500+
tmp_var = self._op_role_var
1501+
14991502
OpRole = core.op_proto_and_checker_maker.OpRole
15001503
self._current_role = OpRole.Optimize
15011504
self._op_role_var = [
15021505
var.name if isinstance(var, Variable) else var
15031506
for var in param_and_grads
15041507
]
15051508
yield
1506-
self._op_role_var = []
1507-
self._current_role = OpRole.Forward
1509+
self._op_role_var = tmp_var
1510+
self._current_role = tmp_role
15081511

15091512
@contextlib.contextmanager
1510-
def _lr_schedule_guard(self):
1513+
def _lr_schedule_guard(self, is_with_opt=False):
15111514
"""
15121515
A with guard to set :code:`LRSched` :code:`OpRole` and
15131516
:code:`OpRoleVar` automatically. The :code:`OpRoleVar` is
15141517
set to the target learning rate.
15151518
15161519
Notes: This is a very low level API. Users should not use it directly.
15171520
1521+
Args:
1522+
is_with_opt: Only set to true if these ops a in the middle
1523+
of a bunch of optimize ops so that it can be treated
1524+
correctly. For example, sgd->lr_op->sgd->lr_op->sgd.
15181525
15191526
Examples:
15201527
@@ -1528,6 +1535,8 @@ def _lr_schedule_guard(self):
15281535

15291536
OpRole = core.op_proto_and_checker_maker.OpRole
15301537
self._current_role = OpRole.LRSched
1538+
if is_with_opt:
1539+
self._current_role = int(OpRole.LRSched) | int(OpRole.Optimize)
15311540
# TODO(typhoonzero): how to set target learning rate var
15321541
self._op_role_var = []
15331542
yield

python/paddle/fluid/optimizer.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def _create_param_lr(self, param_and_grad):
111111
if param_lr == 1.0:
112112
return self._global_learning_rate()
113113
else:
114-
with default_main_program()._lr_schedule_guard():
114+
with default_main_program()._lr_schedule_guard(
115+
is_with_opt=True), framework.name_scope(
116+
'scale_with_param_lr'):
115117
return self._global_learning_rate() * param_lr
116118

117119
def _create_accumulators(self, block, parameters):
@@ -602,7 +604,8 @@ def _finish_update(self, block, param_and_grads):
602604
for param, grad in param_and_grads:
603605
if grad is None:
604606
continue
605-
with param.block.program._optimized_guard([param, grad]):
607+
with param.block.program._optimized_guard(
608+
[param, grad]), name_scope("optimizer"):
606609
beta1_pow_acc = self._get_accumulator(self._beta1_pow_acc_str,
607610
param)
608611
beta2_pow_acc = self._get_accumulator(self._beta2_pow_acc_str,
@@ -740,7 +743,8 @@ def _finish_update(self, block, parameters_and_grads):
740743
for param, grad in parameters_and_grads:
741744
if grad is None:
742745
continue
743-
with param.block.program._optimized_guard([param, grad]):
746+
with param.block.program._optimized_guard(
747+
[param, grad]), name_scope('adamx'):
744748
beta1_pow_acc = self._get_accumulator(self._beta1_pow_acc_str,
745749
param)
746750
main_block.append_op(
@@ -1279,7 +1283,8 @@ def __init__(self,
12791283
for param, grad in self.params_grads:
12801284
if grad is None:
12811285
continue
1282-
with param.block.program._optimized_guard([param, grad]):
1286+
with param.block.program._optimized_guard(
1287+
[param, grad]), name_scope('move_average'):
12831288
self._append_average_accumulate_op(param)
12841289

12851290
self.apply_program = Program()

python/paddle/fluid/regularizer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def append_regularization_ops(parameters_and_grads, regularization=None):
4747
if grad is None:
4848
params_and_grads.append((param, grad))
4949
continue
50-
with param.block.program._optimized_guard([param, grad]):
50+
with param.block.program._optimized_guard(
51+
[param, grad]), framework.name_scope('regularization'):
5152
regularization_term = None
5253
if param.regularizer is not None:
5354
# Add variable for regularization term in grad block

python/paddle/fluid/tests/unittests/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ if(WITH_DISTRIBUTE)
7878
set_tests_properties(test_dist_word2vec PROPERTIES TIMEOUT 200)
7979
py_test_modules(test_dist_se_resnext MODULES test_dist_se_resnext)
8080
set_tests_properties(test_dist_se_resnext PROPERTIES TIMEOUT 1000)
81-
82-
py_test_modules(test_dist_transformer MODULES test_dist_transformer)
83-
set_tests_properties(test_dist_transformer PROPERTIES TIMEOUT 1000)
81+
# FIXME(typhoonzero): add this back
82+
#py_test_modules(test_dist_transformer MODULES test_dist_transformer)
83+
#set_tests_properties(test_dist_transformer PROPERTIES TIMEOUT 1000)
8484
endif(NOT APPLE)
8585
py_test_modules(test_dist_transpiler MODULES test_dist_transpiler)
8686
endif()

python/paddle/fluid/transpiler/distribute_transpiler.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
OP_ROLE_VAR_ATTR_NAME = core.op_proto_and_checker_maker.kOpRoleVarAttrName()
5050
RPC_OP_ROLE_ATTR_NAME = op_role_attr_name = core.op_proto_and_checker_maker.kOpRoleAttrName(
5151
)
52+
OPT_OP_ROLE_ATTR_VALUE = core.op_proto_and_checker_maker.OpRole.Optimize
5253
RPC_OP_ROLE_ATTR_VALUE = core.op_proto_and_checker_maker.OpRole.RPC
5354
DIST_OP_ROLE_ATTR_VALUE = core.op_proto_and_checker_maker.OpRole.Dist
5455
LR_SCHED_OP_ROLE_ATTR_VALUE = core.op_proto_and_checker_maker.OpRole.LRSched
@@ -1717,8 +1718,10 @@ def _get_lr_ops(self):
17171718
lr_ops = []
17181719
block = self.origin_program.global_block()
17191720
for op in block.ops:
1720-
if int(op.attr(RPC_OP_ROLE_ATTR_NAME)) == int(
1721-
LR_SCHED_OP_ROLE_ATTR_VALUE):
1721+
role_id = int(op.attr(RPC_OP_ROLE_ATTR_NAME))
1722+
if role_id == int(LR_SCHED_OP_ROLE_ATTR_VALUE) or \
1723+
role_id == int(LR_SCHED_OP_ROLE_ATTR_VALUE) | \
1724+
int(OPT_OP_ROLE_ATTR_VALUE):
17221725
lr_ops.append(op)
17231726
log("append lr op: ", op.type)
17241727
return lr_ops

0 commit comments

Comments
 (0)