-
Notifications
You must be signed in to change notification settings - Fork 38
/
training.py
301 lines (228 loc) · 10.3 KB
/
training.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from typing import *
import math
import threading
import concurrent.futures
import numpy as np
from asm2vec.asm import Instruction
from asm2vec.internal.repr import FunctionRepository
from asm2vec.internal.repr import VectorizedFunction
from asm2vec.internal.repr import Token
from asm2vec.internal.repr import VectorizedToken
from asm2vec.internal.sampling import NegativeSampler
from asm2vec.internal.atomic import LockContextManager
from asm2vec.internal.atomic import Atomic
from asm2vec.logging import asm2vec_logger
class Asm2VecParams:
def __init__(self, **kwargs):
self.d: int = kwargs.get('d', 200)
self.initial_alpha: float = kwargs.get('alpha', 0.0025)
self.alpha_update_interval: int = kwargs.get('alpha_update_interval', 10000)
self.num_of_rnd_walks: int = kwargs.get('rnd_walks', 3)
self.neg_samples: int = kwargs.get('neg_samples', 25)
self.iteration: int = kwargs.get('iteration', 1)
self.jobs: int = kwargs.get('jobs', 4)
def to_dict(self) -> Dict[str, Any]:
return {
'd': self.d,
'alpha': self.initial_alpha,
'alpha_update_interval': self.alpha_update_interval,
'num_of_rnd_walks': self.num_of_rnd_walks,
'neg_samples': self.neg_samples,
'iteration': self.iteration,
'jobs': self.jobs
}
def populate(self, rep: Dict[bytes, Any]) -> None:
self.d: int = rep.get(b'd', 200)
self.initial_alpha: float = rep.get(b'alpha', 0.0025)
self.alpha_update_interval: int = rep.get(b'alpha_update_interval', 10000)
self.num_of_rnd_walks: int = rep.get(b'rnd_walks', 3)
self.neg_samples: int = rep.get(b'neg_samples', 25)
self.iteration: int = rep.get(b'iteration', 1)
self.jobs: int = rep.get(b'jobs', 4)
class SequenceWindow:
def __init__(self, sequence: List[Instruction], vocabulary: Dict[str, Token]):
self._seq = sequence
self._vocab = vocabulary
self._i = 1
self._prev_ins = None
self._curr_ins = None
self._next_ins = None
self._prev_ins_op = None
self._prev_ins_args = None
self._curr_ins_op = None
self._curr_ins_args = None
self._next_ins_op = None
self._next_ins_args = None
def move_next(self) -> bool:
if self._i >= len(self._seq) - 1:
return False
def token_lookup(name) -> VectorizedToken:
return self._vocab[name].vectorized()
self._prev_ins = self._seq[self._i - 1]
self._curr_ins = self._seq[self._i]
self._next_ins = self._seq[self._i + 1]
self._prev_ins_op = token_lookup(self._prev_ins.op())
self._prev_ins_args = list(map(token_lookup, self._prev_ins.args()))
self._curr_ins_op = token_lookup(self._curr_ins.op())
self._curr_ins_args = list(map(token_lookup, self._curr_ins.args()))
self._next_ins_op = token_lookup(self._next_ins.op())
self._next_ins_args = list(map(token_lookup, self._next_ins.args()))
self._i += 1
return True
def prev_ins(self) -> Instruction:
return self._prev_ins
def prev_ins_op(self) -> VectorizedToken:
return self._prev_ins_op
def prev_ins_args(self) -> List[VectorizedToken]:
return self._prev_ins_args
def curr_ins(self) -> Instruction:
return self._curr_ins
def curr_ins_op(self) -> VectorizedToken:
return self._curr_ins_op
def curr_ins_args(self) -> List[VectorizedToken]:
return self._curr_ins_args
def next_ins(self) -> Instruction:
return self._next_ins
def next_ins_op(self) -> VectorizedToken:
return self._next_ins_op
def next_ins_args(self) -> List[VectorizedToken]:
return self._next_ins_args
class TrainingContext:
class Counter:
def __init__(self, context: 'TrainingContext', name: str, initial: int = 0):
self._context = context
self._name = name
self._val = initial
def val(self) -> int:
with self._context.lock():
return self._val
def inc(self) -> int:
with self._context.lock():
self._val += 1
return self._val
def reset(self) -> int:
with self._context.lock():
v = self._val
self._val = 0
return v
TOKENS_HANDLED_COUNTER: str = "tokens_handled"
def __init__(self, repo: FunctionRepository, params: Asm2VecParams, is_estimating: bool = False):
self._repo = repo
self._params = params
self._alpha = params.initial_alpha
self._sampler = NegativeSampler(list(map(lambda t: (t, t.frequency), repo.vocab().values())))
self._is_estimating = is_estimating
self._counters = dict()
self._lock = threading.Lock()
def repo(self) -> FunctionRepository:
return self._repo
def params(self) -> Asm2VecParams:
return self._params
def lock(self) -> LockContextManager:
return LockContextManager(self._lock)
def alpha(self) -> float:
with self.lock():
return self._alpha
def set_alpha(self, alpha: float) -> None:
with self.lock():
self._alpha = alpha
def sampler(self) -> NegativeSampler:
return self._sampler
def is_estimating(self) -> bool:
return self._is_estimating
def create_sequence_window(self, seq: List[Instruction]) -> SequenceWindow:
return SequenceWindow(seq, self._repo.vocab())
def get_counter(self, name: str) -> Counter:
with self.lock():
return self._counters.get(name)
def add_counter(self, name: str, initial: int = 0) -> Counter:
with self.lock():
c = self.__class__.Counter(self, name, initial)
self._counters[name] = c
return c
def _sigmoid(x: float) -> float:
return 1 / (1 + np.exp(x))
def _identity(cond: bool) -> int:
return 1 if cond else 0
def _dot_sigmoid(lhs: np.ndarray, rhs: np.ndarray) -> float:
# noinspection PyTypeChecker
return _sigmoid(np.dot(lhs, rhs))
def _get_inst_repr(op: VectorizedToken, args: List[VectorizedToken]) -> np.ndarray:
if len(args) == 0:
arg_vec = np.zeros(len(op.v))
else:
arg_vec = np.average(list(map(lambda tk: tk.v, args)), axis=0)
return np.hstack((op.v, arg_vec))
def _train_vectorized(wnd: SequenceWindow, f: VectorizedFunction, context: TrainingContext) -> None:
ct_prev = _get_inst_repr(wnd.prev_ins_op(), wnd.prev_ins_args())
ct_next = _get_inst_repr(wnd.next_ins_op(), wnd.next_ins_args())
delta = np.average([ct_prev, f.v, ct_next], axis=0)
tokens = [wnd.curr_ins_op()] + wnd.curr_ins_args()
f_grad = np.zeros(f.v.shape)
for tk in tokens:
# Negative sampling.
sampled_tokens: Dict[str, VectorizedToken] = \
dict(map(lambda x: (x.name(), x.vectorized()), context.sampler().sample(context.params().neg_samples)))
if tk.name() not in sampled_tokens:
sampled_tokens[tk.name()] = tk
# The following code block tries to update the learning rate when necessary. Not required for now.
# tokens_handled_counter = context.get_counter(TrainingContext.TOKENS_HANDLED_COUNTER)
# if tokens_handled_counter is not None:
# if tokens_handled_counter.val() % context.params().alpha_update_interval == 0:
# # Update the learning rate.
# alpha = 1 - tokens_handled_counter.val() / (
# context.params().iteration * context.repo().num_of_tokens() + 1)
# context.set_alpha(max(alpha, context.params().initial_alpha * 0.0001))
for sp_tk in sampled_tokens.values():
# Accumulate gradient for function vector.
g = (_identity(tk is sp_tk) - _dot_sigmoid(delta, tk.v_pred)) * context.alpha()
f_grad += g / 3 * tk.v_pred
if not context.is_estimating():
with context.lock():
# Update v'_t
tk.v_pred -= g * delta
# Apply function gradient.
with context.lock():
f.v -= f_grad
if not context.is_estimating():
# Apply gradient to instructions.
d = len(f_grad) // 2
with context.lock():
wnd.prev_ins_op().v -= f_grad[:d]
if len(wnd.prev_ins_args()) > 0:
prev_args_grad = f_grad[d:] / len(wnd.prev_ins_args())
for t in wnd.prev_ins_args():
t.v -= prev_args_grad
wnd.next_ins_op().v -= f_grad[:d]
if len(wnd.next_ins_args()) > 0:
next_args_grad = f_grad[d:] / len(wnd.next_ins_args())
for t in wnd.next_ins_args():
t.v -= next_args_grad
def _train_sequence(f: VectorizedFunction, seq: List[Instruction], context: TrainingContext) -> None:
wnd = context.create_sequence_window(seq)
while wnd.move_next():
_train_vectorized(wnd, f, context)
def train(repository: FunctionRepository, params: Asm2VecParams) -> None:
context = TrainingContext(repository, params)
context.add_counter(TrainingContext.TOKENS_HANDLED_COUNTER)
asm2vec_logger().debug('Total number of functions: %d', len(context.repo().funcs()))
progress = Atomic(1)
def train_function(fn: VectorizedFunction):
for seq in fn.sequential().sequences():
_train_sequence(fn, seq, context)
asm2vec_logger().debug('Function "%s" trained, progress: %f%%',
fn.sequential().name(), progress.value() / len(context.repo().funcs()) * 100)
with progress.lock() as prog_proxy:
prog_proxy.set(prog_proxy.value() + 1)
executor = concurrent.futures.ThreadPoolExecutor(max_workers=context.params().jobs)
futures = []
for f in context.repo().funcs():
futures.append(executor.submit(train_function, f))
done, not_done = concurrent.futures.wait(futures, return_when=concurrent.futures.FIRST_EXCEPTION)
if len(not_done) > 0:
raise RuntimeError('Train failed due to one or more failed task.')
def estimate(f: VectorizedFunction, estimate_repo: FunctionRepository, params: Asm2VecParams) -> np.ndarray:
context = TrainingContext(estimate_repo, params, True)
for seq in f.sequential().sequences():
_train_sequence(f, seq, context)
return f.v