-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathihead_data.py
172 lines (149 loc) · 5.84 KB
/
ihead_data.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
from dataclasses import dataclass
import itertools
import logging
import random
import math
import numpy as np
import pickle
import time
import sys
from typing import List, Optional, Tuple
logging.getLogger().setLevel(logging.INFO)
@dataclass
class DataArgs:
k: int = 0
seq_length: int = 256
show_latents: bool = False
fixed_special_toks: bool = False
special_toks_offset: int = 0
output_counter: bool = True
no_repeat: bool = False
class Dataset:
def __init__(self, args: DataArgs,
train_test: Optional[str] = None,
bigram_outs: Optional[bool] = False):
self.k = args.k
self.seq_length = args.seq_length
self.show_latents = args.show_latents
self.train_test = train_test
self.output_counter = args.output_counter
self.no_repeat = args.no_repeat
self.bigram_outs = bigram_outs
# init distributions
self.meta = pickle.load(open('data/meta.pkl', 'rb'))
self.itos = self.meta['itos']
self.stoi = self.meta['stoi']
self.num_tokens = self.meta['vocab_size']
self.tok_range = list(np.arange(self.num_tokens))
# OOD
if self.train_test is not None:
assert not self.bigram_outs # this requires distributions over all tokens
self.n_train_toks = int(0.75 * self.num_tokens)
else:
self.n_train_toks = self.num_tokens
# marginal probabilities over characters
self.marginal = np.zeros(self.num_tokens)
for k, cnt in self.meta['unigrams'].items():
self.marginal[self.stoi[k]] = cnt
self.marginal /= self.marginal.sum()
# conditionals
self.cond = [np.zeros(self.num_tokens) for _ in range(self.num_tokens)]
for (w1, w2), cnt in self.meta['bigrams'].items():
self.cond[self.stoi[w1]][self.stoi[w2]] += cnt
for i in range(self.num_tokens):
self.cond[i] /= self.cond[i].sum()
# special tokens
self.idxs = None
if args.fixed_special_toks:
# use unigram marginals
self.idxs = list(self.marginal.argsort()[self.num_tokens-args.special_toks_offset-self.k:self.num_tokens-args.special_toks_offset])
def decode(self, idxs: List[int]) -> str:
return ''.join(self.itos[idx] for idx in idxs)
def gen_seq(self, rng: np.random.Generator):
# select special tokens for this sequence
if self.idxs is not None:
idxs = self.idxs
else:
idxs = list(rng.choice(self.tok_range, p=self.marginal, size=self.k, replace=False))
# for each special token, select a special next token
# outs = [rng.choice(self.tok_range, p=self.cond[idx]) for idx in idxs]
if self.no_repeat: # prevent next token to be same as idx
pools = [self.tok_range.copy() for idx in idxs]
for i, idx in enumerate(idxs):
pools[i].remove(idx)
else:
pools = [self.tok_range for idx in idxs]
if self.train_test is None:
# outs = [rng.choice(self.tok_range) for idx in idxs]
if self.bigram_outs:
outs = [rng.choice(pool, p=(self.cond[idx][pool] / self.cond[idx][pool].sum())) for pool, idx in zip(pools, idxs)]
else:
outs = [rng.choice(pool) for pool in pools]
elif self.train_test == 'train':
# outs = [rng.choice(self.tok_range[:n_train_toks]) for idx in idxs]
outs = [rng.choice(pool[:self.n_train_toks]) for pool in pools]
elif self.train_test == 'test':
# outs = [rng.choice(self.tok_range[n_train_toks:]) for idx in idxs]
outs = [rng.choice(pool[self.n_train_toks:]) for pool in pools]
else:
assert False
cnts = {}
if self.show_latents:
seq = idxs.copy()
outputs_seq = [-1] * len(idxs) # []
else:
seq = []
outputs_seq = []
seq += [rng.choice(self.tok_range, p=self.marginal)]
while len(seq) < self.seq_length + 1:
last = seq[-1]
if last in idxs:
seq.append(outs[idxs.index(last)])
if self.output_counter:
cnts[last] = cnts.get(last, 0) + 1
outputs_seq.append(cnts[last])
else:
outputs_seq.append(1)
else:
probs = self.cond[last]
outputs_seq.append(0)
seq.append(rng.choice(self.tok_range, p=probs))
outputs_seq.append(0)
return seq, outputs_seq
def gen_seqs(self, rng: np.random.Generator) -> List[str]:
while True:
seq, outputs_seq = self.gen_seq(rng)
yield (seq, outputs_seq)
def gen_batch(self, rng: np.random.Generator, batch_size: int):
seqs = []
outs = []
for _ in range(batch_size):
seq, out = self.gen_seq(rng)
seqs += seq
outs += out
x = np.array(seqs).reshape(batch_size, self.seq_length + 1)
outs = np.array(outs).reshape(batch_size, self.seq_length + 1)
return x, outs
def iterate_batches(dataset: Dataset,
batch_size: int = 20,
num_workers: int = 60,
seed: int = 42):
def worker(queue, rng):
while True:
x, outs = dataset.gen_batch(rng, batch_size)
queue.put((x, outs))
import multiprocessing as mp
q = mp.Queue(maxsize=1000)
processes = [mp.Process(target=worker, args=(q, np.random.default_rng([seed, i]))) for i in range(num_workers)]
for p in processes:
p.start()
seq = []
outputs_seq = []
count = 0
try:
while True:
x, outs = q.get()
yield (x[:,:-1], x[:,1:], outs[:,:-1])
except:
for p in processes:
p.kill()