Merge commit '56b0ab1279' into f-ling32-int (Windows iGPU allocation probe fix, trace lines; one conflict in the tier store init resolved: the SYCL chunk allocation and the trace lines both kept)

This commit is contained in:
Niko1221 committed 2026-12-03 01:18:49 +01:00
commit 78a15b86bd
4 files changed
+89 -3

No files matched your search

+33 -2
View File
@@ -19,6 +19,7 @@
#ifdef LING32_HAVE_CUDA
#include "ling32_engine.hpp"
#include "ling32_tier_store.hpp"
#include "ling32_trace.hpp"
#include "ling32_serve.hpp"
#include "ling32_vision_run.hpp"
#ifdef _WIN32
@@ -301,14 +302,36 @@ uint64_t host_ram_available() {
// Probe it: 1 GiB pieces until the allocation fails (a few seconds at most), everything freed again. Pageable pages registered with the driver (hipHostRegister) are not counted.
uint64_t probe_gpu_alloc_cap(int device) {
cudaSetDevice(device);
std::vector<void*> v;
constexpr size_t kGiB = 1ull << 30;
{ // what the runtime says before anything is allocated (diagnosis of the Windows iGPU stall); STRATA_L32_GPU_CAP_GIB=N: use N GiB and skip the allocate-and-free probe
size_t fr = 0, tot = 0;
cudaMemGetInfo(&fr, &tot);
l32::trace("probe: cudaMemGetInfo before the probe: free %.2f GiB of %.2f GiB", fr / double(kGiB), tot / double(kGiB));
if (const char* ov = std::getenv("STRATA_L32_GPU_CAP_GIB")) if (std::atof(ov) > 0) return static_cast<uint64_t>(std::atof(ov) * double(kGiB));
#if defined(STRATA_USE_HIP) && defined(_WIN32)
// Windows integrated GPU (Strix Halo): DO NOT allocate and free the whole memory to find the cap. Measured on the Strix with the 96 GB carve-out (2026-12-03): the probe "succeeded" with 111 GiB,
// more than the 107.87 GiB the runtime reports (the driver overcommits into shared system memory), and after the free cudaMemGetInfo said 0.00 GiB free; the next device allocation worked but its first
// copy then stalled for good (1 thread, 14 GB working set, no events). Without the probe the same load takes 20 s. The cap is what the runtime reports before anything is allocated; a single
// allocation that fails later falls back to chunked experts (ling32_model.cu). STRATA_L32_PROBE_ALLOC=1 brings the old probe back.
cudaDeviceProp prop;
const char* pa = std::getenv("STRATA_L32_PROBE_ALLOC");
if (cudaGetDeviceProperties(&prop, device) == cudaSuccess && prop.integrated && !(pa && pa[0] == '1') && fr > 0)
return static_cast<uint64_t>(std::min(fr, tot));
#endif
}
std::vector<void*> v;
for (int i = 0; i < 512; ++i) {
void* p = nullptr;
if (cudaMalloc(&p, kGiB) != cudaSuccess) { cudaGetLastError(); break; }
v.push_back(p);
}
l32::trace("probe: %zu GiB allocated, freeing", v.size());
for (void* p : v) cudaFree(p);
{
size_t fr = 0, tot = 0;
cudaMemGetInfo(&fr, &tot);
l32::trace("probe: freed; cudaMemGetInfo free %.2f GiB of %.2f GiB", fr / double(kGiB), tot / double(kGiB));
}
return static_cast<uint64_t>(v.size()) * kGiB;
}
@@ -659,7 +682,11 @@ double measure_h2d_gbps(int device) {
cudaStream_t st = nullptr;
cudaEvent_t e0 = nullptr, e1 = nullptr;
double med = 0.0;
if (cudaMallocHost(&h, n) == cudaSuccess && cudaMalloc(&d, n) == cudaSuccess && cudaStreamCreate(&st) == cudaSuccess && cudaEventCreate(&e0) == cudaSuccess && cudaEventCreate(&e1) == cudaSuccess) {
l32::trace("h2d probe: cudaMallocHost %zu MiB", n >> 20);
const bool probe_host_ok = cudaMallocHost(&h, n) == cudaSuccess;
l32::trace("h2d probe: cudaMallocHost %s; cudaMalloc %zu MiB", probe_host_ok ? "ok" : "failed", n >> 20);
if (probe_host_ok && cudaMalloc(&d, n) == cudaSuccess && cudaStreamCreate(&st) == cudaSuccess && cudaEventCreate(&e0) == cudaSuccess && cudaEventCreate(&e1) == cudaSuccess) {
l32::trace("h2d probe: memset and copies");
std::memset(h, 1, n);
cudaMemcpyAsync(d, h, n, cudaMemcpyHostToDevice, st);
cudaStreamSynchronize(st);
@@ -727,7 +754,9 @@ int cmd_run(const RunOpts& o_in) {
uint64_t plan_vram = fr, plan_ram = host_ram_available(), win_ram_budget = 0;
if (win_igpu && o.tier != "vram") {
constexpr uint64_t kGiBu = 1ull << 30;
l32::trace("main: probing the GPU allocation cap");
const uint64_t cap = probe_gpu_alloc_cap(o.device);
l32::trace("main: allocation cap %.1f GiB", cap / 1073741824.0);
const uint64_t reserve = 4 * kGiBu; // the engine's buffers beyond the planner's own "fixed" figure; hipMalloc blocks instead of failing when the pool is full
const uint64_t floor_ram = 8 * kGiBu + kGiBu * 3 / 2; // 8 GiB of Windows RAM stay free (+ the ring, staging and the process itself)
plan_vram = std::min<uint64_t>(fr, cap > reserve ? cap - reserve : 0);
@@ -806,6 +835,7 @@ int cmd_run(const RunOpts& o_in) {
std::printf("tier store: host-to-device link moved %.1f GB/s (pinned 256 MiB copies, median of 3)\n", h2d_gbps);
}
}
l32::trace("main: plan done, loading the model (%s)", tiered ? "tiered" : "tier A");
GpuModel M;
LoadOptions lo;
lo.load_lookahead = o.lookahead || o.coverage || (tiered && plan.prefetch);
@@ -819,6 +849,7 @@ int cmd_run(const RunOpts& o_in) {
if (!o.vis.file.empty() && (o.vis.backend == "gpu" || (o.vis.backend == "auto" && vision_default_backend(vision_gpu_total_bytes(o.device))[0] == 'g')))
lo.reserve_bytes += 1600ull << 20; // the GPU picture encoder: ~830 MiB of weights plus the activations of one picture
const double t0 = now_s();
l32::trace("main: M.load starts (load_experts=%d)", lo.load_experts ? 1 : 0);
if (!M.load(f, lo, err)) { std::fprintf(stderr, "strata-ling32: %s\n", err.c_str()); return 1; }
std::printf("%s\n", M.report().c_str());
{ // the sidecar's experts become slots of the pool (pinned RAM -> card on demand) when the plan is a pool plan; otherwise they stay a resident block
+24
View File
@@ -2,6 +2,7 @@
#include "ling32_model.hpp"
#include "strata/ling32/ling32_sk.hpp"
#include "ling32_trace.hpp"
#include <algorithm>
#include <chrono>
@@ -372,13 +373,16 @@ struct LoaderImpl {
uint64_t got = 0;
uint64_t limit = total + 2 * kGiB; // STRATA_L32_SEG_DEVMAX_MIB=N: stop at N MiB of device chunks (tests of the pinned part)
if (const char* dv = std::getenv("STRATA_L32_SEG_DEVMAX_MIB")) limit = std::strtoull(dv, nullptr, 10) * kMiB;
l32::trace("segmented: allocating device chunks (1 GiB pieces first)");
for (const size_t piece : {kGiB, 256 * kMiB, 64 * kMiB, 16 * kMiB}) {
while (got + (limit < total + 2 * kGiB ? piece : 0) <= limit && got < limit) {
void* p = nullptr;
if (cudaMalloc(&p, piece) != cudaSuccess) { cudaGetLastError(); break; }
ch.push_back({static_cast<uint8_t*>(p), nullptr, piece, false});
got += piece;
if (piece == kGiB && ch.size() % 8 == 0) l32::trace("segmented: %.0f GiB of device chunks", got / 1073741824.0);
}
l32::trace("segmented: %.2f GiB of device chunks after the %.0f MiB pieces", got / 1073741824.0, piece / 1048576.0);
}
if (got < total && !std::getenv("STRATA_L32_SEG_DEVMAX_MIB")) { // the driver ran out: give back the engine's share (activations, KV, state)
const uint64_t give = std::max<uint64_t>(opt.reserve_bytes, 1280 * kMiB);
@@ -421,9 +425,12 @@ struct LoaderImpl {
void* h = nullptr;
#if defined(_WIN32)
// hipHostMalloc memory counts against the same ~63 GiB the driver lets a process allocate for the GPU as device memory does; ordinary pages registered with the driver do not
l32::trace("segmented: pinned chunk, VirtualAlloc %.2f GiB", cap / 1073741824.0);
h = VirtualAlloc(nullptr, cap, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!h) { err = "VirtualAlloc failed for a pinned chunk of the routed experts"; return false; }
l32::trace("segmented: pinned chunk, cudaHostRegister %.2f GiB", cap / 1073741824.0);
cudaError_t e = cudaHostRegister(h, cap, cudaHostRegisterMapped | cudaHostRegisterPortable);
l32::trace("segmented: pinned chunk registered (%s)", e == cudaSuccess ? "ok" : "failed");
if (e != cudaSuccess) { cudaGetLastError(); VirtualFree(h, 0, MEM_RELEASE); err = cuda_err(e, "cudaHostRegister (pinned chunk of the routed experts)"); return false; }
#else
cudaError_t e = cudaHostAlloc(&h, cap, cudaHostAllocMapped | cudaHostAllocPortable);
@@ -498,7 +505,9 @@ struct LoaderImpl {
if (const char* sv = std::getenv("STRATA_L32_SEGMENTED"); sv && sv[0] == '1') seg = true;
cudaError_t e = cudaSuccess;
if (!seg) {
l32::trace("experts: ONE cudaMalloc of %.2f GiB", total / 1073741824.0);
e = cudaMalloc(reinterpret_cast<void**>(&m.sec4_), total);
l32::trace("experts: the single cudaMalloc %s", e == cudaSuccess ? "succeeded" : "failed");
if (e != cudaSuccess) {
cudaGetLastError();
m.sec4_ = nullptr;
@@ -507,8 +516,10 @@ struct LoaderImpl {
}
}
uint8_t* pin[2] = {nullptr, nullptr};
l32::trace("experts: cudaMallocHost 2 x %.0f MiB staging", kStage / 1048576.0);
for (auto& p : pin)
if ((e = cudaMallocHost(reinterpret_cast<void**>(&p), kStage)) != cudaSuccess) { err = cuda_err(e, "cudaMallocHost"); return false; }
l32::trace("experts: staging pinned, uploading (%s)", seg ? "segmented" : "one block");
const uint64_t base = f.section_offset(4);
bool ok = true;
std::string rerr;
@@ -625,10 +636,13 @@ bool GpuModel::load(const Ling32File& f, const LoadOptions& opt, std::string& er
cudaError_t e = cudaSetDevice(opt.device);
if (e != cudaSuccess) { err = cuda_err(e, "cudaSetDevice"); return false; }
LoaderImpl L(*this, f, opt, err);
l32::trace("loader: start (device %d)", opt.device);
// expert directory -> descriptors
const auto& dir = f.reader().expert_directory();
l32::trace("loader: validating the expert directory (%zu entries)", dir.size());
if (!f.validate_directory(err)) return false;
l32::trace("loader: expert directory valid");
experts.resize(dir.size());
for (size_t i = 0; i < dir.size(); ++i)
experts[i] = {dir[i].body_offset, dir[i].gu_code_id, dir[i].dn_code_id, static_cast<int>(dir[i].dn_offset), static_cast<int>(dir[i].body_bytes)};
@@ -644,19 +658,23 @@ bool GpuModel::load(const Ling32File& f, const LoadOptions& opt, std::string& er
gpu::set_moe_max_code(mx);
std::fprintf(stderr, "ling32: routed experts use S_K codes up to S%d_K\n", mx);
}
l32::trace("loader: reading the rank map");
if (!f.load_rank_map(rank_to_eid, eid_to_rank, err)) { // the rank map is Section 2 (optional for Mode A)
rank_to_eid.clear();
eid_to_rank.clear();
err.clear();
}
l32::trace("loader: rank map done");
// plan the arena (dry pass), then check the memory before allocating anything
L.dry = true;
if (!L.trunk() || (opt.load_lookahead && !L.lookahead())) return false;
trunk_bytes = 0; mla_split_bytes = 0; lookahead_bytes = 0; trunk_tensors_loaded = 0;
arena_size_ = L.cursor;
l32::trace("loader: dry pass done, trunk arena %.2f GiB; cudaMemGetInfo", arena_size_ / 1073741824.0);
size_t free_b = 0, total_b = 0;
cudaMemGetInfo(&free_b, &total_b);
l32::trace("loader: cudaMemGetInfo free %.2f GiB of %.2f GiB", free_b / 1073741824.0, total_b / 1073741824.0);
#if defined(__linux__)
{ // unified memory (GB10, Strix Halo): the GPU shares the host RAM and cudaMemGetInfo counts only MemFree, not the page cache the kernel gives back on demand
cudaDeviceProp prop;
@@ -687,10 +705,13 @@ bool GpuModel::load(const Ling32File& f, const LoadOptions& opt, std::string& er
err = buf;
return false;
}
l32::trace("loader: cudaMalloc trunk arena %.2f GiB", arena_size_ / 1073741824.0);
e = cudaMalloc(reinterpret_cast<void**>(&arena_), arena_size_);
if (e != cudaSuccess) { err = cuda_err(e, "cudaMalloc for the trunk"); arena_ = nullptr; return false; }
l32::trace("loader: cudaMalloc trunk arena done; cudaMallocHost staging %.0f MiB", kStage / 1048576.0);
e = cudaMallocHost(reinterpret_cast<void**>(&L.stage), kStage);
if (e != cudaSuccess) { err = cuda_err(e, "cudaMallocHost"); return false; }
l32::trace("loader: staging buffer pinned; uploading the trunk");
L.dry = false;
L.cursor = 0;
layers.clear();
@@ -698,9 +719,12 @@ bool GpuModel::load(const Ling32File& f, const LoadOptions& opt, std::string& er
cudaFreeHost(L.stage);
L.stage = nullptr;
if (!ok) return false;
l32::trace("loader: trunk uploaded");
// everything not loaded is counted: the trunk table minus what the layer table named
trunk_tensors_skipped = f.man().trunk.size() > trunk_tensors_loaded ? f.man().trunk.size() - trunk_tensors_loaded : 0;
if (opt.load_experts) l32::trace("loader: routed experts, Section 4 = %.2f GiB (load_experts, map_experts=%d)", f.section_bytes(4) / 1073741824.0, opt.map_experts ? 1 : 0);
if (opt.load_experts && !L.experts()) return false;
l32::trace("loader: experts() returned");
std::fprintf(stderr, "ling32: %s\n", "experts() returned");
e = cudaMalloc(reinterpret_cast<void**>(&d_experts), experts.size() * sizeof(ExpertDesc));
if (e != cudaSuccess) { err = cuda_err(e, "cudaMalloc"); d_experts = nullptr; return false; }
+12 -1
View File
@@ -4,6 +4,7 @@
#include "strata/core/dma_batch.hpp"
#include "strata/platform/direct_file.hpp"
#include "ling32_cpu_expert.hpp"
#include "ling32_trace.hpp"
#if defined(_WIN32)
#ifndef NOMINMAX
@@ -511,6 +512,7 @@ struct ExpertStore::Impl {
if (!df.open(f.path(), err)) return false;
if (!cu(cudaStreamCreateWithFlags(&pf, cudaStreamNonBlocking), "stream", err)) return false;
// memory
l32::trace("tier store: start; cudaMalloc hot prefix %.2f GiB", vp / 1073741824.0);
#if defined(STRATA_USE_SYCL)
if (vp) {
std::vector<uint64_t> vcuts; // slab ends inside the hot prefix
@@ -522,8 +524,10 @@ struct ExpertStore::Impl {
#else
if (vp && !cu(cudaMalloc(reinterpret_cast<void**>(&vram), vp), "cudaMalloc (hot prefix)", err)) return false;
#endif
l32::trace("tier store: hot prefix allocated");
gpu_alloc += vp;
if (plan.dyn_slots > 0) {
l32::trace("tier store: cudaMalloc dynamic slots (%d slots)", plan.dyn_slots);
const int nbig = plan.dyn_big > 0 && plan.dyn_big < plan.dyn_slots && plan.slot_small > 0 ? plan.dyn_big : 0;
n_small = plan.dyn_slots - nbig;
slot_small = nbig ? plan.slot_small : slot_bytes;
@@ -544,6 +548,7 @@ struct ExpertStore::Impl {
#endif
gpu_alloc += static_cast<uint64_t>(nbig) * slot_bytes;
}
l32::trace("tier store: dynamic slots allocated (%.2f GiB on the card in all)", gpu_alloc / 1073741824.0);
dyn.assign(static_cast<size_t>(plan.dyn_slots), Dyn{});
for (auto& d : dyn) if (!cu(cudaEventCreateWithFlags(&d.ev, cudaEventDisableTiming), "event", err)) return false;
}
@@ -588,11 +593,14 @@ struct ExpertStore::Impl {
return false;
}
const double tw0 = now_s();
l32::trace("tier store: VirtualAlloc warm prefix %.2f GiB", rp / 1073741824.0);
host = static_cast<uint8_t*>(VirtualAlloc(nullptr, rp, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE));
if (!host) { err = "tier store: VirtualAlloc failed for the warm prefix"; return false; }
host_registered = true;
l32::trace("tier store: cudaHostRegister warm prefix %.2f GiB", rp / 1073741824.0);
// ONE registration of the whole range: a DMA copy of a slab that straddles two separate registrations fails with "invalid argument"
if (!cu(cudaHostRegister(host, rp, cudaHostRegisterPortable), "cudaHostRegister (warm prefix in RAM)", err)) return false;
l32::trace("tier store: warm prefix registered");
{ // the address the GPU uses for this range (kind 2: slabs read in place)
void* dp = nullptr;
host_dev = cudaHostGetDevicePointer(&dp, host, 0) == cudaSuccess && dp ? static_cast<uint8_t*>(dp) : host;
@@ -990,7 +998,9 @@ struct ExpertStore::Impl {
const uint64_t limit_mib = [] { const char* e = std::getenv("STRATA_L32_PIN_LIMIT_MIB"); return e ? static_cast<uint64_t>(std::max(0, std::atoi(e))) : 0ull; }();
const uint64_t min_pin = (force_fail || limit_mib) ? (1ull << 20) : kGiBh;
void* p = nullptr;
l32::trace("tier store: cudaHostAlloc %.2f GiB for %s", bytes / double(kGiBh), what);
const cudaError_t e = force_fail ? cudaErrorMemoryAllocation : cudaHostAlloc(&p, static_cast<size_t>(bytes), cudaHostAllocPortable);
l32::trace("tier store: cudaHostAlloc for %s %s", what, e == cudaSuccess ? "succeeded" : "refused");
if (e == cudaSuccess) { pinned = bytes; pageable = false; return static_cast<uint8_t*>(p); }
(void)cudaGetLastError();
const std::string why = cudaGetErrorString(e);
@@ -1016,7 +1026,8 @@ struct ExpertStore::Impl {
}
if (c < min_pin) break;
const bool refused = limit_mib && c > (limit_mib << 20);
if (!refused && cudaHostRegister(p, static_cast<size_t>(c), cudaHostRegisterPortable) == cudaSuccess) { pinned = c; break; }
l32::trace("tier store: cudaHostRegister %.2f GiB of %s (try %d)", c / double(kGiBh), what, tries + 1);
if (!refused && cudaHostRegister(p, static_cast<size_t>(c), cudaHostRegisterPortable) == cudaSuccess) { pinned = c; l32::trace("tier store: registered"); break; }
(void)cudaGetLastError();
cand = c - std::max<uint64_t>(c / 8, min_pin / 4);
}
+20
View File
@@ -0,0 +1,20 @@
// ling32_trace.hpp: one timestamped, flushed stderr line for the big start-up steps (device allocations, host registrations, uploads), so that a stall is visible in the log within a minute
// (Windows iGPU: a huge allocation or page-lock can sit in the driver for minutes with no output). Lines: "[ 12.34s] what".
#pragma once
#include <chrono>
#include <cstdarg>
#include <cstdio>
namespace l32 {
inline void trace(const char* fmt, ...) {
static const std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now();
const double s = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
char b[480];
va_list a;
va_start(a, fmt);
std::vsnprintf(b, sizeof b, fmt, a);
va_end(a);
std::fprintf(stderr, "[%7.2fs] %s\n", s, b);
std::fflush(stderr);
}
} // namespace l32