-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathaction_sensitivity_analysis.py
193 lines (166 loc) · 6.24 KB
/
action_sensitivity_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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.
"""Estimate the immediate reward of different actions using random trials.
This script estimates the immediate reward that running a specific action has by
running trials. A trial is a random episode that ends with the determined
action.
Example Usage
-------------
Evaluate the impact of three passes on the codesize of the cBench-crc32
benchmark:
$ bazel run -c opt //compiler_gym/bin:action_sensitivity_analysis -- \
--env=llvm-v0 --reward=IrInstructionCountO3 \
--benchmark=cbench-v1/crc32 --num_trials=100 \
--action=AddDiscriminatorsPass,AggressiveDcepass,AggressiveInstCombinerPass
Evaluate the single-step immediate reward of all actions on LLVM codesize:
$ bazel run -c opt //compiler_gym/bin:action_ensitivity_analysis -- \
--env=llvm-v0 --reward=IrInstructionCountO3
"""
import random
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import cpu_count
from pathlib import Path
from typing import List, Optional
import numpy as np
from absl import app, flags
from sensitivity_analysis.sensitivity_analysis_eval import (
SensitivityAnalysisResult,
run_sensitivity_analysis,
)
from compiler_gym.envs import CompilerEnv
from compiler_gym.util.flags.benchmark_from_flags import benchmark_from_flags
from compiler_gym.util.flags.env_from_flags import env_from_flags
from compiler_gym.util.gym_type_hints import ActionType
from compiler_gym.util.logs import create_logging_dir
from compiler_gym.util.timer import Timer
flags.DEFINE_integer(
"num_action_sensitivity_trials",
100,
"The number of trials to perform when estimating the reward of each action. "
"A trial is a random episode that ends with the determined action. Increasing "
"this number increases the number of trials performed, leading to a higher "
"fidelity estimate of the reward of an action.",
)
flags.DEFINE_integer(
"max_warmup_steps",
25,
"The maximum number of random steps to make before determining the reward of an action.",
)
flags.DEFINE_list(
"action",
[],
"An optional list of actions to evaluate. If not specified, all actions will be evaluated.",
)
flags.DEFINE_integer(
"max_action_attempts_multiplier",
5,
"A trial may fail because the environment crashes, or an action produces an invalid state. "
"Limit the total number of trials performed for each action to "
"max_action_attempts_multiplier * num_trials.",
)
FLAGS = flags.FLAGS
def get_rewards(
action: int,
action_name: str,
reward_space: str,
num_trials: int,
max_warmup_steps: int,
max_attempts_multiplier: int = 5,
) -> SensitivityAnalysisResult:
"""Run random trials to get a list of num_trials immediate rewards."""
rewards, runtimes = [], []
benchmark = benchmark_from_flags()
num_attempts = 0
while (
num_attempts < max_attempts_multiplier * num_trials
and len(rewards) < num_trials
):
num_attempts += 1
with env_from_flags(benchmark=benchmark) as env:
env.observation_space = None
env.reward_space = None
env.reset(benchmark=benchmark)
with Timer() as t:
reward = run_one_trial(env, reward_space, action, max_warmup_steps)
if reward is not None:
rewards.append(reward)
runtimes.append(t.time)
return SensitivityAnalysisResult(
name=action_name, runtimes=np.array(runtimes), rewards=np.array(rewards)
)
def run_one_trial(
env: CompilerEnv, reward_space: str, action: int, max_warmup_steps: int
) -> Optional[float]:
"""Run a random number of "warmup" steps in an environment, then compute
the immediate reward of the given action.
:return: An immediate reward.
"""
num_warmup_steps = random.randint(0, max_warmup_steps)
warmup_actions = [env.action_space.sample() for _ in range(num_warmup_steps)]
env.reward_space = reward_space
_, _, done, _ = env.multistep(warmup_actions)
if done:
return None
_, (reward,), done, _ = env.step(action, reward_spaces=[reward_space])
return None if done else reward
def run_action_sensitivity_analysis(
actions: List[ActionType],
rewards_path: Path,
runtimes_path: Path,
reward_space: str,
num_trials: int,
max_warmup_steps: int,
nproc: int = cpu_count(),
max_attempts_multiplier: int = 5,
):
"""Estimate the immediate reward of a given list of actions."""
with env_from_flags() as env:
action_names = env.action_space.names
with ThreadPoolExecutor(max_workers=nproc) as executor:
analysis_futures = {
executor.submit(
get_rewards,
action,
action_names[action],
reward_space,
num_trials,
max_warmup_steps,
max_attempts_multiplier,
)
for action in actions
}
return run_sensitivity_analysis(
analysis_futures=analysis_futures,
runtimes_path=runtimes_path,
rewards_path=rewards_path,
)
def main(argv):
"""Main entry point."""
argv = FLAGS(argv)
if len(argv) != 1:
raise app.UsageError(f"Unknown command line arguments: {argv[1:]}")
with env_from_flags() as env:
action_names = env.action_space.names
if FLAGS.action:
actions = [action_names.index(a) for a in FLAGS.action]
else:
actions = list(range(len(action_names)))
logs_dir = Path(
FLAGS.output_dir or create_logging_dir("benchmark_sensitivity_analysis")
)
rewards_path = logs_dir / f"actions_{FLAGS.reward}.rewards.csv"
runtimes_path = logs_dir / f"actions_{FLAGS.reward}.runtimes.csv"
run_action_sensitivity_analysis(
rewards_path=rewards_path,
runtimes_path=runtimes_path,
actions=actions,
reward=FLAGS.reward,
num_trials=FLAGS.num_action_sensitivity_trials,
max_warmup_steps=FLAGS.max_warmup_steps,
nproc=FLAGS.nproc,
max_attempts_multiplier=FLAGS.max_action_attempts_multiplier,
)
if __name__ == "__main__":
app.run(main)