core: #409 unified-memory free-memory accounting (device_free_bytes), its integrated-GPU sizing part
From PR #409 (af404df), only device_free_bytes(): on an integrated GPU (cudaDevAttrIntegrated, on HIP hipDeviceAttributeIntegrated; the Ryzen AI Max 8060S) the expert cache sizing and checks count /proc/meminfo MemAvailable less STRATA_UMA_HEADROOM_GIB (6) when that is more than cudaMemGetInfo: the page cache of experts read from the GGUF in place gives way to a device allocation. The PR's aarch64 / DGX Spark CPU parts are not taken.
This commit is contained in:
1 parent
41c89b1708
commit
004b15cd4d
4 files changed
+50
-4
No files matched your search
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user