compiler_gym.wrappers¶
The compiler_gym.wrappers
module provides.
Document contents:
Base wrappers¶
- class compiler_gym.wrappers.CompilerEnvWrapper(env: compiler_gym.envs.compiler_env.CompilerEnv)[source]¶
Wraps a
CompilerEnv
environment to allow a modular transformation.This class is the base class for all wrappers. This class must be used rather than
gym.Wrapper
to support the CompilerGym API extensions such as thefork()
method.- __init__(env: compiler_gym.envs.compiler_env.CompilerEnv)[source]¶
Constructor.
- Parameters
env – The environment to wrap.
- Raises
TypeError – If
env
is not aCompilerEnv
.
- class compiler_gym.wrappers.ActionWrapper(env: compiler_gym.envs.compiler_env.CompilerEnv)[source]¶
Wraps a
CompilerEnv
environment to allow an action space transformation.
- class compiler_gym.wrappers.ObservationWrapper(env: compiler_gym.envs.compiler_env.CompilerEnv)[source]¶
Wraps a
CompilerEnv
environment to allow an observation space transformation.
- class compiler_gym.wrappers.RewardWrapper(env: compiler_gym.envs.compiler_env.CompilerEnv)[source]¶
Wraps a
CompilerEnv
environment to allow an reward space transformation.
Action space wrappers¶
- class compiler_gym.wrappers.CommandlineWithTerminalAction(env: compiler_gym.envs.compiler_env.CompilerEnv, terminal=CommandlineFlag(name='end-of-episode', flag='# end-of-episode', description='End the episode'))[source]¶
Creates a new action space with a special “end of episode” terminal action at the start. If step() is called with it, the “done” flag is set.
- __init__(env: compiler_gym.envs.compiler_env.CompilerEnv, terminal=CommandlineFlag(name='end-of-episode', flag='# end-of-episode', description='End the episode'))[source]¶
Constructor.
- Parameters
env – The environment to wrap.
terminal – The flag to use as the terminal action. Optional.
- class compiler_gym.wrappers.ConstrainedCommandline(env: compiler_gym.envs.compiler_env.CompilerEnv, flags: Iterable[str], name: Optional[str] = None)[source]¶
Constrains a Commandline action space to a subset of the original space’s flags.
- __init__(env: compiler_gym.envs.compiler_env.CompilerEnv, flags: Iterable[str], name: Optional[str] = None)[source]¶
Constructor.
- Parameters
env – The environment to wrap.
flags – A list of entries from
env.action_space.flags
denoting flags that are available in this wrapped environment.name – The name of the new action space.
- class compiler_gym.wrappers.TimeLimit(env: compiler_gym.envs.compiler_env.CompilerEnv, max_episode_steps: Optional[int] = None)[source]¶
A step-limited wrapper that is compatible with CompilerGym.
Example usage:
>>> env = TimeLimit(env, max_episode_steps=3) >>> env.reset() >>> _, _, done, _ = env.step(0) >>> _, _, done, _ = env.step(0) >>> _, _, done, _ = env.step(0) >>> done True
Datasets wrappers¶
- class compiler_gym.wrappers.IterateOverBenchmarks(env: compiler_gym.envs.compiler_env.CompilerEnv, benchmarks: Iterable[Union[str, compiler_gym.datasets.benchmark.Benchmark]], fork_shares_iterator: bool = False)[source]¶
Iterate over a (possibly infinite) sequence of benchmarks on each call to reset(). Will raise
StopIteration
onreset()
once the iterator is exhausted. UseCycleOverBenchmarks
orRandomOrderBenchmarks
for wrappers which will loop over the benchmarks.- __init__(env: compiler_gym.envs.compiler_env.CompilerEnv, benchmarks: Iterable[Union[str, compiler_gym.datasets.benchmark.Benchmark]], fork_shares_iterator: bool = False)[source]¶
Constructor.
- Parameters
env – The environment to wrap.
benchmarks – An iterable sequence of benchmarks.
fork_shares_iterator – If
True
, thebenchmarks
iterator will bet shared by a forked environment created byenv.fork()
. This means that callingenv.reset()
with one environment will advance the iterator in the other. IfFalse
, forked environments will useitertools.tee()
to create a copy of the iterator so that each iterator may advance independently. However, this requires shared buffers between the environments which can lead to memory overheads ifenv.reset()
is called many times more in one environment than the other.
- class compiler_gym.wrappers.CycleOverBenchmarks(env: compiler_gym.envs.compiler_env.CompilerEnv, benchmarks: Iterable[Union[str, compiler_gym.datasets.benchmark.Benchmark]], fork_shares_iterator: bool = False)[source]¶
Cycle through a list of benchmarks on each call to
reset()
. Same asIterateOverBenchmarks
except the list of benchmarks repeats once exhausted.- __init__(env: compiler_gym.envs.compiler_env.CompilerEnv, benchmarks: Iterable[Union[str, compiler_gym.datasets.benchmark.Benchmark]], fork_shares_iterator: bool = False)[source]¶
Constructor.
- Parameters
env – The environment to wrap.
benchmarks – An iterable sequence of benchmarks.
fork_shares_iterator – If
True
, thebenchmarks
iterator will be shared by a forked environment created byenv.fork()
. This means that callingenv.reset()
with one environment will advance the iterator in the other. IfFalse
, forked environments will useitertools.tee()
to create a copy of the iterator so that each iterator may advance independently. However, this requires shared buffers between the environments which can lead to memory overheads ifenv.reset()
is called many times more in one environment than the other.
- class compiler_gym.wrappers.RandomOrderBenchmarks(env: compiler_gym.envs.compiler_env.CompilerEnv, benchmarks: Iterable[Union[str, compiler_gym.datasets.benchmark.Benchmark]], rng: Optional[numpy.random._generator.Generator] = None)[source]¶
Select randomly from a list of benchmarks on each call to
reset()
.Note
Uniform random selection is provided by evaluating the input benchmarks iterator into a list and sampling randomly from the list. This will not work for random iteration over infinite or very large iterables of benchmarks.
- __init__(env: compiler_gym.envs.compiler_env.CompilerEnv, benchmarks: Iterable[Union[str, compiler_gym.datasets.benchmark.Benchmark]], rng: Optional[numpy.random._generator.Generator] = None)[source]¶
Constructor.
- Parameters
env – The environment to wrap.
benchmarks – An iterable sequence of benchmarks. The entirety of this input iterator is evaluated during construction.
rng – A random number generator to use for random benchmark selection.