Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion zig/src/families/dsv41/prompt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ fn indexer(e: *const Engine, ch: *Chunk, cs: *const Caches, sh: *Shared, li: usi
try e.exact.bf16Scale(e.s, ch.wl, ch.iw, n * ih, exact.indexScale(id, ih));
const kk = @min(c.index_topk, n_comp_end);
// rows in blocks so the [rows, n_comp] keys stay bounded at long contexts
const rb = @max(16, @min(n, (@as(usize, 1) << 26) / (4 * @max(n_comp_end, 1))));
const rb = indexBlockRows(n, n_comp_end);
if (rb * n_comp_end > key_elems) return error.ChunkTooLong;
const keyed = li != c.candidate_source and !(c.candidate_source < li) and kk & (kk - 1) == 0;
const k: tri_index.IndexK = .{ .fp4 = .{ .codes = cs.idx_codes[src], .scales = cs.idx_scales[src] } };
Expand Down Expand Up @@ -985,6 +985,31 @@ fn indexer(e: *const Engine, ch: *Chunk, cs: *const Caches, sh: *Shared, li: usi
sh.kk = kk;
}

/// Even multi-block strides preserve 16-byte VIS alignment without increasing workspace.
fn indexBlockRows(rows: usize, compressed: usize) usize {
const limit = @max(16, @min(rows, (@as(usize, 1) << 26) / (4 * @max(compressed, 1))));
return if (limit < rows) limit & ~@as(usize, 1) else limit;
}

test "prompt index blocks preserve captured VIS alignment at long ragged contexts" {
try std.testing.expectEqual(@as(usize, 464), indexBlockRows(2048, 36075));
try std.testing.expectEqual(@as(usize, 465), indexBlockRows(465, 36075));
for ([_]usize{ 1, 15, 16, 17, 465, 466, 1024, 2048 }) |rows| {
for ([_]usize{ 1, 512, 8192, 32768, 32769, 36075, 65535, 65536, 100000, 524288 }) |compressed| {
const stride = indexBlockRows(rows, compressed);
try std.testing.expect(stride > 0);
var covered: usize = 0;
while (covered < rows) {
try std.testing.expectEqual(@as(usize, 0), (covered * 8) % 16);
const count = @min(stride, rows - covered);
try std.testing.expect(count * compressed <= (@as(usize, 1) << 24));
covered += count;
}
try std.testing.expectEqual(rows, covered);
}
}
}

/// topk_select's torch step, keys.topk(k).values: the top-k set of the unique int64 keys (ops.topkI64).
pub fn torchTopk(ctx: ?*anyopaque, _: tri.Tri, keys: u64, ks: usize, rows: usize, n: usize, k: usize, top: u64) anyerror!void {
const e: *const Engine = @ptrCast(@alignCast(ctx.?));
Expand Down