-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsample.py
235 lines (201 loc) · 8.14 KB
/
sample.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
"""
Generate samples from our Storium models
"""
import logging
from typing import Any, Dict, List, Optional, Set, Union
import torch
from torch.nn import functional as F
from transformers import GPT2Config, PreTrainedModel
from data.parallel import StaticDataParallel
from data.preprocess import get_tokenizer
from data.preprocess.gpt2 import SpecialToken
from data.utils import EntryList, collate
from model import GPT2SegmentedModel
class SampleGenerator:
"""
A class that encapsulates all the functionality needed to generate samples
from a model
"""
def __init__(
self,
top_k: int = 0,
top_p: float = 0.9,
temperature: float = 0.7,
repetition_penalty: float = 1.0,
cache_dir: Optional[str] = None,
):
"""
Initialize the generator
"""
self.top_k = top_k
self.top_p = top_p
self.temperature = temperature
self.repetition_penalty = repetition_penalty
self.cache_dir = cache_dir
self.tokenizer = get_tokenizer(
"gpt2", cache_dir=cache_dir, additional_special_tokens=list(SpecialToken)
)
self.move_id = self.tokenizer.convert_tokens_to_ids(SpecialToken.move)
self.separator_id = self.tokenizer.convert_tokens_to_ids(SpecialToken.separator)
self.model: PreTrainedModel
def load(self, checkpoint_path: str):
"""
Load the model from the specified path
"""
logging.info("Loading model")
config = GPT2Config.from_pretrained(checkpoint_path)
config.output_past = True
model = GPT2SegmentedModel.from_pretrained(
checkpoint_path, config=config, cache_dir=self.cache_dir
)
if torch.cuda.is_available():
model = model.cuda()
self.model = StaticDataParallel(model)
def filter(self, logits: torch.Tensor):
"""
Do top-k/top-p filtering of the logits
Based on: https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317
"""
top_k = min(self.top_k, logits.size(-1)) # Safety check
if top_k > 0:
# Remove all tokens with a probability less than the last token of the top-k
indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
logits[indices_to_remove] = -float("inf")
top_p = self.top_p
if top_p > 0.0:
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
# Remove tokens with cumulative probability above the threshold
sorted_indices_to_remove = cumulative_probs > top_p
# Shift the indices to the right to keep also the first token above the threshold
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[
..., :-1
].clone()
sorted_indices_to_remove[..., 0] = 0
# scatter sorted tensors to original indexing
indices_to_remove = sorted_indices_to_remove.scatter(
dim=1, index=sorted_indices, src=sorted_indices_to_remove
)
logits[indices_to_remove] = -float("inf")
return logits
def sample(
self,
entries: EntryList,
generated: Optional[List[Set[int]]] = None,
lengths: Union[int, List[int]] = 256,
max_length: int = 1024,
skip_special_tokens: bool = True,
) -> List[str]:
"""
Sample entry text given the list of summaries up to the specified length
"""
if not generated:
generated = [set() for _ in range(len(entries))]
if isinstance(lengths, int):
lengths = [lengths] * len(entries)
entry_lengths = [len(e["tokens"]) for e in entries]
desired_lengths = [
min(s + l, max_length) for s, l in zip(entry_lengths, lengths)
]
num_steps = max(l - s for s, l in zip(entry_lengths, desired_lengths))
with torch.no_grad():
self.model.eval()
batch = self.sample_first(collate(entries), generated)
outputs = [t.tolist() for t in batch["tokens"]]
generated = [g | set(o) for g, o in zip(generated, outputs)]
done = [t.item() == self.tokenizer.eos_token_id for t in batch["tokens"]]
# Already completed one step of sampling above, so decrement steps by 1
for _ in range(num_steps - 1):
if all(done):
# Break out early if all samples have been generated
break
batch = self.sample_next(batch, generated, desired_lengths)
for idx, (token, output) in enumerate(zip(batch["tokens"], outputs)):
next_token = token.item()
done[idx] = done[idx] or (next_token == self.tokenizer.eos_token_id)
if done[idx]:
continue
output.append(next_token)
generated[idx].add(next_token)
return [
self.tokenizer.decode(output, skip_special_tokens=skip_special_tokens)
for output in outputs
]
def sample_logits(
self,
logits: torch.Tensor,
generated: Optional[List[Set[int]]] = None,
length_penalties: Optional[List[float]] = None,
) -> torch.Tensor:
"""
Perform sampling on the passed in logits
"""
# First apply the repetition penalty
if generated:
for idx, tokens in enumerate(generated):
for token in tokens:
logits[idx, token] /= self.repetition_penalty
# Then apply a length penalty if specified
if length_penalties:
for idx, penalty in enumerate(length_penalties):
logits[idx, self.tokenizer.eos_token_id] *= penalty
# Ensure we cannot get a separator, as that shouldn't occur
logits[:, self.separator_id] = 0
# Then filter the logits
temperature = self.temperature
logits = self.filter(logits / (temperature if temperature > 0 else 1.0))
return (
torch.argmax(logits, dim=-1)
if temperature == 0 # greedy sampling
else torch.multinomial(F.softmax(logits, dim=-1), num_samples=1).squeeze(-1)
)
def sample_first(
self, batch: Dict[str, Any], generated: List[Set[int]]
) -> Dict[str, Any]:
"""
Sample the first token. The first token is a special case since we pass
in the full context and generate the "past" hidden states.
"""
lengths = batch["lengths"]
indices = torch.LongTensor(lengths) # type:ignore
batch_size = len(lengths)
outputs = self.model(batch)
next_token = self.sample_logits(
outputs[0][range(batch_size), indices - 1], generated=generated
)
# Return an updated batch
return {
"past": outputs[1],
"tokens": next_token.unsqueeze(-1),
"segments": next_token.new_full((batch_size, 1, 1), self.move_id),
"segment_masks": next_token.new_ones((batch_size, 1, 1)),
"lengths": [l + 1 for l in lengths],
"num_tokens": batch["num_tokens"] + batch_size,
}
def sample_next(
self,
batch: Dict[str, Any],
generated: List[Set[int]],
desired_lengths: List[int],
) -> Dict[str, Any]:
"""
Sample the next token for the passed in batch
"""
outputs = self.model(batch)
next_token = self.sample_logits(
outputs[0][:, 0],
generated=generated,
length_penalties=[
len(tokens) / l for tokens, l in zip(generated, desired_lengths)
],
)
# Return an updated batch
lengths = batch["lengths"]
return {
"past": outputs[1],
"tokens": next_token.unsqueeze(-1),
"segments": batch["segments"],
"segment_masks": batch["segment_masks"],
"lengths": [l + 1 for l in lengths],
"num_tokens": batch["num_tokens"] + len(lengths),
}