预测阶段内存持续增长
Created by: Angus07
使用paddle 1.6.1 在预测压测,发现内存持续增长。
代码如下, 首先是建立一个全局的网络,包括reader
def main_test():
"""
创建网络
"""
global test_exe
global reader
global test_prog
global test_pyreader, test_graph_vars
global place
ernie_config = ErnieConfig(args.ernie_config_path)
ernie_config.print_config()
gpus = os.getenv("FLAGS_selected_gpus").split(",")
gpu_id = int(gpus[0])
gpus = fluid.core.get_cuda_device_count()
if args.use_cuda:
place = fluid.CUDAPlace(gpu_id)
print("use gpu")
dev_count = len(gpus) if args.is_distributed else gpus
else:
place = fluid.CPUPlace()
print("use cpu")
dev_count = int(os.environ.get('CPU_NUM', multiprocessing.cpu_count()))
reader = task_reader.Seq2SeqReader(
vocab_path=args.vocab_path,
label_map_config=args.label_map_config,
max_seq_len=args.max_seq_len,
do_lower_case=args.do_lower_case,
in_tokens=args.in_tokens,
random_seed=args.random_seed,
tgt_type_id=args.tgt_type_id,
tokenizer=args.tokenizer,
multi_segment=args.multi_segment)
startup_prog = fluid.Program()
if args.random_seed is not None:
startup_prog.random_seed = args.random_seed
test_prog = fluid.Program()
with fluid.program_guard(test_prog, startup_prog):
with fluid.unique_name.guard():
test_pyreader, test_graph_vars = create_model(
args,
pyreader_name='test_reader',
ernie_config=ernie_config,
is_prediction=args.do_dec)
test_prog = test_prog.clone(for_test=True)
exe = fluid.Executor(place)
exe.run(startup_prog)
if not args.init_checkpoint:
raise ValueError("args 'init_checkpoint' should be set if"
"only doing validation or testing!")
init_checkpoint(
exe,
args.init_checkpoint,
main_program=startup_prog,
use_fp16=args.use_fp16)
test_exe = exe
在回调函数中实现主体预测程序。
test_pyreader.decorate_tensor_provider(
reader.data_generator_from_input(
src_contents,
batch_size=args.batch_size,
epoch=1,
dev_count=1,
shuffle=False,
phase='test',
do_dec=args.do_dec,
place=place))
output = infer_test(test_exe, test_prog, test_pyreader, test_graph_vars, "test", do_dec=args.do_dec,
vocab_path=args.vocab_path, features=reader.get_features("test"),
decode_path=args.decode_path + "/test_final_preds")
其中Infer_test封装的代码如下:
pyreader.start()
while True:
try:
outputs = exe.run(program=program, fetch_list=fetch_list,
return_numpy=return_numpy)
seq_ids, seq_scores, data_ids = outputs
seq_ids_list, seq_scores_list = [seq_ids], [
seq_scores] if isinstance(
seq_ids, paddle.fluid.core.LoDTensor) else (seq_ids, seq_scores)
data_ids = np.array(data_ids).reshape(-1).tolist()
data_idx = 0
......
score = np.array(seq_scores)[sub_end - 1]
data_id = data_ids[data_idx]
# not support batch
dec_out[data_id].append((tokens, score))
if cnt >= 2:
break
cnt += 1
data_idx += 1
except fluid.core.EOFException:
pyreader.reset()
break
补充代码:
pyreader = fluid.layers.py_reader(
capacity=50,
shapes=[[-1, args.max_seq_len, 1], [-1, args.max_seq_len, 1],
[-1, args.max_seq_len, 1], [-1, args.max_seq_len, args.max_seq_len],
[-1, args.max_seq_len, args.max_seq_len],
[-1, 1], [-1, 1]],
dtypes=['int64', 'int64', 'int64', 'float32', 'float32', 'int64', 'int64'],
od_levels=[0, 0, 0, 0, 0, 0, 0],
name=pyreader_name,
use_double_buffer=True)