-
Notifications
You must be signed in to change notification settings - Fork 128
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 #432 from facebookresearch/unrolling-example-service
Added an example service that focuses on loop unrolling optimization
- Loading branch information
Showing
20 changed files
with
1,086 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
filegroup( | ||
name = "header", | ||
srcs = ["header.h"], | ||
visibility = ["//visibility:public"], | ||
) |
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,29 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2019, Ameer Haj Ali (UC Berkeley), and Intel Corporation | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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,51 @@ | ||
/* | ||
Copyright (c) 2019, Ameer Haj Ali (UC Berkeley), and Intel Corporation | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <sys/time.h> | ||
|
||
/** | ||
* Warmup and then measure. | ||
* | ||
* Adapted from Neurovectorizer's implementation: | ||
* https://github.com/intel/neuro-vectorizer/blob/d1b068998c08865c59f1586845bb947229f70a51/training_data/header.h | ||
* | ||
* Which was in turn adapted from LLVM: | ||
* https://github.com/llvm/llvm-test-suite/blob/7eca159e29ca4308256ef6e35560a2d884ac6b01/SingleSource/UnitTests/Vectorizer/gcc-loops.cpp#L330-L336 | ||
*/ | ||
#define BENCH(NAME, RUN_LINE, ITER, DIGEST_LINE) \ | ||
{ \ | ||
struct timeval Start, End; \ | ||
RUN_LINE; \ | ||
gettimeofday(&Start, 0); \ | ||
for (int i = 0; i < (ITER); ++i) RUN_LINE; \ | ||
gettimeofday(&End, 0); \ | ||
unsigned r = DIGEST_LINE; \ | ||
long mtime, s, us; \ | ||
s = End.tv_sec - Start.tv_sec; \ | ||
us = End.tv_usec - Start.tv_usec; \ | ||
mtime = (s * 1000 + us / 1000.0) + 0.5; \ | ||
printf("%ld", mtime); \ | ||
} |
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,44 @@ | ||
# 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. | ||
|
||
import subprocess | ||
import sys | ||
from signal import Signals | ||
from typing import List | ||
|
||
|
||
def run_command(cmd: List[str], timeout: int): | ||
process = subprocess.Popen( | ||
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True | ||
) | ||
stdout, stderr = communicate(process, timeout=timeout) | ||
if process.returncode: | ||
returncode = process.returncode | ||
try: | ||
# Try and decode the name of a signal. Signal returncodes | ||
# are negative. | ||
returncode = f"{returncode} ({Signals(abs(returncode)).name})" | ||
except ValueError: | ||
pass | ||
raise OSError( | ||
f"Compilation job failed with returncode {returncode}\n" | ||
f"Command: {' '.join(cmd)}\n" | ||
f"Stderr: {stderr.strip()}" | ||
) | ||
return stdout | ||
|
||
|
||
def communicate(process, input=None, timeout=None): | ||
"""subprocess.communicate() which kills subprocess on timeout.""" | ||
try: | ||
return process.communicate(input=input, timeout=timeout) | ||
except subprocess.TimeoutExpired: | ||
# kill() was added in Python 3.7. | ||
if sys.version_info >= (3, 7, 0): | ||
process.kill() | ||
else: | ||
process.terminate() | ||
process.communicate(timeout=timeout) # Wait for shutdown to complete. | ||
raise |
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,38 @@ | ||
# 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. | ||
load("@rules_python//python:defs.bzl", "py_library", "py_test") | ||
|
||
py_library( | ||
name = "example_unrolling_service", | ||
srcs = ["__init__.py"], | ||
data = [ | ||
"//examples/example_unrolling_service/benchmarks", | ||
"//examples/example_unrolling_service/service_py:example-unrolling-service-py", | ||
], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//compiler_gym/envs/llvm", | ||
"//compiler_gym/util", | ||
], | ||
) | ||
|
||
py_test( | ||
name = "env_tests", | ||
srcs = ["env_tests.py"], | ||
deps = [ | ||
":example_unrolling_service", | ||
"//compiler_gym", | ||
"//tests:test_main", | ||
], | ||
) | ||
|
||
py_binary( | ||
name = "example", | ||
srcs = ["example.py"], | ||
deps = [ | ||
":example_unrolling_service", | ||
"//compiler_gym", | ||
], | ||
) |
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,30 @@ | ||
# Unrolling CompilerGym Service Example | ||
|
||
This is an example of how to create your own CompilerGym environment. All paths listed below are relative to the path of this README file. | ||
|
||
* Actions: this environment focuses on the unrolling optimization. The actions are the different unrolling factors. | ||
- The actions are listed in `action_spaces` struct in `service_py/example_service.py` | ||
- The actions are implemented in `apply_action(...)` function in `service_py/example_service.py` | ||
* Observations: the observations are: textual form of the LLVM IR, statistical features of different types of IR instructions, runtime execution, or code size | ||
- The observations are listed in `observation_spaces` struct in `service_py/example_service.py`. | ||
- The observations are implemented in `get_observation(...)` function in `service_py/example_service.py` | ||
* Rewards: the rewards could be runtime or code size. | ||
- The rewards are implemented in `__init__.py` and they reuse the runtime and code size observations mentioned above | ||
* Benchmarks: this environment expects your benchmarks to follow the templates from the [Neruovectorizer repo](https://github.com/intel/neuro-vectorizer/tree/master/training_data) repo, that was in turn adapted from the [LLVM loop test suite](https://github.com/llvm/llvm-test-suite/blob/main/SingleSource/UnitTests/Vectorizer/gcc-loops.cpp). | ||
- To implement your benchmark, you need to: include the `header.h` file, implement your benchmark in a custom function, then invoke it using `BENCH` macro inside the `main()` function. | ||
- Following this template is necessary in order for the benchmark to measure the execution runtime and write it to stdout, which is in turn parsed by this environment to measure the runtime reward. | ||
- You can view and add examples of benchmarks in `benchmarks` directory | ||
- Also, when adding your own benchmark, you need to add it to the `UnrollingDataset` class in `__init__.py` | ||
|
||
## Usage | ||
|
||
Run `example.py` example: | ||
```sh | ||
$ bazel run //examples/example_unrolling_service:example | ||
``` | ||
|
||
Run `env_tests.py` unit tests: | ||
|
||
```sh | ||
$ bazel test //examples/example_unrolling_service:env_tests | ||
``` |
Oops, something went wrong.