facebookresearch / CompilerGym

Reinforcement learning environments for compiler and program optimization tasks
https://compilergym.ai/
MIT License
906 stars 127 forks source link

Add a wrapper to synchronously log environment state transitions to SQLite database #679

Closed ChrisCummins closed 2 years ago

ChrisCummins commented 2 years ago

This adds a compiler_gym.envs.wrappers.SynchronousSqliteLogger wrapper for an LLVM environment that logs all transitions to an sqlite database.

Wrap an existing LLVM environment and then use it as per normal:

>>> env = SynchronousSqliteLogger(
...     env=gym.make("llvm-autophase-ic-v0"),
...     db_path="example.db",
... )

Connect to the database file you specified:

$ sqlite3 example.db

There are two tables:

  1. States: records every unique combination of benchmark + actions. For each entry, records an identifying state ID, the episode reward, and whether the episode is terminated:
sqlite> .mode markdown
sqlite> .headers on
sqlite> select * from States limit 5;
|      benchmark_uri       | done | ir_instruction_count_oz_reward |                                 state_id | actions |
|--------------------------|------|--------------------------------|------------------------------------------|----------------|
| generator://csmith-v0/99 | 0    | 0.0                            | d625b874e58f6d357b816e21871297ac5c001cf0 |                |
| generator://csmith-v0/99 | 0    | 0.0                            | d625b874e58f6d357b816e21871297ac5c001cf0 | 31             |
| generator://csmith-v0/99 | 0    | 0.0                            | 52f7142ef606d8b1dec2ff3371c7452c8d7b81ea | 31 116         |
| generator://csmith-v0/99 | 0    | 0.268005818128586              | d8c05bd41b7a6c6157b6a8f0f5093907c7cc7ecf | 31 116 103     |
| generator://csmith-v0/99 | 0    | 0.288621664047241              | c4d7ecd3807793a0d8bc281104c7f5a8aa4670f9 | 31 116 103 109 |
  1. Observations: records pickled, compressed, and text observation values for each unique state.

Caveats of this implementation:

  1. Only LlvmEnv environments may be wrapped.
  2. The wrapped environment must have an observation space and reward space set.
  3. The observation spaces and reward spaces that are logged to database are hardcoded. To change what is recorded, you must copy and modify this implementation.
  4. Writing to the database is synchronous and adds significant overhead to the compute cost of the environment.

Fixes #678.

codecov-commenter commented 2 years ago

Codecov Report

Merging #679 (426f873) into development (947b567) will decrease coverage by 0.24%. The diff coverage is 92.59%.

@@               Coverage Diff               @@
##           development     #679      +/-   ##
===============================================
- Coverage        88.61%   88.36%   -0.25%     
===============================================
  Files              126      127       +1     
  Lines             7597     7678      +81     
===============================================
+ Hits              6732     6785      +53     
- Misses             865      893      +28     
Impacted Files Coverage Δ
compiler_gym/util/timer.py 100.00% <ø> (ø)
compiler_gym/wrappers/sqlite_logger.py 92.40% <92.40%> (ø)
compiler_gym/wrappers/__init__.py 100.00% <100.00%> (ø)
compiler_gym/__init__.py 87.50% <0.00%> (-6.25%) :arrow_down:
...loop_tool/service/loop_tool_compilation_session.py 86.48% <0.00%> (-4.73%) :arrow_down:
compiler_gym/util/download.py 94.11% <0.00%> (-2.95%) :arrow_down:
compiler_gym/service/connection.py 78.66% <0.00%> (-2.00%) :arrow_down:
compiler_gym/envs/llvm/datasets/cbench.py 79.42% <0.00%> (-1.09%) :arrow_down:
compiler_gym/random_search.py 92.10% <0.00%> (-0.53%) :arrow_down:
...ompiler_gym/service/client_service_compiler_env.py 90.85% <0.00%> (-0.42%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 947b567...426f873. Read the comment docs.