From 60f60d331bc4812e0d06acb731e81837e44ded2e Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Sat, 3 Oct 2026 08:29:12 +0200 Subject: [PATCH] Guard KAR CUDA smoke against shared GPU memory pressure --- gpu/kar/README.md | 2 ++ gpu/kar/src/kar_gpu_smoke.cpp | 36 ++++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/gpu/kar/README.md b/gpu/kar/README.md index 17b566b9..c2ea3bb1 100644 --- a/gpu/kar/README.md +++ b/gpu/kar/README.md @@ -28,4 +28,6 @@ provenance and is not a serving loader. The smoke program validates the KAR container and payload CRCs, reserves a chosen tensor's compressed level prefix in VRAM, decodes it on a CUDA stream, and reports a warm kernel median over the requested iterations. Compare `output_fnv1a64` with the native CPU command above using the selected archive, tensor, and level. This is a **kernel smoke measurement**, not an end-to-end inference comparison or a model quality result. It does not yet link the decoder into Strata's serving path. Use the original model digest, archive digest, driver/CUDA versions, GPU, tensor, cache state, and iteration count when recording results. +Before any CUDA allocation, the smoke program checks the selected prefix, F32 output, and optional row ID against currently free VRAM. It leaves at least 512 MiB or one sixteenth of total VRAM free, whichever is larger, and refuses the run if that reserve would be crossed. This admission check protects shared GPU experiments; another process can still allocate memory afterward, so CUDA allocation errors remain possible. It is not an inference scheduler. + `--row ROW_ID` selects the CUDA row-gather decoder for one row of a compressed record. The row ID is local to the selected KAR part, so `--row 0` on a split PLE part addresses that part's first row. This mode stages the same complete compressed level prefix with `kar_reserve`, then allocates and writes only one F32 row. It reports `mode=row` and the one-row output checksum. Compare a real-model result with `model checksum-kar-row` on the selected archive and part name; that CPU command verifies the complete archive before bounded range decoding. The one-row synthetic fixture above has the same checksum as its full-tensor decode. Row mode does not reduce the initial PCIe transfer of that part and does not make the full model fit in VRAM. diff --git a/gpu/kar/src/kar_gpu_smoke.cpp b/gpu/kar/src/kar_gpu_smoke.cpp index 0e6caf33..c998d623 100644 --- a/gpu/kar/src/kar_gpu_smoke.cpp +++ b/gpu/kar/src/kar_gpu_smoke.cpp @@ -143,8 +143,15 @@ int main(int argc, char** argv) { std::cerr << "smoke tensor must contain 1..100 million elements\n"; return 1; } - if (row_mode && (tensor->storage_class == KAR_RAW || !tensor->row_words || - row_id >= tensor->row_count)) { + if (tensor->storage_class == KAR_RAW) { + std::cerr << "smoke requires a compressed KAR record\n"; + return 1; + } + if (level < tensor->planes_min || level > tensor->planes_max) { + std::cerr << "level outside the selected record's declared dial\n"; + return 1; + } + if (row_mode && (!tensor->row_words || row_id >= tensor->row_count)) { std::cerr << "row mode requires a compressed record with an addressable row ID\n"; return 1; } @@ -155,6 +162,28 @@ int main(int argc, char** argv) { !cuda_ok(cudaRuntimeGetVersion(&runtime), "cudaRuntimeGetVersion")) { return 1; } + const size_t output_elements = + row_mode ? tensor->row_len : static_cast(tensor->n_elements); + const size_t output_bytes = output_elements * sizeof(float); + const kar_cost cost = row_mode + ? kar_decode_rows_cost(archive, static_cast(index), level, 1) + : kar_decode_cost(archive, static_cast(index), level); + if (cost.resident_bytes == 0 || cost.output_bytes != output_bytes || + cost.resident_bytes > std::numeric_limits::max() - cost.output_bytes - + (row_mode ? sizeof(row_id) : 0)) { + std::cerr << "selected KAR level has invalid GPU memory geometry\n"; + return 1; + } + const uint64_t required_bytes = cost.resident_bytes + cost.output_bytes + + (row_mode ? sizeof(row_id) : 0); + size_t free_bytes = 0, total_bytes = 0; + if (!cuda_ok(cudaMemGetInfo(&free_bytes, &total_bytes), "cudaMemGetInfo")) return 1; + const uint64_t headroom = std::max(512ull << 20, total_bytes / 16); + if (free_bytes <= headroom || required_bytes > free_bytes - headroom) { + std::cerr << "insufficient GPU memory for isolated KAR smoke: required=" << required_bytes + << " free=" << free_bytes << " reserved_headroom=" << headroom << '\n'; + return 1; + } cudaStream_t stream = nullptr; cudaEvent_t start = nullptr, stop = nullptr; float* output = nullptr; @@ -164,9 +193,6 @@ int main(int argc, char** argv) { if (!cuda_ok(cudaStreamCreate(&stream), "cudaStreamCreate") || !cuda_ok(cudaEventCreate(&start), "cudaEventCreate") || !cuda_ok(cudaEventCreate(&stop), "cudaEventCreate")) break; - const size_t output_elements = - row_mode ? tensor->row_len : static_cast(tensor->n_elements); - const size_t output_bytes = output_elements * sizeof(float); if (!cuda_ok(cudaMalloc(&output, output_bytes), "cudaMalloc")) break; if (row_mode && (!cuda_ok(cudaMalloc(&device_row_id, sizeof(row_id)), "cudaMalloc row ID") ||