Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

metal: the M1 Max prompt kernels - a select with a device load in the arm loses the threadgroup

Niko1221831ba47afd90
diff --git a/docs/MAC.md b/docs/MAC.md
index 668ee1c3e..df6566b63 100644
--- a/docs/MAC.md
+++ b/docs/MAC.md
@@ -5,8 +5,9 @@ kernels are replaced by Metal ones (`metal/`), and the host code and the server
`START-HERE.bat` does on Windows. There is no ready-made Mac engine: setup builds it from source on your Mac
(about 1-15 minutes). The NVIDIA, AMD and Intel engines are unchanged.
-**Status:** new. It is measured on two Macs: an M5 Max with 128 GB, where the model stays in memory, and an M4 Mac mini
-with 32 GB, where the experts stream from the SSD. Other chips and memory sizes are untested, and reports help
+**Status:** new. It is measured on three Macs: an M5 Max with 128 GB, where the model stays in memory; and an M4 Mac mini
+with 32 GB and an M1 Max with 34 GB, where the experts stream from the SSD (the M1 Max: 7.9 tok/s decode, 3.2 tok/s prompt,
+Q2_0, one 24-token request, the expert cache at 64 slots). Other chips are untested, and reports help
([COMMUNITY_BENCHMARKS.md](COMMUNITY_BENCHMARKS.md)).
## What you need
diff --git a/metal/kernels/qsa_prompt.metal b/metal/kernels/qsa_prompt.metal
index df7caf791..7029684ef 100644
--- a/metal/kernels/qsa_prompt.metal
+++ b/metal/kernels/qsa_prompt.metal
@@ -64,8 +64,11 @@ kernel void qsa_attn_mm(device const half* qh [[buffer(0)]], device const char*
for (uint16_t i = 0; i < rk.get_capacity(); ++i) {
const auto ix = rk.get_multidimensional_index(i);
const int k = k0 + ix[0], id = cid[ix[1]];
- const uint row = uint(id) * QKV + g;
- rk[i] = id >= 0 ? half(float(kq[row * QD + k]) * float(ksc[row * 4 + (k >> 6)])) : half(0.0f);
+ // the row is clamped and the select is on the value: a select with the device load in the arm
+ // loses the threadgroup on the M1 (macOS 26, measured); the discarded value cannot matter
+ const uint row = uint(max(id, 0)) * QKV + g;
+ const float v = float(kq[row * QD + k]) * float(ksc[row * 4 + (k >> 6)]);
+ rk[i] = id >= 0 ? half(v) : half(0.0f);
}
} else
#pragma clang loop unroll(full)
@@ -120,7 +123,9 @@ kernel void qsa_attn_mm(device const half* qh [[buffer(0)]], device const char*
for (uint16_t i = 0; i < rv.get_capacity(); ++i) {
const auto ix = rv.get_multidimensional_index(i); // (cell, dim)
const int c = c0 + ix[0], d = d0 + ix[1], id = cid[c];
- rv[i] = id >= 0 ? half(float(vq[(uint(id) * QKV + g) * QD + d]) * float(vs[c][d >> 6])) : half(0.0f);
+ const uint row = uint(max(id, 0)) * QKV + g; // clamped, as in the S fill above
+ const float v = float(vq[row * QD + d]) * float(vs[c][d >> 6]);
+ rv[i] = id >= 0 ? half(v) : half(0.0f);
}
tPh = tP.template slice<32, 16>(c0, 0);
op.run(tPh, rv, acc);
@@ -262,8 +267,9 @@ kernel void qsa_flash(device const half* qh [[buffer(0)]], device const char* kq
for (uint16_t i = 0; i < rk.get_capacity(); ++i) {
const auto ix = rk.get_multidimensional_index(i);
const int k = k0 + ix[0], id = cid[ix[1]];
- const uint row = uint(id) * QKV + g;
- rk[i] = id >= 0 ? half(float(kq[row * QD + k]) * float(ksc[row * 4 + (k >> 6)])) : half(0.0f);
+ const uint row = uint(max(id, 0)) * QKV + g; // clamped, as in qsa_attn_mm
+ const float v = float(kq[row * QD + k]) * float(ksc[row * 4 + (k >> 6)]);
+ rk[i] = id >= 0 ? half(v) : half(0.0f);
}
} else
#pragma clang loop unroll(full)
@@ -318,7 +324,9 @@ kernel void qsa_flash(device const half* qh [[buffer(0)]], device const char* kq
for (uint16_t i = 0; i < rv.get_capacity(); ++i) {
const auto ix = rv.get_multidimensional_index(i); // (cell, dim)
const int c = c0 + ix[0], d = d0 + ix[1], id = cid[c];
- rv[i] = id >= 0 ? half(float(vq[(uint(id) * QKV + g) * QD + d]) * float(vsc[(uint(id) * QKV + g) * 4 + sg])) : half(0.0f);
+ const uint row = uint(max(id, 0)) * QKV + g; // clamped, as in qsa_attn_mm
+ const float v = float(vq[row * QD + d]) * float(vsc[row * 4 + sg]);
+ rv[i] = id >= 0 ? half(v) : half(0.0f);
}
tPh = tP.template slice<32, 16>(c0, 0);
opO.run(tPh, rv, acc);