Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to issue #648 #739

Merged
merged 3 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions compiler_gym/wrappers/time_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from typing import Optional
from typing import Iterable, Optional

from compiler_gym.envs import CompilerEnv
from compiler_gym.util.gym_type_hints import ActionType
Expand Down Expand Up @@ -32,12 +32,23 @@ def __init__(self, env: CompilerEnv, max_episode_steps: Optional[int] = None):
self._max_episode_steps = max_episode_steps
self._elapsed_steps = None

def step(self, action: ActionType, **kwargs):
# def step(self, action: ActionType, **kwargs):
# assert (
# self._elapsed_steps is not None
# ), "Cannot call env.step() before calling reset()"
# observation, reward, done, info = self.env.step(action, **kwargs)
# self._elapsed_steps += 1
# if self._elapsed_steps >= self._max_episode_steps:
# info["TimeLimit.truncated"] = not done
# done = True
# return observation, reward, done, info
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete these lines. Don't submit commented-out code


def multistep(self, actions: Iterable[ActionType], **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def multistep(self, actions: Iterable[ActionType], **kwargs):
def multistep(self, actions: Iterable[ActionType], **kwargs):
actions = list(actions)

I would suggest forcing the conversion of actions to a list here, since there could be a subtle bug if actions is an iterator, as it would break the len(actions) below.

assert (
self._elapsed_steps is not None
), "Cannot call env.step() before calling reset()"
observation, reward, done, info = self.env.step(action, **kwargs)
self._elapsed_steps += 1
observation, reward, done, info = self.env.multistep(actions, **kwargs)
self._elapsed_steps += len(actions)
if self._elapsed_steps >= self._max_episode_steps:
info["TimeLimit.truncated"] = not done
done = True
Expand Down
18 changes: 17 additions & 1 deletion tests/wrappers/time_limit_wrappers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# 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 TimeLimit
from compiler_gym.wrappers import CycleOverBenchmarks, TimeLimit
from tests.test_main import main

pytest_plugins = ["tests.pytest_plugins.llvm"]
Expand Down Expand Up @@ -81,5 +81,21 @@ def test_time_limit_fork(env: LlvmEnv):
fkd.close()


# @pytest.mark.xfail(strict=True, reason="https://github.com/facebookresearch/CompilerGym/issues/648")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't commit commented out code

def test_time_limit(env: LlvmEnv):
"""Check CycleOverBenchmarks does not break TimeLimit"""
env = TimeLimit(env, max_episode_steps=1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest testing with max_episode_steps=3, so that you can test that done is False for the first two calls to step().

env = CycleOverBenchmarks(
env,
benchmarks=[
"benchmark://cbench-v1/crc32",
],
)
env.reset()
_, _, done, _ = env.step(0)

assert done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert done
assert done
assert info["TimeLimit.truncated"], info

Best to also test the reason why done=True.



if __name__ == "__main__":
main()