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++

core: #409 unified-memory free-memory accounting (device_free_bytes), its integrated-GPU sizing part

Niko1221004b15cd4da6
diff --git a/include/strata/core/expert_cache.hpp b/include/strata/core/expert_cache.hpp
index 4a708355f..849083a6d 100644
--- a/include/strata/core/expert_cache.hpp
+++ b/include/strata/core/expert_cache.hpp
@@ -73,6 +73,12 @@ std::vector<std::pair<int32_t, int32_t>> rank_learned_profile(int64_t n_layers,
bool write_expert_profile(const std::string& path, int64_t n_layers, int64_t n_expert,
const std::vector<std::pair<int32_t, int32_t>>& ranked, std::string& err);
+/// Free device memory for the expert cache. On a unified memory system - one memory for the CPU and the GPU, such as
+/// the DGX Spark (GB10); CUDA reports it as cudaDevAttrIntegrated - cudaMemGetInfo counts only MemFree, not the page cache - which, with the experts read from the GGUF in place, is mostly those
+/// files' clean pages and gives way to a device allocation. There the figure is MemAvailable less 6 GiB for the OS
+/// and the engine's host side (STRATA_UMA_HEADROOM_GIB). Elsewhere it is cudaMemGetInfo's.
+size_t device_free_bytes();
+
class ExpertCache {
public:
ExpertCache() = default;
diff --git a/include/strata/hip_compat/cuda_runtime.h b/include/strata/hip_compat/cuda_runtime.h
index 40c1c79c4..f6d551960 100644
--- a/include/strata/hip_compat/cuda_runtime.h
+++ b/include/strata/hip_compat/cuda_runtime.h
@@ -19,6 +19,7 @@
#define cudaDevAttrClockRate hipDeviceAttributeClockRate
#define cudaDevAttrComputeCapabilityMajor hipDeviceAttributeComputeCapabilityMajor
#define cudaDevAttrComputeCapabilityMinor hipDeviceAttributeComputeCapabilityMinor
+#define cudaDevAttrIntegrated hipDeviceAttributeIntegrated
#define cudaDeviceGetAttribute hipDeviceGetAttribute
#define cudaDeviceProp hipDeviceProp_t
#define cudaDeviceSynchronize hipDeviceSynchronize
diff --git a/src/core/expert_cache.cpp b/src/core/expert_cache.cpp
index dc6444367..8cc1c71fb 100644
--- a/src/core/expert_cache.cpp
+++ b/src/core/expert_cache.cpp
@@ -6,11 +6,49 @@
#include <algorithm>
#include <cstdio>
#include <filesystem>
+#include <cstdlib>
#include <utility>
#include <cstring>
namespace strata::core {
+// See the header. NVIDIA's DGX Spark Porting Guide (section 5.5, "Memory reporting on UMA systems") recommends the same:
+// not to rely on cudaMemGetInfo alone but to count the memory the OS can reclaim. Swap is not counted here (unlike
+// NVIDIA's reference snippet): an expert cache that pushes the system into swap would be far slower than a smaller one.
+size_t device_free_bytes() {
+ size_t free_b = 0, total_b = 0;
+ cudaMemGetInfo(&free_b, &total_b);
+#if defined(__linux__)
+ static const bool unified_memory = [] {
+ int dev = 0, v = 0;
+ return cudaGetDevice(&dev) == cudaSuccess && cudaDeviceGetAttribute(&v, cudaDevAttrIntegrated, dev) == cudaSuccess && v;
+ }();
+ if (unified_memory) {
+ if (FILE* m = std::fopen("/proc/meminfo", "r")) {
+ char line[256];
+ unsigned long long kb = 0;
+ while (std::fgets(line, sizeof line, m))
+ if (std::sscanf(line, "MemAvailable: %llu kB", &kb) == 1) break;
+ std::fclose(m);
+ // STRATA_UMA_HEADROOM_GIB: a whole number of GiB, 0..1024; anything else keeps the default 6 (said once)
+ static const long gib = [] {
+ const char* h = std::getenv("STRATA_UMA_HEADROOM_GIB");
+ if (h == nullptr) return 6L;
+ char* end = nullptr;
+ const long v = std::strtol(h, &end, 10);
+ if (end != h && *end == '\0' && v >= 0 && v <= 1024) return v;
+ std::fprintf(stderr, "strata: STRATA_UMA_HEADROOM_GIB=%s is not a whole number of GiB (0-1024): using 6\n", h);
+ return 6L;
+ }();
+ const unsigned long long head = (unsigned long long) gib << 30;
+ const unsigned long long avail = kb << 10;
+ if (avail > head && avail - head > free_b) free_b = (size_t) (avail - head);
+ }
+ }
+#endif
+ return free_b;
+}
+
bool read_expert_profile(const std::string& path, int64_t n_layers, int64_t n_expert,
std::vector<std::pair<int32_t, int32_t>>& ranked, int64_t& slots, std::string& err) {
std::FILE* f = std::fopen(path.c_str(), "rb");
@@ -180,6 +218,7 @@ bool ExpertCache::open(int64_t n_slots, int64_t n_layers, int64_t n_expert, int6
// and the two numbers are named in the refusal.
size_t free_b = 0, total_b = 0;
if (cudaMemGetInfo(&free_b, &total_b) == cudaSuccess) {
+ free_b = device_free_bytes(); // unified memory: what the OS can give back counts (see the header)
if ((uint64_t) free_b < want) {
char buf[320];
std::snprintf(buf, sizeof buf,
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index c447dd767..711f23035 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -2688,7 +2688,7 @@ int main(int argc, char** argv) {
const bool auto_cache = o.expert_cache < 0;
if (o.expert_cache < 0) {
size_t free_b = 0, total_b = 0;
- cudaMemGetInfo(&free_b, &total_b);
+ free_b = strata::core::device_free_bytes(); (void) total_b;
// Plan v0.3 P5: the batched prompt path's chunk buffers are allocated later, so they are reserved here -
// under WDDM an over-subscribed allocation does not fail, it pages to system memory and crawls.
// (with borrowing - the default with a profile - the prompt path lends cache slots instead; `pf_borrow` is
@@ -2713,7 +2713,7 @@ int main(int argc, char** argv) {
// fails with "device buffers ... do not fit" (with borrowing - the default with a profile - the path lends
// slots instead and `prefill_mib` is 0, so only the reserve is checked)
size_t free_b = 0, total_b = 0;
- cudaMemGetInfo(&free_b, &total_b);
+ free_b = strata::core::device_free_bytes(); (void) total_b;
const int64_t prefill_mib = (o.prefill_chunk > 0 && !pf_borrow) ? 160 + (o.prefill_chunk * 680) / 1024 : 0;
const int64_t reserve = ((int64_t) o.vram_reserve_mib + prefill_mib) << 20;
const int64_t fit = std::max<int64_t>(((int64_t) free_b - reserve) / (int64_t) strata::kernels::cpu::expert_layout().max_blob, 0);
@@ -2733,7 +2733,7 @@ int main(int argc, char** argv) {
std::vector<int64_t> sized_slots;
if (native_pack && o.expert_cache > 0 && !profile.empty() && !o.expert_cache_per_layer) {
size_t free_b = 0, total_b = 0;
- cudaMemGetInfo(&free_b, &total_b);
+ free_b = strata::core::device_free_bytes(); (void) total_b;
const auto& lay = strata::kernels::cpu::expert_layout();
const uint64_t budget = (uint64_t) o.expert_cache * lay.max_blob; // what the uniform sizing granted
uint64_t used = 0;
@@ -2819,7 +2819,7 @@ int main(int argc, char** argv) {
cudaMemset(xcache.device_slot(0), 0, (size_t) xcache.bytes());
cudaDeviceSynchronize();
size_t free_b = 0, total_b = 0;
- cudaMemGetInfo(&free_b, &total_b);
+ free_b = strata::core::device_free_bytes(); (void) total_b;
const int64_t want = (int64_t) o.vram_reserve_mib << 20;
if ((int64_t) free_b >= want - (64ll << 20)) break;
// short by (want - free); a figure of 0 only says "at least": the first two such reads give back 1 GiB