def test_eager_reward_space(env: CompilerEnv): env.eager_reward_space = "IrInstructionCount" assert env.eager_reward_space == "IrInstructionCount" env.eager_reward_space = None assert env.eager_reward_space is None invalid = "invalid value" with pytest.raises(LookupError) as ctx: env.eager_reward_space = invalid assert str(ctx.value) == f"Reward space not found: {invalid}"
def test_eager_reward(env: CompilerEnv): env.eager_reward_space = "codesize" env.reset() observation, reward, done, info = env.step(0) assert observation is None assert reward == 0 assert not done
def test_step(env: CompilerEnv, observation_space: str, reward_space: str): """Request every combination of observation and reward in a fresh environment.""" env.eager_reward_space = None env.eager_observation_space = None env.reset(benchmark="cBench-v0/crc32") observation = env.observation[observation_space] assert observation is not None reward = env.reward[reward_space] assert reward is not None
def test_step(env: CompilerEnv, action_name: str): """Run each action on a single benchmark.""" env.eager_reward_space = "IrInstructionCount" env.eager_observation_space = "Autophase" env.reset(benchmark="cBench-v0/crc32") observation, reward, done, info = env.step( env.action_space.from_string(action_name) ) if done: assert observation is None assert reward is None else: assert isinstance(observation, np.ndarray) assert observation.shape == (56,) assert reward < 0
def test_service_env_dies_reset(env: CompilerEnv): env.eager_observation_space = "Autophase" env.eager_reward_space = "IrInstructionCount" env.reset("cBench-v0/crc32") # Kill the service. env.service.close() # Check that the environment doesn't fall over. observation, reward, done, _ = env.step(0) assert done assert observation is None assert reward is None # Reset the environment and check that it works. env.reset(benchmark="cBench-v0/crc32") observation, reward, done, _ = env.step(0) assert not done assert observation is not None assert reward is not None
def replay_actions_from_logs(env: CompilerEnv, logdir: Path, benchmark=None) -> None: best_actions_path = logdir / logs.BEST_ACTIONS_NAME meta_path = logdir / logs.METADATA_NAME assert best_actions_path.is_file(), f"File not found: {best_actions_path}" assert meta_path.is_file(), f"File not found: {meta_path}" with open(meta_path, "rb") as f: meta = json.load(f) with open(best_actions_path) as f: actions = [l.strip() for l in f.readlines() if l.strip()] benchmark = benchmark or meta["benchmark"] env.eager_reward_space = meta["reward"] env.reset(benchmark=benchmark) replay_actions(env, actions, logdir) env.close()
def test_invalid_eager_reward_space(env: CompilerEnv, ): with pytest.raises(LookupError): env.eager_reward_space = 100