-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoding.zig
More file actions
371 lines (345 loc) · 14 KB
/
Copy pathdecoding.zig
File metadata and controls
371 lines (345 loc) · 14 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
const std = @import("std");
pub const SamplingConfig = struct {
/// Zero selects greedy decoding. Positive values scale logits before
/// truncation and sampling.
temperature: f64 = 1,
/// Zero keeps the full vocabulary.
top_k: usize = 0,
/// One keeps the full post-top-k distribution.
top_p: f64 = 1,
/// Values above one reduce logits for tokens present in `recent_tokens`.
repetition_penalty: f64 = 1,
pub fn validate(self: SamplingConfig) !void {
if (!std.math.isFinite(self.temperature) or self.temperature < 0) return error.InvalidTemperature;
if (!std.math.isFinite(self.top_p) or self.top_p <= 0 or self.top_p > 1) return error.InvalidTopP;
if (!std.math.isFinite(self.repetition_penalty) or self.repetition_penalty < 1) return error.InvalidRepetitionPenalty;
}
};
pub const Candidate = struct {
token: usize,
logit: f64,
probability: f64,
};
pub const BeamSearchConfig = struct {
width: usize = 4,
minimum_tokens: usize = 0,
maximum_tokens: usize = 32,
length_penalty: f64 = 0.6,
fn validate(self: BeamSearchConfig) !void {
if (self.width == 0 or self.maximum_tokens == 0 or
self.minimum_tokens > self.maximum_tokens or
!std.math.isFinite(self.length_penalty) or self.length_penalty < 0)
{
return error.InvalidBeamSearch;
}
}
};
pub const BeamSearchResult = struct {
allocator: std.mem.Allocator,
tokens: []usize,
log_probability: f64,
normalized_score: f64,
pub fn deinit(self: *BeamSearchResult) void {
self.allocator.free(self.tokens);
self.* = undefined;
}
};
const Beam = struct {
tokens: []usize,
log_probability: f64,
ended: bool,
};
/// Length-normalized beam search over an autoregressive next-token callback.
/// The callback receives the complete prefix, including `start_token`, and
/// fills one log probability per vocabulary item.
pub fn beamSearch(
allocator: std.mem.Allocator,
start_token: usize,
end_token: usize,
vocabulary_size: usize,
config: BeamSearchConfig,
context: anytype,
nextLogProbabilities: anytype,
) !BeamSearchResult {
try config.validate();
if (vocabulary_size < 2 or start_token >= vocabulary_size or
end_token >= vocabulary_size)
{
return error.InvalidVocabulary;
}
var beams = try allocator.alloc(Beam, 1);
beams[0] = .{
.tokens = try allocator.dupe(usize, &.{start_token}),
.log_probability = 0,
.ended = false,
};
errdefer {
for (beams) |beam| allocator.free(beam.tokens);
allocator.free(beams);
}
for (0..config.maximum_tokens) |_| {
var candidates: std.ArrayList(Beam) = .empty;
defer candidates.deinit(allocator);
errdefer for (candidates.items) |candidate| allocator.free(candidate.tokens);
for (beams) |beam| {
if (beam.ended) {
try candidates.append(allocator, .{
.tokens = try allocator.dupe(usize, beam.tokens),
.log_probability = beam.log_probability,
.ended = true,
});
continue;
}
const log_probabilities = try allocator.alloc(f64, vocabulary_size);
defer allocator.free(log_probabilities);
try nextLogProbabilities(context, beam.tokens, log_probabilities);
for (log_probabilities, 0..) |log_probability, token| {
const generated_length = beam.tokens.len;
if (token == end_token and generated_length < config.minimum_tokens) {
continue;
}
if (!std.math.isFinite(log_probability)) return error.InvalidLogProbability;
const sequence = try allocator.alloc(usize, beam.tokens.len + 1);
@memcpy(sequence[0..beam.tokens.len], beam.tokens);
sequence[beam.tokens.len] = token;
try candidates.append(allocator, .{
.tokens = sequence,
.log_probability = beam.log_probability + log_probability,
.ended = token == end_token,
});
}
}
std.mem.sort(Beam, candidates.items, config, struct {
fn before(search_config: BeamSearchConfig, left: Beam, right: Beam) bool {
const left_score = normalizedBeamScore(left, search_config.length_penalty);
const right_score = normalizedBeamScore(right, search_config.length_penalty);
if (left_score != right_score) return left_score > right_score;
return lexicographicBefore(left.tokens, right.tokens);
}
}.before);
const retained = @min(config.width, candidates.items.len);
const next_beams = try allocator.alloc(Beam, retained);
for (candidates.items[0..retained], next_beams) |candidate, *next_beam| {
next_beam.* = candidate;
}
for (candidates.items[retained..]) |candidate| allocator.free(candidate.tokens);
for (beams) |beam| allocator.free(beam.tokens);
allocator.free(beams);
beams = next_beams;
var all_ended = true;
for (beams) |beam| all_ended = all_ended and beam.ended;
if (all_ended) break;
}
defer {
for (beams) |beam| allocator.free(beam.tokens);
allocator.free(beams);
}
std.mem.sort(Beam, beams, config, struct {
fn before(search_config: BeamSearchConfig, left: Beam, right: Beam) bool {
const left_score = normalizedBeamScore(left, search_config.length_penalty);
const right_score = normalizedBeamScore(right, search_config.length_penalty);
if (left_score != right_score) return left_score > right_score;
return lexicographicBefore(left.tokens, right.tokens);
}
}.before);
const best = beams[0];
const tokens = try allocator.dupe(usize, best.tokens[1..]);
return .{
.allocator = allocator,
.tokens = tokens,
.log_probability = best.log_probability,
.normalized_score = normalizedBeamScore(best, config.length_penalty),
};
}
fn normalizedBeamScore(beam: Beam, length_penalty: f64) f64 {
const generated = @max(@as(usize, 1), beam.tokens.len - 1);
const penalty = std.math.pow(
f64,
(@as(f64, @floatFromInt(generated)) + 5.0) / 6.0,
length_penalty,
);
return beam.log_probability / penalty;
}
fn lexicographicBefore(left: []const usize, right: []const usize) bool {
for (left[0..@min(left.len, right.len)], right[0..@min(left.len, right.len)]) |a, b| {
if (a != b) return a < b;
}
return left.len < right.len;
}
/// Returns the retained distribution in descending logit order. Top-k is
/// applied before nucleus top-p, matching common generation APIs.
pub fn distribution(
allocator: std.mem.Allocator,
logits: []const f64,
recent_tokens: []const usize,
config: SamplingConfig,
) ![]Candidate {
try config.validate();
if (logits.len == 0) return error.EmptyVocabulary;
const candidates = try allocator.alloc(Candidate, logits.len);
errdefer allocator.free(candidates);
const retained = try distributionInto(candidates, logits, recent_tokens, config);
return allocator.realloc(candidates, retained.len);
}
/// Allocation-free distribution construction for persistent inference
/// sessions. The caller supplies one candidate slot per vocabulary entry.
pub fn distributionInto(
workspace: []Candidate,
logits: []const f64,
recent_tokens: []const usize,
config: SamplingConfig,
) ![]Candidate {
try config.validate();
if (logits.len == 0) return error.EmptyVocabulary;
if (workspace.len < logits.len) return error.InsufficientWorkspace;
const candidates = workspace[0..logits.len];
for (logits, candidates, 0..) |logit, *candidate, token| {
if (!std.math.isFinite(logit)) return error.InvalidLogit;
var adjusted = applyRepetitionPenalty(logit, token, recent_tokens, config.repetition_penalty);
if (config.temperature > 0) adjusted /= config.temperature;
candidate.* = .{ .token = token, .logit = adjusted, .probability = 0 };
}
sortDescending(candidates);
if (config.temperature == 0) {
candidates[0].probability = 1;
return candidates[0..1];
}
var retained = if (config.top_k == 0) candidates.len else @min(config.top_k, candidates.len);
const maximum = candidates[0].logit;
var total: f64 = 0;
for (candidates[0..retained]) |*candidate| {
candidate.probability = @exp(candidate.logit - maximum);
total += candidate.probability;
}
for (candidates[0..retained]) |*candidate| candidate.probability /= total;
if (config.top_p < 1) {
var cumulative: f64 = 0;
var nucleus_size: usize = 0;
while (nucleus_size < retained) : (nucleus_size += 1) {
cumulative += candidates[nucleus_size].probability;
if (cumulative >= config.top_p) {
nucleus_size += 1;
break;
}
}
retained = @max(@as(usize, 1), nucleus_size);
}
total = 0;
for (candidates[0..retained]) |candidate| total += candidate.probability;
for (candidates[0..retained]) |*candidate| candidate.probability /= total;
return candidates[0..retained];
}
pub fn sample(
allocator: std.mem.Allocator,
random: std.Random,
logits: []const f64,
recent_tokens: []const usize,
config: SamplingConfig,
) !usize {
const candidates = try distribution(allocator, logits, recent_tokens, config);
defer allocator.free(candidates);
const draw = random.float(f64);
var cumulative: f64 = 0;
for (candidates) |candidate| {
cumulative += candidate.probability;
if (draw <= cumulative) return candidate.token;
}
return candidates[candidates.len - 1].token;
}
/// Allocation-free sampling counterpart to `sample`.
pub fn sampleWithWorkspace(
random: std.Random,
logits: []const f64,
recent_tokens: []const usize,
config: SamplingConfig,
workspace: []Candidate,
) !usize {
const candidates = try distributionInto(workspace, logits, recent_tokens, config);
const draw = random.float(f64);
var cumulative: f64 = 0;
for (candidates) |candidate| {
cumulative += candidate.probability;
if (draw <= cumulative) return candidate.token;
}
return candidates[candidates.len - 1].token;
}
fn applyRepetitionPenalty(logit: f64, token: usize, recent_tokens: []const usize, penalty: f64) f64 {
if (penalty == 1 or std.mem.indexOfScalar(usize, recent_tokens, token) == null) return logit;
return if (logit >= 0) logit / penalty else logit * penalty;
}
fn sortDescending(candidates: []Candidate) void {
std.mem.sort(Candidate, candidates, {}, struct {
fn before(_: void, lhs: Candidate, rhs: Candidate) bool {
return lhs.logit > rhs.logit or (lhs.logit == rhs.logit and lhs.token < rhs.token);
}
}.before);
}
test "top-k and top-p retain the smallest probability nucleus" {
const candidates = try distribution(std.testing.allocator, &.{ 4, 3, 2, 1 }, &.{}, .{ .top_k = 3, .top_p = 0.8 });
defer std.testing.allocator.free(candidates);
try std.testing.expectEqual(@as(usize, 2), candidates.len);
try std.testing.expectEqual(@as(usize, 0), candidates[0].token);
try std.testing.expectEqual(@as(usize, 1), candidates[1].token);
try std.testing.expectApproxEqAbs(@as(f64, 1), candidates[0].probability + candidates[1].probability, 1e-12);
}
test "zero temperature is greedy after repetition penalty" {
const candidates = try distribution(std.testing.allocator, &.{ 4, 3.5, 1 }, &.{0}, .{ .temperature = 0, .repetition_penalty = 2 });
defer std.testing.allocator.free(candidates);
try std.testing.expectEqual(@as(usize, 1), candidates.len);
try std.testing.expectEqual(@as(usize, 1), candidates[0].token);
try std.testing.expectEqual(@as(f64, 1), candidates[0].probability);
}
test "sampling workspace matches allocating distribution" {
var workspace: [4]Candidate = undefined;
const retained = try distributionInto(&workspace, &.{ 4, 3, 2, 1 }, &.{}, .{ .top_k = 3, .top_p = 0.8 });
try std.testing.expectEqual(@as(usize, 2), retained.len);
try std.testing.expectEqual(@as(usize, 0), retained[0].token);
try std.testing.expectEqual(@as(usize, 1), retained[1].token);
}
test "seeded nucleus samples never escape retained candidates" {
var prng = std.Random.DefaultPrng.init(42);
for (0..100) |_| {
const token = try sample(std.testing.allocator, prng.random(), &.{ 4, 3, 2, 1 }, &.{}, .{ .top_p = 0.7 });
try std.testing.expect(token == 0 or token == 1);
}
}
test "beam search recovers a sequence hidden by greedy choice" {
const Scorer = struct {
fn score(_: void, prefix: []const usize, output: []f64) !void {
@memset(output, -100);
if (prefix.len == 1) {
output[1] = @log(@as(f64, 0.6));
output[2] = @log(@as(f64, 0.4));
} else if (prefix[prefix.len - 1] == 1) {
output[3] = @log(@as(f64, 0.55));
output[1] = @log(@as(f64, 0.45));
} else {
output[3] = @log(@as(f64, 0.99));
output[2] = @log(@as(f64, 0.01));
}
}
};
var greedy = try beamSearch(
std.testing.allocator,
0,
3,
4,
.{ .width = 1, .maximum_tokens = 2, .length_penalty = 0 },
{},
Scorer.score,
);
defer greedy.deinit();
var beam = try beamSearch(
std.testing.allocator,
0,
3,
4,
.{ .width = 2, .maximum_tokens = 2, .length_penalty = 0 },
{},
Scorer.score,
);
defer beam.deinit();
try std.testing.expectEqualSlices(usize, &.{ 1, 3 }, greedy.tokens);
try std.testing.expectEqualSlices(usize, &.{ 2, 3 }, beam.tokens);
try std.testing.expect(beam.log_probability > greedy.log_probability);
}