diff --git a/paddle/fluid/framework/details/memory_optimize_pass.cc b/paddle/fluid/framework/details/memory_optimize_pass.cc index c426059a6a6ea640c3c13a667c42d56fbe4fb7c3..fabcd2ecd2b54961c80c40fbcf5dbeb9fe294684 100644 --- a/paddle/fluid/framework/details/memory_optimize_pass.cc +++ b/paddle/fluid/framework/details/memory_optimize_pass.cc @@ -128,7 +128,7 @@ std::unique_ptr MemoryOptimizePass::ApplyImpl( } } } - graph->ResolveHazard(var_nodes_); + // graph->ResolveHazard(var_nodes_); return graph; } @@ -324,6 +324,32 @@ void MemoryOptimizePass::RenameVarInGraphNode(const std::string& var, } } +void MemoryOptimizePass::ClearControlDepVars(ir::Graph* graph) const { + for (auto& op : graph->Nodes()) { + if (!op->IsOp()) continue; + { + auto& nodes = op->inputs; + nodes.erase( + std::remove_if(nodes.begin(), nodes.end(), + [&](ir::Node* var) { return var->IsCtrlVar(); }), + nodes.end()); + } + { + auto& nodes = op->outputs; + nodes.erase( + std::remove_if(nodes.begin(), nodes.end(), + [&](ir::Node* var) { return var->IsCtrlVar(); }), + nodes.end()); + } + } + + for (auto& node : graph->Nodes()) { + if (node->IsCtrlVar()) { + graph->RemoveNode(node); + } + } +} + } // namespace details } // namespace framework } // namespace paddle diff --git a/paddle/fluid/framework/details/memory_optimize_pass.h b/paddle/fluid/framework/details/memory_optimize_pass.h index 593ffc10fc99d26b1ee9174ceef081581126e7e8..f5d188101ffe60fb190702f15753491535f68a3a 100644 --- a/paddle/fluid/framework/details/memory_optimize_pass.h +++ b/paddle/fluid/framework/details/memory_optimize_pass.h @@ -48,6 +48,7 @@ class MemoryOptimizePass : public ir::Pass { void RenameVarInGraphNode(const std::string& var, const std::string& cache_var, size_t idx, ir::Graph* graph) const; + void ClearControlDepVars(ir::Graph* graph) const; void SubGraphOptimize(OpDesc* op_desc) const; // 1. scan op with subblock and collect the output/input vars. diff --git a/python/paddle/fluid/tests/unittests/test_fuse_elewise_add_act_pass.py b/python/paddle/fluid/tests/unittests/test_fuse_elewise_add_act_pass.py index 03471a4432f2b6bf6220e79e99aa506628b1535b..c1fb53ecf52d953fa470998c120930b2bec6325b 100644 --- a/python/paddle/fluid/tests/unittests/test_fuse_elewise_add_act_pass.py +++ b/python/paddle/fluid/tests/unittests/test_fuse_elewise_add_act_pass.py @@ -121,6 +121,8 @@ class TestMNIST(TestParallelExecutorBase): regularization=fluid.regularizer.L2Decay(1e-6)) return optimizer + # NOTE(dzh): + # need to make it compatible with elewise fuse act not_fuse_op_first_loss, not_fuse_op_last_loss = self.check_network_convergence( model, feed_dict={"image": img, @@ -128,6 +130,7 @@ class TestMNIST(TestParallelExecutorBase): use_cuda=use_cuda, fuse_elewise_add_act_ops=False, memory_opt=False, + use_ir_memory_optimize=False, optimizer=_optimizer) fuse_op_first_loss, fuse_op_last_loss = self.check_network_convergence( model, @@ -136,6 +139,7 @@ class TestMNIST(TestParallelExecutorBase): use_cuda=use_cuda, fuse_elewise_add_act_ops=True, memory_opt=False, + use_ir_memory_optimize=False, optimizer=_optimizer) for loss in zip(not_fuse_op_first_loss, fuse_op_first_loss):