Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

gemm: name the cuBLAS/CUDA call that failed at init, and flag resource shortages

lukmanfauzie9726c926d6bf
diff --git a/include/strata/prefill/gemm.hpp b/include/strata/prefill/gemm.hpp
index c9ddc6f4d..f5fc71723 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 4742d423b..a0fa8b843 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;
}