未验证 提交 c194b0c8 编写于 作者: Z Zeng Jinle 提交者: GitHub

Try to deprecate unstable python memory optimize (#18983)

* deprecate python memory optimize, test=develop

* remove memory_optimize in unittests, test=develop

* add unittests to deprecated interfaces, test=develop
上级 5a80cc84
......@@ -102,8 +102,6 @@ class TestDistSaveLoad2x2(TestDistSimnetBow2x2):
test_program, avg_cost, train_reader, test_reader, batch_acc, predict = \
self.get_model(batch_size=2)
if args.mem_opt:
fluid.memory_optimize(fluid.default_main_program(), skip_grads=True)
if args.update_method == "pserver":
t = self.get_transpiler(args.trainer_id,
fluid.default_main_program(),
......
......@@ -43,7 +43,6 @@ class BuildIrMemOptBase(unittest.TestCase):
def check_network_convergence(self,
network,
use_cuda=True,
memory_opt=True,
use_ir_memory_optimize=True,
enable_inplace=True,
iter=5):
......@@ -68,13 +67,8 @@ class BuildIrMemOptBase(unittest.TestCase):
optimizer = fluid.optimizer.Adam(learning_rate=0.001)
optimizer.minimize(cost)
build_strategy = fluid.BuildStrategy()
build_strategy.enable_inplace = False
build_strategy.memory_optimize = False
if memory_opt:
fluid.memory_optimize(fluid.default_main_program())
else:
build_strategy.enable_inplace = use_ir_memory_optimize
build_strategy.memory_optimize = enable_inplace
build_strategy.enable_inplace = enable_inplace
build_strategy.memory_optimize = use_ir_memory_optimize
# execution
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
......@@ -134,7 +128,7 @@ class TestIrMemOptBase(BuildIrMemOptBase):
self.network)
cur_first_loss, cur_last_loss = self.check_network_convergence(
self.network, memory_opt=False)
self.network)
self.assertAlmostEquals(
np.mean(baseline_last_loss),
......
......@@ -33,7 +33,6 @@ class TestParallelExecutorBase(unittest.TestCase):
def check_network_convergence(cls,
method,
use_cuda=True,
memory_opt=False,
iter=50,
batch_size=None,
feed_dict=None,
......@@ -59,8 +58,7 @@ class TestParallelExecutorBase(unittest.TestCase):
main.random_seed = 1
with fluid.program_guard(main, startup):
feed_dict, loss = cls.build_model(feed_dict, get_data_from_feeder,
main, memory_opt, method,
optimizer)
main, method, optimizer)
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)
......@@ -112,7 +110,6 @@ class TestParallelExecutorBase(unittest.TestCase):
def check_pass_conflict(cls,
method,
use_cuda=True,
memory_opt=False,
feed_dict=None,
get_data_from_feeder=None,
use_reduce=False,
......@@ -130,8 +127,7 @@ class TestParallelExecutorBase(unittest.TestCase):
startup = fluid.Program()
with fluid.program_guard(main, startup):
feed_dict, loss = cls.build_model(feed_dict, get_data_from_feeder,
main, memory_opt, method,
optimizer)
main, method, optimizer)
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)
......@@ -175,16 +171,17 @@ class TestParallelExecutorBase(unittest.TestCase):
return build_strategy, exec_strategy
@classmethod
def build_model(cls, feed_dict, get_data_from_feeder, main, memory_opt,
method, optimizer):
def build_model(cls, feed_dict, get_data_from_feeder, main, method,
optimizer):
loss = method(use_feed=feed_dict is not None)
# NOTE(zjl): memory_optimize/inplace pass would not require
# that loss.persistable = True
loss.persistable = memory_opt
# that loss.persistable = True.
# We set loss.persistable = False here to verify our memory
# optimization strategies intentionally.
loss.persistable = False
if optimizer:
optimizer().minimize(loss)
if memory_opt:
fluid.memory_optimize(main)
if get_data_from_feeder is not None:
assert feed_dict is None
feed_dict = get_data_from_feeder()
......
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import paddle.fluid as fluid
import unittest
from simple_nets import simple_fc_net
class DeprecatedMemoryOptimizationInterfaceTest(unittest.TestCase):
def setUp(self):
self.method = fluid.memory_optimize
def build_network(self, call_interface):
startup_prog = fluid.Program()
main_prog = fluid.Program()
with fluid.program_guard(main_prog, startup_prog):
with fluid.unique_name.guard():
loss = simple_fc_net()
opt = fluid.optimizer.Adam(learning_rate=1e-3)
opt.minimize(loss)
if call_interface:
self.method(main_prog)
return main_prog
def assert_program_equal(self, prog1, prog2):
block_num = prog1.num_blocks
self.assertEquals(block_num, prog2.num_blocks)
for block_id in range(block_num):
block1 = prog1.block(block_id)
block2 = prog2.block(block_id)
self.assertEquals(len(block1.ops), len(block2.ops))
for op1, op2 in zip(block1.ops, block2.ops):
self.assertEquals(op1.input_arg_names, op2.input_arg_names)
self.assertEquals(op1.output_arg_names, op2.output_arg_names)
self.assertEquals(len(block1.vars), len(block2.vars))
for var1 in block1.vars.values():
self.assertTrue(var1.name in block2.vars)
var2 = block2.vars.get(var1.name)
self.assertEquals(var1.name, var2.name)
def test_main(self):
prog1 = self.build_network(False)
prog2 = self.build_network(True)
self.assert_program_equal(prog1, prog2)
class ReleaseMemoryTest(DeprecatedMemoryOptimizationInterfaceTest):
def setUp(self):
self.method = fluid.release_memory
if __name__ == '__main__':
unittest.main()
......@@ -108,10 +108,6 @@ class TestDistRunnerBase(object):
test_program, avg_cost, train_reader, test_reader, batch_acc, predict = \
self.get_model(batch_size=args.batch_size)
if args.mem_opt:
my_print(type(self).__name__, "begin to run memory optimize")
fluid.memory_optimize(fluid.default_main_program(), skip_grads=True)
my_print(type(self).__name__, "trainer run memory optimize done.")
if args.update_method == "pserver":
my_print(
type(self).__name__,
......@@ -327,7 +323,6 @@ def runtime_main(test_class):
parser.add_argument(
'--current_endpoint', type=str, required=False, default="")
parser.add_argument('--sync_mode', action='store_true')
parser.add_argument('--mem_opt', action='store_true')
parser.add_argument('--use_cuda', action='store_true')
parser.add_argument('--use_dgc', action='store_true')
parser.add_argument('--use_reduce', action='store_true')
......@@ -387,7 +382,6 @@ class TestDistBase(unittest.TestCase):
self._python_interp = sys.executable
self._sync_mode = True
self._enforce_place = None
self._mem_opt = False
self._use_reduce = False
self._dc_asgd = False # must use with async mode
self._use_reader_alloc = True
......@@ -435,9 +429,6 @@ class TestDistBase(unittest.TestCase):
if self._sync_mode:
ps0_cmd += " --sync_mode"
ps1_cmd += " --sync_mode"
if self._mem_opt:
ps0_cmd += " --mem_opt"
ps1_cmd += " --mem_opt"
print(ps0_cmd)
print(ps1_cmd)
......@@ -530,9 +521,6 @@ class TestDistBase(unittest.TestCase):
if self._sync_mode:
tr0_cmd += " --sync_mode"
tr1_cmd += " --sync_mode"
if self._mem_opt:
tr0_cmd += " --mem_opt"
tr1_cmd += " --mem_opt"
if self._use_reduce:
tr0_cmd += " --use_reduce"
tr1_cmd += " --use_reduce"
......@@ -603,8 +591,6 @@ class TestDistBase(unittest.TestCase):
(self._python_interp, model, self._ps_endpoints,
trainer_id, ep, update_method, self._lr)
if self._mem_opt:
tr_cmd += " --mem_opt"
if self._use_reduce:
tr_cmd += " --use_reduce"
if self._use_reader_alloc:
......
......@@ -49,7 +49,6 @@ class TestFuseAllReduceOpsBase(TestParallelExecutorBase):
use_cuda=use_cuda,
fuse_all_reduce_ops=False,
fuse_all_optimizer_ops=fuse_all_optimizer_ops,
memory_opt=False,
optimizer=optimizer)
fuse_op_first_loss, fuse_op_last_loss = self.check_network_convergence(
model,
......@@ -58,7 +57,6 @@ class TestFuseAllReduceOpsBase(TestParallelExecutorBase):
use_cuda=use_cuda,
fuse_all_reduce_ops=True,
fuse_all_optimizer_ops=fuse_all_optimizer_ops,
memory_opt=False,
optimizer=optimizer)
for loss in zip(not_fuse_op_first_loss, fuse_op_first_loss):
......
......@@ -47,7 +47,6 @@ class TestMNIST(TestParallelExecutorBase):
"label": label},
use_cuda=use_cuda,
fuse_elewise_add_act_ops=False,
memory_opt=False,
use_ir_memory_optimize=False,
enable_inplace=False,
optimizer=_optimizer)
......@@ -57,7 +56,6 @@ class TestMNIST(TestParallelExecutorBase):
"label": label},
use_cuda=use_cuda,
fuse_elewise_add_act_ops=True,
memory_opt=False,
use_ir_memory_optimize=False,
enable_inplace=False,
optimizer=_optimizer)
......
......@@ -46,7 +46,6 @@ class TestFuseOptimizationOps(TestParallelExecutorBase):
get_data_from_feeder=get_data_from_feeder,
use_cuda=use_cuda,
fuse_all_optimizer_ops=False,
memory_opt=False, # avoid the gradient's name changed in Python side.
optimizer=optimizer)
fuse_op_first_loss, fuse_op_last_loss = self.check_network_convergence(
model,
......@@ -54,7 +53,6 @@ class TestFuseOptimizationOps(TestParallelExecutorBase):
get_data_from_feeder=get_data_from_feeder,
use_cuda=use_cuda,
fuse_all_optimizer_ops=True,
memory_opt=False, # avoid the gradient's name changed in Python side.
optimizer=optimizer)
for loss in zip(not_fuse_op_first_loss, fuse_op_first_loss):
......@@ -152,7 +150,6 @@ class TestPassConflictBase(TestFuseAdamOps):
get_data_from_feeder=get_data_from_feeder,
use_cuda=use_cuda,
fuse_all_optimizer_ops=True,
memory_opt=False, # avoid the gradient's name changed in Python side.
optimizer=optimizer,
enable_sequential_execution=True)
......
......@@ -120,7 +120,6 @@ class TestMNIST(TestParallelExecutorBase):
use_cuda=use_cuda,
fuse_relu_depthwise_conv=True,
use_ir_memory_optimize=True,
memory_opt=False,
optimizer=_optimizer)
not_fuse_op_first_loss, not_fuse_op_last_loss = self.check_network_convergence(
model,
......@@ -128,7 +127,6 @@ class TestMNIST(TestParallelExecutorBase):
"label": label},
use_cuda=use_cuda,
fuse_relu_depthwise_conv=False,
memory_opt=False,
optimizer=_optimizer)
for loss in zip(not_fuse_op_first_loss, fuse_op_first_loss):
......
......@@ -109,9 +109,6 @@ class TestSaveInferenceModel(unittest.TestCase):
exe = executor.Executor(place)
exe.run(init_program, feed={}, fetch_list=[])
memory_optimize(program, print_log=True)
self.assertEqual(program._is_mem_optimized, True)
# will print warning message
save_inference_model(MODEL_DIR, ["x", "y"], [avg_cost], exe, program)
......
......@@ -47,10 +47,7 @@ class TestIrInplace(TestParallelExecutorBase):
def setUpClass(cls):
os.environ['CPU_NUM'] = str(4)
def _fc_with_batchnorm(self,
ir_memory_optimize,
enable_inplace,
memory_opt=False):
def _fc_with_batchnorm(self, ir_memory_optimize, enable_inplace):
if not core.is_compiled_with_cuda():
return
......@@ -62,7 +59,6 @@ class TestIrInplace(TestParallelExecutorBase):
feed_dict={"image": img,
"label": label},
use_cuda=True,
memory_opt=memory_opt,
use_ir_memory_optimize=ir_memory_optimize,
enable_inplace=enable_inplace)
......
......@@ -33,7 +33,9 @@ from ir_memory_optimize_net_base import TestIrMemOptBase
class TestIrMemoryOptimizeIfElseOp(unittest.TestCase):
def check_network_convergence(self, use_cuda=True, py_opt=False,
def check_network_convergence(self,
use_cuda=True,
use_mem_opt=False,
iter_num=5):
prog = Program()
startup_prog = Program()
......@@ -75,11 +77,14 @@ class TestIrMemoryOptimizeIfElseOp(unittest.TestCase):
exec_strategy = fluid.ExecutionStrategy()
exec_strategy.use_cuda = use_cuda
if py_opt:
fluid.memory_optimize(fluid.default_main_program())
build_strategy = fluid.BuildStrategy()
build_strategy.memory_optimize = use_mem_opt
train_cp = compiler.CompiledProgram(fluid.default_main_program())
train_cp = train_cp.with_data_parallel(
loss_name=avg_loss.name, exec_strategy=exec_strategy)
loss_name=avg_loss.name,
exec_strategy=exec_strategy,
build_strategy=build_strategy)
fetch_list = [avg_loss.name]
exe.run(startup_prog)
......@@ -116,7 +121,6 @@ class TestIrMemoryOptimizeIfElseOp(unittest.TestCase):
ret2 = self.check_network_convergence(True, False)
print(ret2)
self.assertTrue(np.allclose(ret1, ret2))
#self.assertEqual(ret1, ret2)
if __name__ == "__main__":
......
......@@ -86,7 +86,7 @@ class TestMNIST(TestParallelExecutorBase):
label = np.ones(shape=[32, 1], dtype='int64')
return img, label
def _compare_ir_and_python_memory_optimize(self, model, use_cuda):
def _compare_ir_memory_optimize(self, model, use_cuda):
if use_cuda and not core.is_compiled_with_cuda():
return
......@@ -96,14 +96,12 @@ class TestMNIST(TestParallelExecutorBase):
feed_dict={"image": img,
"label": label},
use_cuda=use_cuda,
memory_opt=False,
use_ir_memory_optimize=False)
first_loss1, last_loss1 = self.check_network_convergence(
model,
feed_dict={"image": img,
"label": label},
use_cuda=use_cuda,
memory_opt=False,
use_ir_memory_optimize=True)
for loss in zip(first_loss0, first_loss1):
self.assertAlmostEqual(loss[0], loss[1], delta=1e-6)
......@@ -111,12 +109,12 @@ class TestMNIST(TestParallelExecutorBase):
self.assertAlmostEqual(loss[0], loss[1], delta=1e-6)
def test_simple_fc_net(self):
self._compare_ir_and_python_memory_optimize(simple_fc_net, False)
self._compare_ir_and_python_memory_optimize(simple_fc_net, True)
self._compare_ir_memory_optimize(simple_fc_net, False)
self._compare_ir_memory_optimize(simple_fc_net, True)
def test_fc_with_reshape_net(self):
self._compare_ir_and_python_memory_optimize(fc_with_inplace_net, False)
self._compare_ir_and_python_memory_optimize(fc_with_inplace_net, True)
self._compare_ir_memory_optimize(fc_with_inplace_net, False)
self._compare_ir_memory_optimize(fc_with_inplace_net, True)
if __name__ == '__main__':
......
......@@ -57,16 +57,11 @@ class TestTransformerWithIR(TestParallelExecutorBase):
self.check_network_convergence(
transformer,
use_cuda=True,
memory_opt=True,
use_ir_memory_optimize=False,
iter=2)
# check IR memory optimize
self.check_network_convergence(
transformer,
use_cuda=True,
memory_opt=False,
use_ir_memory_optimize=True,
iter=2)
transformer, use_cuda=True, use_ir_memory_optimize=True, iter=2)
if __name__ == '__main__':
......
......@@ -111,8 +111,6 @@ class TestLearningRateDecay(unittest.TestCase):
exe.run(startup_prog)
fluid.memory_optimize(main_prog)
for step in range(10):
lr_val, = exe.run(main_prog, feed={}, fetch_list=[decayed_lr])
python_decayed_lr = python_decay_fn(
......
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import unittest
import paddle.fluid as fluid
import paddle.fluid.layers as layers
import paddle.fluid.optimizer as optimizer
from paddle.fluid.framework import Program, program_guard
from paddle.fluid.transpiler import memory_optimize
def _get_vars(prog):
assert (isinstance(prog, Program))
all_vars = set()
for op in prog.global_block().ops:
all_vars.update(op.input_arg_names)
all_vars.update(op.output_arg_names)
return all_vars
class TestControlFlowGraph(unittest.TestCase):
def setUp(self):
program = Program()
with program_guard(program, startup_program=Program()):
x = layers.data(name='x', shape=[13], dtype='float32')
y_predict = layers.fc(input=x, size=1, act=None)
y = layers.data(name='y', shape=[1], dtype='float32')
cost = layers.square_error_cost(input=y_predict, label=y)
avg_cost = layers.mean(cost)
opt = optimizer.SGD(learning_rate=0.001)
opt = opt.minimize(avg_cost)
self.program = program
def test_control_flow_graph(self):
result_program = self.program.clone()
memory_optimize(self.program)
old_vars = _get_vars(self.program)
new_vars = _get_vars(result_program)
self.assertTrue(old_vars != new_vars)
class TestMemoryTranspiler2(unittest.TestCase):
def setUp(self):
program = Program()
with program_guard(program, startup_program=Program()):
x = layers.data(name='x', shape=[13], dtype='float32')
fc = layers.fc(input=x, size=10, act=None)
reshape = layers.reshape(x=fc, shape=[-1, 2, 5])
fc = layers.reshape(x=reshape, shape=[-1, 5, 2])
y_predict = layers.fc(input=fc, size=1, act=None)
y = layers.data(name='y', shape=[1], dtype='float32')
cost = layers.square_error_cost(input=y_predict, label=y)
avg_cost = layers.mean(cost)
opt = optimizer.SGD(learning_rate=0.001)
opt.minimize(avg_cost)
self.skip_set = set([cost.name, fc.name])
self.program = program
def test_inplace_ops(self):
result_program = self.program.clone()
memory_optimize(self.program)
old_vars = _get_vars(self.program)
new_vars = _get_vars(result_program)
self.assertTrue(old_vars != new_vars)
def test_skip_opt(self):
result_program = self.program.clone()
memory_optimize(self.program, skip_opt_set=self.skip_set)
old_vars = _get_vars(self.program)
new_vars = _get_vars(result_program)
self.assertTrue(old_vars != new_vars)
class TestMemoryTranspiler3(unittest.TestCase):
def setUp(self):
program = Program()
with program_guard(program, startup_program=Program()):
word = fluid.layers.data(name='word', shape=[1], dtype='int64')
emb = [
fluid.layers.embedding(
word, size=[65536, 256], param_attr='emb') for _ in range(6)
]
left = emb.pop(0)
while len(emb) != 0:
right = emb.pop(0)
left = fluid.layers.concat([left, right])
emb = fluid.layers.mean(left)
fluid.backward.append_backward(emb)
self.program = program
def test_cascade_reuse(self):
block = self.program.block(0)
# variable reuse in programdesc
# TODO(dzhwinter): confirm cascade strategy. disable temporialy
self.assertTrue("concat_4.tmp_0@GRAD" in block.vars)
# self.assertTrue("concat_3.tmp_0@GRAD" not in block.vars)
# self.assertTrue("concat_2.tmp_0@GRAD" not in block.vars)
# self.assertTrue("concat_1.tmp_0@GRAD" not in block.vars)
# self.assertTrue("concat_0.tmp_0@GRAD" not in block.vars)
if __name__ == "__main__":
unittest.main()
......@@ -93,10 +93,6 @@ class TestFetchAndFeed(unittest.TestCase):
10).astype(np.int64)
yield img, l
# TODO(zcd): I found that onece the memory optimizer is open,
# parallel_exe doesn't fetch some variable, such as conv2d_0.b_0@GRAD,
# conv2d_1.b_0@GRAD. Those variables should not be pruned.
# fluid.memory_optimize(main)
fetch_list = []
all_vars = compiled_program._program.global_block().vars
......
......@@ -142,10 +142,6 @@ def test_main(use_cuda, use_py_func_op, use_parallel_executor):
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
#FIXME force use old memory optimzie strategy here to pass the unittest
#since open the new strategy will crash the unittest
fluid.memory_optimize(fluid.default_main_program())
train_cp = compiler.CompiledProgram(fluid.default_main_program())
if use_parallel_executor:
train_cp = train_cp.with_data_parallel(loss_name=loss.name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册