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
42 changes: 42 additions & 0 deletions engine/src/inkling2/metal/ink_metal_codecs.metal
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,48 @@ kernel void ink_linear_bf16_mv1(
if (h3) { const float v3 = simd_sum(a3); if (lane == 0) Y[row0 + 3] = v3; }
}

// M = 2..8 rows with ink_linear_bf16_mv1's exact summation order for every row: the same lane-to-k assignment (8 weights per
// step as two float4 dots added before the accumulate), the same K & 4 tail on lane 0, the same simd_sum. A verify window
// then returns the logits a one-token step would, so speculative decoding cannot change greedy tokens. Grid (N + 31) / 32, 256 threads.
kernel void ink_linear_bf16_mv_m1order(
device const ushort* W [[buffer(0)]],
device const float* X [[buffer(1)]],
device float* Y [[buffer(2)]],
constant int& N [[buffer(3)]],
constant int& K [[buffer(4)]],
constant int& M [[buffer(5)]],
uint tg [[threadgroup_position_in_grid]],
uint tid [[thread_index_in_threadgroup]]) {
const uint sg = tid >> 5, lane = tid & 31;
const int row0 = int(tg) * 32 + int(sg) * 4;
if (row0 >= N) return;
const int mm = min(M, 8);
for (int r = 0; r < 4; ++r) {
const int row = row0 + r;
if (row >= N) return;
device const ushort4* wp = (device const ushort4*)(W + (size_t)row * K);
const int K8 = K >> 3;
float a[8] = {0, 0, 0, 0, 0, 0, 0, 0};
for (int k = int(lane); k < K8; k += 32) {
const int k2 = k * 2;
const float4 w0 = bf16x4_to_f32(wp[k2]), w1 = bf16x4_to_f32(wp[k2 + 1]);
for (int i = 0; i < mm; ++i) {
device const float4* xp = (device const float4*)(X + (size_t)i * K);
a[i] += dot(w0, xp[k2]) + dot(w1, xp[k2 + 1]);
}
}
if ((K & 4) != 0 && lane == 0) {
const int k2 = K8 * 2;
const float4 w0 = bf16x4_to_f32(wp[k2]);
for (int i = 0; i < mm; ++i) a[i] += dot(w0, ((device const float4*)(X + (size_t)i * K))[k2]);
}
for (int i = 0; i < mm; ++i) {
const float v = simd_sum(a[i]);
if (lane == 0) Y[(size_t)i * N + row] = v;
}
}
}

// --- BF16 simdgroup_matrix 8x64 GEMM for M=2..8 (ZERO M-waste for DSpark M=8 verify & draft!)
kernel void ink_linear_bf16_mm8(
device const ushort* W [[buffer(0)]],
Expand Down
24 changes: 19 additions & 5 deletions engine/src/inkling2/metal/ink_metal_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ struct InkMetalRunner {
// What DFlash 2 takes: the widened weights and the Q8 unembed screen (about 1.1 GiB) plus the per-slot context rings (about 43 MiB each); the load prints what it really took.
static uint64_t df2_bytes_estimate(int slots) { return (1118ull << 20) + (uint64_t)std::max(1, slots) * (43ull << 20); }
bool is_apple10 = false;
// STRATA_INK_M_INVARIANT=1 (opt-in, the Metal side of #6960's switch): a verify window of M = 2..8 rows (DFlash 2 checks kBlock = 8) returns bit-identical logits to
// one-token steps, so drafting cannot change greedy tokens. Two things differ otherwise: on every GPU the BF16 mv / mm8 kernels
// sum a row in another order than mv1, and Apple10 switches the trunk to MPP from M = 4..5 and S6 to mm8 from M = 2.
bool verify_m1_match = false;
bool use_mpp = false, use_mpp_trunk_f16 = false, use_mpp_trunk_i8 = false, use_dspark_f16 = false;
int mpp_decomp_cap = 0;

Expand All @@ -151,7 +155,7 @@ struct InkMetalRunner {
mtl::Pipeline p_top128_part, p_top128_red;

// Pipelines
mtl::Pipeline p_lin_bf16_mv1, p_lin_bf16_mv, p_lin_bf16_mm8, p_lin_bf16_mm;
mtl::Pipeline p_lin_bf16_mv1, p_lin_bf16_mv, p_lin_bf16_mm8, p_lin_bf16_mm, p_lin_bf16_mv_m1order;
mtl::Pipeline p_lin_q8_mv1, p_lin_q8_mv, p_lin_q8_mm8, p_lin_q8_mm;
mtl::Pipeline p_lin_f32, p_lin_f32_mm, p_lin_f32_exact_mm;
mtl::Pipeline p_lin_sk[9], p_lin_sk5_mm8, p_lin_sk6_mm8;
Expand Down Expand Up @@ -305,6 +309,7 @@ struct InkMetalRunner {
// Create pipelines
p_lin_bf16_mv1 = cx->pipeline(lib_codecs, "ink_linear_bf16_mv1");
p_lin_bf16_mv = cx->pipeline(lib_codecs, "ink_linear_bf16_mv");
p_lin_bf16_mv_m1order = cx->pipeline(lib_codecs, "ink_linear_bf16_mv_m1order");
p_lin_bf16_mm8 = cx->pipeline(lib_codecs, "ink_linear_bf16_mm8");
p_lin_bf16_mm = cx->pipeline(lib_codecs, "ink_linear_bf16_mm");
p_lin_q8_mv1 = cx->pipeline(lib_codecs, "ink_linear_q8row_mv1");
Expand Down Expand Up @@ -1036,9 +1041,13 @@ struct InkMetalRunner {
}
}

if (!(m5.active && !m5.exact)) { // not the M5 Max FAST mode, which is lossy by design (its EXACT mode has exact_verify_m1_match)
const char* e = std::getenv("STRATA_INK_M_INVARIANT");
verify_m1_match = (e && e[0] == '1');
}
fprintf(stderr,
"[ink_metal] runner ready: slots=%d ctx=%d batch=%d dflash2=%d dspark=%d vision=%s mpp=%d(trunk_f16=%d,trunk_i8=%d,kv_fp8=%d) m5max=%s | KV=%.1f MiB SC=%.1f MiB TotalAlloc=%.2f GiB\n",
max_slots, max_ctx, max_batch, (int)use_dflash2, (int)use_dspark,
"[ink_metal] runner ready: slots=%d ctx=%d batch=%d dflash2=%d verify_m1=%d dspark=%d vision=%s mpp=%d(trunk_f16=%d,trunk_i8=%d,kv_fp8=%d) m5max=%s | KV=%.1f MiB SC=%.1f MiB TotalAlloc=%.2f GiB\n",
max_slots, max_ctx, max_batch, (int)use_dflash2, (int)verify_m1_match, (int)use_dspark,
vis.loaded ? "loaded" : (pending_vision_path.empty() ? "off" : "lazy"),
(int)use_mpp, (int)use_mpp_trunk_f16, (int)use_mpp_trunk_i8, (int)kv_is_fp8,
m5.active ? (m5.exact ? "EXACT" : "FAST") : "OFF",
Expand Down Expand Up @@ -1133,6 +1142,11 @@ struct InkMetalRunner {
d.p = &p_lin_bf16_mv1;
d.grid[0] = (uint32_t)((N + 31) / 32);
d.tg[0] = 256;
} else if (verify_m1_match && M >= 2 && M <= 8) {
// A verify window: every row summed in mv1's order, so its logits equal a one-token step's (the unembed included)
d.p = &p_lin_bf16_mv_m1order;
d.grid[0] = (uint32_t)((N + 31) / 32);
d.tg[0] = 256;
} else if (M >= 2 && M <= 8 && N >= 32768 && !(m5.active && m5.exact_verify_m1_match)) {
// On huge vocab unembed (N=200k), simdgroup_half8x8 mm8 beats register mv at M=2..8 (cuts DSpark k=2..4 by -0.7 ms!)
d.p = &p_lin_bf16_mm8;
Expand Down Expand Up @@ -1187,7 +1201,7 @@ struct InkMetalRunner {
d.tg[0] = 128;
}
} else if (ti.fmt >= FMT_S2 && ti.fmt <= FMT_S8) {
if (ti.fmt == FMT_S6 && M >= 2 && M <= 8 && is_apple10) {
if (ti.fmt == FMT_S6 && M >= 2 && M <= 8 && is_apple10 && !verify_m1_match) {
d.p = &p_lin_sk6_mm8;
d.grid[0] = (uint32_t)((N + 63) / 64);
d.tg[0] = 256;
Expand Down Expand Up @@ -1669,7 +1683,7 @@ struct InkMetalRunner {
// Measured exact Apple10 crossover: BF16 mv beats MPP at M<=3, MPP wins at M>=4 (-6.7 ms at M=8!);
// Q8_ROW 1B mv beats MPP at M<=4 (-4.4 ms at M=1!), MPP wins at M>=5 (-5.8 ms at M=8!).
const bool trunk_is_q8 = (mod->man.get(p + ".attn.wq_du.weight").fmt == FMT_Q8ROW);
const int mpp_trunk_min_m = (m5.active && m5.exact_verify_m1_match) ? 9 : (is_apple10 ? (trunk_is_q8 ? 5 : 4) : 16);
const int mpp_trunk_min_m = ((m5.active && m5.exact_verify_m1_match) || verify_m1_match) ? 9 : (is_apple10 ? (trunk_is_q8 ? 5 : 4) : 16);
const int i8_mm_min_m = []() {
const char* e = std::getenv("STRATA_INK_M5MAX_I8_MM_MIN_M");
return (e && std::atoi(e) > 0) ? std::atoi(e) : 5;
Expand Down
39 changes: 36 additions & 3 deletions terminal/data/settings-register.json
Original file line number Diff line number Diff line change
Expand Up @@ -19218,7 +19218,7 @@
"reads": [
{
"file": "engine/src/inkling2/metal/ink_metal_runner.h",
"line": 1674,
"line": 1688,
"backend": "metal",
"fn": "getenv"
}
Expand Down Expand Up @@ -19290,7 +19290,7 @@
"reads": [
{
"file": "engine/src/inkling2/metal/ink_metal_runner.h",
"line": 2174,
"line": 2188,
"backend": "metal",
"fn": "getenv"
}
Expand Down Expand Up @@ -19323,7 +19323,7 @@
"reads": [
{
"file": "engine/src/inkling2/metal/ink_metal_runner.h",
"line": 2830,
"line": 2844,
"backend": "metal",
"fn": "getenv"
}
Expand Down Expand Up @@ -19381,6 +19381,39 @@
"retire_after": "",
"retired_in": ""
},
{
"name": "STRATA_INK_M_INVARIANT",
"backends": [
"metal"
],
"reads": [
{
"file": "engine/src/inkling2/metal/ink_metal_runner.h",
"line": 1045,
"backend": "metal",
"fn": "getenv"
}
],
"parse": "flag",
"default_state": "off",
"code_default": null,
"engine_sets": [],
"engine_defaults": [],
"launch_files": [],
"launch_sets": [],
"launch_reads": [],
"doc_mentions": [],
"first_commit": null,
"first_date": null,
"since": null,
"last_commit": null,
"last_date": null,
"status": "opt-in",
"owner": "",
"note": "Metal: verify windows of 2..8 rows (DFlash 2 / DSpark) use the one-token kernels, so drafting keeps the greedy tokens of --draft off (#6671). BF16 linears take ink_linear_bf16_mv_m1order, the Apple10 MPP trunk starts at M = 9, sk6_mm8 is skipped. Off by default; not applied in the M5 Max FAST mode.",
"retire_after": "",
"retired_in": ""
},
{
"name": "STRATA_INK_NO_CAND",
"backends": [
Expand Down
Loading