From a606e3a9668c5e32a93351747fa2caa9495a3a06 Mon Sep 17 00:00:00 2001 From: sergiywith Date: Wed, 30 Sep 2026 22:25:39 +0300 Subject: [PATCH] Start: name the GPU and stop at once when the build has no code for it; STRATA_EMULATE_CC for tests - generate.cpp: before the expert arena loads, the engine names the card and its compute capability and checks that the build has code for it (device_code_error: cudaFuncGetAttributes on a kernel). A binary built for other GPUs used to fail at the first kernel, after the whole arena had loaded; now it stops in ~0.3 s and says why. - include/strata/core/emulate.hpp: STRATA_EMULATE_CC=75|80|86|89 makes the host's device queries (compute capability, shared memory per block) answer as that generation, for a PTX build of it JIT-compiled on a newer card - ggml's MMQ config, the QSA kernels' choice, fused_gr's shared memory. Tests only. - STRATA_QSA_WARP=1|select|attn: the pre-sm_80 QSA kernels on any card (A/B). --- include/strata/core/device.hpp | 4 ++++ include/strata/core/emulate.hpp | 36 +++++++++++++++++++++++++++++ src/core/device.cu | 9 ++++++++ src/kernels/cuda/fused_gr.cu | 2 ++ src/kernels/cuda/qsa.cu | 2 ++ src/kernels/cuda/qsa_prompt_attn.cu | 7 +++++- src/kernels/cuda/qsa_select.cu | 7 +++++- src/prefill/ggml_cuda_host.cu | 5 ++-- src/program/generate.cpp | 17 ++++++++++++++ 9 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 include/strata/core/emulate.hpp diff --git a/include/strata/core/device.hpp b/include/strata/core/device.hpp index 8fb93ad6..2c180263 100644 --- a/include/strata/core/device.hpp +++ b/include/strata/core/device.hpp @@ -43,6 +43,10 @@ const char* compiled_gpu_archs(); // this is the matching check at run time (a binary can be carried to a different machine). DeviceInfo device_info(int ordinal = 0); +/// "" when this build has device code for the current device, else CUDA's error: a build for other GPUs would +/// otherwise fail at its first kernel launch, with nothing that names the cause. +std::string device_code_error(); + class CudaError : public std::runtime_error { public: CudaError(const std::string& what, int code) : std::runtime_error(what), code_(code) {} diff --git a/include/strata/core/emulate.hpp b/include/strata/core/emulate.hpp new file mode 100644 index 00000000..6f28b97f --- /dev/null +++ b/include/strata/core/emulate.hpp @@ -0,0 +1,36 @@ +// strata/core/emulate.hpp - STRATA_EMULATE_CC: another GPU generation's code paths, run on this card (tests only). +// +// A build whose device code is PTX for an older architecture (CMAKE_CUDA_ARCHITECTURES=86-virtual) runs on a newer +// card through the driver's JIT, and every kernel then sees __CUDA_ARCH__ = 860. What the host decides from the +// device must agree with that, or it picks paths and shared-memory sizes the device code was not built for: +// STRATA_EMULATE_CC=75|80|86|89 makes each such query (compute capability, shared memory per block) answer as that +// card would. Speed under it says nothing about the real card; results do - the kernels are the real card's. +#pragma once + +#include + +namespace strata { + +/// the emulated compute capability as 10 * major + minor, or 0 (off) +inline int emulated_cc() { + static const int cc = [] { + const char* e = std::getenv("STRATA_EMULATE_CC"); + const int v = e ? std::atoi(e) : 0; + return v >= 75 && v < 120 ? v : 0; + }(); + return cc; +} +inline int cc_major_of(int real_major) { return emulated_cc() ? emulated_cc() / 10 : real_major; } +inline int cc_minor_of(int real_minor) { return emulated_cc() ? emulated_cc() % 10 : real_minor; } +/// cudaDevAttrMaxSharedMemoryPerBlockOptin as the emulated card reports it: 64 KB on Turing, 163 KB on the A100, +/// 99 KB on the other Ampere and Ada cards +inline int smem_optin_of(int real) { + switch (emulated_cc()) { + case 0: return real; + case 75: return 65536; + case 80: return 166912; + default: return real < 101376 ? real : 101376; + } +} + +} // namespace strata diff --git a/src/core/device.cu b/src/core/device.cu index 010aee0f..4d8e350e 100644 --- a/src/core/device.cu +++ b/src/core/device.cu @@ -92,6 +92,15 @@ std::string gpu_arch_problem(int ordinal) { #endif } +std::string device_code_error() { + // every .cu of the engine is compiled for the same CMAKE_CUDA_ARCHITECTURES, so this kernel stands for all + cudaFuncAttributes a{}; + const cudaError_t e = cudaFuncGetAttributes(&a, poison_kernel); + if (e == cudaSuccess) return {}; + cudaGetLastError(); + return cudaGetErrorString(e); +} + DeviceInfo device_info(int ordinal) { int count = 0; check(cudaGetDeviceCount(&count), "cudaGetDeviceCount"); diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu index c022beca..7b346b09 100644 --- a/src/kernels/cuda/fused_gr.cu +++ b/src/kernels/cuda/fused_gr.cu @@ -1,4 +1,5 @@ // src/kernels/cuda/fused_gr.cu - see include/strata/kernels/fused_gr.hpp. +#include "strata/core/emulate.hpp" #include "strata/kernels/fused_gr.hpp" #include "strata/kernels/bf16_bits.hpp" #include "strata/kernels/verify_kernels.hpp" @@ -563,6 +564,7 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi if (dev >= 0 && dev < 64 && !attr[dev]) { int optin = 0; cudaDeviceGetAttribute(&optin, cudaDevAttrMaxSharedMemoryPerBlockOptin, dev); + optin = strata::smem_optin_of(optin); // the down kernel stages n_tok*TILEV floats of dynamic shared memory - 80 KB at the full 8 tokens of the // CUDA tile. sm_75 gets the smaller tile: all eight tokens fit one 40 KiB launch there (no more slicing), // the TQ-5 prefetch holds half the registers, and the smaller blocks raise how many of the 41-block grid diff --git a/src/kernels/cuda/qsa.cu b/src/kernels/cuda/qsa.cu index d1338969..a635e81e 100644 --- a/src/kernels/cuda/qsa.cu +++ b/src/kernels/cuda/qsa.cu @@ -40,6 +40,7 @@ // Every comparison still passed at any sane tolerance, the spare key landing 0.4 f32 ulp away; only a // BIT-EXACT assertion plus a full-precision probe of the intermediate found it. That is the strongest // argument in this file for keeping both. +#include "strata/core/emulate.hpp" #include "strata/kernels/qsa.hpp" #include "strata/kernels/f16_bits.hpp" @@ -673,6 +674,7 @@ void qsa_attend_step(const float* q, const uint16_t* k_scratch, const uint16_t* int dev = 0, max_shared = 0; cudaGetDevice(&dev); cudaDeviceGetAttribute(&max_shared, cudaDevAttrMaxSharedMemoryPerBlockOptin, dev); + max_shared = strata::smem_optin_of(max_shared); if ((int) smem > max_shared) { std::fprintf(stderr, "qsa: qsa_attend: max_ids %lld needs %zu B of shared, over the %d B limit\n", (long long) max_ids, smem, max_shared); diff --git a/src/kernels/cuda/qsa_prompt_attn.cu b/src/kernels/cuda/qsa_prompt_attn.cu index e87ec02c..482713ed 100644 --- a/src/kernels/cuda/qsa_prompt_attn.cu +++ b/src/kernels/cuda/qsa_prompt_attn.cu @@ -1,4 +1,7 @@ // src/kernels/cuda/qsa_prompt_attn.cu - see include/strata/kernels/qsa_prompt_attn.hpp. +#include "strata/core/emulate.hpp" +#include +#include #include "strata/kernels/qsa_prompt_attn.hpp" #include "strata/kernels/kv_q8.hpp" #include "strata/kernels/kv_q4.hpp" @@ -703,7 +706,9 @@ bool qsa_prompt_attn_batch(const float* q, const QsaAttnPools& pools, const int3 cudaGetLastError(); return false; } - cc_major[dev] = major; + // STRATA_QSA_WARP=1|attn (an A/B arm): the pre-sm_80 kernels on any card, as RTX 20 runs them + const char* w = std::getenv("STRATA_QSA_WARP"); + cc_major[dev] = w && (!std::strcmp(w, "1") || !std::strcmp(w, "attn")) ? 7 : strata::cc_major_of(major); } if (cc_major[dev] < 7) return false; turing = cc_major[dev] < 8; diff --git a/src/kernels/cuda/qsa_select.cu b/src/kernels/cuda/qsa_select.cu index dd3ba537..a780fc87 100644 --- a/src/kernels/cuda/qsa_select.cu +++ b/src/kernels/cuda/qsa_select.cu @@ -1,4 +1,7 @@ // src/kernels/cuda/qsa_select.cu - see include/strata/kernels/qsa_select.hpp. +#include "strata/core/emulate.hpp" +#include +#include #include "strata/kernels/qsa_select.hpp" #include @@ -517,7 +520,9 @@ bool qsa_block_scores_tc(const float* pooled, const float* dead, const float* q_ cudaGetLastError(); return false; } - cc_major[dev] = major; + // STRATA_QSA_WARP=1|select (an A/B arm): the pre-sm_80 kernels on any card, as RTX 20 runs them + const char* w = std::getenv("STRATA_QSA_WARP"); + cc_major[dev] = w && (!std::strcmp(w, "1") || !std::strcmp(w, "select")) ? 7 : strata::cc_major_of(major); } if (cc_major[dev] < 8) return false; } diff --git a/src/prefill/ggml_cuda_host.cu b/src/prefill/ggml_cuda_host.cu index 12f4c8af..c8f8284b 100644 --- a/src/prefill/ggml_cuda_host.cu +++ b/src/prefill/ggml_cuda_host.cu @@ -1,5 +1,6 @@ // src/prefill/ggml_cuda_host.cu - prompt-speed plan step 2b: the host-side symbols of llama.cpp's ggml-cuda that its MMQ // and quantize code reference, for the MMQ kernels compiled into strata_mmq without the rest of ggml-cuda.cu. +#include "strata/core/emulate.hpp" #include "common.cuh" #include @@ -80,8 +81,8 @@ const ggml_cuda_device_info & ggml_cuda_info() { d.integrated = false; d.supports_cooperative_launch = false; #else - d.cc = 100 * prop.major + 10 * prop.minor; - d.smpbo = prop.sharedMemPerBlockOptin; + d.cc = 100 * strata::cc_major_of(prop.major) + 10 * strata::cc_minor_of(prop.minor); // STRATA_EMULATE_CC + d.smpbo = strata::smem_optin_of((int) prop.sharedMemPerBlockOptin); d.integrated = prop.integrated != 0; d.supports_cooperative_launch = prop.cooperativeLaunch != 0; #endif diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 58236eda..5c127d84 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -55,6 +55,8 @@ #include "strata/spec/suffix_drafter.hpp" #include "strata/kernels/cvec.hpp" #include "strata/core/progress.hpp" +#include "strata/core/device.hpp" +#include "strata/core/emulate.hpp" #ifndef NOMINMAX #define NOMINMAX // gguf_reader.hpp includes windows.h #endif @@ -2391,6 +2393,21 @@ int main(int argc, char** argv) { // CUDA error left set by the failed `cudaHostRegister` and read later by `gr_read`'s launch check. See the // note in `pinned.cu`. strata::core::FileExpertSource src; + { // the card, and whether this build has code for it (a binary built for other GPUs fails at its first kernel + // otherwise, after the whole expert arena has loaded) - before the arena starts loading + int dev = 0; + cudaDeviceProp p{}; + if (cudaGetDevice(&dev) == cudaSuccess && cudaGetDeviceProperties(&p, dev) == cudaSuccess) + std::fprintf(stderr, "strata generate: GPU %d: %s, compute capability %d.%d%s\n", dev, p.name, + strata::cc_major_of(p.major), strata::cc_minor_of(p.minor), + strata::emulated_cc() ? " (STRATA_EMULATE_CC: a test mode, the card is emulated)" : ""); + const std::string e = strata::core::device_code_error(); + if (!e.empty()) { + std::fprintf(stderr, "strata generate: this engine has no code for %s (sm_%d%d): %s - rebuild it for this " + "card (setup does: START-HERE.bat --setup)\n", p.name, p.major, p.minor, e.c_str()); + return 1; + } + } strata::core::ArenaExpertSource arena_src; strata::core::ExpertSource* srcp = nullptr; if (o.mmap_experts) {