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++

strata-vision: flash attention off on the CPU only when ggml has AVX-512

shefowle7ec6b1abb14
diff --git a/docs/DETAILS.md b/docs/DETAILS.md
index 43da2ffe8..580cd037d 100644
--- a/docs/DETAILS.md
+++ b/docs/DETAILS.md
@@ -1113,6 +1113,23 @@ a community Q8_0 file, at 300, 768 and 1,024 tokens), and 0.9988 and 0.9987 on t
the command above (300 tokens; the worst single token 0.94-0.96). Encode time above 768 tokens is the same as BF16's
within about 3%. Numbers on more pictures (charts, small text) are welcome in #625.
+**Flash attention on the CPU (#660):** ggml's fast CPU kernel for flash attention needs the encoder's head size (72)
+to be a multiple of the vector width: 8 floats with AVX2, which the release builds use, but 16 with AVX-512, which a
+build from source gets on a CPU that has it (Zen 4 and newer, some Intel). There ggml falls back to a kernel that is
+several times slower and accumulates in FP16, so `strata-vision` turns flash attention off on the CPU when its ggml has
+AVX-512 and leaves it at `auto` (on) otherwise; `--flash-attn on|off|auto` overrides that. A 1024x1024 picture
+(1,024 image tokens) on a Ryzen 7 7700X, 8 threads, an F16 mmproj, one encode per fresh process:
+
+| Build | Flash attention | Encode | Peak RSS | Output vs FP32 attention |
+| --- | --- | --- | --- | --- |
+| AVX2 (as released) | on (default) | 8.3 / 9.9 s | 1,152 MiB | 1.2% |
+| AVX2 | off | 15.2 / 14.0 s | 2,194 MiB | 1.1% |
+| AVX-512 (from source) | on | 43.5 s | 1,152 MiB | 26% |
+| AVX-512 (from source) | off (default) | 13.3 s | 2,195 MiB | - |
+
+The last column is the relative L2 distance of the embeddings from the AVX-512 run without flash attention; two builds
+differ by about 1% anyway.
+
**A spare GPU for the encoder (0.1.33, #408):** with a card the engine doesn't use, add `"cuda_device": 2` (numbered
like `nvidia-smi`) to the `"vision"` section of `strata-<model>.json`: the encoder then runs on that card alone. Lower
`--vram-reserve-mib` in `"args"` to 700 as well, so the engine's cards keep that VRAM for the expert cache. The
diff --git a/tools/vision/strata_vision.cpp b/tools/vision/strata_vision.cpp
index c766f9438..d8ef58fe8 100644
--- a/tools/vision/strata_vision.cpp
+++ b/tools/vision/strata_vision.cpp
@@ -6,12 +6,14 @@
//
// strata-vision --mmproj <mmproj.gguf> --model <text model .gguf, first split> [--gpu] [--threads N]
// [--max-tokens N] [--min-tokens N] [--flash-attn on|off|auto]
+// Flash attention defaults to auto, and to off on the CPU when ggml is built with AVX-512 (see main).
//
// Resident: prints "READY <n_embd>", then per stdin line
// ENC <image path> <output path> -> "OK <n_tokens> <nx> <ny> <ms>" or "ERR <message>"
// QUIT
// The output file is int32 {0x31455653 'SVE1', n_tokens, nx, ny, n_embd} then float32 [n_tokens][n_embd],
// row i at grid position (x = i % nx, y = i / nx). The text model is opened vocab-only (no weights).
+#include "ggml-cpu.h"
#include "gguf.h"
#include "llama.h"
#include "mtmd.h"
@@ -57,6 +59,7 @@ int main(int argc, char** argv) {
bool gpu = false;
int threads = 0, max_tokens = 0, min_tokens = 0;
llama_flash_attn_type fa = LLAMA_FLASH_ATTN_TYPE_AUTO;
+ bool fa_given = false;
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
auto next = [&]() -> std::string {
@@ -73,6 +76,7 @@ int main(int argc, char** argv) {
const std::string v = next();
fa = v == "on" ? LLAMA_FLASH_ATTN_TYPE_ENABLED : v == "off" ? LLAMA_FLASH_ATTN_TYPE_DISABLED
: LLAMA_FLASH_ATTN_TYPE_AUTO;
+ fa_given = true;
}
else { std::fprintf(stderr, "unknown argument %s\n", a.c_str()); return 2; }
}
@@ -117,6 +121,12 @@ int main(int argc, char** argv) {
cp.use_gpu = gpu;
cp.print_timings = false;
cp.warmup = false;
+ // On the CPU "auto" turns flash attention on. ggml's fast (tiled) CPU kernel for it needs the head size, 72 in this
+ // encoder, to be a multiple of the vector width: 8 floats with AVX2 (the release builds), 16 with AVX-512 (a build
+ // from source on a CPU that has it), where ggml falls back to a kernel that is several times slower and accumulates
+ // in FP16. A 1024x1024 picture on a Ryzen 7 7700X, 8 threads: AVX2 8-10 s with it, 14-15 s without; AVX-512 44 s
+ // with it (26% off the FP32 attention's output), 13 s without.
+ if (!gpu && !fa_given && ggml_cpu_has_avx512()) fa = LLAMA_FLASH_ATTN_TYPE_DISABLED;
cp.flash_attn_type = fa;
// on the CPU without --threads: one per core (mtmd's own default is 4 threads)
if (threads <= 0 && !gpu) threads = std::max(1u, std::thread::hardware_concurrency() / 2);