Skip to content
Open
Show file tree
Hide file tree
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
76 changes: 53 additions & 23 deletions zig/src/families/inkling2/metal/attention.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn prepTree(k: *const Kernels, e: mtl.ComputeEncoder, r: planning.Rows, l: p
prepLaunch(k.prep_tree, e, r, l, c, meta, raw, scales, q, rel, kv);
}

fn attendImpl(k: *const Kernels, e: anytype, r: planning.Rows, l: planning.Layer, stride: u32, slots: usize, meta: RowMeta, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref, sink: Ref, gate: Ref, out: Ref, cached: ?Ref) !void {
fn partialsImpl(k: *const Kernels, e: anytype, r: planning.Rows, l: planning.Layer, stride: u32, slots: usize, meta: RowMeta, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref, cached: ?Ref) !planning.Launch {
const launch = try planning.attend(r, l, stride, slots);
explain.reportAttention(launch, r.rows);
const scale: f32 = 1.0 / @sqrt(@as(f32, planning.head_dim));
Expand Down Expand Up @@ -176,12 +176,18 @@ fn attendImpl(k: *const Kernels, e: anytype, r: planning.Rows, l: planning.Layer
},
}
e.dispatchGroups(mtl.Size.of(launch.grid[0], launch.grid[1], launch.grid[2]), mtl.Size.of(launch.threads, 1, 1));
return launch;
}
pub fn partials(k: *const Kernels, e: anytype, r: planning.Rows, l: planning.Layer, stride: u32, slots: usize, meta: RowMeta, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref) !planning.Launch {
return partialsImpl(k, e, r, l, stride, slots, meta, q, kv, rel, proj, part_o, part_ml, null);
}

fn attendImpl(k: *const Kernels, e: anytype, r: planning.Rows, l: planning.Layer, stride: u32, slots: usize, meta: RowMeta, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref, sink: Ref, gate: Ref, out: Ref, cached: ?Ref) !void {
const launch = try partialsImpl(k, e, r, l, stride, slots, meta, q, kv, rel, proj, part_o, part_ml, cached);
e.barrier();
e.setPipeline(k.merge);
bind(e, &.{ part_o, part_ml, sink, gate, meta.pos, out }, 0);
e.setValue(planning.MergeArgs{ .heads = r.heads, .window = l.window, .chunk = l.chunk, .c0 = 0, .chunks = launch.partials_rh, .subs = launch.subs }, 6);
e.dispatchGroups(mtl.Size.of(launch.merge_grid[0], 1, 1), mtl.Size.of(planning.merge_threads, 1, 1));
mergeRaw(k, e, r, l, launch.partials_rh, launch.subs, meta, part_o, part_ml, sink, gate, out);
}

pub fn attend(k: *const Kernels, e: anytype, r: planning.Rows, l: planning.Layer, stride: u32, slots: usize, meta: RowMeta, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref, sink: Ref, gate: Ref, out: Ref) !void {
return attendImpl(k, e, r, l, stride, slots, meta, q, kv, rel, proj, part_o, part_ml, sink, gate, out, null);
}
Expand Down Expand Up @@ -226,7 +232,7 @@ pub fn attendCached(k: *const Kernels, e: anytype, r: planning.Rows, l: planning
}

/// The table is opt-out at sixteen active global chunks, and never serves prompt, tree or batched paths.
pub fn attendReusable(k: *const Kernels, e: anytype, r: planning.Rows, l: planning.Layer, stride: u32, slots: usize, meta: RowMeta, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref, sink: Ref, gate: Ref, out: Ref, min_context: u32, segments: usize, tree: bool, decode: bool, tab: Ref) !void {
pub fn partialsReusable(k: *const Kernels, e: anytype, r: planning.Rows, l: planning.Layer, stride: u32, slots: usize, meta: RowMeta, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref, min_context: u32, segments: usize, tree: bool, decode: bool, tab: Ref) !planning.Launch {
const decode_slots = @as(usize, r.rows) * r.heads * stride * planning.subs;
const launch = try planning.attend(r, l, stride, if (decode) decode_slots else slots);
const kind: relbias.Kind = switch (launch.path) {
Expand All @@ -244,10 +250,17 @@ pub fn attendReusable(k: *const Kernels, e: anytype, r: planning.Rows, l: planni
tree,
kind,
)) {
return attendImpl(k, e, r, l, stride, slots, meta, q, kv, rel, proj, part_o, part_ml, sink, gate, out, null);
return partialsImpl(k, e, r, l, stride, slots, meta, q, kv, rel, proj, part_o, part_ml, null);
}
try projectRelTable(k, e, r, meta.pos, rel, proj, tab);
return attendImpl(k, e, r, l, stride, decode_slots, meta, q, kv, rel, proj, part_o, part_ml, sink, gate, out, tab);
e.barrier();
return partialsImpl(k, e, r, l, stride, decode_slots, meta, q, kv, rel, proj, part_o, part_ml, tab);
}

pub fn attendReusable(k: *const Kernels, e: anytype, r: planning.Rows, l: planning.Layer, stride: u32, slots: usize, meta: RowMeta, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref, sink: Ref, gate: Ref, out: Ref, min_context: u32, segments: usize, tree: bool, decode: bool, tab: Ref) !void {
const launch = try partialsReusable(k, e, r, l, stride, slots, meta, q, kv, rel, proj, part_o, part_ml, min_context, segments, tree, decode, tab);
e.barrier();
mergeRaw(k, e, r, l, launch.partials_rh, launch.subs, meta, part_o, part_ml, sink, gate, out);
}

fn fillBatchedSegs(e: mtl.ComputeEncoder, caches: []const BatchedCache, usage: usize, out: *[planning.max_batched_segments]planning.BatchedSeg) ![]const planning.BatchedSeg {
Expand Down Expand Up @@ -298,35 +311,52 @@ pub fn attendBatched(k: *const Kernels, e: mtl.ComputeEncoder, launch: planning.
e.dispatchGroups(mtl.Size.of(launch.merge_grid[0], 1, 1), mtl.Size.of(planning.merge_threads, 1, 1));
}

pub fn attendTree(k: *const Kernels, e: mtl.ComputeEncoder, r: planning.Rows, l: planning.Layer, stride: u32, start: u32, meta: RowMeta, tree_paths: Ref, tail_o: Ref, tail_ml: Ref, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref, sink: Ref, gate: Ref, out: Ref) !void {
const launch = try planning.attendTree(r, l, stride, start);
explain.reportTree(launch, r.rows);
pub fn treePrefix(k: *const Kernels, e: mtl.ComputeEncoder, launch: planning.TreeLaunch, r: planning.Rows, l: planning.Layer, meta: RowMeta, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref, tail_o: Ref, tail_ml: Ref) bool {
if (!launch.global or launch.p_tile == 0) return false;
const scale: f32 = 1.0 / @sqrt(@as(f32, planning.head_dim));
const prompt_segs = meta.prompt_segs orelse meta.segs;
const args = planning.PromptTreeArgs{ .heads = r.heads, .kv_heads = r.kv_heads, .ring = l.ring, .chunk = l.chunk, .c0 = 0, .chunks = launch.partials_rh, .scale = scale, .p_tile = launch.p_tile, .c_off = launch.c_off, .max_pos = r.last_pos };
e.setPipeline(k.prompt_tree_prefix);
bind(e, &.{ q, kv.k, kv.v, rel, proj, prompt_segs, part_o, part_ml, meta.pos, tail_o, tail_ml }, 0);
e.setValue(args, 11);
e.dispatchGroups(mtl.Size.of(launch.prefix_grid[0], launch.prefix_grid[1], launch.prefix_grid[2]), mtl.Size.of(launch.threads, 1, 1));
return true;
}

pub fn treePartials(k: *const Kernels, e: mtl.ComputeEncoder, launch: planning.TreeLaunch, r: planning.Rows, l: planning.Layer, meta: RowMeta, tree_paths: Ref, tail_o: Ref, tail_ml: Ref, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref) void {
const scale: f32 = 1.0 / @sqrt(@as(f32, planning.head_dim));
if (launch.global) {
const prompt_segs = meta.prompt_segs orelse meta.segs;
const args = planning.PromptTreeArgs{ .heads = r.heads, .kv_heads = r.kv_heads, .ring = l.ring, .chunk = l.chunk, .c0 = 0, .chunks = launch.partials_rh, .scale = scale, .p_tile = launch.p_tile, .c_off = launch.c_off, .max_pos = r.last_pos };
if (launch.p_tile > 0) {
e.setPipeline(k.prompt_tree_prefix);
bind(e, &.{ q, kv.k, kv.v, rel, proj, prompt_segs, part_o, part_ml, meta.pos, tail_o, tail_ml }, 0);
e.setValue(args, 11);
e.dispatchGroups(mtl.Size.of(launch.prefix_grid[0], launch.prefix_grid[1], launch.prefix_grid[2]), mtl.Size.of(launch.threads, 1, 1));
e.barrier();
}
e.setPipeline(k.prompt_tree_tail);
bind(e, &.{ q, kv.k, kv.v, rel, proj, prompt_segs, part_o, part_ml, meta.pos, tree_paths, tail_o, tail_ml }, 0);
e.setValue(args, 12);
e.dispatchGroups(mtl.Size.of(launch.tail_grid[0], launch.tail_grid[1], launch.tail_grid[2]), mtl.Size.of(launch.threads, 1, 1));
} else {
e.setPipeline(k.decode_tree);
bind(e, &.{ q, kv.k, kv.v, rel, proj, meta.segs, part_o, part_ml, meta.pos, meta.seg_of, tree_paths }, 0);
e.setValue(planning.AttnArgs{ .heads = r.heads, .kv_heads = r.kv_heads, .ring = l.ring, .window = l.window, .chunk = l.chunk, .c0 = 0, .chunks = launch.partials_rh, .scale = scale }, 11);
e.dispatchGroups(mtl.Size.of(launch.tail_grid[0], launch.tail_grid[1], launch.tail_grid[2]), mtl.Size.of(launch.threads, 1, 1));
}
e.barrier();
e.dispatchGroups(mtl.Size.of(launch.tail_grid[0], launch.tail_grid[1], launch.tail_grid[2]), mtl.Size.of(launch.threads, 1, 1));
}

pub fn mergeRaw(k: *const Kernels, e: anytype, r: planning.Rows, l: planning.Layer, partials_rh: u32, subs: u32, meta: RowMeta, part_o: Ref, part_ml: Ref, sink: Ref, gate: Ref, out: Ref) void {
e.setPipeline(k.merge);
bind(e, &.{ part_o, part_ml, sink, gate, meta.pos, out }, 0);
e.setValue(planning.MergeArgs{ .heads = r.heads, .window = l.window, .chunk = l.chunk, .c0 = 0, .chunks = launch.partials_rh, .subs = 1 }, 6);
e.dispatchGroups(mtl.Size.of(launch.merge_grid[0], 1, 1), mtl.Size.of(planning.merge_threads, 1, 1));
e.setValue(planning.MergeArgs{ .heads = r.heads, .window = l.window, .chunk = l.chunk, .c0 = 0, .chunks = partials_rh, .subs = subs }, 6);
e.dispatchGroups(mtl.Size.of(r.rows * r.heads, 1, 1), mtl.Size.of(planning.merge_threads, 1, 1));
}

pub fn merge(k: *const Kernels, e: mtl.ComputeEncoder, launch: planning.Launch, r: planning.Rows, l: planning.Layer, meta: RowMeta, part_o: Ref, part_ml: Ref, sink: Ref, gate: Ref, out: Ref) void {
mergeRaw(k, e, r, l, launch.partials_rh, launch.subs, meta, part_o, part_ml, sink, gate, out);
}

pub fn attendTree(k: *const Kernels, e: mtl.ComputeEncoder, r: planning.Rows, l: planning.Layer, stride: u32, start: u32, meta: RowMeta, tree_paths: Ref, tail_o: Ref, tail_ml: Ref, q: Ref, kv: Cache, rel: Ref, proj: Ref, part_o: Ref, part_ml: Ref, sink: Ref, gate: Ref, out: Ref) !void {
const launch = try planning.attendTree(r, l, stride, start);
explain.reportTree(launch, r.rows);
if (treePrefix(k, e, launch, r, l, meta, q, kv, rel, proj, part_o, part_ml, tail_o, tail_ml)) e.barrier();
treePartials(k, e, launch, r, l, meta, tree_paths, tail_o, tail_ml, q, kv, rel, proj, part_o, part_ml);
e.barrier();
mergeRaw(k, e, r, l, launch.partials_rh, 1, meta, part_o, part_ml, sink, gate, out);
}

test {
Expand Down
Loading
Loading