From 9726c926d6bf820fd337abdb7b2a4c8e5138f68d Mon Sep 17 00:00:00 2001 From: lukmanfauzie <119554523+lukmanfauzie@users.noreply.github.com> Date: Wed, 30 Sep 2026 20:21:17 +0800 Subject: [PATCH] gemm: name the cuBLAS/CUDA call that failed at init, and flag resource shortages `Gemm::init` collapsed five distinct failures into three bare strings ("cublasCreate failed", "workspace", "dequant scratch of N MiB"), so an init failure on a tight machine could not be told apart from a driver or install problem without a debugger. Each call is now checked through two small helpers that report the operation and the real status - `cuBLAS status N` or `cudaGetErrorString` - and optionally set `resource_failure` when the failure means the machine is out of room (`CUBLAS_STATUS_ALLOC_FAILED`, `cudaErrorMemoryAllocation`). A caller can then retry with a smaller cache or prompt instead of treating a shortage as fatal. The parameter is defaulted to nullptr, so every existing caller is source-compatible; the upstream HIP/hipblaslt block is untouched. --- include/strata/prefill/gemm.hpp | 5 ++++- src/prefill/gemm.cu | 35 +++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/include/strata/prefill/gemm.hpp b/include/strata/prefill/gemm.hpp index c9ddc6f4..f5fc7172 100644 --- a/include/strata/prefill/gemm.hpp +++ b/include/strata/prefill/gemm.hpp @@ -20,7 +20,10 @@ public: Gemm& operator=(const Gemm&) = delete; /// `scratch_elems`: BF16 elements of the dequantization scratch (the largest weight dequantized at once). - bool init(void* stream, int64_t scratch_elems, std::string& err); + /// `resource_failure`, when given, is set to true only for a failure that means the machine ran out of room + /// (a cuBLAS allocation or a `cudaMalloc`), so a caller can retry with a smaller cache instead of treating + /// every init failure as fatal. It is always set (false on success or a non-resource error). + bool init(void* stream, int64_t scratch_elems, std::string& err, bool* resource_failure = nullptr); /// The same with caller-owned device buffers (the prompt path borrowing expert-cache slots). bool init_external(void* stream, uint16_t* scratch, int64_t scratch_elems, void* workspace, size_t ws_bytes, std::string& err); diff --git a/src/prefill/gemm.cu b/src/prefill/gemm.cu index 4742d423..a0fa8b84 100644 --- a/src/prefill/gemm.cu +++ b/src/prefill/gemm.cu @@ -315,24 +315,39 @@ void Gemm::rebind(uint16_t* scratch, int64_t scratch_elems, void* workspace, siz #endif } -bool Gemm::init(void* stream, int64_t scratch_elems, std::string& err) { +bool Gemm::init(void* stream, int64_t scratch_elems, std::string& err, bool* resource_failure) { + if (resource_failure) *resource_failure = false; + // Every failure below is reported with the call that produced it and, for the two that mean "the machine is + // out of room", with `resource_failure` set so a caller can tell "no VRAM" from "a broken install" without + // parsing the message. The distinction matters on a low-RAM box: a shortage is worth retrying with a smaller + // cache or prompt, while an unsupported math mode never is. + auto blas = [&](cublasStatus_t s, const char* op) { + if (s == CUBLAS_STATUS_SUCCESS) return true; + if (resource_failure) *resource_failure = s == CUBLAS_STATUS_ALLOC_FAILED; + err = std::string("prefill gemm: ") + op + ": cuBLAS status " + std::to_string((int) s); + return false; + }; + auto alloc = [&](cudaError_t s, const char* op) { + if (s == cudaSuccess) return true; + if (resource_failure) *resource_failure = s == cudaErrorMemoryAllocation; + err = std::string("prefill gemm: ") + op + ": " + cudaGetErrorString(s); + return false; + }; cublasHandle_t h = nullptr; - if (cublasCreate(&h) != CUBLAS_STATUS_SUCCESS) { err = "prefill gemm: cublasCreate failed"; return false; } + if (!blas(cublasCreate(&h), "create")) return false; handle_ = h; stream_ = stream; - cublasSetStream(h, (cudaStream_t) stream); + if (!blas(cublasSetStream(h, (cudaStream_t) stream), "stream")) return false; // A fixed workspace so the handle never allocates on the way (and graphs could capture it later). const size_t ws = 32u << 20; - if (cudaMalloc(&workspace_, ws) != cudaSuccess) { err = "prefill gemm: workspace"; return false; } - cublasSetWorkspace(h, workspace_, ws); - cublasSetMathMode(h, CUBLAS_DEFAULT_MATH); + if (!alloc(cudaMalloc(&workspace_, ws), "workspace")) return false; + if (!blas(cublasSetWorkspace(h, workspace_, ws), "workspace config") || + !blas(cublasSetMathMode(h, CUBLAS_DEFAULT_MATH), "math mode")) return false; #if defined(__HIPCC__) && defined(STRATA_HIPBLASLT_AVAILABLE) hipblaslt_state_ = create_hipblaslt_state(workspace_, ws).release(); #endif - if (scratch_elems > 0 && cudaMalloc((void**) &scratch_, (size_t) scratch_elems * 2) != cudaSuccess) { - err = "prefill gemm: dequant scratch of " + std::to_string(scratch_elems * 2 >> 20) + " MiB"; - return false; - } + if (scratch_elems > 0 && !alloc(cudaMalloc((void**) &scratch_, (size_t) scratch_elems * 2), + "dequant scratch")) return false; scratch_elems_ = scratch_elems; return true; }