Skip to content

Commit

Permalink
[examples] Overhaul the example service README.
Browse files Browse the repository at this point in the history
The documentation for the example services has fallen out of date, as
reported in #466. This patch updates the README to include the latest
usage instructions.
  • Loading branch information
ChrisCummins committed Oct 13, 2021
1 parent 2412790 commit ee4e19d
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions examples/example_compiler_gym_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,60 @@ RPC interface that the frontend interacts with.

This directory contains an example backend service implemented in C++ and
Python. Both implementations have the same features. They don't do any actual
compilation, but can be used as a starting point for writing new services, or
for debugging and testing frontend code.
compilation, but can be used as a starting point for adding support for new
compilers, or for debugging and testing frontend code.

Features:
If you have any questions please [file an
issue](https://github.com/facebookresearch/CompilerGym/issues/new/choose).

* Enforces the service contract, e.g. `StartSession()` must be called before
`EndSession()`, list indices must be in-bounds, etc.
* Implements all of the RPC endpoints.

## Features

* A static action space with three items: `["a", "b", "c"]`. The action space
never changes. Actions never end the episode.
* There are two observation spaces:
* `ir` which returns the string "Hello, world!".
* `features` which returns an `int64_list` of `[0, 0, 0]`.
* A single reward space `runtime` which returns 0.
* It has a single dataset "benchmark://example-v0" with two programs "foo" and
"bar".
* It has a static action space with three items: `["a", "b", "c"]`. The action
space never changes. Actions never end the episode.
* There are two observation spaces:
* `ir` which returns the string "Hello, world!".
* `features` which returns an `int64_list` of `[0, 0, 0]`.
* There is a single reward space `runtime` which returns 0.
* Supports default observation and reward spaces.

See [service_cc/ExampleService.h](service_cc/ExampleService.h) for the C++
service implementation,
[service_py/example_service.py](service_py/example_service.py) for the Python
version, [__init__.py](__init__.py) for a python module that registers this
service with the gym on import, and [env_tests.py](env_tests.py) for tests.

## Implementation

There are two identical service implementations, one in Python, one in C++. See
[service_cc/ExampleService.h](service_cc/ExampleService.h) for the C++ service,
and [service_py/example_service.py](service_py/example_service.py) for the
Python service. The module [__init__.py](__init__.py) defines the reward space,
dataset, and registers two new environments using these services.

The file [demo.py](demo.py) demonstrates how to use these example environments
using CompilerGym's bazel build system. The file [env_tests.py](env_tests.py)
contains unit tests for the example services. Because the Python and C++
services implement the same interface, the same tests are run against both
environments.

## Usage

Start an example service using:
Run the demo script using:

```sh
$ bazel run -c opt //examples/example_compiler_gym_service/service -- \
--port=8080 --working_dir=/tmp
$ bazel run -c opt //examples/example_compiler_gym_service:demo
```

The service never terminates and does not print logging messages. Interact with
the RPC endpoints using your frontend of choice, for example, the manual
environment:
Run the unit tests using:

```sh
$ bazel run -c opt //compiler_gym/bin:manual_env -- --service=localhost:8080
$ bazel test //examples/example_compiler_gym_service/...
```

Kill the service using C-c when you are done.
### Using the python service without bazel

Because the python service contains no compiled code, it can be run directly as
a standalone script without using the bazel build system. From the root of the
CompilerGym repository, run:

```sh
$ cd examples
$ python3 example_compiler_gym_service/demo_without_bazel.py
```

0 comments on commit ee4e19d

Please sign in to comment.