-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneration.cpp
More file actions
378 lines (324 loc) · 11.1 KB
/
Copy pathgeneration.cpp
File metadata and controls
378 lines (324 loc) · 11.1 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "gpt2/generation.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <optional>
#include <numeric>
#include <stdexcept>
#include <vector>
namespace gpt2 {
namespace {
constexpr double negative_infinity =
-std::numeric_limits<double>::infinity();
// Turns one draw from the engine into a double in [0, 1). Doing this by
// hand rather than with std::uniform_real_distribution keeps a seeded
// run reproducible, because the standard fixes the engine's output but
// not any distribution's algorithm.
double next_unit_double(std::mt19937_64& generator) {
constexpr double scale = 1.0 / 9007199254740992.0; // 2^-53
return static_cast<double>(generator() >> 11U) * scale;
}
std::span<const float> last_row(const Tensor& logits) {
const Tensor::Shape& shape = logits.shape();
if (shape.size() != 2 || shape[0] == 0 || shape[1] == 0) {
throw std::logic_error(
"model returned logits with an unexpected shape"
);
}
const std::size_t vocabulary_size = shape[1];
return std::span<const float>(
logits.data() + (shape[0] - 1) * vocabulary_size,
vocabulary_size
);
}
std::size_t most_likely_token(std::span<const float> scores) {
// A strict comparison keeps the lowest ID when scores tie, and
// non-finite scores cannot become candidates.
std::size_t best_token = scores.size();
float best_score = -std::numeric_limits<float>::infinity();
for (std::size_t token = 0; token < scores.size(); ++token) {
const float score = scores[token];
if (std::isfinite(score) && score > best_score) {
best_score = score;
best_token = token;
}
}
if (best_token == scores.size()) {
throw std::runtime_error(
"model produced no finite logit to generate from"
);
}
return best_token;
}
void validate_sampling_options(const SamplingOptions& options) {
if (!std::isfinite(options.temperature) ||
options.temperature <= 0.0F) {
throw std::invalid_argument(
"sampling temperature must be finite and greater than zero"
);
}
if (!std::isfinite(options.top_p) ||
options.top_p <= 0.0F ||
options.top_p > 1.0F) {
throw std::invalid_argument(
"sampling top_p must lie between zero and one"
);
}
}
// Softmax over the scores, skipping the tokens already ruled out.
// Subtracting the highest score first keeps the exponentials in range,
// and a ruled-out score of negative infinity becomes zero.
//
// The work happens in double even though the scores arrive as float,
// because a sharpening temperature spreads them far enough that a float
// subtraction would cost several digits in the exponent.
std::vector<double> softmax(std::span<const double> scores) {
double highest = negative_infinity;
for (const double score : scores) {
if (score > highest) {
highest = score;
}
}
std::vector<double> probabilities(scores.size(), 0.0);
double total = 0.0;
for (std::size_t token = 0; token < scores.size(); ++token) {
if (scores[token] == negative_infinity) {
continue;
}
const double weight = std::exp(scores[token] - highest);
probabilities[token] = weight;
total += weight;
}
if (!(total > 0.0)) {
throw std::runtime_error(
"no token kept a usable probability"
);
}
for (double& probability : probabilities) {
probability /= total;
}
return probabilities;
}
// Keeps every token scoring at least as high as the top_k-th score.
// Hugging Face compares against that score rather than truncating a
// sorted list, so a tie at the boundary keeps more than top_k tokens.
void apply_top_k(std::vector<double>& scores, std::size_t top_k) {
if (top_k == 0 || top_k >= scores.size()) {
return;
}
std::vector<double> ordered(scores);
std::nth_element(
ordered.begin(),
ordered.begin() + static_cast<std::ptrdiff_t>(top_k - 1),
ordered.end(),
std::greater<double>()
);
const double threshold = ordered[top_k - 1];
for (double& score : scores) {
if (score < threshold) {
score = negative_infinity;
}
}
}
// Drops the least likely tokens while their running probability stays
// at or below 1 - top_p, always keeping the highest-scoring one.
// Accumulating from the least likely token is what Hugging Face does;
// accumulating from the most likely one is equivalent in exact
// arithmetic but disagrees in floating point at the boundary.
void apply_top_p(std::vector<double>& scores, float top_p) {
if (top_p >= 1.0F) {
return;
}
const std::vector<double> probabilities = softmax(scores);
std::vector<std::size_t> ascending(scores.size());
std::iota(ascending.begin(), ascending.end(), std::size_t{0});
std::sort(
ascending.begin(),
ascending.end(),
[&scores](std::size_t left, std::size_t right) {
if (scores[left] != scores[right]) {
return scores[left] < scores[right];
}
return left < right;
}
);
const double limit = 1.0 - static_cast<double>(top_p);
double cumulative = 0.0;
for (std::size_t position = 0;
position + 1 < ascending.size();
++position) {
const std::size_t token = ascending[position];
cumulative += probabilities[token];
if (cumulative <= limit) {
scores[token] = negative_infinity;
}
}
}
} // namespace
std::vector<float> sampling_distribution(
std::span<const float> scores,
const SamplingOptions& options
) {
if (scores.empty()) {
throw std::invalid_argument(
"sampling requires at least one score"
);
}
validate_sampling_options(options);
std::vector<double> working(scores.size());
const double temperature = static_cast<double>(options.temperature);
bool any_finite = false;
for (std::size_t token = 0; token < scores.size(); ++token) {
if (std::isnan(scores[token])) {
working[token] = negative_infinity;
continue;
}
working[token] = static_cast<double>(scores[token]) / temperature;
any_finite = any_finite || std::isfinite(working[token]);
}
if (!any_finite) {
throw std::runtime_error(
"no finite score is available to sample from"
);
}
apply_top_k(working, options.top_k);
apply_top_p(working, options.top_p);
const std::vector<double> probabilities = softmax(working);
std::vector<float> result(probabilities.size());
for (std::size_t token = 0; token < probabilities.size(); ++token) {
result[token] = static_cast<float>(probabilities[token]);
}
return result;
}
std::size_t sample_token(
std::span<const float> scores,
const SamplingOptions& options,
std::mt19937_64& generator
) {
const std::vector<float> probabilities =
sampling_distribution(scores, options);
const double target = next_unit_double(generator);
double cumulative = 0.0;
for (std::size_t token = 0; token < probabilities.size(); ++token) {
cumulative += static_cast<double>(probabilities[token]);
if (target < cumulative) {
return token;
}
}
// Rounding can leave the total a hair below the draw, so fall back
// to the last token that could have been chosen.
for (std::size_t token = probabilities.size(); token > 0; --token) {
if (probabilities[token - 1] > 0.0F) {
return token - 1;
}
}
throw std::runtime_error("no token kept a usable probability");
}
namespace {
template <typename ChooseToken>
Generation run_generation(
const Gpt2Model& model,
std::span<const std::size_t> prompt_token_ids,
const GenerationLimits& limits,
ChooseToken choose_token
) {
if (prompt_token_ids.empty()) {
throw std::invalid_argument(
"generation requires at least one prompt token"
);
}
const std::size_t context_length =
static_cast<std::size_t>(model.config().context_length);
if (prompt_token_ids.size() > context_length) {
throw std::invalid_argument(
"prompt exceeds the checkpoint context length"
);
}
Generation generation;
generation.new_token_ids.reserve(std::min(
limits.maximum_new_tokens,
context_length - prompt_token_ids.size()
));
// The cached path feeds the prompt once and then one token per
// step; the uncached path replays the whole sequence every time.
std::optional<KvCache> cache;
std::vector<std::size_t> sequence;
std::vector<std::size_t> pending;
if (limits.use_cache) {
cache.emplace(model.config());
pending.assign(
prompt_token_ids.begin(),
prompt_token_ids.end()
);
} else {
sequence.reserve(context_length);
sequence.assign(
prompt_token_ids.begin(),
prompt_token_ids.end()
);
}
std::size_t length = prompt_token_ids.size();
while (generation.new_token_ids.size() < limits.maximum_new_tokens) {
if (length >= context_length) {
generation.stop = GenerationStop::context_limit;
return generation;
}
// Only the final row of forward's logits is ever used below,
// so forward_last_token_logits is asked to project only that
// row rather than the whole sequence; see docs/profiling.md.
const Tensor logits = cache.has_value()
? model.forward_last_token_logits(pending, *cache)
: model.forward_last_token_logits(sequence);
const std::size_t next_token = choose_token(last_row(logits));
if (cache.has_value()) {
pending.assign(1, next_token);
} else {
sequence.push_back(next_token);
}
++length;
generation.new_token_ids.push_back(next_token);
if (limits.end_of_text_id.has_value() &&
next_token == *limits.end_of_text_id) {
generation.stop = GenerationStop::end_of_text;
return generation;
}
}
generation.stop = GenerationStop::token_limit;
return generation;
}
} // namespace
Generation generate_greedy(
const Gpt2Model& model,
std::span<const std::size_t> prompt_token_ids,
const GenerationLimits& limits
) {
return run_generation(
model,
prompt_token_ids,
limits,
[](std::span<const float> scores) {
return most_likely_token(scores);
}
);
}
Generation generate_sampled(
const Gpt2Model& model,
std::span<const std::size_t> prompt_token_ids,
const GenerationLimits& limits,
const SamplingOptions& sampling,
std::mt19937_64& generator
) {
validate_sampling_options(sampling);
return run_generation(
model,
prompt_token_ids,
limits,
[&sampling, &generator](std::span<const float> scores) {
return sample_token(scores, sampling, generator);
}
);
}
} // namespace gpt2