-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
133 lines (115 loc) · 4.56 KB
/
__init__.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
"""
This file register gcc_multienv-v0 environment with CompilerGym
and defines the reward function for it
"""
from pathlib import Path
from compiler_gym.spaces import Reward
from compiler_gym.util.registration import register
from compiler_gym.util.runfiles_path import runfiles_path
from compiler_gym.util.gym_type_hints import ActionType
from compiler_gym.envs.gcc_multienv.datasets import *
import math
GCC_MULTIENV_SERVICE_BINARY: Path = runfiles_path(
"compiler_gym/envs/gcc_multienv/service/gcc-multienv-service"
)
class SizeRuntimeReward(Reward):
def __init__(self):
super().__init__(
name="size_runtime",
observation_spaces=[
"runtime_sec",
"runtime_percent",
"size",
"base_size",
"base_runtime_sec",
"base_runtime_percent",
],
default_value=0,
default_negates_returns=False,
deterministic=False,
platform_dependent=True,
)
self.RUNTIME_WEIGHT = 0.5
def reset(self, benchmark: str, observation_view):
"""
Record information about the initital state
"""
self.prev_runtime_percent = observation_view["runtime_percent"]
self.prev_runtime_sec = observation_view["runtime_sec"]
self.prev_size = observation_view["size"]
def update(self, action, observations, observation_view):
"""
Calculate and normalize size and runtime reduction in new state compared to previous state.
If function runtime took less than 1 percent of overall program runtime we consider
runtime value to be too noisy and not very important to overall runtime, and do not include it
in reward calculation. Normalized runtime reduction is multiplied by RUNTIME_WEIGHT<1,
to promote reducing size (while keeping runtime somewhat constant or improving it)
"""
size_diff_norm = (self.prev_size - observation_view["size"]) / self.prev_size
if self.prev_runtime_percent < 1 and observation_view["runtime_percent"] < 1:
runtime_diff_norm = 0
else:
if self.prev_runtime_sec == 0:
runtime_diff_norm = 1
else:
runtime_diff_norm = (
self.prev_runtime_sec - observation_view["runtime_sec"]
) / self.prev_runtime_sec
diff = size_diff_norm + runtime_diff_norm * (
self.RUNTIME_WEIGHT if runtime_diff_norm > 0 else 1
)
self.prev_runtime_percent = observation_view["runtime_percent"]
self.prev_runtime_sec = observation_view["runtime_sec"]
self.prev_size = observation_view["size"]
return diff
class GAReward(Reward):
def __init__(self):
super().__init__(
name="ga_reward",
observation_spaces=[
"runtime_sec",
"runtime_percent",
"size",
"base_size",
"base_runtime_sec",
"base_runtime_percent",
],
default_value=0,
default_negates_returns=False,
deterministic=False,
platform_dependent=True,
)
self.base_runtime_sec = None
self.base_runtime_percent = None
self.base_size = None
def reset(self, benchmark: str, observation_view):
self.base_runtime_sec = observation_view["base_runtime_sec"]
self.base_runtime_percent = observation_view["base_runtime_percent"]
self.base_size = observation_view["base_size"]
def update(self, action, observations, observation_view):
size = observation_view["size"]
if size == 0:
return 0
size_delta = (self.base_size - size) / self.base_size
if size_delta < 0:
return size_delta
else:
runtime = observation_view["runtime_sec"]
if self.base_runtime_percent < 0.5 and observation_view["runtime_percent"] < 0.5:
runtime_delta = 0
else:
runtime_delta = (self.base_runtime_sec - runtime) / self.base_runtime_sec
if runtime_delta >= 0:
return size_delta
else:
return size_delta + 2 * runtime_delta
return size_delta
register(
id="gcc_multienv-v0",
entry_point="compiler_gym.service.client_service_compiler_env:ClientServiceCompilerEnv",
kwargs={
"service": GCC_MULTIENV_SERVICE_BINARY,
"rewards": [SizeRuntimeReward(), GAReward()],
"datasets": [MultienvDataset()],
},
)