forked from facebookresearch/CompilerGym
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request facebookresearch#682 from ChrisCummins/feature/und…
…o-wrapper [wrappers] Add a ForkOnStep wrapper.
- Loading branch information
Showing
9 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
"""This module implements fork wrappers.""" | ||
from typing import List | ||
|
||
from compiler_gym.envs import CompilerEnv | ||
from compiler_gym.wrappers import CompilerEnvWrapper | ||
|
||
|
||
class ForkOnStep(CompilerEnvWrapper): | ||
"""A wrapper that creates a fork of the environment before every step. | ||
This wrapper creates a new fork of the environment before every call to | ||
:meth:`env.reset() <compiler_gym.envs.CompilerEnv.reset>`. Because of this, | ||
this environment supports an additional :meth:`env.undo() | ||
<compiler_gym.wrappers.ForkOnStep.undo>` method that can be used to | ||
backtrack. | ||
Example usage: | ||
>>> env = ForkOnStep(compiler_gym.make("llvm-v0")) | ||
>>> env.step(0) | ||
>>> env.actions | ||
[0] | ||
>>> env.undo() | ||
>>> env.actions | ||
[] | ||
:ivar stack: A fork of the environment before every previous call to | ||
:meth:`env.reset() <compiler_gym.envs.CompilerEnv.reset>`, ordered | ||
oldest to newest. | ||
:vartype stack: List[CompilerEnv] | ||
""" | ||
|
||
def __init__(self, env: CompilerEnv): | ||
"""Constructor. | ||
:param env: The environment to wrap. | ||
""" | ||
super().__init__(env) | ||
self.stack: List[CompilerEnv] = [] | ||
|
||
def undo(self) -> CompilerEnv: | ||
"""Undo the previous action. | ||
:returns: Self. | ||
""" | ||
if not self.stack: | ||
return | ||
self.env.close() | ||
self.env = self.stack.pop() | ||
return self.env | ||
|
||
def close(self) -> None: | ||
for env in self.stack: | ||
env.close() | ||
self.stack: List[CompilerEnv] = [] | ||
self.env.close() | ||
self.custom_close = True | ||
|
||
def reset(self, *args, **kwargs): | ||
self.env.reset() | ||
for env in self.stack: | ||
env.close() | ||
self.stack: List[CompilerEnv] = [] | ||
|
||
def step(self, *args, **kwargs): | ||
self.stack.append(self.env.fork()) | ||
return self.env.step(*args, **kwargs) | ||
|
||
def fork(self): | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
"""Unit tests for //compiler_gym/wrappers.""" | ||
from compiler_gym.envs.llvm import LlvmEnv | ||
from compiler_gym.wrappers import ForkOnStep | ||
from tests.test_main import main | ||
|
||
pytest_plugins = ["tests.pytest_plugins.llvm"] | ||
|
||
|
||
def test_ForkOnStep_step(env: LlvmEnv): | ||
with ForkOnStep(env) as env: | ||
env.reset() | ||
assert env.stack == [] | ||
|
||
env.step(0) | ||
assert env.actions == [0] | ||
assert len(env.stack) == 1 | ||
assert env.stack[0].actions == [] | ||
|
||
env.step(1) | ||
assert env.actions == [0, 1] | ||
assert len(env.stack) == 2 | ||
assert env.stack[1].actions == [0] | ||
assert env.stack[0].actions == [] | ||
|
||
|
||
def test_ForkOnStep_reset(env: LlvmEnv): | ||
with ForkOnStep(env) as env: | ||
env.reset() | ||
|
||
env.step(0) | ||
assert env.actions == [0] | ||
assert len(env.stack) == 1 | ||
|
||
env.reset() | ||
assert env.actions == [] | ||
assert env.stack == [] | ||
|
||
|
||
def test_ForkOnStep_double_close(env: LlvmEnv): | ||
with ForkOnStep(env) as env: | ||
env.close() | ||
env.close() | ||
|
||
|
||
def test_ForkOnStep_undo(env: LlvmEnv): | ||
with ForkOnStep(env) as env: | ||
env.reset() | ||
|
||
env.step(0) | ||
assert env.actions == [0] | ||
assert len(env.stack) == 1 | ||
|
||
env.undo() | ||
assert env.actions == [] | ||
assert not env.stack | ||
|
||
# Undo of an empty stack: | ||
env.undo() | ||
assert env.actions == [] | ||
assert not env.stack | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |