-
Notifications
You must be signed in to change notification settings - Fork 130
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 #683 from ChrisCummins/feature/counter-wrapper
[wrappers] Add a Counter wrapper class.
- Loading branch information
Showing
7 changed files
with
149 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,59 @@ | ||
# 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 a wrapper that counts calls to operations. | ||
""" | ||
from typing import Dict | ||
|
||
from compiler_gym.envs import CompilerEnv | ||
from compiler_gym.wrappers import CompilerEnvWrapper | ||
|
||
|
||
class Counter(CompilerEnvWrapper): | ||
"""A wrapper that counts the number of calls to its operations. | ||
The counters are _not_ reset by :meth:`env.reset() | ||
<compiler_gym.envs.CompilerEnv.reset>`. | ||
Example usage: | ||
>>> env = Counter(compiler_gym.make("llvm-v0")) | ||
>>> env.counters | ||
{"close": 0, "reset": 0, "step": 0, "fork": 0} | ||
>>> env.step(0) | ||
{"close": 0, "reset": 0, "step": 1, "fork": 0} | ||
:ivar counters: A dictionary of counters for different operation types. | ||
:vartype counters: Dict[str, int] | ||
""" | ||
|
||
def __init__(self, env: CompilerEnv): | ||
"""Constructor. | ||
:param env: The environment to wrap. | ||
""" | ||
super().__init__(env) | ||
self.counters: Dict[str, int] = { | ||
"close": 0, | ||
"reset": 0, | ||
"step": 0, | ||
"fork": 0, | ||
} | ||
|
||
def close(self) -> None: | ||
self.counters["close"] += 1 | ||
self.env.close() | ||
|
||
def reset(self, *args, **kwargs): | ||
self.counters["reset"] += 1 | ||
return self.env.reset(*args, **kwargs) | ||
|
||
def step(self, *args, **kwargs): | ||
self.counters["step"] += 1 | ||
return self.env.step(*args, **kwargs) | ||
|
||
def fork(self): | ||
self.counters["fork"] += 1 | ||
return self.env.fork() |
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,65 @@ | ||
# 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 Counter | ||
from tests.test_main import main | ||
|
||
pytest_plugins = ["tests.pytest_plugins.llvm"] | ||
|
||
|
||
def test_Counter_reset(env: LlvmEnv): | ||
with Counter(env) as env: | ||
env.reset() | ||
assert env.counters == { | ||
"close": 0, | ||
"fork": 0, | ||
"reset": 1, | ||
"step": 0, | ||
} | ||
|
||
env.reset() | ||
assert env.counters == { | ||
"close": 0, | ||
"fork": 0, | ||
"reset": 2, | ||
"step": 0, | ||
} | ||
|
||
|
||
def test_Counter_step(env: LlvmEnv): | ||
with Counter(env) as env: | ||
env.reset() | ||
env.step(0) | ||
assert env.counters == { | ||
"close": 0, | ||
"fork": 0, | ||
"reset": 1, | ||
"step": 1, | ||
} | ||
|
||
|
||
def test_Counter_double_close(env: LlvmEnv): | ||
with Counter(env) as env: | ||
env.close() | ||
env.close() | ||
assert env.counters == { | ||
"close": 2, | ||
"fork": 0, | ||
"reset": 0, | ||
"step": 0, | ||
} | ||
|
||
# Implicit close in `with` statement. | ||
assert env.counters == { | ||
"close": 3, | ||
"fork": 0, | ||
"reset": 0, | ||
"step": 0, | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |