-
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 #467 from ChrisCummins/examples
[examples] Update and improve the example service implementations and documentation
- Loading branch information
Showing
10 changed files
with
297 additions
and
37 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
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,35 @@ | ||
# 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 script demonstrates how the example services defined in this directory | ||
can be used as gym environments. Usage: | ||
$ bazel run -c opt //examples/example_compiler_gym_service:demo | ||
""" | ||
import logging | ||
|
||
import gym | ||
|
||
# To use the example services we simply need to import the module which | ||
# registers the environments. | ||
import examples.example_compiler_gym_service # noqa Register environments | ||
|
||
|
||
def main(): | ||
# Use debug verbosity to print out extra logging information. | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
# Create the environment using the regular gym.make(...) interface. We could | ||
# use either the C++ service "example-cc-v0" or the Python service | ||
# "example-py-v0". | ||
with gym.make("example-cc-v0") as env: | ||
env.reset() | ||
for _ in range(20): | ||
observation, reward, done, info = env.step(env.action_space.sample()) | ||
if done: | ||
env.reset() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
114 changes: 114 additions & 0 deletions
114
examples/example_compiler_gym_service/demo_without_bazel.py
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,114 @@ | ||
# 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 script demonstrates how the Python example service without needing | ||
to use the bazel build system. Usage: | ||
$ python example_compiler_gym_service/demo_without_bazel.py | ||
It is equivalent in behavior to the demo.py script in this directory. | ||
""" | ||
import logging | ||
from pathlib import Path | ||
from typing import Iterable | ||
|
||
import gym | ||
|
||
from compiler_gym.datasets import Benchmark, Dataset | ||
from compiler_gym.spaces import Reward | ||
from compiler_gym.util.registration import register | ||
from compiler_gym.util.runfiles_path import site_data_path | ||
|
||
EXAMPLE_PY_SERVICE_BINARY: Path = Path( | ||
"example_compiler_gym_service/service_py/example_service.py" | ||
) | ||
assert EXAMPLE_PY_SERVICE_BINARY.is_file(), "Service script not found" | ||
|
||
|
||
class RuntimeReward(Reward): | ||
"""An example reward that uses changes in the "runtime" observation value | ||
to compute incremental reward. | ||
""" | ||
|
||
def __init__(self): | ||
super().__init__( | ||
id="runtime", | ||
observation_spaces=["runtime"], | ||
default_value=0, | ||
default_negates_returns=True, | ||
deterministic=False, | ||
platform_dependent=True, | ||
) | ||
self.previous_runtime = None | ||
|
||
def reset(self, benchmark: str, observation_view): | ||
del benchmark # unused | ||
self.previous_runtime = None | ||
|
||
def update(self, action, observations, observation_view): | ||
del action | ||
del observation_view | ||
|
||
if self.previous_runtime is None: | ||
self.previous_runtime = observations[0] | ||
|
||
reward = float(self.previous_runtime - observations[0]) | ||
self.previous_runtime = observations[0] | ||
return reward | ||
|
||
|
||
class ExampleDataset(Dataset): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__( | ||
name="benchmark://example-v0", | ||
license="MIT", | ||
description="An example dataset", | ||
site_data_base=site_data_path("example_dataset"), | ||
) | ||
self._benchmarks = { | ||
"benchmark://example-v0/foo": Benchmark.from_file_contents( | ||
"benchmark://example-v0/foo", "Ir data".encode("utf-8") | ||
), | ||
"benchmark://example-v0/bar": Benchmark.from_file_contents( | ||
"benchmark://example-v0/bar", "Ir data".encode("utf-8") | ||
), | ||
} | ||
|
||
def benchmark_uris(self) -> Iterable[str]: | ||
yield from self._benchmarks.keys() | ||
|
||
def benchmark(self, uri: str) -> Benchmark: | ||
if uri in self._benchmarks: | ||
return self._benchmarks[uri] | ||
else: | ||
raise LookupError("Unknown program name") | ||
|
||
|
||
# Register the environment for use with gym.make(...). | ||
register( | ||
id="example-v0", | ||
entry_point="compiler_gym.envs:CompilerEnv", | ||
kwargs={ | ||
"service": EXAMPLE_PY_SERVICE_BINARY, | ||
"rewards": [RuntimeReward()], | ||
"datasets": [ExampleDataset()], | ||
}, | ||
) | ||
|
||
|
||
def main(): | ||
# Use debug verbosity to print out extra logging information. | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
# Create the environment using the regular gym.make(...) interface. | ||
with gym.make("example-v0") as env: | ||
env.reset() | ||
for _ in range(20): | ||
observation, reward, done, info = env.step(env.action_space.sample()) | ||
if done: | ||
env.reset() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
10 changes: 10 additions & 0 deletions
10
examples/example_compiler_gym_service/demo_without_bazel_test.py
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,10 @@ | ||
# 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. | ||
"""Smoke test for examples/example_compiler_gym_service/demo_without_bazel.py""" | ||
from example_compiler_gym_service.demo_without_bazel import main | ||
|
||
|
||
def test_demo_without_bazel(): | ||
main() |
Oops, something went wrong.