def test_reward_space(env: LlvmEnv): env.reward_space = "IrInstructionCount" assert env.reward_space.id == "IrInstructionCount" env.reward_space = None assert env.reward_space is None invalid = "invalid value" with pytest.raises(LookupError) as ctx: env.reward_space = invalid assert str(ctx.value) == f"Reward space not found: {invalid}"
def test_apply_state(env: LlvmEnv): """Test that apply() on a clean environment produces same state.""" env.reward_space = "IrInstructionCount" env.reset(benchmark="cbench-v1/crc32") env.step(env.action_space.flags.index("-mem2reg")) with gym.make("llvm-v0", reward_space="IrInstructionCount") as other: other.apply(env.state) assert other.state == env.state
def test_connection_dies_default_reward_negated(env: LlvmEnv): env.reward_space = "IrInstructionCount" env.reset(benchmark="cBench-v0/crc32") env.reward_space.default_negates_returns = True env.reward_space.default_value = 2.5 env.episode_reward = 10 env.service.close() observation, reward, done, _ = env.step(0) assert done assert reward == -7.5 # negates reward.
def test_same_reward_after_reset(env: LlvmEnv): """Check that running the same action after calling reset() produces same reward. """ env.reward_space = "IrInstructionCount" env.benchmark = "cbench-v1/dijkstra" action = env.action_space.flags.index("-instcombine") env.reset() _, reward_a, _, _ = env.step(action) assert reward_a, "Sanity check that action produces a reward" env.reset() _, reward_b, _, _ = env.step(action) assert reward_a == reward_b
def test_connection_dies_default_reward(env: LlvmEnv): env.reward_space = "IrInstructionCount" env.reset(benchmark="cbench-v1/crc32") env.reward_space.default_negates_returns = False env.reward_space.default_value = 2.5 env.episode_reward = 10 # Kill the service. Note killing the service for a ManagedConnection will # result in a ServiceError because we have not ended the session we started # with env.reset() above. For UnmanagedConnection, this error will not be # raised. try: env.service.close() except ServiceError as e: assert "Service exited with returncode " in str(e) _, reward, done, _ = env.step(0) assert done assert reward == 2.5
def test_set_reward_space_from_spec(env: LlvmEnv): env.reward_space = env.reward.spaces["IrInstructionCount"] reward = env.reward_space env.reward_space = "IrInstructionCount" assert env.reward_space == reward