From 8d588c2a1be0fe8355dbbef42bbc542a2dc5ee55 Mon Sep 17 00:00:00 2001 From: Gautam <13942232+GautamtmD@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:49:46 +0530 Subject: [PATCH] fix(inkling cuda): ink_cuda_mem_info reads the GPU it is asked about cudaMemGetInfo reports the current device. ink_cuda_mem_info took a device argument but ignored it, so on a layer split the later stages' expert budgets (and the per-stage checks of elastic global KV growth) were sized from the first GPU's free VRAM. When the first GPU had less room, experts of a later GPU went to host RAM, where they are skipped, and the output was wrong. Switch to the requested device for the query and switch back. Callers that pass no device, or the current one, are unchanged. Co-Authored-By: Claude Opus 5.5 --- engine/src/inkling2/cuda/ink_cuda.cuh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/engine/src/inkling2/cuda/ink_cuda.cuh b/engine/src/inkling2/cuda/ink_cuda.cuh index cd13a9a9ce2..05056dfd97f 100644 --- a/engine/src/inkling2/cuda/ink_cuda.cuh +++ b/engine/src/inkling2/cuda/ink_cuda.cuh @@ -96,7 +96,16 @@ inline strata::inkling2::ArchProfile current_arch_profile(int dev = 0) { // cudaMemGetInfo subtracts reclaimable OS page cache (like the 19 GB .strata file just read!), // so take max(cuda_free, /proc/meminfo MemAvailable) to prevent false hybrid spill! inline cudaError_t ink_cuda_mem_info(size_t* vfree, size_t* vtot, int dev = -1) { + // cudaMemGetInfo reports the CURRENT device: query `dev` when given (multi-GPU callers pass a stage's GPU + // without switching to it first), then restore the caller's device. + int prev = -1; + if (dev >= 0 && cudaGetDevice(&prev) == cudaSuccess && prev != dev) { + if (cudaSetDevice(dev) != cudaSuccess) prev = -1; + } else { + prev = -1; + } cudaError_t ce = cudaMemGetInfo(vfree, vtot); + if (prev >= 0) cudaSetDevice(prev); if (ce != cudaSuccess) return ce; #if defined(__linux__) int cur = dev;