def test_compiler_env_state_writer_no_header(): buf = StringIO() writer = CompilerEnvStateWriter(buf, header=False) writer.write_state( CompilerEnvState( benchmark="benchmark://cbench-v0/foo", walltime=5, commandline="-a -b -c", reward=2, ), flush=True, ) assert buf.getvalue() == "benchmark://cbench-v0/foo,2.0,5.0,-a -b -c\n"
def _experiment_worker( autotuner: Autotuner, benchmark: str, results_path: Path, errors_path: Path, seed: int, ) -> None: try: with autotuner.optimization_target.make_env(benchmark) as env: env.seed(seed) env.action_space.seed(seed) state = autotuner(env, seed=seed) except Exception as e: # pylint: disable=broad-except logger.warning("Autotuner failed on benchmark %s: %s", benchmark, e) with open(errors_path, "w") as f: json.dump( { "benchmark": benchmark, "error_type": type(e).__name__, "error_message": str(e), }, f, ) return logger.info("State %s", state) with CompilerEnvStateWriter(open(results_path, "w")) as writer: writer.write_state(state, flush=True)
def test_state_serialize_deserialize_equality_no_reward(): original_state = CompilerEnvState(benchmark="benchmark://cbench-v0/foo", walltime=100, commandline="-a -b -c") buf = StringIO() CompilerEnvStateWriter(buf).write_state(original_state) buf.seek(0) # Rewind the buffer for reading. state_from_csv = next(iter(CompilerEnvStateReader(buf))) assert state_from_csv.benchmark == "benchmark://cbench-v0/foo" assert state_from_csv.walltime == 100 assert state_from_csv.reward is None assert state_from_csv.commandline == "-a -b -c"
def test_compiler_env_state_writer_with_statement(tmpwd: Path, flush: bool): path = Path("results.csv") assert not path.is_file() # Sanity check. f = open(path, "w") with CompilerEnvStateWriter(f) as writer: writer.write_state( CompilerEnvState( benchmark="benchmark://cbench-v0/foo", walltime=5, commandline="-a -b -c", reward=2, ), flush=flush, ) assert f.closed with open(path) as f: assert f.read() == ("benchmark,reward,walltime,commandline\n" "benchmark://cbench-v0/foo,2.0,5.0,-a -b -c\n")