-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathu_grammar_predictor.py
347 lines (316 loc) · 12.2 KB
/
u_grammar_predictor.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
from collections import defaultdict
from typing import (
Callable,
Dict,
Generic,
Iterable,
List,
Literal,
Set,
Tuple,
Optional,
TypeVar,
Union,
)
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import Tensor
from synth.syntax.grammars.tagged_u_grammar import TaggedUGrammar, ProbUGrammar
from synth.syntax.grammars.u_grammar import DerivableProgram, UGrammar
from synth.syntax.program import Constant, Primitive, Program, Variable
from synth.syntax.type_system import Type
A = TypeVar("A")
U = TypeVar("U")
V = TypeVar("V")
W = TypeVar("W")
class TensorLogProbUGrammar(TaggedUGrammar[Tensor, U, V, W]):
"""
Special version to compute with Tensors
"""
def __init__(
self,
grammar: UGrammar[U, V, W],
tags: Dict[Tuple[Type, U], Dict[DerivableProgram, Dict[V, Tensor]]],
start_tags: Dict[Tuple[Type, U], Tensor],
):
super().__init__(grammar, tags, start_tags)
some_start = list(self.starts)[0]
some_P = list(self.tags[some_start].keys())[0]
some_V = list(self.tags[some_start][some_P].keys())[0]
self.device = self.tags[some_start][some_P][some_V].device
def log_probability(
self,
program: Program,
start: Optional[Tuple[Type, U]] = None,
) -> Tensor:
device = self.device
return self.reduce_derivations(
lambda current, S, P, V: current + self.tags[S][P][tuple(V)], # type: ignore
torch.zeros((1,)).to(device),
program,
start,
)[0]
def to_prob_u_grammar(self) -> ProbUGrammar[U, V, W]:
probabilities = {
S: {
P: {key: np.exp(t_prob.item()) for key, t_prob in dico.items()}
for P, dico in self.tags[S].items()
}
for S in self.tags
}
start_probs = {
S: np.exp(t_prob.item()) for S, t_prob in self.start_tags.items()
}
out = ProbUGrammar(self.grammar, probabilities, start_probs)
out.normalise()
return out
class UGrammarPredictorLayer(nn.Module, Generic[A, U, V, W]):
"""
Parameters:
------------
- input_size: int - the input size of the tensor to this layer
- grammars: Iterable[DetGrammar[U, V, W]] - the set of all supported grammars
- variable_probability: float = 0.2 - the probability mass of all variable at any given derivation level
"""
def __init__(
self,
input_size: int,
grammars: Iterable[UGrammar[U, V, W]],
abstraction: Callable[[Tuple[Type, U]], A],
variable_probability: float = 0.2,
):
super(UGrammarPredictorLayer, self).__init__()
self.grammar_dictionary = {
grammar.type_request: grammar for grammar in grammars
}
self.variable_probability = variable_probability
# Compute all pairs (A, P) where A is an abstraction of S
self.abs2real: Dict[A, Set[Tuple[Type, U]]] = defaultdict(set)
self.real2abs: Dict[Tuple[Type, U], A] = {}
self.all_pairs: Dict[Optional[A], Set[Primitive]] = {}
self.all_starts_abs: List[A] = []
for grammar in grammars:
for S in grammar.rules:
abstract = abstraction(S)
self.abs2real[abstract].add(S)
self.real2abs[S] = abstract
key = abstract
if not key in self.all_pairs:
self.all_pairs[key] = set()
for P in grammar.rules[S]:
if not isinstance(P, (Variable, Constant)):
self.all_pairs[key].add(P)
for S in grammar.starts:
a = abstraction(S)
if a not in self.all_starts_abs:
self.all_starts_abs.append(a)
output_size = sum(len(self.all_pairs[S]) for S in self.all_pairs) + len(
self.all_starts_abs
)
self.output_size = output_size
self.abs2index: Dict[
Optional[A],
Tuple[int, int, Dict[Primitive, int]],
] = {}
current_index = 0
for okey, set_for_key in self.all_pairs.items():
self.abs2index[okey] = (
current_index,
len(set_for_key),
{P: i for i, P in enumerate(self.all_pairs[okey])},
)
current_index += len(set_for_key)
self.log_probs_predictor = nn.Linear(
input_size,
output_size,
)
def forward(self, x: Tensor) -> Tensor:
"""
batch_IOs is a tensor of size
(batch_size, input_size)
returns: (batch_size, self.output_size)
"""
y: Tensor = self.log_probs_predictor(x)
return y
def tensor2log_prob_grammar(
self,
x: Tensor,
type_request: Type,
total_variable_order: bool = True,
) -> TensorLogProbUGrammar[U, V, W]:
"""
Parameters:
------------
- x: Tensor - the tensor to be transformed into a TensorLogProbUGrammar
- type_request: Type - the type request of the PUCFG
- total_variable_order: bool = True - reduce very slighlty (1e-7) some variable probabilities to ensure they are totally ordered in terms of probablities
"""
device = x.device
self.__normalize__(x, x)
grammar = self.grammar_dictionary[type_request]
tags: Dict[Tuple[Type, U], Dict[DerivableProgram, Dict[V, Tensor]]] = {}
for S in grammar.rules:
tags[S] = {}
key = self.real2abs[S]
start, length, symbol2index = self.abs2index[key]
y = x[start : start + length]
# List of all variables derivable from S
variables: List[Variable] = []
constants: List[Constant] = []
# For each derivation parse probabilities
for P in grammar.rules[S]:
cpy_P = P
tags[S][cpy_P] = {}
if isinstance(P, Primitive):
primitive_index = symbol2index[P]
for v in grammar.rules[S][P]:
kv = v
if isinstance(v, List):
kv = tuple(v) # type: ignore
tags[S][cpy_P][kv] = y[primitive_index]
elif isinstance(P, Variable):
V: Variable = P # ensure typing
variables.append(V)
# All variables together have probability mass self.variable_probability
# then the probability of selecting a variable is uniform
elif isinstance(P, Constant):
C: Constant = P # ensure typing
constants.append(C)
else:
continue
# If there are variables we need to normalise
total = sum(
sum(np.exp(t_prob.item()) for t_prob in dico.values())
for P, dico in tags[S].items()
)
if variables or constants:
var_probability = self.variable_probability
if total > 0:
# Normalise rest
to_add: float = np.log((1 - self.variable_probability) / total)
for P in tags[S]:
tags[S][P] = {
z: prob + to_add for z, prob in tags[S][P].items()
}
else:
# There are no other choices than variables
var_probability = 1
# Normalise variable probability
normalised_variable_logprob: float = np.log(
var_probability / (len(variables) + len(constants))
)
for P in variables:
for v in grammar.rules[S][P]:
tags[S][P][tuple(v)] = torch.tensor( # type: ignore
normalised_variable_logprob
).to(device)
# Trick to allow a total ordering on variables
if total_variable_order:
normalised_variable_logprob = np.log(
np.exp(normalised_variable_logprob) - 1e-7
)
for P in constants:
for v in grammar.rules[S][P]:
tags[S][P][tuple(v)] = torch.tensor( # type: ignore
normalised_variable_logprob
).to(device)
else:
# We still need to normalise probabilities
# Since all derivations aren't possible
to_add = np.log(1 / total)
for P in tags[S]:
tags[S][P] = {z: prob + to_add for z, prob in tags[S][P].items()}
start_tags: Dict[Tuple[Type, U], Tensor] = {}
z = x[self.output_size - len(self.all_starts_abs) :]
for i, abs in enumerate(self.all_starts_abs):
all_S = self.abs2real[abs]
for S in all_S:
if S in grammar.starts:
start_tags[S] = z[i]
total = sum(np.exp(t_prob.item()) for t_prob in start_tags.values())
to_add = np.log(1 / total)
start_tags = {S: t_prob + to_add for S, t_prob in start_tags.items()}
grammar = TensorLogProbUGrammar(grammar, tags, start_tags)
return grammar
def encode(
self,
program: Program,
type_request: Type,
device: Union[torch.device, str, Literal[None]] = None,
) -> Tensor:
out: Tensor = torch.zeros((self.output_size), device=device)
grammar = self.grammar_dictionary[type_request]
grammar.reduce_derivations(__reduce_encoder__, (self, out), program)
return out
def __normalize__(self, src: Tensor, dst: Tensor) -> None:
# Normalize
if len(dst.shape) == 1:
for _, (start, length, _) in self.abs2index.items():
dst[start : start + length] = F.log_softmax(
src[start : start + length], dim=-1
)
else:
for _, (start, length, _) in self.abs2index.items():
dst[:, start : start + length] = F.log_softmax(
src[:, start : start + length], dim=-1
)
def loss_mse(
self,
programs: Iterable[Program],
type_requests: Iterable[Type],
batch_outputs: Tensor,
reduce: Optional[Callable[[Tensor], Tensor]] = torch.mean,
) -> Tensor:
target = torch.log(
1e-5
+ torch.stack(
[
self.encode(prog, tr, device=batch_outputs.device)
for prog, tr in zip(programs, type_requests)
]
)
).to(device=batch_outputs.device)
dst = torch.zeros_like(batch_outputs)
self.__normalize__(batch_outputs, dst)
out = F.mse_loss(dst, target)
if reduce:
out = reduce(out)
return out
def loss_negative_log_prob(
self,
programs: Iterable[Program],
log_pgrammars: Iterable[TensorLogProbUGrammar[U, V, W]],
reduce: Optional[Callable[[Tensor], Tensor]] = torch.mean,
length_normed: bool = True,
) -> Tensor:
"""
Computes the negative log prob of each solution program.
This works independently of the abstraction used.
"""
if length_normed:
log_prob_list = [
log_pgrammar.log_probability(p) / p.size()
for p, log_pgrammar in zip(programs, log_pgrammars)
]
else:
log_prob_list = [
log_pgrammar.log_probability(p)
for p, log_pgrammar in zip(programs, log_pgrammars)
]
out = -torch.stack(log_prob_list)
if reduce:
out = reduce(out)
return out
def __reduce_encoder__(
t: Tuple[UGrammarPredictorLayer[A, U, V, W], Tensor],
S: Tuple[Type, U],
P: DerivableProgram,
_: V,
) -> Tuple[UGrammarPredictorLayer[A, U, V, W], Tensor]:
if isinstance(P, Primitive):
G, tensor = t
start, __, symbol2index = G.abs2index[G.real2abs[S]]
tensor[start + symbol2index[P]] = 1
return t