diff --git a/tools/vision/own/own_reader.cpp b/tools/vision/own/own_reader.cpp index b8c621f750..2deb8361e5 100644 --- a/tools/vision/own/own_reader.cpp +++ b/tools/vision/own/own_reader.cpp @@ -34,7 +34,7 @@ bool parse_enc(const std::string& line, std::string& img, std::string& out) { } // namespace -bool own_reader_supported(const std::string& mmproj, bool gpu, std::string& why) { +bool own_reader_supported(const std::string& mmproj, bool gpu, int max_tokens, std::string& why) { if (gpu) { #ifdef STRATA_VIT_GPU bool bf16 = false; // the layer matrices' type decides on Pascal (vit_gpu.h) @@ -44,7 +44,7 @@ bool own_reader_supported(const std::string& mmproj, bool gpu, std::string& why) if (g0.open(mmproj, e0)) if (const GgufTensorInfo* t = g0.tensor("v.blk.0.attn_qkv.weight")) bf16 = t->type == GT_BF16; } - if (!VitGpu::available(why) || VitGpu::prefer_mtmd(bf16, why)) return false; + if (!VitGpu::available(why) || VitGpu::prefer_mtmd(bf16, max_tokens, why)) return false; #else why = "this build has no GPU backend for the own tower"; return false; diff --git a/tools/vision/own/own_reader.h b/tools/vision/own/own_reader.h index 3d01c36583..6f3d2ca28f 100644 --- a/tools/vision/own/own_reader.h +++ b/tools/vision/own/own_reader.h @@ -17,7 +17,7 @@ struct OwnReaderOptions { }; // 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, std::string& why); +bool own_reader_supported(const std::string& mmproj, bool gpu, int max_tokens, std::string& why); // max_tokens: --max-tokens (0: not given) // 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); diff --git a/tools/vision/own/test_gguf_geometry.cpp b/tools/vision/own/test_gguf_geometry.cpp index 91289ae6d0..aaf0588387 100644 --- a/tools/vision/own/test_gguf_geometry.cpp +++ b/tools/vision/own/test_gguf_geometry.cpp @@ -37,9 +37,15 @@ static uint16_t f32_to_f16(float f) { // good enough for the values used here int main(int argc, char** argv) { // --- 0. the GPU classes where `--tower auto` keeps mtmd (vit_gpu.h) --- - CHECK(vit_gpu_slower_nvidia(6, 0, false) && vit_gpu_slower_nvidia(6, 1, false) && vit_gpu_slower_nvidia(6, 1, true) && vit_gpu_slower_nvidia(6, 2, true)); - CHECK(!vit_gpu_slower_nvidia(6, 0, true)); - CHECK(!vit_gpu_slower_nvidia(5, 2, false) && !vit_gpu_slower_nvidia(7, 0, false) && !vit_gpu_slower_nvidia(8, 6, true) && !vit_gpu_slower_nvidia(12, 0, false)); + for (int mt : {0, 300, 1024}) { // at or below the default limit: the speed rule (Pascal keeps mtmd, except cc 6.0 with BF16 matrices) + CHECK(vit_gpu_slower_nvidia(6, 0, false, mt) && vit_gpu_slower_nvidia(6, 1, false, mt) && vit_gpu_slower_nvidia(6, 1, true, mt) && vit_gpu_slower_nvidia(6, 2, true, mt)); + CHECK(!vit_gpu_slower_nvidia(6, 0, true, mt)); + CHECK(!vit_gpu_slower_nvidia(5, 2, false, mt) && !vit_gpu_slower_nvidia(7, 0, false, mt) && !vit_gpu_slower_nvidia(8, 6, true, mt) && !vit_gpu_slower_nvidia(12, 0, false, mt)); + } + for (int mt : {1025, 4096, 16384}) { // above it: ours on every CUDA class + CHECK(!vit_gpu_slower_nvidia(6, 0, false, mt) && !vit_gpu_slower_nvidia(6, 1, false, mt) && !vit_gpu_slower_nvidia(6, 1, true, mt) && !vit_gpu_slower_nvidia(6, 2, true, mt)); + CHECK(!vit_gpu_slower_nvidia(6, 0, true, mt) && !vit_gpu_slower_nvidia(8, 6, true, mt) && !vit_gpu_slower_nvidia(12, 0, false, mt)); + } 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("")); const std::string tmp = std::string(std::getenv("TMPDIR") ? std::getenv("TMPDIR") : ".") + "/strata_own_test_" + std::to_string((long) std::rand()) + ".gguf"; diff --git a/tools/vision/own/vit_gpu.cu b/tools/vision/own/vit_gpu.cu index 1c0a11f97a..18c93c4ab5 100644 --- a/tools/vision/own/vit_gpu.cu +++ b/tools/vision/own/vit_gpu.cu @@ -470,10 +470,11 @@ bool VitGpu::available(std::string& why) { return true; } -bool VitGpu::prefer_mtmd(bool bf16_matrices, std::string& why) { +bool VitGpu::prefer_mtmd(bool bf16_matrices, int max_tokens, std::string& why) { #if defined(STRATA_VIT_HIP) hipDeviceProp_t p; (void) bf16_matrices; + (void) max_tokens; if (hipGetDeviceProperties(&p, 0) != hipSuccess) { (void) hipGetLastError(); return false; } if (vit_gpu_slower_amd(p.gcnArchName)) { why = std::string(p.gcnArchName) + ": rocBLAS has only fallback kernels there, the own GPU tower is slower than mtmd"; @@ -483,7 +484,7 @@ bool VitGpu::prefer_mtmd(bool bf16_matrices, std::string& why) { #else cudaDeviceProp p; if (cudaGetDeviceProperties(&p, 0) != cudaSuccess) { (void) cudaGetLastError(); return false; } - if (vit_gpu_slower_nvidia(p.major, p.minor, bf16_matrices)) { + if (vit_gpu_slower_nvidia(p.major, p.minor, bf16_matrices, max_tokens)) { why = "compute capability " + std::to_string(p.major) + "." + std::to_string(p.minor) + " (Pascal)" + (bf16_matrices ? "" : " with F16 matrices") + ": the own GPU tower is slower than mtmd there"; return true; diff --git a/tools/vision/own/vit_gpu.h b/tools/vision/own/vit_gpu.h index 51ca7f57e2..96cfd55938 100644 --- a/tools/vision/own/vit_gpu.h +++ b/tools/vision/own/vit_gpu.h @@ -25,7 +25,14 @@ struct VitGpuImpl; // F16-accumulating variant of ours was as fast but missed rule 1: 0.9972), so with F16 matrices (Qwen3.6) ours stays slower (P100: at best 1.10 of mtmd-GPU, // RESULTS-OWN-3V.md 16.1). With BF16 matrices (Qwen3.8) mtmd multiplies in float32 too, and ours (BF16 held as F16, the larger score chunk) passes the whole // gate on a P100 (cc 6.0: 0.31 to 0.99 of mtmd-GPU's time, 16.1). Other Pascal chips (6.1, 6.2: GTX 10xx, Jetson TX2) are not measured: mtmd stays there. -inline bool vit_gpu_slower_nvidia(int cc_major, int cc_minor, bool bf16_matrices) { return cc_major == 6 && !(cc_minor == 0 && bf16_matrices); } +// Above the default GPU limit of 1,024 image tokens the speed rule gives way to correctness: mtmd's GPU reader drifts from mtmd's CPU path as pictures +// grow (global cosine 0.983 at 2,116 tokens, 0.81 to 0.83 at 16,384 on the A4000, RTX PRO 5000 and P100), ours stays at 0.9996 to 0.99998 (RESULTS-OWN-3V.md +// 16.7), so a reader started with --max-tokens above 1,024 takes our tower on every CUDA class, however slow (P100 at 16,384 tokens: 196.6 s against 129.0 s). +// max_tokens 0 (not given) counts as the default. AMD gfx1151 keeps its rule (no large-picture data there yet). +constexpr int kVitGpuDefaultLimit = 1024; +inline bool vit_gpu_slower_nvidia(int cc_major, int cc_minor, bool bf16_matrices, int max_tokens) { + return max_tokens <= kVitGpuDefaultLimit && cc_major == 6 && !(cc_minor == 0 && bf16_matrices); +} // AMD gfx1151 (Strix Halo, Radeon 8060S): the ROCm package's rocBLAS ships only its generic "fallback" Tensile kernels for gfx1151, so every hipBLAS product runs // untuned (Windows, Strix: 1.73 to 3.03 of mtmd-HIP's time; the weight products alone 730 ms of a 1,024-token picture against mtmd-HIP's 690 ms for all of it; // 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, @@ -45,7 +52,7 @@ public: // True on a GPU class where this tower is known to be slower than mtmd's GPU reader, so `--tower auto` keeps mtmd there (`--tower own` still runs // this tower): NVIDIA Pascal (compute capability 6.x: no tensor cores; mtmd's F16 products accumulate in F16 at twice the float32 rate, ours in float32; // measured on a P100, RESULTS-OWN-3V.md 12.1). `why` says which. - static bool prefer_mtmd(bool bf16_matrices, std::string& why); // bf16_matrices: the mmproj's layer matrices are BF16 + static bool prefer_mtmd(bool bf16_matrices, int max_tokens, std::string& why); // bf16_matrices: the mmproj's layer matrices are BF16; max_tokens: --max-tokens (0: not given) // Uploads the weights to `device` and sizes the work buffers for pictures of up to `max_patches` patches (grown by encode when a picture // needs more). `g` must be supported(). diff --git a/tools/vision/own/vit_sycl.cpp b/tools/vision/own/vit_sycl.cpp index 21a84d2237..69b1f0ba76 100644 --- a/tools/vision/own/vit_sycl.cpp +++ b/tools/vision/own/vit_sycl.cpp @@ -414,8 +414,9 @@ bool VitGpu::available(std::string& why) { return pick_gpu(d, why); } -bool VitGpu::prefer_mtmd(bool bf16_matrices, std::string& why) { +bool VitGpu::prefer_mtmd(bool bf16_matrices, int max_tokens, std::string& why) { (void) bf16_matrices; + (void) max_tokens; (void) why; return false; } diff --git a/tools/vision/strata_vision.cpp b/tools/vision/strata_vision.cpp index 1454be2f02..7dcd72d52e 100644 --- a/tools/vision/strata_vision.cpp +++ b/tools/vision/strata_vision.cpp @@ -131,7 +131,7 @@ int main(int argc, char** argv) { // auto: our tower where it can read this file on this backend, else mtmd if (tower == "auto") { std::string why; - tower = !mmproj.empty() && strata_vit::own_reader_supported(mmproj, gpu, why) ? "own" : "mtmd"; + tower = !mmproj.empty() && strata_vit::own_reader_supported(mmproj, gpu, max_tokens, why) ? "own" : "mtmd"; } if (mmproj.empty() || (model.empty() && tower != "own")) { // our tower needs no text model: it never tokenizes std::fprintf(stderr, "usage: strata-vision --mmproj --model [--gpu] [--threads N] "