-
Notifications
You must be signed in to change notification settings - Fork 484
/
test_samplers.py
254 lines (196 loc) · 7.89 KB
/
test_samplers.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
import math
import pytest
import torch
from outlines.samplers import (
BeamSearchSampler,
GreedySampler,
MultinomialSampler,
beam_search,
greedy,
keep_top_k_logits,
keep_top_p_logits,
multinomial,
rescale_logits,
)
def compute_logprobs(logits):
return torch.nn.functional.log_softmax(logits, dim=-1)
def test_aliases():
assert greedy == GreedySampler
assert multinomial == MultinomialSampler
assert beam_search == BeamSearchSampler
def test_greedy():
sampler = GreedySampler()
logits = torch.tensor([[1.0, 2.0, 5.0]])
weights = torch.tensor([0])
next_token_ids, ancestors, weights = sampler(logits, weights, None)
logprobs = compute_logprobs(logits)
assert next_token_ids.equal(torch.tensor([[2]]))
assert ancestors.equal(torch.tensor([0]))
assert weights.equal(logprobs[..., 2])
sampler = GreedySampler()
logits = torch.tensor([[10.0, 0.0, 3.0], [-math.inf, 2.0, 5.0]])
weights = torch.tensor([0, 0])
next_token_ids, ancestors, weights = sampler(logits, weights, None)
logprobs = compute_logprobs(logits)
assert next_token_ids.equal(torch.tensor([[0], [2]]))
assert ancestors.equal(torch.tensor([0, 1]))
assert weights.equal(torch.tensor([logprobs[0, 0], logprobs[1, 2]]))
def test_multinomial():
rng = torch.Generator()
rng.manual_seed(239)
sampler = MultinomialSampler()
logits = torch.tensor([[1.0, 4.0, 5.0]])
weights = torch.tensor([0])
next_token_ids, ancestors, weights = sampler(logits, weights, rng)
logprobs = compute_logprobs(logits)
assert next_token_ids.equal(torch.tensor([[2]]))
assert ancestors.equal(torch.tensor([0]))
assert weights.equal(torch.tensor([logprobs[:, 2]]))
sampler = MultinomialSampler()
logits = torch.tensor([[10.0, 0.0, 9.0], [-math.inf, 4.0, 5.0]])
weights = torch.tensor([0, 0])
next_token_ids, ancestors, weights = sampler(logits, weights, rng)
logprobs = compute_logprobs(logits)
assert next_token_ids.equal(torch.tensor([[0], [2]]))
assert ancestors.equal(torch.tensor([0, 1]))
assert weights.equal(torch.tensor([logprobs[0, 0], logprobs[1, 2]]))
def test_multinomial_init():
sampler = MultinomialSampler()
assert sampler.logits_processors == []
sampler = MultinomialSampler(3)
assert sampler.logits_processors == []
sampler = MultinomialSampler(top_k=1)
assert len(sampler.logits_processors) == 1
sampler = MultinomialSampler(top_p=0.95)
assert len(sampler.logits_processors) == 1
sampler = MultinomialSampler(top_k=1, top_p=0.95)
assert len(sampler.logits_processors) == 1
sampler = MultinomialSampler(temperature=1.0)
assert len(sampler.logits_processors) == 1
sampler = MultinomialSampler(top_k=1, temperature=1.0)
assert len(sampler.logits_processors) == 2
sampler = MultinomialSampler(top_p=0.95, temperature=1.0)
assert len(sampler.logits_processors) == 2
def test_top_k():
with pytest.raises(ValueError, match="`k` must be a strictly"):
keep_top_k_logits(-1)
with pytest.raises(ValueError, match="`k` must be a strictly"):
keep_top_k_logits(0.1)
logits = torch.tensor([[1.0, 2.0, 3.0, 4.0]])
logits_processor = keep_top_k_logits(1)
result = logits_processor(logits)
assert result.equal(torch.tensor([[-math.inf, -math.inf, -math.inf, 4.0]]))
logits_processor = keep_top_k_logits(10)
result = logits_processor(logits)
assert result.equal(torch.tensor([[1.0, 2.0, 3.0, 4.0]]))
logits = torch.tensor([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]])
logits_processor = keep_top_k_logits(2)
result = logits_processor(logits)
assert result.equal(
torch.tensor(
[[-math.inf, -math.inf, 3.0, 4.0], [-math.inf, -math.inf, 7.0, 8.0]]
)
)
def test_top_p():
with pytest.raises(ValueError, match="`p` must be a floating point"):
keep_top_p_logits(-0.1)
with pytest.raises(ValueError, match="`p` must be a floating point"):
keep_top_p_logits(0.0)
with pytest.raises(ValueError, match="`p` must be a floating point"):
keep_top_p_logits(1.1)
logits = torch.tensor([[1.0, 1.01, 1.02, 4.0]])
logits_processor = keep_top_p_logits(0.1)
result = logits_processor(logits)
assert result.equal(torch.tensor([[-math.inf, -math.inf, -math.inf, 4.0]]))
logits_processor = keep_top_p_logits(0.95)
result = logits_processor(logits)
assert result.equal(torch.tensor([[-math.inf, 1.01, 1.02, 4.0]]))
logits_processor = keep_top_p_logits(1.0)
result = logits_processor(logits)
assert result.equal(torch.tensor([[1.0, 1.01, 1.02, 4.0]]))
logits = torch.tensor([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]])
logits_processor = keep_top_p_logits(0.1)
result = logits_processor(logits)
assert result.equal(
torch.tensor(
[
[-math.inf, -math.inf, -math.inf, 4.0],
[-math.inf, -math.inf, -math.inf, 8.0],
]
)
)
logits_processor = keep_top_p_logits(0.95)
result = logits_processor(logits)
assert result.equal(
torch.tensor(
[
[-math.inf, 2.0, 3.0, 4.0],
[-math.inf, 6.0, 7.0, 8.0],
]
)
)
logits_processor = keep_top_p_logits(1.0)
result = logits_processor(logits)
assert result.equal(
torch.tensor(
[
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
]
)
)
def test_rescale():
with pytest.raises(ValueError, match="`temperature` must"):
rescale_logits(1)
with pytest.raises(ValueError, match="`temperature` must"):
rescale_logits(-0.1)
with pytest.raises(ValueError, match="Please use the greedy sampler"):
rescale_logits(0.0)
def test_beam_search():
# Two beams, single sequence
sampler = BeamSearchSampler(2)
logits = torch.tensor([[0.0, 1.0], [2.0, 0.0]])
init_weights = torch.tensor([0, 1.0])
next_token_ids, ancestors, weights = sampler(logits, init_weights, None)
logprobs = compute_logprobs(logits)
assert next_token_ids.equal(torch.tensor([[0], [1]]))
assert ancestors.equal(torch.tensor([1, 0]))
assert weights.equal(
torch.tensor([init_weights[1] + logprobs[1][0], logprobs[0][1]])
)
# Make sure that initial samples are different
sampler = BeamSearchSampler(2)
logits = torch.tensor([[0.0, 1.0], [0.0, 1.0]])
init_weights = torch.tensor([0, 0])
next_token_ids, ancestors, weights = sampler(logits, init_weights, None)
logprobs = compute_logprobs(logits)
assert next_token_ids.equal(torch.tensor([[1], [0]]))
assert ancestors.equal(torch.tensor([0, 0]))
assert weights.equal(torch.tensor([logprobs[0][1], logprobs[0][0]]))
# One beam, batch of two sequences. Reduces to Greedy Search.
sampler = BeamSearchSampler(1)
logits = torch.tensor([[0.0, 1.0], [2.0, 0.0]])
weights = torch.tensor([0, 0])
next_token_ids, ancestors, weights = sampler(logits, weights, None)
logprobs = compute_logprobs(logits)
assert next_token_ids.equal(torch.tensor([[1], [0]]))
assert ancestors.equal(torch.tensor([0, 1]))
assert weights.equal(torch.tensor([logprobs[0][1], logprobs[1][0]]))
# Two beams, batch of two sequences
sampler = BeamSearchSampler(2)
logits = torch.tensor([[0.0, 1.0], [2.0, 0.0], [3.0, 2.0], [0.0, 1.0]])
init_weights = torch.tensor([0, 0, 2.0, 0])
next_token_ids, ancestors, weights = sampler(logits, init_weights, None)
logprobs = compute_logprobs(logits)
assert next_token_ids.equal(torch.tensor([[0], [1], [0], [1]]))
assert ancestors.equal(torch.tensor([1, 0, 2, 2]))
assert weights.equal(
torch.tensor(
[
logprobs[1][0],
logprobs[0][1],
init_weights[2] + logprobs[2][0],
init_weights[2] + logprobs[2][1],
]
)
)