|
| 1 | +//! The keyed host sampler over a bf16 logits row, in one pass: the top-k candidates by an order-preserving integer key |
| 2 | +//! of each bf16 word, then lanes.sampling.choose. The candidates, their order and the draw equal `reference`, which is |
| 3 | +//! qwen3_5/backend.zig's draw without its mask (two passes in f64), kept here for wide top-k and the tests. |
| 4 | +const std = @import("std"); |
| 5 | +const lanes = @import("lanes"); |
| 6 | + |
| 7 | +/// k at most this takes the one-pass path; a wider (or unlimited) top-k takes the reference's. |
| 8 | +pub const MAX_K = 256; |
| 9 | + |
| 10 | +/// A bf16 word's order as an unsigned key: larger floats give larger keys, -0 and +0 the same. |
| 11 | +fn key(word: u16) u16 { |
| 12 | + const w = if (word == 0x8000) 0 else word; |
| 13 | + return if (w & 0x8000 != 0) ~w else w | 0x8000; |
| 14 | +} |
| 15 | + |
| 16 | +fn value(word: u16) f64 { |
| 17 | + return @as(f32, @bitCast(@as(u32, word) << 16)); |
| 18 | +} |
| 19 | + |
| 20 | +/// qwen3_5/backend.zig's draw without a mask: the unchanged top-k candidates for the keyed sampler. |
| 21 | +pub fn reference(gpa: std.mem.Allocator, logits: []const u16, p: lanes.Sampling, position: u64) !u32 { |
| 22 | + var best: ?usize = null; |
| 23 | + for (logits, 0..) |word, id| { |
| 24 | + const x = value(word); |
| 25 | + if (!std.math.isFinite(x)) return error.NonfiniteQwenLogits; |
| 26 | + if (best == null or x > value(logits[best.?])) best = id; |
| 27 | + } |
| 28 | + if (p.temperature <= 0) return @intCast(best.?); |
| 29 | + const allowed = logits.len; |
| 30 | + const k = if (p.top_k == 0) allowed else @min(allowed, p.top_k); |
| 31 | + const values = try gpa.alloc(f64, k); |
| 32 | + defer gpa.free(values); |
| 33 | + const ids = try gpa.alloc(u64, k); |
| 34 | + defer gpa.free(ids); |
| 35 | + var filled: usize = 0; |
| 36 | + for (logits, 0..) |word, id| { |
| 37 | + const x = value(word); |
| 38 | + if (k == allowed) { |
| 39 | + values[filled] = x; |
| 40 | + ids[filled] = id; |
| 41 | + filled += 1; |
| 42 | + continue; |
| 43 | + } |
| 44 | + if (filled == k and x <= values[k - 1]) continue; |
| 45 | + var at = @min(filled, k - 1); |
| 46 | + while (at > 0 and x > values[at - 1]) : (at -= 1) { |
| 47 | + values[at] = values[at - 1]; |
| 48 | + ids[at] = ids[at - 1]; |
| 49 | + } |
| 50 | + values[at] = x; |
| 51 | + ids[at] = id; |
| 52 | + filled = @min(filled + 1, k); |
| 53 | + } |
| 54 | + return @intCast(try lanes.sampling.choose(gpa, values, ids, position, p)); |
| 55 | +} |
| 56 | + |
| 57 | +/// Row `row` of a [rows, vocab] bf16 logits buffer whose bytes start at `base`. |
| 58 | +pub fn row(base: [*]const u8, vocab: usize, at: usize) []const u16 { |
| 59 | + return @as([*]const u16, @ptrCast(@alignCast(base + at * vocab * 2)))[0..vocab]; |
| 60 | +} |
| 61 | + |
| 62 | +/// The draw for `logits` with `p` keyed at `position` (p.temperature above 0). |
| 63 | +pub fn draw(gpa: std.mem.Allocator, logits: []const u16, p: lanes.Sampling, position: u64) !u32 { |
| 64 | + const k: usize = p.top_k; |
| 65 | + if (k == 0 or k > MAX_K or k >= logits.len) return reference(gpa, logits, p, position); |
| 66 | + var keys: [MAX_K]u16 = undefined; |
| 67 | + var ids: [MAX_K]u64 = undefined; |
| 68 | + var filled: usize = 0; |
| 69 | + for (logits, 0..) |word, id| { |
| 70 | + if (word & 0x7f80 == 0x7f80) return error.NonfiniteQwenLogits; // exponent all ones: inf or nan |
| 71 | + const x = key(word); |
| 72 | + if (filled == k and x <= keys[k - 1]) continue; |
| 73 | + var at = @min(filled, k - 1); |
| 74 | + while (at > 0 and x > keys[at - 1]) : (at -= 1) { |
| 75 | + keys[at] = keys[at - 1]; |
| 76 | + ids[at] = ids[at - 1]; |
| 77 | + } |
| 78 | + keys[at] = x; |
| 79 | + ids[at] = id; |
| 80 | + filled = @min(filled + 1, k); |
| 81 | + } |
| 82 | + var values: [MAX_K]f64 = undefined; |
| 83 | + for (ids[0..filled], values[0..filled]) |id, *v| v.* = value(logits[id]); |
| 84 | + return @intCast(try lanes.sampling.choose(gpa, values[0..filled], ids[0..filled], position, p)); |
| 85 | +} |
| 86 | + |
| 87 | +test "the one-pass draw equals the reference on random rows with ties, signed zeros and every top-k" { |
| 88 | + const gpa = std.testing.allocator; |
| 89 | + var rng = std.Random.DefaultPrng.init(7); |
| 90 | + const r = rng.random(); |
| 91 | + const words = try gpa.alloc(u16, 4096); |
| 92 | + defer gpa.free(words); |
| 93 | + for (0..40) |trial| { |
| 94 | + for (words) |*w| { |
| 95 | + const f: f32 = @floatCast(r.floatNorm(f64) * 3.0); |
| 96 | + w.* = @intCast(@as(u32, @bitCast(f)) >> 16); |
| 97 | + } |
| 98 | + for (0..64) |j| words[r.uintLessThan(usize, words.len)] = words[j]; // ties |
| 99 | + words[r.uintLessThan(usize, words.len)] = 0x8000; |
| 100 | + words[r.uintLessThan(usize, words.len)] = 0; |
| 101 | + for ([_]u32{ 1, 2, 20, 64, 256 }) |k| for ([_]f64{ 0.6, 1.0, 1.5 }) |t| { |
| 102 | + const p: lanes.Sampling = .{ .seed = trial * 31 + k, .temperature = t, .top_k = k, .top_p = 0.95 }; |
| 103 | + const pos: u64 = trial * 7 + k; |
| 104 | + try std.testing.expectEqual(try reference(gpa, words, p, pos), try draw(gpa, words, p, pos)); |
| 105 | + }; |
| 106 | + } |
| 107 | +} |
0 commit comments