def test_service_env_dies_reset(env: CompilerEnv): env.observation_space = "Autophase" env.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, info = env.step(0) assert done, info["error_details"] assert not env.in_episode # Check that default values are returned. np.testing.assert_array_equal(observation, np.zeros(AUTOPHASE_FEATURE_DIM)) assert reward == 0 # Reset the environment and check that it works. env.reset(benchmark="cBench-v0/crc32") assert env.in_episode observation, reward, done, info = env.step(0) assert not done, info["error_details"] assert observation is not None assert reward is not None
def test_eager_features_observation(env: CompilerEnv, ): env.observation_space = "features" observation = env.reset() assert isinstance(observation, np.ndarray) assert observation.shape == (3, ) assert observation.dtype == np.int64 assert observation.tolist() == [0, 0, 0]
def test_service_env_dies_reset(env: CompilerEnv): env.observation_space = "Autophase" env.reward_space = "IrInstructionCount" env.reset("cbench-v1/crc32") # 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) # Check that the environment doesn't fall over. observation, reward, done, info = env.step(0) assert done, info["error_details"] assert not env.in_episode # Check that default values are returned. np.testing.assert_array_equal(observation, np.zeros(AUTOPHASE_FEATURE_DIM)) assert reward == 0 # Reset the environment and check that it works. env.reset(benchmark="cbench-v1/crc32") assert env.in_episode observation, reward, done, info = env.step(0) assert not done, info["error_details"] assert observation is not None assert reward is not None
def test_default_features_observation(env: CompilerEnv): """Test default observation space.""" env.observation_space = "features" observation = env.reset() assert isinstance(observation, np.ndarray) assert observation.shape == (3, ) assert observation.dtype == np.int64 assert observation.tolist() == [0, 0, 0]
def test_default_autophase_dict_observation(env: CompilerEnv): """Test default autophase dict observation space.""" env.observation_space = "AutophaseDict" observation = env.reset() assert isinstance(observation, dict) assert sorted(observation.keys()) == sorted(AUTOPHASE_FEATURE_NAMES) assert len(observation.values()) == len(AUTOPHASE_FEATURE_NAMES) assert all(obs >= 0 for obs in observation.values())
def test_default_autophase_observation(env: CompilerEnv): """Test default autophase observation space.""" env.observation_space = "Autophase" observation = env.reset() assert isinstance(observation, np.ndarray) assert observation.shape == (len(AUTOPHASE_FEATURE_NAMES), ) assert observation.dtype == np.int64 assert all(obs >= 0 for obs in observation.tolist())
def test_default_inst2vec_observation(env: CompilerEnv): """Test default inst2vec observation space.""" env.observation_space = "Inst2vec" observation = env.reset() assert isinstance(observation, np.ndarray) assert len(observation) >= 0 assert observation.dtype == np.int64 assert all(obs >= 0 for obs in observation.tolist())
def test_eager_ir_observation(env: CompilerEnv): env.observation_space = "ir" observation = env.reset() assert observation == "Hello, world!" observation, reward, done, info = env.step(0) assert observation == "Hello, world!" assert reward is None assert not done
def test_default_ir_observation(env: CompilerEnv): """Test default observation space.""" env.observation_space = "ir" observation = env.reset() assert observation == "Hello, world!" observation, reward, done, info = env.step(0) assert observation == "Hello, world!" assert reward is None assert not done
def test_default_ir_observation(env: CompilerEnv): """Test default observation space.""" env.observation_space = "ir" observation = env.reset() assert len(observation) > 0 observation, reward, done, info = env.step(0) assert not done, info assert len(observation) > 0 assert reward is None
def test_step(env: CompilerEnv, observation_space: str, reward_space: str): """Request every combination of observation and reward in a fresh environment.""" env.reward_space = None env.observation_space = None env.reset(benchmark="cbench-v1/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.reward_space = "IrInstructionCount" env.observation_space = "Autophase" env.reset(benchmark="cBench-v0/crc32") observation, reward, done, _ = env.step( env.action_space.from_string(action_name)) assert isinstance(observation, np.ndarray) assert observation.shape == (AUTOPHASE_FEATURE_DIM, ) assert isinstance(reward, float) assert isinstance(done, bool)
def test_step(env: CompilerEnv, action_name: str): """Run each action on a single benchmark.""" env.reward_space = "IrInstructionCount" env.observation_space = "Autophase" env.reset(benchmark="cbench-v1/crc32") observation, reward, done, _ = env.step( env.action_space.from_string(action_name)) assert isinstance(observation, np.ndarray) assert observation.shape == (AUTOPHASE_FEATURE_DIM, ) assert isinstance(reward, float) assert isinstance(done, bool) try: env.close() except ServiceError as e: # env.close() will raise an error if the service terminated # ungracefully. In that case, the "done" flag should have been set. assert done, f"Service error was raised when 'done' flag not set: {e}"
def test_service_env_dies_reset(env: CompilerEnv): env.observation_space = "Autophase" env.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 test_invalid_observation_space(env: CompilerEnv): """Test error handling with invalid observation space.""" with pytest.raises(LookupError): env.observation_space = 100
def test_invalid_observation_space(env: CompilerEnv, ): with pytest.raises(LookupError): env.observation_space = 100