mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
strata-vision: auto picks Strata's own CPU picture reader on x86 CPUs without AVX2 too (no more exception for the scalar, SSE4.1 and AVX tiers)
Until now `auto` kept llama.cpp's reader whenever the CPU's best kernel tier was below AVX2, because the only tier there was the slow portable one. The SSE4.1 and AVX tiers (0991006d2,117473f64) and the run-time dispatch (0c83610e5) change that, and the owner asked for ours as the default on these CPUs ("Yes make ours the default"). `auto` now gives our tower on every CPU tier for a tower it can run (both public reader files: Qwen3.8 BF16, Qwen3.6 F16). The setting that picks llama.cpp's reader (STRATA_VISION_TOWER=mtmd, or --tower mtmd) works as before, and --tower own as before. No new setting, no new line in a log. What a user on such a CPU gets (p2006, Zen 4 running the forced tier, 8 threads, medians, ours / llama.cpp's reader built for the same CPU class; big1024 = 1,024 tokens, shapes = 260 tokens; rule 1 = the lowest cosine over 18 pictures against llama.cpp's portable AVX2 reader, the bar is 0.999): | CPU class (our tier / llama.cpp build) | model | big1024 | shapes | rule 1 minimum | |---|---|---|---|---| | no AVX (sse41 / SSE4.2 build) | Qwen3.8 | 25.2 s / 150.8 s (0.167) | 3.5 s / 37.1 s (0.095) | 0.999029 | | no AVX | Qwen3.6 | 25.3 s / 123.3 s (0.205) | 3.5 s / 29.3 s (0.121) | 0.999975 | | AVX, no F16C (avx / avx build) | Qwen3.8 | 11.8 s / 35.7 s (0.331) | 1.37 s / 8.2 s (0.167) | 0.999674 | | AVX, no F16C | Qwen3.6 | 12.2 s / 123.1 s (0.099) | 1.41 s / 30.4 s (0.046) | 0.999975 | | AVX with F16C (avx / avx_f16c build) | Qwen3.8 | 12.1 s / 34.8 s (0.346) | 1.34 s / 8.0 s (0.166) | 0.999674 | | AVX with F16C | Qwen3.6 | 12.1 s / 11.6 s (1.040) | 1.39 s / 2.17 s (0.643) | 0.999975 | The one slower cell is Qwen3.6 on a CPU with AVX and F16C, on the large picture: 1.04 of llama.cpp's time (pair ratios 1.024 to 1.056); on the 260-token picture the same pair is 0.643. Everywhere else below AVX2 ours takes 0.05 to 0.35 of llama.cpp's time. The AVX2 and AVX-512 CPUs are not affected: they already took our tower. The code: own_reader_supported() loses the "best tier below AVX2" rule; the CPU half is own_cpu_reader_supported(geometry, tier) (true for every tier when the tower's operations are implemented) and the reader a setting gives is vision_tower_resolve(setting, own_ok), so the selection is unit tested for the CPU classes no AVX, AVX without F16C, AVX with F16C, AVX2 and AVX-512 (and a CPU older than SSE4.1), for both reader files' geometry, and for the explicit setting both ways (strata-vision-own-test). cpu_caps.h gets isa_ok_for / best_isa_for (the same two functions for a given feature set); best_isa() and isa_ok() are unchanged. Checks run for this commit are in phaseA-tests/cleanup/RESULTS-OWN-3V.md PART H (unit and Python suites on p1006; a default start with the tier set by STRATA_FORCE_ISA on p2006; a default start under qemu-user with the Westmere CPU model on z440).
This commit is contained in:
1 parent
117473f643
commit
971bff01ad
5 files changed
+89
-21
No files matched your search
+16
-10
@@ -69,8 +69,8 @@ inline const Caps& caps() {
|
||||
return c;
|
||||
}
|
||||
|
||||
inline bool isa_ok(Isa i) {
|
||||
const Caps& c = caps();
|
||||
// the same two questions for any feature set (the tests give the sets of the CPU classes; the program asks caps())
|
||||
inline bool isa_ok_for(const Caps& c, Isa i) {
|
||||
if (i == Isa::Avx512) return c.avx512 && c.avx2 && c.fma;
|
||||
if (i == Isa::Avx2) return c.avx2 && c.fma && c.f16c;
|
||||
if (i == Isa::Avx) return c.avx && c.sse41;
|
||||
@@ -83,22 +83,28 @@ inline bool isa_ok(Isa i) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// the best tier this CPU runs, capped by STRATA_FORCE_ISA=scalar|sse41|avx|avx2|avx512 (on AArch64: neon, or scalar)
|
||||
inline Isa best_isa() {
|
||||
inline bool isa_ok(Isa i) { return isa_ok_for(caps(), i); }
|
||||
|
||||
// the best tier a CPU with feature set `c` runs, capped by `force` (STRATA_FORCE_ISA's value, may be null): scalar|sse41|avx|avx2|avx512 (on AArch64: neon, or scalar)
|
||||
inline Isa best_isa_for(const Caps& c, const char* force) {
|
||||
#if defined(__aarch64__) || defined(_M_ARM64)
|
||||
if (const char* e = std::getenv("STRATA_FORCE_ISA"))
|
||||
if (std::string(e) == "scalar") return Isa::Scalar;
|
||||
(void) c;
|
||||
if (force && std::string(force) == "scalar") return Isa::Scalar;
|
||||
return Isa::Neon;
|
||||
#endif
|
||||
Isa best = isa_ok(Isa::Avx512) ? Isa::Avx512 : isa_ok(Isa::Avx2) ? Isa::Avx2 : isa_ok(Isa::Avx) ? Isa::Avx : isa_ok(Isa::Sse41) ? Isa::Sse41 : Isa::Scalar;
|
||||
if (const char* e = std::getenv("STRATA_FORCE_ISA")) {
|
||||
const std::string s(e);
|
||||
#else
|
||||
Isa best = isa_ok_for(c, Isa::Avx512) ? Isa::Avx512 : isa_ok_for(c, Isa::Avx2) ? Isa::Avx2 : isa_ok_for(c, Isa::Avx) ? Isa::Avx : isa_ok_for(c, Isa::Sse41) ? Isa::Sse41 : Isa::Scalar;
|
||||
if (force) {
|
||||
const std::string s(force);
|
||||
const Isa cap = s == "scalar" ? Isa::Scalar : s == "sse41" ? Isa::Sse41 : s == "avx" ? Isa::Avx : s == "avx2" ? Isa::Avx2 : Isa::Avx512;
|
||||
if ((int) cap < (int) best) best = cap;
|
||||
}
|
||||
return best;
|
||||
#endif
|
||||
}
|
||||
|
||||
// the best tier this CPU runs, capped by STRATA_FORCE_ISA
|
||||
inline Isa best_isa() { return best_isa_for(caps(), std::getenv("STRATA_FORCE_ISA")); }
|
||||
|
||||
inline const char* isa_name(Isa i) {
|
||||
return i == Isa::Neon ? "neon" : i == Isa::Avx512 ? "avx512" : i == Isa::Avx2 ? "avx2" : i == Isa::Avx ? "avx" : i == Isa::Sse41 ? "sse41" : "scalar";
|
||||
}
|
||||
|
||||
@@ -62,24 +62,30 @@ bool own_reader_supported(const std::string& mmproj, bool gpu, int max_tokens, s
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
|
||||
else if ((int) quant::best_isa() < (int) quant::Isa::Avx2) { // every x86 tier below AVX2 (scalar, sse41, avx): unchanged by those tiers until gated
|
||||
// an x86 CPU with only the portable kernel tier (no AVX2): measured 2.1 to 3.7 times mtmd-CPU's time (RESULTS-OWN-3V.md 12.7), so `auto` keeps mtmd
|
||||
// here; `--tower own` still runs ours. Not on Arm: mtmd's portable Arm build has no fast BF16 path and our NEON tier is faster than mtmd-CPU on BF16
|
||||
// and F16 weights alike (DGX Spark, 12.12)
|
||||
why = "only the portable CPU kernels on this CPU: the own CPU tower is slower than mtmd there";
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
// (an MSVC x86 build kept mtmd here until 11-11; since 54f9cc3c0 it is 0.986 of mtmd-CPU's time at 1,024 tokens, RESULTS-OWN-3V.md D2, so that rule is gone)
|
||||
Gguf gg;
|
||||
VitGeometry g;
|
||||
if (!gg.open(mmproj, why) || !vit_geometry_from_gguf(gg, g, why)) return false;
|
||||
if (!gpu) return own_cpu_reader_supported(g, quant::best_isa(), why);
|
||||
if (!vit_geometry_supported(g, why)) return false;
|
||||
if (gpu && g.head_dim > 96) { why = "a head size above 96 is not handled by the GPU attention"; return false; } // `auto` then reads with mtmd
|
||||
if (g.head_dim > 96) { why = "a head size above 96 is not handled by the GPU attention"; return false; } // `auto` then reads with mtmd
|
||||
return true;
|
||||
}
|
||||
|
||||
bool own_cpu_reader_supported(const VitGeometry& g, quant::Isa isa, std::string& why) {
|
||||
// Every CPU tier is allowed (owner 11-11, "make ours the default" below AVX2 too). Until then an x86 CPU whose best tier was scalar, sse41 or avx kept mtmd under
|
||||
// `auto`; the tiers were gated since (RESULTS-OWN-3V.md PART H: rule 1 on both reader files, byte-identical repeats, no instruction above the CPU's tier outside the
|
||||
// CPU-checked kernels, timed against llama.cpp's reader built for the same CPU class: 0.05 to 0.35 of its time, one cell 1.04). `isa` stays an argument so a
|
||||
// per-tier rule is one line here and has a test.
|
||||
(void) isa;
|
||||
return vit_geometry_supported(g, why);
|
||||
}
|
||||
|
||||
std::string vision_tower_resolve(const std::string& tower, bool own_ok) {
|
||||
if (tower == "mtmd" || tower == "own") return tower; // an explicit choice is never overruled, either way
|
||||
return own_ok ? "own" : "mtmd"; // auto
|
||||
}
|
||||
|
||||
int own_reader_run(const OwnReaderOptions& o) {
|
||||
using clock = std::chrono::steady_clock;
|
||||
const auto load_t0 = clock::now();
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
// The output file is int32 {0x31455653 'SVE1', n_tokens, nx, ny, n_embd} then float32 [n_tokens][n_embd] (row i at x = i % nx, y = i / nx).
|
||||
#pragma once
|
||||
|
||||
#include "cpu_caps.h"
|
||||
#include "vit_geometry.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace strata_vit {
|
||||
@@ -33,6 +36,13 @@ bool own_reader_inject_failure(const char* point);
|
||||
// True when our tower can read this mmproj with this backend; otherwise `why` says what is missing (`--tower auto` falls back to mtmd then).
|
||||
bool own_reader_supported(const std::string& mmproj, bool gpu, int max_tokens, std::string& why); // max_tokens: --max-tokens (0: not given)
|
||||
|
||||
// The CPU half of that decision, for a tower of geometry `g` on a CPU whose best kernel tier is `isa`: true for every tier (the no-AVX2 exception is gone), false
|
||||
// only when the tower itself uses an operation this code does not have (`why`).
|
||||
bool own_cpu_reader_supported(const VitGeometry& g, quant::Isa isa, std::string& why);
|
||||
|
||||
// The reader a tower setting gives: "mtmd" and "own" as asked (an explicit setting is never overruled), "auto" gives "own" when `own_ok`, else "mtmd".
|
||||
std::string vision_tower_resolve(const std::string& tower, bool own_ok);
|
||||
|
||||
// Runs the reader on stdin/stdout; returns the process exit code (0 after QUIT or end of input).
|
||||
int own_reader_run(const OwnReaderOptions& o);
|
||||
|
||||
|
||||
@@ -57,6 +57,52 @@ int main(int argc, char** argv) {
|
||||
CHECK(vision_tower_default("mtmd", note) == "mtmd" && vision_tower_default("own", note) == "own" && vision_tower_default("auto", note) == "auto" && note.empty());
|
||||
CHECK(vision_tower_default("llama", note) == "auto" && note.find("STRATA_VISION_TOWER=llama") != std::string::npos);
|
||||
}
|
||||
{ // `--tower auto` on the CPU path by CPU class (owner 11-11: ours below AVX2 too), for both reader files' geometry; the explicit settings are never overruled
|
||||
VitGeometry g38; // the Qwen3.8 tower (projection 2560) and the Qwen3.6 tower (2048): the two public mmproj files' numbers
|
||||
g38.projector = "qwen3vl_merger"; g38.layers = 27; g38.width = 1152; g38.heads = 16; g38.head_dim = 72; g38.ffn = 4304; g38.patch = 16; g38.merge = 2;
|
||||
g38.image_size = 768; g38.pos_grid = 48; g38.out_dim = 2560; g38.patch_taps = 2; g38.merger_in = 4608; g38.merger_hidden = 4608;
|
||||
VitGeometry g36 = g38;
|
||||
g36.out_dim = 2048;
|
||||
quant::Caps none_; // an x86-64 CPU older than SSE4.1: the scalar tier
|
||||
quant::Caps nehalem = none_; nehalem.sse41 = true; // no AVX: Nehalem, Westmere
|
||||
quant::Caps sandy = nehalem; sandy.avx = true; // AVX, no F16C: Sandy Bridge
|
||||
quant::Caps ivy = sandy; ivy.f16c = true; // AVX with F16C: Ivy Bridge
|
||||
quant::Caps haswell = ivy; haswell.avx2 = true; haswell.fma = true;
|
||||
quant::Caps skylake_x = haswell; skylake_x.avx512 = true;
|
||||
struct Cls { const char* name; const quant::Caps* c; quant::Isa tier; };
|
||||
const Cls classes[] = {{"no SSE4.1", &none_, quant::Isa::Scalar}, {"no AVX", &nehalem, quant::Isa::Sse41}, {"AVX without F16C", &sandy, quant::Isa::Avx},
|
||||
{"AVX with F16C", &ivy, quant::Isa::Avx}, {"AVX2", &haswell, quant::Isa::Avx2}, {"AVX-512", &skylake_x, quant::Isa::Avx512}};
|
||||
VitGeometry g_rms = g38;
|
||||
g_rms.norm = NormKind::RMSNorm; // a tower this code cannot run: `auto` keeps mtmd on every tier
|
||||
for (const Cls& k : classes) {
|
||||
std::string why;
|
||||
#if defined(__x86_64__) || defined(_M_X64)
|
||||
const quant::Isa tier = quant::best_isa_for(*k.c, nullptr);
|
||||
if (tier != k.tier) { std::printf("FAIL tier of the %s class: %s\n", k.name, quant::isa_name(tier)); ++g_fail; }
|
||||
#else
|
||||
const quant::Isa tier = k.tier;
|
||||
#endif
|
||||
for (const VitGeometry* g : {&g38, &g36}) {
|
||||
CHECK(own_cpu_reader_supported(*g, tier, why)); // ours on every tier, both reader files
|
||||
CHECK(vision_tower_resolve("auto", own_cpu_reader_supported(*g, tier, why)) == "own");
|
||||
}
|
||||
CHECK(!own_cpu_reader_supported(g_rms, tier, why) && !why.empty());
|
||||
CHECK(vision_tower_resolve("auto", own_cpu_reader_supported(g_rms, tier, why)) == "mtmd");
|
||||
}
|
||||
#if defined(__x86_64__) || defined(_M_X64)
|
||||
// the tier mask (STRATA_FORCE_ISA) caps the tier a CPU runs, never raises it
|
||||
CHECK(quant::best_isa_for(skylake_x, "sse41") == quant::Isa::Sse41 && quant::best_isa_for(skylake_x, "avx") == quant::Isa::Avx && quant::best_isa_for(skylake_x, "avx2") == quant::Isa::Avx2);
|
||||
CHECK(quant::best_isa_for(skylake_x, "scalar") == quant::Isa::Scalar && quant::best_isa_for(sandy, "avx2") == quant::Isa::Avx && quant::best_isa_for(nehalem, "avx512") == quant::Isa::Sse41);
|
||||
CHECK(quant::isa_ok_for(ivy, quant::Isa::Avx) && !quant::isa_ok_for(ivy, quant::Isa::Avx2) && !quant::isa_ok_for(nehalem, quant::Isa::Avx) && !quant::isa_ok_for(none_, quant::Isa::Sse41));
|
||||
#endif
|
||||
// the explicit settings, both ways, whatever `own_ok` says
|
||||
CHECK(vision_tower_resolve("mtmd", true) == "mtmd" && vision_tower_resolve("mtmd", false) == "mtmd");
|
||||
CHECK(vision_tower_resolve("own", true) == "own" && vision_tower_resolve("own", false) == "own");
|
||||
CHECK(vision_tower_resolve("auto", true) == "own" && vision_tower_resolve("auto", false) == "mtmd");
|
||||
std::string note;
|
||||
CHECK(vision_tower_resolve(vision_tower_default("mtmd", note), true) == "mtmd" && vision_tower_resolve(vision_tower_default("own", note), false) == "own");
|
||||
CHECK(vision_tower_resolve(vision_tower_default(nullptr, note), true) == "own"); // no setting at all: auto
|
||||
}
|
||||
{ // a start failure under auto comes back as kOwnStartFailed with its reason (nothing on stdout), so strata-vision can start mtmd's reader
|
||||
OwnReaderOptions o;
|
||||
o.mmproj = "/nonexistent/strata-own-test-mmproj.gguf";
|
||||
|
||||
@@ -138,7 +138,7 @@ int main(int argc, char** argv) {
|
||||
bool own_may_fall_back = false, own_fell_back = false;
|
||||
if (tower == "auto") {
|
||||
std::string why;
|
||||
tower = !mmproj.empty() && strata_vit::own_reader_supported(mmproj, gpu, max_tokens, why) ? "own" : "mtmd";
|
||||
tower = strata_vit::vision_tower_resolve(tower, !mmproj.empty() && strata_vit::own_reader_supported(mmproj, gpu, max_tokens, why));
|
||||
own_may_fall_back = tower == "own" && !model.empty(); // mtmd needs the text model's header
|
||||
if (tower == "mtmd" && !why.empty()) std::fprintf(stderr, "strata-vision: llama.cpp's reader (%s)\n", why.c_str());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user