compiler_gym.views

The CompilerEnv exposes the available observation spaces and reward spaces through view objects. These views provide a flexible interface into lazily computed values. At any point during the lifetime of an environment, any of the available observation and reward spaces can be queried through the observation and reward attributes, respectively.

ObservationView

class compiler_gym.views.ObservationView(raw_step: Callable[[List[int], List[ObservationType], List[float]], Tuple[Optional[Union[ObservationType, List[ObservationType]]], Optional[Union[float, List[float]]], bool, Dict[str, Any]]], spaces: List[compiler_gym.service.proto.compiler_gym_service_pb2.ObservationSpace])[source]

A view into the available observation spaces of a service.

Example usage:

>>> env = gym.make("llvm-v0")
>>> env.reset()
>>> env.observation.spaces.keys()
["Autophase", "Ir"]
>>> env.observation.spaces["Autophase"].space
Box(56,)
>>> env.observation["Autophase"]
[0, 1, ..., 2]
>>> observation["Ir"]
int main() {...}
__getitem__(observation_space: str) → ObservationType[source]

Request an observation from the given space.

Parameters

observation_space – The observation space to query.

Returns

An observation.

Raises
add_derived_space(id: str, base_id: str, **kwargs) → None[source]

Alias to ObservationSpaceSpec.make_derived_space() that adds the derived space to the observation view.

Example usage:

>>> env.observation.add_derived_space(
    id="src_len",
    base_id="src",
    translate=lambda src: np.array([len(src)], dtype=np.int32),
    shape=Box(shape=(1,), dtype=np.int32),
)
>>> env.observation["src_len"]
1029
Parameters

ObservationSpaceSpec

class compiler_gym.views.ObservationSpaceSpec(id: str, index: int, space: gym.spaces.space.Space, translate: Callable[[Union[ObservationType, compiler_gym.service.proto.compiler_gym_service_pb2.Observation]], ObservationType], to_string: Callable[[ObservationType], str], deterministic: bool, platform_dependent: bool, default_value: ObservationType)[source]

Specification of an observation space.

Variables
  • id (str) – The name of the observation space.

  • index (int) – The index into the list of observation spaces that the service supports.

  • space (Space) – The space.

  • deterministic (bool) – Whether the observation space is deterministic.

  • platform_dependent (bool) – Whether the observation values depend on the execution environment of the service.

  • default_value – A default observation. This value will be returned by CompilerEnv.step() if CompilerEnv.observation_space is set and the service terminates.

make_derived_space(id: str, translate: Callable[[ObservationType], ObservationType], space: Optional[gym.spaces.space.Space] = None, deterministic: Optional[bool] = None, default_value: Optional[ObservationType] = None, platform_dependent: Optional[bool] = None, to_string: Callable[[ObservationType], str] = None) → compiler_gym.views.observation_space_spec.ObservationSpaceSpec[source]

Create a derived observation space.

Parameters
  • id – The name of the derived observation space.

  • translate – A callback function to compute a derived observation from the base observation.

  • space – The gym.Space describing the observation space.

  • deterministic – Whether the observation space is deterministic. If not provided, the value is inherited from the base observation space.

  • default_value – The default value for the observation space. If not provided, the value is derived from the default value of the base observation space.

  • platform_dependent – Whether the derived observation space is platform-dependent. If not provided, the value is inherited from the base observation space.

  • to_string – A callback to convert and observation to a string representation. If not provided, the callback is inherited from the base observation space.

Returns

A new ObservationSpaceSpec.

RewardView

class compiler_gym.views.RewardView(spaces: List[compiler_gym.spaces.reward.Reward], observation_view: compiler_gym.views.observation.ObservationView)[source]

A view into a set of reward spaces.

Example usage:

>>> env = gym.make("llvm-v0")
>>> env.reset()
>>> env.reward.spaces["codesize"].range
(-np.inf, 0)
>>> env.reward["codesize"]
-1243
Variables

spaces (Dict[str, Reward]) – Specifications of available reward spaces.

__getitem__(reward_space: str) → float[source]

Request an observation from the given space.

Parameters

reward_space – The reward space to query.

Returns

A reward.

Raises
add_space(space: compiler_gym.spaces.reward.Reward) → None[source]

Register a new Reward space.

Parameters

space – The reward space to be added.

reset(benchmark: compiler_gym.datasets.benchmark.Benchmark) → None[source]

Reset the rewards space view. This is called on env.reset().

Parameters

benchmark – The benchmark that is used for this episode.