f.write(msg) sys.exit(1) def check_result(completed_process): if completed_process.returncode: log_failure(f"Command failed: {{' '.join(completed_process.args)}}") return completed_process # ============================================================================= # == Check that subprocess matches parent ===================================== # ============================================================================= if os.path.realpath(sys.executable) != "{parent_interpreter}": log_failure( "Interpreter mismatch:\n" f" {{os.path.realpath(sys.executable)}}\n vs.\n {parent_interpreter}" ) if torch.__file__ != "{torch_file}": log_failure( "PyTorch does not match expected file:\n" f" {{torch.__file__}}\n vs.\n {torch_file}" ) # ============================================================================= # == User specified setup ===================================================== # ============================================================================= # Load serialized globals {load_globals} # User setup str {setup} for _ in range({warmup_number}): {indented_stmt} # ============================================================================= # == Callgrind management ===================================================== # ============================================================================= with open("{stat_log}", "wb") as stat_file: # If many instances of callgrind are running at once, the output of # `callgrind_control` may exceed 16kb which would cause `subprocess.PIPE` # to deadlock. So instead we use a file. callgrind_stat = check_result(subprocess.run( ["callgrind_control", "--stat"], stdout=stat_file, stderr=subprocess.STDOUT, )) with open("{stat_log}", "rt") as stat_file: stat_lines = stat_file.read().splitlines() if f"PID {{PID}}: python {{__file__}}" not in stat_lines: log_failure("Process does not appear to be running callgrind.") gc.collect() time.sleep(0.01) # ============================================================================= # == User code block ========================================================== # ============================================================================= for _ in range({repeats}): callgrind_bindings._valgrind_toggle() {blocked_stmt} callgrind_bindings._valgrind_toggle_and_dump_stats() gc.collect() {baseline} r