-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoy_composition.py
More file actions
165 lines (125 loc) · 5.07 KB
/
toy_composition.py
File metadata and controls
165 lines (125 loc) · 5.07 KB
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
import random
import math
import numpy as np
from functools import partial
# Toy example of compositional MCMC sampling
# p has nonzero probability of generating error token E which leads to "off the rails repetition" of all 1's: ...E111111
# q has very low probability of generating off the rails repeated 1's
# p and q typically sample from uniform random sequences otherwise
# p * q should avoid sampling E and 1
# Naively adding the logits does not avoid the error token, but MCMC sampling from p * q does.
def error_token_p(prefix, T):
# If prefix contains error token 'E', next token is always '1'.
# tokens: {1, 2, ... ,T, E}
if 'E' in prefix:
output_probs = {"E": 0.0}
for i in range(1, T+1):
output_probs[str(i)] = 0.0
output_probs["1"] = 1.0
return output_probs
else:
# No 'E' in prefix, so we have 1/T for E, half the remainder for 0 and 1.
p_e = 1.0 / (T+1)
output_probs = {'E': p_e}
for i in range(1, T+1):
output_probs[str(i)] = p_e
return output_probs
def error_token_q(prefix, T):
# Exponentially low probability of generating '1'
alpha = math.exp(-T) # Probability of '1'
p_others = 1.0 - alpha
output_probs = {"E": p_others/T}
for i in range(1, T+1):
output_probs[str(i)] = p_others/T
output_probs["1"] = alpha
return output_probs
def normalize(dist):
norm_dist = {}
norm_Z = sum([dist[key] for key in dist.keys()])
for key in dist.keys():
norm_dist[key] = dist[key]*1/norm_Z
return norm_dist
def set_product(output_p, output_q):
output_pq = {}
for key in output_p.keys():
output_pq[key] = output_p[key]*output_q[key]
return output_pq
def sample_autoregressive(next_token_dist, T, seq_len=None):
if seq_len is None:
seq_len = 2 * T
prefix = []
for _ in range(seq_len):
dist = next_token_dist(prefix, T) # distribution for the next token
tokens = list(dist.keys())
probs = list(dist.values())
# sample from the distribution
chosen_token = random.choices(tokens, weights=probs, k=1)[0]
prefix.append(chosen_token)
return prefix
def naive_composition(p, q, T, context=None, seq_len=None):
if seq_len is None:
seq_len = 2 * T
if context == None:
context = []
prefix = context.copy()
else:
prefix = context.copy()
log_probs_norm = []
log_probs_unnorm = []
for _ in range(seq_len - len(context)):
dist_unnorm = set_product(p(prefix, T), q(prefix, T)) # naive product
dist = normalize(dist_unnorm)
tokens = list(dist.keys())
probs = list(dist.values())
# sample from the distribution
chosen_token = random.choices(tokens, weights=probs, k=1)[0]
prefix.append(chosen_token)
log_probs_norm.append(np.log(dist[chosen_token]))
log_probs_unnorm.append(np.log(dist_unnorm[chosen_token]))
return prefix, log_probs_norm, log_probs_unnorm
def compositional_sampler(p, q, mcmc_steps, T, context=[], seq_len=None):
if seq_len is None:
seq_len = 2 * T
c = len(context)
gen = []
if context is not None:
gen = context.copy()
log_probs_norm = []
log_probs_unnorm = []
for _ in range(seq_len - c):
dist_unnorm = set_product(p(gen, T), q(gen, T)) # naive product
dist = normalize(dist_unnorm)
tokens = list(dist.keys())
probs = list(dist.values())
# sample from the distribution
chosen_token = random.choices(tokens, weights=probs, k=1)[0]
gen.append(chosen_token)
log_probs_norm.append(np.log(dist[chosen_token]))
log_probs_unnorm.append(np.log(dist_unnorm[chosen_token]))
for _ in range(mcmc_steps):
t = len(gen)
idx = random.randint(c, t-1)
prop, log_prob_prop, target_log_prob_prop = naive_composition(p, q, T, gen[:idx], seq_len=t)
log_prob_cur = log_probs_norm.copy()[idx-c:]
target_log_prob_cur = log_probs_unnorm.copy()[idx-c:]
log_r = sum(target_log_prob_prop) + sum(log_prob_cur) - sum(target_log_prob_cur) - sum(log_prob_prop)
assert(len(gen) == len(prop) == t)
if np.random.rand() < np.exp(log_r):
assert(len(gen) == len(prop))
gen = prop.copy()
assert(len(log_probs_norm[idx-c:]) == len(log_prob_prop))
assert(len(log_probs_unnorm[idx-c:]) == len(target_log_prob_cur))
log_probs_norm[idx-c:] = log_prob_prop.copy()
log_probs_unnorm[idx-c:] = target_log_prob_prop.copy()
del prop
del log_prob_prop
del target_log_prob_cur
return gen, log_probs_norm, log_probs_unnorm
N = 20
for _ in range(N):
naive, _, _ = naive_composition(error_token_p, error_token_q, T=20)
print("|".join(naive))
print("-----------------")
for _ in range(N):
gen, _, _ = compositional_sampler(error_token_p, error_token_q, mcmc_steps=20, T=20, seq_len=None)
print("|".join(gen))