tools/vision: our picture reader is the default (owner 11-11: "Our tower, the other selectable"); llama.cpp's mtmd reader stays selectable and takes over by itself where ours was measured slower or does not start

- the default tower is `auto` (was mtmd); without --tower, STRATA_VISION_TOWER=mtmd|own|auto decides (the run config's env carries it when someone picks llama.cpp's reader; an older strata-vision ignores it); an explicit --tower wins
- `auto` now takes ours on Metal (gate passed on the M5 Max, RESULTS-OWN-3V.md D1/D4-D8) and on MSVC x86 builds (rule cab87a2e8 removed: 0.986 of mtmd-CPU since 54f9cc3c0, D2)
- kept: gfx1151, Pascal with F16 matrices at <= 1,024 tokens, x86 without AVX2; new hold: every card on Windows HIP (not measured with the Windows ROCm package; remove when one RX 9000 / RX 7000 run on Windows is in)
- under `auto`, a failure of ours before READY (no GPU, the load, the warm-up) writes nothing on stdout; strata-vision logs the reason and starts mtmd's reader in the same process; `--tower own` keeps its hard failure
- READY names the reader: "READY <n> own", "READY <n> mtmd", "READY <n> mtmd fallback" (servers read only the start)
- test hook STRATA_VIT_TEST_FAIL=gpu|load|warmup; unit tests for the default, the Windows HIP hold, the soft start and the hook (p2006 job p2006-20261111-0743-7d3b: strata-vision-own-test ok, cpu-test all passed)
This commit is contained in:
Niko1221 committed 2026-11-11 07:44:24 +01:00
1 parent 54f9cc3c0c
commit 59e0758810
9 files changed
+111 -34

No files matched your search

+27 -7
View File
@@ -34,6 +34,18 @@ bool parse_enc(const std::string& line, std::string& img, std::string& out) {
} // namespace
std::string vision_tower_default(const char* env, std::string& note) {
const std::string v = env ? env : "";
if (v == "mtmd" || v == "own" || v == "auto") return v;
if (!v.empty()) note = "STRATA_VISION_TOWER=" + v + " is not mtmd, own or auto; using auto";
return "auto";
}
bool own_reader_inject_failure(const char* point) {
const char* v = std::getenv("STRATA_VIT_TEST_FAIL");
return v && point && std::string(v) == point;
}
bool own_reader_supported(const std::string& mmproj, bool gpu, int max_tokens, std::string& why) {
if (gpu) {
#ifdef STRATA_VIT_GPU
@@ -59,10 +71,7 @@ bool own_reader_supported(const std::string& mmproj, bool gpu, int max_tokens, s
return false;
}
#endif
else if (vit_cpu_build_slower_than_mtmd()) {
why = "this build of the own CPU tower (MSVC, x86) is slower than mtmd's CPU reader";
return false;
}
// (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;
@@ -74,11 +83,17 @@ bool own_reader_supported(const std::string& mmproj, bool gpu, int max_tokens, s
int own_reader_run(const OwnReaderOptions& o) {
using clock = std::chrono::steady_clock;
const auto load_t0 = clock::now();
auto fail = [](const std::string& msg, int code) {
// every return before READY goes through here: under `auto` the reason goes back to strata-vision, which starts mtmd's reader (nothing on stdout yet)
auto fail = [&o](const std::string& msg, int code) {
if (o.start_error) {
*o.start_error = msg;
return kOwnStartFailed;
}
std::printf("ERR %s\n", msg.c_str());
std::fflush(stdout);
return code;
};
const std::string injected = " (injected for a test: STRATA_VIT_TEST_FAIL)";
#ifndef STRATA_VIT_GPU
if (o.gpu) return fail("this build has no GPU backend for the own tower (use --tower mtmd)", 3);
#endif
@@ -94,12 +109,14 @@ int own_reader_run(const OwnReaderOptions& o) {
std::unique_ptr<VitGpu> gpu;
if (o.gpu) {
std::string why;
if (own_reader_inject_failure("gpu")) return fail("this picture reader found no GPU" + injected, 3);
if (!VitGpu::available(why)) return fail("this picture reader found no GPU (" + why + ")", 3);
gpu.reset(new VitGpu());
// buffers for the warm-up picture below (2048 x 2048: 4,096 tokens at most, fewer under a lower limit): claimed now, before READY. A larger limit
// (the app allows up to 16,384 image tokens) grows them when such a picture comes: slower for that picture, never an error.
const int tokens = std::min(o.max_tokens > 0 ? o.max_tokens : geo.max_tokens, 4096);
if (!gpu->load(gg, geo, 0, tokens * 4, err)) return fail("cannot load the vision encoder " + o.mmproj + " (" + err + ")", 1);
if (own_reader_inject_failure("load")) return fail("cannot load the vision encoder " + o.mmproj + injected, 1); // after the load: its memory is freed on return
}
#endif
if (!o.gpu) {
@@ -112,6 +129,7 @@ int own_reader_run(const OwnReaderOptions& o) {
cpu.reset(new VitRef());
if (!cpu->load(gg, geo, err)) return fail("cannot load the vision encoder " + o.mmproj + " (" + err + ")", 1);
}
if (own_reader_inject_failure("load")) return fail("cannot load the vision encoder " + o.mmproj + injected, 1);
if (threads <= 0) threads = (int) std::max(1u, std::min(16u, std::thread::hardware_concurrency() / 2));
}
std::fprintf(stderr, "strata-vision: own tower (%s, %d layers, width %d) loaded in %.1f s, %s\n", geo.projector.c_str(), geo.layers, geo.width,
@@ -132,14 +150,16 @@ int own_reader_run(const OwnReaderOptions& o) {
PreparedImage wp;
std::vector<float> rows;
int nx = 0, ny = 0;
if (prepare_image(gray, geo, o.min_tokens, o.max_tokens, wp, err) && encode(wp, rows, nx, ny, err))
err.clear();
if (own_reader_inject_failure("warmup")) err = "out of GPU memory" + injected;
if (err.empty() && prepare_image(gray, geo, o.min_tokens, o.max_tokens, wp, err) && encode(wp, rows, nx, ny, err))
std::fprintf(stderr, "strata-vision: warmed up at %d image tokens in %.1f s, %.0f MiB of GPU memory\n", nx * ny,
std::chrono::duration<double>(clock::now() - w0).count(), (double) gpu->device_bytes() / 1048576.0);
else
return fail("the vision encoder failed its warm-up (" + err + ")", 1);
}
#endif
std::printf("READY %d\n", geo.out_dim);
std::printf("READY %d own\n", geo.out_dim); // the third word names the reader (a server reads only the start: "READY")
std::fflush(stdout);
std::string line;
+15 -1
View File
@@ -1,6 +1,6 @@
// tools/vision/own/own_reader.h - strata-vision's reader loop on our own tower (`--tower own`): the same protocol as the mtmd one.
//
// prints "READY <out_dim>", then per stdin line ENC <image> <output> -> "OK <n_tokens> <nx> <ny> <ms>" or "ERR <message>"; QUIT ends.
// prints "READY <out_dim> own", then per stdin line ENC <image> <output> -> "OK <n_tokens> <nx> <ny> <ms>" or "ERR <message>"; QUIT ends.
// 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
@@ -14,8 +14,22 @@ struct OwnReaderOptions {
int threads = 0; // CPU threads (<= 0: half the hardware threads, at most 16, as the mtmd reader)
int max_tokens = 0; // the helper's --max-tokens (0: the tower's own limit); a picture over it by more than an eighth is refused
int min_tokens = 0;
// `auto` (the default): a failure before READY (no GPU, the load, the warm-up) prints nothing on stdout; own_reader_run returns kOwnStartFailed with the
// reason in *start_error, and strata-vision starts mtmd's reader in its place. Null: the failure is an "ERR ..." line, as with `--tower own`.
std::string* start_error = nullptr;
};
// The tower when no --tower is given: STRATA_VISION_TOWER's value (env, may be null) if it is mtmd, own or auto, else auto; `note` gets a line for a value that
// is none of these.
std::string vision_tower_default(const char* env, std::string& note);
// own_reader_run's return when start_error is set and our tower did not reach READY
constexpr int kOwnStartFailed = 100;
// Test hook (STRATA_VIT_TEST_FAIL=gpu|load|warmup): our tower fails at that point before READY, as if there were no GPU, the load had failed, or the warm-up had
// run out of memory, so the fallback to mtmd can be checked on a box where ours would start. True when `point` is the one named.
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)
-6
View File
@@ -224,12 +224,6 @@ static void test_round_choice() {
bool ok = true;
for (const Case& c : cs) ok = ok && vit_cpu_round_kind(c.kind, c.env) == c.want;
check(ok, "rounding default per weight type and its override", "-", 0, 0);
// `--tower auto` keeps mtmd on an MSVC x86 build of the CPU tower (vit_cpu.h); any other compiler or CPU family uses ours
#if defined(_MSC_VER) && !defined(__clang__) && (defined(_M_X64) || defined(_M_IX86))
check(vit_cpu_build_slower_than_mtmd(), "an MSVC x86 build is marked slower than mtmd", "-", 0, 0);
#else
check(!vit_cpu_build_slower_than_mtmd(), "a non-MSVC build is not marked slower than mtmd", "-", 0, 0);
#endif
}
int main(int argc, char** argv) {
+30
View File
@@ -3,6 +3,7 @@
// strata-vision-own-test [mmproj.gguf ...]
// Exit 0 when every check passes. A real mmproj given on the command line is parsed and its geometry printed; the two known Qwen
// towers (27 layers, width 1152, out 2560 or 2048) are also checked against the numbers read from their headers.
#include "own_reader.h"
#include "vit_geometry.h"
#include "vit_gpu.h"
@@ -48,6 +49,35 @@ int main(int argc, char** argv) {
}
CHECK(vit_gpu_slower_amd("gfx1151") && vit_gpu_slower_amd("gfx1151:xnack-"));
CHECK(!vit_gpu_slower_amd("gfx1150") && !vit_gpu_slower_amd("gfx1201") && !vit_gpu_slower_amd("gfx11510") && !vit_gpu_slower_amd("gfx115") && !vit_gpu_slower_amd(""));
CHECK(vit_gpu_hip_windows_not_measured(true) && !vit_gpu_hip_windows_not_measured(false)); // Windows HIP keeps mtmd under auto; Linux HIP does not
{ // the default tower (no --tower): auto, or STRATA_VISION_TOWER's value when it is a known one
std::string note;
CHECK(vision_tower_default(nullptr, note) == "auto" && note.empty());
CHECK(vision_tower_default("", note) == "auto" && note.empty());
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);
}
{ // 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";
std::string why;
o.start_error = &why;
CHECK(own_reader_run(o) == kOwnStartFailed && why.find("cannot load the vision encoder") != std::string::npos);
}
{ // the test hook names one point at a time
#ifdef _WIN32
_putenv_s("STRATA_VIT_TEST_FAIL", "load");
#else
setenv("STRATA_VIT_TEST_FAIL", "load", 1);
#endif
CHECK(own_reader_inject_failure("load") && !own_reader_inject_failure("gpu") && !own_reader_inject_failure("warmup"));
#ifdef _WIN32
_putenv_s("STRATA_VIT_TEST_FAIL", "");
#else
unsetenv("STRATA_VIT_TEST_FAIL");
#endif
CHECK(!own_reader_inject_failure("load"));
}
const std::string tmp = std::string(std::getenv("TMPDIR") ? std::getenv("TMPDIR") : ".") + "/strata_own_test_" + std::to_string((long) std::rand()) + ".gguf";
// --- 1. a file with every value kind and the four tensor types ---
-12
View File
@@ -23,18 +23,6 @@ namespace strata_vit {
// 2 F16, anything else: none). Default: on for BF16 weights, off for F16; env (STRATA_VIT_REF_ROUND's value, may be null) "0" / "1" overrides.
int vit_cpu_round_kind(int weight_kind, const char* env);
// True when this build of the CPU tower is known to be slower than mtmd's CPU reader, so `--tower auto` keeps mtmd (`--tower own` still runs ours): an x86 build
// compiled by MSVC. Measured (owner PC, Ryzen 5 7600, MSVC 19.44 /O2): 33.4 s against mtmd-CPU's 18.5 s for a 1,024-token picture (1.46 to 1.82 over the pictures),
// while the same kernels built by GCC take 0.80 to 0.95 of mtmd-CPU's time on Zen 4 (RESULTS-OWN-3V.md 12.14). Keyed on the compiler, not the OS: a GCC or clang
// build for Windows does not have the problem.
constexpr bool vit_cpu_build_slower_than_mtmd() {
#if defined(_MSC_VER) && !defined(__clang__) && (defined(_M_X64) || defined(_M_IX86))
return true;
#else
return false;
#endif
}
class VitCpu {
public:
VitCpu();
+9
View File
@@ -480,6 +480,15 @@ bool VitGpu::prefer_mtmd(bool bf16_matrices, int max_tokens, std::string& why) {
why = std::string(p.gcnArchName) + ": rocBLAS has only fallback kernels there, the own GPU tower is slower than mtmd";
return true;
}
#ifdef _WIN32
const bool windows = true;
#else
const bool windows = false;
#endif
if (vit_gpu_hip_windows_not_measured(windows)) {
why = std::string(p.gcnArchName) + " on Windows: the own HIP tower is not measured with the Windows ROCm package yet, mtmd stays";
return true;
}
return false;
#else
cudaDeviceProp p;
+4
View File
@@ -38,6 +38,10 @@ inline bool vit_gpu_slower_nvidia(int cc_major, int cc_minor, bool bf16_matrices
// RESULTS-OWN-3V.md 12.14). Windows and Linux alike until a Linux gfx1151 box is measured. `gfx` is the device's arch name (hipDeviceProp_t::gcnArchName,
// possibly with feature suffixes such as ":xnack-").
inline bool vit_gpu_slower_amd(const std::string& gfx) { return gfx.rfind("gfx1151", 0) == 0 && (gfx.size() == 7 || gfx[7] == ':'); }
// AMD on Windows, every card (coordinator 11-11, with the default flip to our tower): the Windows HIP package's rocBLAS is the one whose gfx1151 kernels are
// fallback-only, and no other card has been measured with that package, so `auto` keeps mtmd's reader on Windows HIP. Not measured on Windows HIP; remove when
// one RX 9000 / RX 7000 run on Windows is in. Linux HIP (gfx12 measured on the R9700) is not affected.
inline bool vit_gpu_hip_windows_not_measured(bool windows) { return windows; }
class VitGpu {
public:
+4 -3
View File
@@ -357,11 +357,12 @@ bool VitGpu::available(std::string& why) {
}
bool VitGpu::prefer_mtmd(bool bf16_matrices, int max_tokens, std::string& why) {
// the Metal tower has not passed its gate yet: `--tower auto` keeps mtmd's Metal reader (`--tower own --gpu` runs this tower)
// the Metal tower passed its gate on the M5 Max (RESULTS-OWN-3V.md D1, D4-D8: rule 1 and 2, 0.47 to 0.79 of mtmd-Metal's time, less memory, the 16,384-token
// picture that mtmd-Metal aborts on), so `auto` (the default) takes it on every Mac (owner 11-11: our tower the default, mtmd selectable)
(void) bf16_matrices;
(void) max_tokens;
why = "the own Metal tower is not gated yet: mtmd's Metal reader stays under auto";
return true;
(void) why;
return false;
}
bool VitGpu::load(const Gguf& gg, const VitGeometry& g, int device, int max_patches, std::string& err) {
+22 -5
View File
@@ -8,7 +8,7 @@
// [--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
// Resident: prints "READY <n_embd> <reader>" (reader: own or mtmd; older builds print two words), 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],
@@ -91,7 +91,13 @@ int main(int argc, char** argv) {
int threads = 0, max_tokens = 0, min_tokens = 0;
llama_flash_attn_type fa = LLAMA_FLASH_ATTN_TYPE_AUTO;
bool fa_given = false;
std::string tower = "mtmd"; // --tower mtmd|own|auto: whose picture reader runs (own-stack phase 3). mtmd stays the default until the gates pass.
// --tower mtmd|own|auto: whose picture reader runs (own-stack phase 3). The default is auto (owner 11-11: our tower the default, mtmd selectable): ours
// where it was measured no slower and correct, mtmd's by itself where it was measured slower (own_reader_supported). Without --tower the environment's
// STRATA_VISION_TOWER decides (the model's run config carries STRATA_VISION_TOWER=mtmd when someone picks llama.cpp's reader in the settings; an older
// strata-vision ignores it). An explicit --tower wins.
std::string tower_note;
std::string tower = strata_vit::vision_tower_default(std::getenv("STRATA_VISION_TOWER"), tower_note);
if (!tower_note.empty()) std::fprintf(stderr, "strata-vision: %s\n", tower_note.c_str());
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
auto next = [&]() -> std::string {
@@ -128,10 +134,13 @@ int main(int argc, char** argv) {
}
else { std::fprintf(stderr, "unknown argument %s\n", a.c_str()); return 2; }
}
// auto: our tower where it can read this file on this backend, else mtmd
// auto: our tower where it can read this file on this backend, else mtmd; when ours then fails before READY, mtmd's reader starts in its place
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";
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());
}
if (mmproj.empty() || (model.empty() && tower != "own")) { // our tower needs no text model: it never tokenizes
std::fprintf(stderr, "usage: strata-vision --mmproj <mmproj.gguf> --model <model.gguf> [--gpu] [--threads N] "
@@ -156,7 +165,14 @@ int main(int argc, char** argv) {
o.threads = threads;
o.max_tokens = max_tokens;
o.min_tokens = min_tokens;
return strata_vit::own_reader_run(o);
std::string start_error;
if (own_may_fall_back) o.start_error = &start_error;
const int rc = strata_vit::own_reader_run(o);
if (rc != strata_vit::kOwnStartFailed) return rc;
// nothing was written to stdout yet: the server still waits for READY, which mtmd's reader below gives (with "mtmd" as its third word)
std::fprintf(stderr, "strata-vision: Strata's picture reader did not start (%s); reading pictures with llama.cpp's reader\n", start_error.c_str());
std::fflush(stderr);
own_fell_back = true;
}
llama_log_set(quiet_log, nullptr);
mtmd_helper_log_set(quiet_log, nullptr);
@@ -300,7 +316,8 @@ int main(int argc, char** argv) {
mtmd_input_chunks_free(chunks);
if (bm) mtmd_bitmap_free(bm);
}
std::printf("READY %d\n", n_embd);
// the third word names the reader; a fourth, "fallback", says our tower failed before READY (a server reads only the start: "READY")
std::printf("READY %d mtmd%s\n", n_embd, own_fell_back ? " fallback" : "");
std::fflush(stdout);
std::string line;