Skip to content

Commit d519c08

Browse files
committed
Flash Next's wide top-k draw keeps a uniform that rounds to 1: its +inf noise wins as in the reference
lanes.sampling.uniform's largest value, (2^53 - 1) 2^-53 + 2^-54, rounds to exactly 1.0 in f64, so the reference's Gumbel score is +inf there; the early stop assumed noise of at most 37.43 and could pass that candidate over (a seed that hits it: expected 4, found 0). Past a 38-nat shortfall the draw now still asks each candidate whether its uniform is 1 (one hash, no logs) and stops only once the best score is +inf; the pruning bound reads 1 - u from the f64 uniform itself (exact), so a u of 1 is never passed over. The reviewer's case is a host test, with the same candidate inside the bound and behind another leader. Speed unchanged (0.33-0.63 ms a row at 248k).
1 parent b7372ac commit d519c08

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

‎zig/src/families/flashnext/sample.zig‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ fn wide(gpa: std.mem.Allocator, logits: []const u16, p: lanes.Sampling, position
142142
const x = value(logits[id]) / t;
143143
if (j > 0) {
144144
const d = best_score - x;
145-
if (d > 38) break; // a draw's noise is at most -log(-log(1 - 2^-54)) = 37.43, and x only falls from here
146-
if (d > 1 and beyondReach(p.seed, position, id, d)) continue;
145+
if (d > 38) { // finite noise is at most -log(-log(1 - 2^-52)) = 36.04; a uniform that rounds to 1 gives +inf
146+
if (best_score == std.math.inf(f64)) break; // the first +inf stands: a later one ties, never passes
147+
if (lanes.sampling.uniform(p.seed, position, id) != 1.0) continue;
148+
} else if (d > 1 and beyondReach(p.seed, position, id, d)) continue;
147149
}
148150
const score = x - log(-log(lanes.sampling.uniform(p.seed, position, id)));
149151
if (j == 0 or score > best_score) {
@@ -162,10 +164,10 @@ const below_e = blk: {
162164
};
163165

164166
/// Whether the noise for `id` stays a nat short of `d`: -log(-log u) <= -log(1 - u), so 1 - u >= exp(-(d - 1))
165-
/// bounds it by d - 1, read from the uniform's integer bits against exp of a whole nat at or below d - 1.
167+
/// bounds it by d - 1, against exp of a whole nat at or below d - 1. 1 - u is exact for the f64 u the draw uses
168+
/// (Sterbenz past 0.5; below it the bound cannot hold), and a u rounded to 1 gives 0: never passed over.
166169
fn beyondReach(seed: u64, position: u64, id: u64, d: f64) bool {
167-
const m = lanes.sampling.keyBits(seed, position, id) >> 11;
168-
const one_minus_u = @as(f64, @floatFromInt((@as(u64, 1) << 53) - m)) * 0x1p-53 - 0x1p-54;
170+
const one_minus_u = 1.0 - lanes.sampling.uniform(seed, position, id);
169171
const j: usize = @intFromFloat(@floor(d) - 1);
170172
return one_minus_u >= below_e[j] * (1 + 1e-9);
171173
}
@@ -193,3 +195,20 @@ test "the one-pass draw equals the reference on random rows with ties, signed ze
193195
};
194196
}
195197
}
198+
199+
test "wide draw preserves reference when uniform rounds to one" {
200+
const gpa = std.testing.allocator;
201+
var words: [300]u16 = @splat(0);
202+
words[0] = 0x4280; // 64.0; other logits are zero
203+
const p: lanes.Sampling = .{ .seed = 2870505676250484375, .temperature = 1, .top_k = 0, .top_p = 1, .min_p = 0 };
204+
try std.testing.expectEqual(@as(f64, 1), lanes.sampling.uniform(p.seed, 0, 4));
205+
const expected = try reference(gpa, &words, p, 0);
206+
try std.testing.expectEqual(@as(u32, 4), expected);
207+
try std.testing.expectEqual(expected, try draw(gpa, &words, p, 0));
208+
// the same candidate within the pruning bound (shortfall under 38), and behind a different leader
209+
words[0] = 0x41a0; // 20.0
210+
try std.testing.expectEqual(try reference(gpa, &words, p, 0), try draw(gpa, &words, p, 0));
211+
words[0] = 0;
212+
words[2] = 0x4280;
213+
try std.testing.expectEqual(try reference(gpa, &words, p, 0), try draw(gpa, &words, p, 0));
214+
}

0 commit comments

Comments
 (0)