in compiler_gym/random_search.py [0:0]
def replay_actions(env: CompilerEnv, action_names: List[str], outdir: Path):
logs_path = outdir / logs.BEST_ACTIONS_PROGRESS_NAME
start_time = time()
if isinstance(env, LlvmEnv):
env.write_bitcode(outdir / "unoptimized.bc")
with open(str(logs_path), "w") as f:
ep_reward = 0
for i, action in enumerate(action_names, start=1):
_, reward, done, _ = env.step(env.action_space.names.index(action))
assert not done
ep_reward += reward
print(
f"Step [{i:03d} / {len(action_names):03d}]: reward={reward:.4f} \t"
f"episode={ep_reward:.4f} \taction={action}"
)
progress = RandomSearchProgressLogEntry(
runtime_seconds=time() - start_time,
total_episode_count=1,
total_step_count=i,
num_passes=i,
reward=reward,
)
print(progress.to_csv(), action, file=f, sep=",")
if isinstance(env, LlvmEnv):
env.write_bitcode(outdir / "optimized.bc")
print(
tabulate(
[
(
"IR instruction count",
env.observation["IrInstructionCountO0"],
env.observation["IrInstructionCountOz"],
env.observation["IrInstructionCount"],
),
(
"Object .text size (bytes)",
env.observation["ObjectTextSizeO0"],
env.observation["ObjectTextSizeOz"],
env.observation["ObjectTextSizeBytes"],
),
],
headers=("", "-O0", "-Oz", "final"),
)
)