Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9922582
inkling2 metal: the native build finds the checkpoint file cache and …
sethforprivacy Nov 21, 2026
c68340d
inkling2 metal: norm binds its arguments and its output where sp_norm…
sethforprivacy Nov 21, 2026
2f386ca
inkling2 metal: routed experts as one simdgroup an output
sethforprivacy Nov 21, 2026
49c7986
inkling2 metal: loop gates and router as short launches, a simdgroup …
sethforprivacy Nov 21, 2026
a889fac
inkling2 metal: launch records for the rewritten experts, gates and r…
sethforprivacy Nov 21, 2026
d447ca7
lean: the router scores comment fits 120 columns
sethforprivacy Nov 21, 2026
6f0f294
inkling2 metal: a later stream of a shared forward reads its relative…
sethforprivacy Nov 22, 2026
ef5bd87
inkling2 metal: the depth mixes and the loop gates take every row in …
sethforprivacy Nov 21, 2026
2c8338c
inkling2 metal: routed experts grouped by expert for chunks of four r…
sethforprivacy Nov 21, 2026
eaea488
inkling2 metal: sliding attention in 32-key chunks, global attention …
sethforprivacy Nov 21, 2026
49e474a
lean: the global chunk comment fits 120 columns
sethforprivacy Nov 22, 2026
e4f793b
inkling2 metal: experts grouped from eight rows, and a projection of …
sethforprivacy Nov 22, 2026
d87e869
inkling2 metal: the forward on a concurrent encoder in five command b…
sethforprivacy Nov 22, 2026
ee248f9
inkling2 metal: routed experts on the tensor units at every width, so…
sethforprivacy Nov 22, 2026
dffe470
inkling2 metal: a greedy row without a token mask is picked sixteen l…
sethforprivacy Nov 22, 2026
ee33237
inkling2 metal: the depth mix reads four values a thread, and each si…
sethforprivacy Nov 22, 2026
55de80a
inkling2 metal: the combine orders a token's routes once a threadgroup
sethforprivacy Nov 22, 2026
a9fae61
inkling2 metal: prompt chunks of up to 256 rows, so a tensor-unit til…
sethforprivacy Nov 22, 2026
530fca3
inkling2 metal: the prompt pass a chunk a call, so other streams deco…
sethforprivacy Nov 22, 2026
5712db3
inkling2 metal: the logits kernel reads a vocabulary row once for up …
sethforprivacy Nov 22, 2026
ad3d31f
inkling2 metal: logits on the tensor units, one pass of the vocabular…
sethforprivacy Nov 22, 2026
3ebdde7
inkling2 metal: a prompt's rows take their projections on the tensor …
sethforprivacy Nov 22, 2026
2d7a9c9
inkling2 metal: a prompt's rows take the sliding layers' attention on…
sethforprivacy Nov 22, 2026
642bb4f
inkling2 metal: a chunk wider than a window stages its convolution ta…
sethforprivacy Nov 22, 2026
9f88b80
inkling2 metal: prompt chunks of up to 1,024 rows, which the host may…
sethforprivacy Nov 22, 2026
a09f3e7
inkling2 metal: on M5 the prompt kernel reads a tile's keys and value…
sethforprivacy Nov 22, 2026
84ab2c6
inkling2 metal: wide chunks take the routed experts on turned tiles, …
sethforprivacy Nov 22, 2026
232ee08
inkling2 metal: the prompt kernel keeps one set of sums in registers,…
sethforprivacy Nov 22, 2026
2b1243e
Inkling 2 Mini on Metal: routed experts of a prompt chunk on simdgrou…
BlivionIaG Nov 23, 2026
01b5373
inkling2 metal: a prompt's projections and the loop gates on simdgrou…
BlivionIaG Nov 23, 2026
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
87 changes: 87 additions & 0 deletions zig/kernels/metal/inkling2/dense_mma.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Prompt projections on simdgroup matrices for GPUs without tensor units: sp_matvec's sums, bit for bit.
#include <metal_stdlib>
using namespace metal;

struct Ink2DenseMmaArgs {
uint rows, n, k, ldy;
};

using ink2_dense_frag = simdgroup_matrix<float, 8, 8>;

// Row tiles ROWS and column tiles COLS of the 16 by 16 tile that hold outputs; simdgroup g keeps lanes 8g .. 8g + 7.
template <uint ROWS, uint COLS>
inline void ink2_dense_mma(const device bfloat* X, const device bfloat* W, constant Ink2DenseMmaArgs& a,
device bfloat* Y, uint2 tg, uint g, uint lane, threadgroup float (&part)[4][2][2][8][8]) {
const uint r0 = tg.y * 16u, n0 = tg.x * 16u;
const uint qid = lane >> 2u;
const uint fm = (qid & 4u) + ((lane >> 1u) & 3u);
const uint fn = (qid & 2u) * 2u + (lane & 1u) * 2u;
// The first four columns of an octet go through one MMA and the last four through another.
const bool low = fn < 4u;
// A matvec lane adds, per 256 columns, its first four products to its next four, then to its running sum.
ink2_dense_frag acc[ROWS][COLS][8];
_Pragma("clang loop unroll(full)") for (uint i = 0u; i < ROWS; ++i)
_Pragma("clang loop unroll(full)") for (uint j = 0u; j < COLS; ++j)
_Pragma("clang loop unroll(full)") for (uint c = 0u; c < 8u; ++c) acc[i][j][c] = ink2_dense_frag(0.0f);
const device bfloat* x = X + ulong(r0) * a.k + 64u * g;
const device bfloat* w = W + ulong(n0) * a.k + 64u * g;
for (uint col = 0u; col < a.k; col += 256u) {
_Pragma("clang loop unroll(full)") for (uint c = 0u; c < 8u; ++c) {
simdgroup_matrix<bfloat, 8, 8> fa[ROWS][2], fb[COLS];
_Pragma("clang loop unroll(full)") for (uint i = 0u; i < ROWS; ++i) {
simdgroup_matrix<bfloat, 8, 8> v;
simdgroup_load(v, x + ulong(8u * i) * a.k + col + 8u * c, a.k);
const bfloat z = bfloat(0.0f);
fa[i][0].thread_elements()[0] = low ? v.thread_elements()[0] : z;
fa[i][0].thread_elements()[1] = low ? v.thread_elements()[1] : z;
fa[i][1].thread_elements()[0] = low ? z : v.thread_elements()[0];
fa[i][1].thread_elements()[1] = low ? z : v.thread_elements()[1];
}
_Pragma("clang loop unroll(full)") for (uint j = 0u; j < COLS; ++j)
simdgroup_load(fb[j], w + ulong(8u * j) * a.k + col + 8u * c, a.k, ulong2(0, 0), true);
_Pragma("clang loop unroll(full)") for (uint i = 0u; i < ROWS; ++i)
_Pragma("clang loop unroll(full)") for (uint j = 0u; j < COLS; ++j) {
ink2_dense_frag d0(0.0f), d1(0.0f);
simdgroup_multiply_accumulate(d0, fa[i][0], fb[j], d0);
simdgroup_multiply_accumulate(d1, fa[i][1], fb[j], d1);
acc[i][j][c].thread_elements()[0] += d0.thread_elements()[0] + d1.thread_elements()[0];
acc[i][j][c].thread_elements()[1] += d0.thread_elements()[1] + d1.thread_elements()[1];
}
}
}
// The eight chains of the group add adjacent first, as simd_sum's first three levels do.
_Pragma("clang loop unroll(full)") for (uint i = 0u; i < ROWS; ++i)
_Pragma("clang loop unroll(full)") for (uint j = 0u; j < COLS; ++j)
_Pragma("clang loop unroll(full)") for (uint e = 0u; e < 2u; ++e) {
float v[8];
_Pragma("clang loop unroll(full)") for (uint c = 0u; c < 8u; ++c) v[c] = acc[i][j][c].thread_elements()[e];
_Pragma("clang loop unroll(full)") for (uint s = 1u; s < 8u; s <<= 1u)
_Pragma("clang loop unroll(full)") for (uint c = 0u; c < 8u; c += 2u * s) v[c] = v[c] + v[c + s];
part[g][i][j][fm][fn + e] = v[0];
}
threadgroup_barrier(mem_flags::mem_threadgroup);
// Then the four groups: (0 + 1) + (2 + 3).
for (uint o = g * 32u + lane; o < ROWS * COLS * 64u; o += 128u) {
const uint i = o >> (COLS == 2u ? 7u : 6u), j = COLS == 2u ? (o >> 6u) & 1u : 0u, m = (o >> 3u) & 7u, n = o & 7u;
const float t = (part[0][i][j][m][n] + part[1][i][j][m][n]) + (part[2][i][j][m][n] + part[3][i][j][m][n]);
const uint row = r0 + i * 8u + m, col = n0 + j * 8u + n;
if (row < a.rows) Y[ulong(row) * a.ldy + col] = bfloat(t);
}
}

// Threadgroup (x, y): outputs 16x .. 16x + 15 of rows 16y .. 16y + 15; n is a multiple of 8, k of 256.
[[kernel, max_total_threads_per_threadgroup(128)]] void tf_ink2_dense_mma(
const device bfloat* X [[buffer(0)]], const device bfloat* W [[buffer(1)]],
constant Ink2DenseMmaArgs& a [[buffer(2)]], device bfloat* Y [[buffer(3)]],
uint2 tg [[threadgroup_position_in_grid]], uint g [[simdgroup_index_in_threadgroup]],
uint lane [[thread_index_in_simdgroup]]) {
threadgroup float part[4][2][2][8][8];
const bool two_rows = a.rows - tg.y * 16u > 8u, two_cols = a.n - tg.x * 16u > 8u;
if (two_rows) {
if (two_cols) ink2_dense_mma<2u, 2u>(X, W, a, Y, tg, g, lane, part);
else ink2_dense_mma<2u, 1u>(X, W, a, Y, tg, g, lane, part);
} else {
if (two_cols) ink2_dense_mma<1u, 2u>(X, W, a, Y, tg, g, lane, part);
else ink2_dense_mma<1u, 1u>(X, W, a, Y, tg, g, lane, part);
}
}
2 changes: 2 additions & 0 deletions zig/kernels/metal/inkling2/dense_sources.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//! Inkling 2 Mini's prompt projections on simdgroup matrices, for GPUs without tensor units.
pub const dense_mma = @embedFile("dense_mma.metal");
Loading
Loading