Skip to content

Commit 371562b

Browse files
committed
Fix seed of dropout.
1 parent b0ba9c6 commit 371562b

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

graph_net/paddle/test_compiler.py

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
from graph_net import test_compiler_util
1818

1919

20+
def set_seed(random_seed):
21+
paddle.seed(random_seed)
22+
random.seed(random_seed)
23+
np.random.seed(random_seed)
24+
25+
2026
def get_hardward_name(args):
2127
if args.device == "cuda":
2228
hardware = paddle.device.cuda.get_device_name(0)
@@ -94,38 +100,30 @@ def get_compiled_model(args, model):
94100
full_graph=True,
95101
)
96102
compiled_model.eval()
103+
program = compiled_model.forward.concrete_program.main_program
97104
return compiled_model
98105

99106

100-
def count_number_of_ops(args, model, eager_mode):
101-
if eager_mode:
102-
static_model = paddle.jit.to_static(
103-
model,
104-
input_spec=get_input_spec(args),
105-
full_graph=True,
106-
backend=None,
107-
)
108-
static_model.eval()
109-
program = static_model.forward.concrete_program.main_program
110-
else:
111-
program = model.forward.concrete_program.main_program
112-
print(program)
113-
114-
num_ops = 0
115-
for block in program.blocks:
116-
for op in block.ops:
117-
if op.name() != "pd_op.data" and not op.name().startswith("builtin."):
118-
num_ops += 1
119-
print(f"Totally {num_ops} ops.")
120-
print("")
121-
return num_ops
107+
def get_static_model(args, model):
108+
static_model = paddle.jit.to_static(
109+
model,
110+
input_spec=get_input_spec(args),
111+
full_graph=True,
112+
backend=None,
113+
)
114+
static_model.eval()
115+
program = static_model.forward.concrete_program.main_program
116+
return static_model
122117

123118

124-
def measure_performance(model_call, args, synchronizer_func):
119+
def measure_performance(model_call, args, synchronizer_func, profile=False):
120+
runtime_seed = 1024
125121
stats = {}
126122

127-
# Warmup runs
123+
paddle.seed(runtime_seed)
128124
outs = model_call()
125+
126+
# Warmup runs
129127
for _ in range(args.warmup):
130128
model_call()
131129
synchronizer_func()
@@ -146,6 +144,8 @@ def measure_performance(model_call, args, synchronizer_func):
146144
e2e_times = []
147145
gpu_times = []
148146

147+
if profile:
148+
paddle.base.core.nvprof_start()
149149
for i in range(args.trials):
150150
# End-to-end timing (naive_timer)
151151
duration_box = test_compiler_util.DurationBox(-1)
@@ -166,6 +166,8 @@ def measure_performance(model_call, args, synchronizer_func):
166166
file=sys.stderr,
167167
flush=True,
168168
)
169+
if profile:
170+
paddle.base.core.nvprof_stop()
169171

170172
stats["e2e"] = test_compiler_util.get_timing_stats(e2e_times)
171173
stats["gpu"] = test_compiler_util.get_timing_stats(gpu_times)
@@ -216,16 +218,6 @@ def regular_outputs(origin_outputs):
216218
outputs.append(item)
217219
return outputs
218220

219-
def print_cmp(key, func, **kwargs):
220-
try:
221-
cmp_ret = func(expected_out, compiled_out, **kwargs)
222-
except Exception as e:
223-
cmp_ret = f"{key} failed: {str(e)}\n{traceback.format_exc()}"
224-
print(
225-
f"{args.log_prompt} {key} model_path:{args.model_path} {cmp_ret}",
226-
file=sys.stderr,
227-
)
228-
229221
if type_match:
230222
expected_out = regular_outputs(expected_out)
231223
compiled_out = regular_outputs(compiled_out)
@@ -248,36 +240,38 @@ def test_single_model(args):
248240
model = get_model(args)
249241
model.eval()
250242

251-
num_eager_ops = count_number_of_ops(args, model, eager_mode=True)
243+
# num_eager_ops = count_number_of_ops(args, model, eager_mode=True)
252244

253245
test_compiler_util.print_basic_config(
254246
args, get_hardward_name(args), get_compile_framework_version(args)
255247
)
256248

257249
# Run on eager mode
258-
running_eager_success = False
250+
eager_success = False
259251
try:
260252
print("Run model in eager mode.")
253+
static_model = get_static_model(args, model)
261254
expected_out, eager_time_stats = measure_performance(
262-
lambda: model(**input_dict), args, synchronizer_func
255+
lambda: static_model(**input_dict), args, synchronizer_func, profile=False
263256
)
264-
running_eager_success = True
257+
eager_success = True
265258
except Exception as e:
266259
print(f"Run model in eager mode failed: {str(e)}\n{traceback.format_exc()}")
267260

268261
# Run on compiling mode
269-
running_compiled_success = False
262+
compiled_success = False
270263
try:
271264
print("Run model in compiled mode.")
272265
compiled_model = get_compiled_model(args, model)
273266
compiled_out, compiled_time_stats = measure_performance(
274-
lambda: compiled_model(**input_dict), args, synchronizer_func
267+
lambda: compiled_model(**input_dict), args, synchronizer_func, profile=False
275268
)
276-
running_compiled_success = True
269+
compiled_success = True
277270
except Exception as e:
278271
print(f"Run model in compiled mode failed: {str(e)}\n{traceback.format_exc()}")
279272

280-
if running_eager_success and running_compiled_success:
273+
test_compiler_util.print_running_status(args, eager_success, compiled_success)
274+
if eager_success and compiled_success:
281275
check_outputs(args, expected_out, compiled_out)
282276

283277
test_compiler_util.print_times_and_speedup(
@@ -342,10 +336,8 @@ def main(args):
342336
assert os.path.isdir(args.model_path)
343337
assert args.compiler == "cinn"
344338

345-
random_seed = 123
346-
paddle.seed(random_seed)
347-
random.seed(random_seed)
348-
np.random.seed(random_seed)
339+
initalize_seed = 123
340+
set_seed(random_seed=initalize_seed)
349341

350342
if path_utils.is_single_model_dir(args.model_path):
351343
test_single_model(args)

graph_net/test_compiler_util.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ def print_basic_config(args, hardware_name, compile_framework_version):
7575
)
7676

7777

78+
def print_running_status(args, eager_success, compiled_success):
79+
def convert_to_str(b):
80+
return "success" if b else "failed"
81+
82+
print_with_log_prompt(
83+
"[Result][status]",
84+
f"eager:{convert_to_str(eager_success)} compiled:{convert_to_str(compiled_success)}",
85+
args.log_prompt,
86+
)
87+
88+
7889
def print_times_and_speedup(args, eager_stats, compiled_stats):
7990
print_with_log_prompt(
8091
"[Performance][eager]:", json.dumps(eager_stats), args.log_prompt

0 commit comments

Comments
 (0)