From 71b77aaf90562f8ee15d0b13d672d4f33388ed66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Schmied?= <600316+jschmied@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:24:30 +0100 Subject: [PATCH 1/3] xpu: Sysman lists asked for their count first, and zesInit only when a prompt cache needs free memory Under WSL2 (Intel Iris Xe 0x9a49 over /dev/dxg, compute runtime 23.43.27642, Level Zero loader 1.16) zesInit(0) segfaults inside libze_intel_gpu.so.1. tensorfold-native died at startup in ze.Gpu.memory(), which the lane host called for the prompt cache budget even when no cache is made. The lane host now reads free memory only for drafted engines, the only ones that keep a cache. zesDriverGet, zesDeviceGet and zesDeviceEnumMemoryModules ask for the count first, clamp it and skip null handles, as the core enumeration does. --- zig/src/native/xpu_nem.zig | 3 ++- zig/src/xpu/ze.zig | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/zig/src/native/xpu_nem.zig b/zig/src/native/xpu_nem.zig index 7c8283ae7de..8e7eea45bcb 100644 --- a/zig/src/native/xpu_nem.zig +++ b/zig/src/native/xpu_nem.zig @@ -155,7 +155,8 @@ fn openFamily(comptime S: type, a: Allocator, gpa: Allocator, io: std.Io, o: api h.host = api.LaneHost.init(gpa, io, &h.core, .{ .name = S.name, .lanes = h.backend.streams, .context_window = @intCast(window), .context_fitted = true, .startup = h.startup, .prefill_step = if (drafted and !@hasDecl(S.Engine, "copy_only")) S.promptRows(h.engine) else 0 }); // kept prompt states (with sequences): --prompt-cache-gib, else 4 GiB, inside the device memory left past 2 GiB const want: u64 = @intFromFloat((o.prompt_cache_gib orelse 4) * (1 << 30)); - const free = freeMemory(&h.ctx); + // Sysman only when a cache can use it: zesInit faults inside the driver under WSL2 (Iris Xe, NEO 23.43) + const free = if (drafted) freeMemory(&h.ctx) else 0; const budget: u64 = if (free > 2 << 30) @min(want, free - (2 << 30)) else 0; h.cache = null; std.log.info("xpu: prompt cache budget {d} MiB (free {d} MiB, asked {d} MiB)", .{ budget >> 20, free >> 20, want >> 20 }); diff --git a/zig/src/xpu/ze.zig b/zig/src/xpu/ze.zig index a931e68df5a..6343dd22911 100644 --- a/zig/src/xpu/ze.zig +++ b/zig/src/xpu/ze.zig @@ -271,23 +271,32 @@ pub const Gpu = struct { pub fn memory(g: *Gpu) Error!struct { free: u64, size: u64 } { const sys = try resolve(SysApi, &g.lib); try check(sys.zesInit(0)); - var nd: u32 = 8; + var nd: u32 = 0; // counts first: loader 1.16 + NEO 23.43 keep a larger count as asked, padded with nulls + try check(sys.zesDriverGet(&nd, null)); var drivers: [8]H = @splat(null); + nd = @min(nd, drivers.len); try check(sys.zesDriverGet(&nd, &drivers)); for (drivers[0..nd]) |drv| { - var nv: u32 = 8; + if (drv == null) continue; + var nv: u32 = 0; + try check(sys.zesDeviceGet(drv, &nv, null)); var devs: [8]H = @splat(null); + nv = @min(nv, devs.len); try check(sys.zesDeviceGet(drv, &nv, &devs)); for (devs[0..nv]) |dev| { + if (dev == null) continue; var p: SysDeviceProperties = .{}; try check(sys.zesDeviceGetProperties(dev, &p)); if (!std.mem.eql(u8, &p.core.uuid, &g.props.uuid)) continue; - var nm: u32 = 16; + var nm: u32 = 0; + try check(sys.zesDeviceEnumMemoryModules(dev, &nm, null)); var mods: [16]H = @splat(null); + nm = @min(nm, mods.len); try check(sys.zesDeviceEnumMemoryModules(dev, &nm, &mods)); var left: u64 = 0; var all: u64 = 0; for (mods[0..nm]) |m| { + if (m == null) continue; var mp: MemProperties = .{}; try check(sys.zesMemoryGetProperties(m, &mp)); if (mp.location != 1) continue; From a4bf9de291658020da89f34cbb51858de00c559c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Schmied?= <600316+jschmied@users.noreply.github.com> Date: Fri, 30 Oct 2026 16:06:54 +0100 Subject: [PATCH 2/3] xpu: without Sysman (WSL2), Gpu.memory falls back to the core total, less what the process allocated Under WSL2 the GPU is a WDDM adapter (/dev/dxg, no /dev/dri). NEO 23.43's zesInit walks /dev/dri/renderD128..191, then opens /dev/dxg and faults inside libze_intel_gpu.so.1 (SIGSEGV at 0x100000000, an AVX-512 string compare in libc on a bad pointer), with or without ZES_ENABLE_SYSMAN. With ZES_ENABLE_SYSMAN=1 set before zeInit, zesInit returns ZE_RESULT_ERROR_UNINITIALIZED instead; Sysman has no WDDM backend either way. Gpu.memory() now asks Sysman only where /dev/dri exists (or /dev/dxg does not). Elsewhere free and size are the core total (zeDeviceGetMemoryProperties, asked with room for 8 modules: NEO answers a count query with 0), and the lane host's freeMemory subtracts this process's allocations as before, so a drafted engine's prompt cache is sized on WSL too. tf-xpu-memory-test asks free memory the way the lane host does, before and after a 512 MiB allocation. --- zig/build/xpu.zig | 2 +- zig/src/xpu/ze.zig | 37 ++++++++++++++++++++++++++++++++++++- zig/tests/xpu/memory.zig | 29 +++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 zig/tests/xpu/memory.zig diff --git a/zig/build/xpu.zig b/zig/build/xpu.zig index 2fc4c3ff2c5..702bff34bc9 100644 --- a/zig/build/xpu.zig +++ b/zig/build/xpu.zig @@ -229,7 +229,7 @@ fn spirv(b: *std.Build, ocloc: []const u8, device: []const u8, k: Kernel) std.Bu /// The standalone op and model tests, each its own program `tf-xpu--test` (step `xpu-tests`). const test_programs = [_][]const u8{ - "panic", "nem_rows", "nem_prefill", "nem_gen", "nem_mtp", "nem_kl", "nem_norm_bench", "nem_mv_bench", "nem_slot", "nem_format", "nem_seam", "nem_attn_dec", "nem_fp64", + "panic", "nem_rows", "nem_prefill", "nem_gen", "nem_mtp", "nem_kl", "nem_norm_bench", "nem_mv_bench", "nem_slot", "nem_format", "nem_seam", "nem_attn_dec", "nem_fp64", "memory", }; /// `-Dxpu` (Linux): Nemotron's SPIR-V images, `tensorfold-xpu` and `tf-xpu-test`; needs ocloc, not nvcc or CUDA. diff --git a/zig/src/xpu/ze.zig b/zig/src/xpu/ze.zig index 6343dd22911..c7b918ac9c5 100644 --- a/zig/src/xpu/ze.zig +++ b/zig/src/xpu/ze.zig @@ -43,6 +43,26 @@ pub const Api = struct { zeContextDestroy: *const fn (H) callconv(.c) u32, }; +/// The core memory table, Gpu.memory's fallback where Sysman cannot run. +pub const MemApi = struct { + zeDeviceGetMemoryProperties: *const fn (H, *u32, ?[*]DeviceMemoryProperties) callconv(.c) u32, +}; +/// ze_device_memory_properties_t. +pub const DeviceMemoryProperties = extern struct { + stype: u32 = 0x5, // ZE_STRUCTURE_TYPE_DEVICE_MEMORY_PROPERTIES + pNext: ?*anyopaque = null, + flags: u32 = 0, + max_clock_rate: u32 = 0, + max_bus_width: u32 = 0, + total_size: u64 = 0, + name: [256]u8 = @splat(0), +}; + +/// Sysman needs DRM: under WSL2 (/dev/dxg, no /dev/dri) NEO 23.43's zesInit faults in libze_intel_gpu (C2 receipt). +fn sysmanUsable() bool { + return std.c.access("/dev/dri", 0) == 0 or std.c.access("/dev/dxg", 0) != 0; +} + /// Sysman, for the free memory of the device's own memory modules (Gpu.freeMemory). pub const SysApi = struct { zesInit: *const fn (u32) callconv(.c) u32, @@ -267,8 +287,12 @@ pub const Gpu = struct { return error.NoDevice; } - /// On-board bytes free and in all by Sysman (device matched by UUID); an allocation leaves `free` once written. + /// On-board bytes free and in all by Sysman (by UUID); without Sysman (WSL2) both are the core total, less nothing. pub fn memory(g: *Gpu) Error!struct { free: u64, size: u64 } { + if (!sysmanUsable()) { + const total = try g.coreMemory(); + return .{ .free = total, .size = total }; + } const sys = try resolve(SysApi, &g.lib); try check(sys.zesInit(0)); var nd: u32 = 0; // counts first: loader 1.16 + NEO 23.43 keep a larger count as asked, padded with nulls @@ -311,6 +335,17 @@ pub const Gpu = struct { return error.NoDevice; } + /// The memory modules' total by the core API, asked with room for 8 (NEO answers a count query with 0). + pub fn coreMemory(g: *Gpu) Error!u64 { + const mem = try resolve(MemApi, &g.lib); + var props: [8]DeviceMemoryProperties = @splat(.{}); + var n: u32 = props.len; + try check(mem.zeDeviceGetMemoryProperties(g.device, &n, &props)); + var total: u64 = 0; + for (props[0..@min(n, props.len)]) |m| total += m.total_size; + return total; + } + pub fn deviceAlloc(g: *Gpu, size: usize) Error!*anyopaque { var p: ?*anyopaque = null; try check(g.api.zeMemAllocDevice(g.context, &.{}, size, 4096, g.device, &p)); diff --git a/zig/tests/xpu/memory.zig b/zig/tests/xpu/memory.zig new file mode 100644 index 00000000000..f17e243efaa --- /dev/null +++ b/zig/tests/xpu/memory.zig @@ -0,0 +1,29 @@ +//! Free device memory as the lane host asks it (Gpu.memory, less this process's allocations): Sysman or the core total. + +const std = @import("std"); +const xpu = @import("xpu"); + +pub const panic = std.debug.FullPanic(@import("rig.zig").panicDrain); + +fn free(ctx: *xpu.Context) !u64 { + const mem = try ctx.gpu.memory(); + return @min(mem.free, mem.size -| xpu.rt.alloc_now); +} + +pub fn main() !void { + xpu.stop.install(); + var drv = try xpu.Driver.open(); + var ctx = try xpu.Context.init(&drv, null); + const stream = try xpu.Stream.init(&ctx); + var r = try xpu.rt.Runtime.init(&ctx, stream); + const sysman = std.c.access("/dev/dri", 0) == 0 or std.c.access("/dev/dxg", 0) != 0; + const mem = try ctx.gpu.memory(); + std.debug.print("{s}: {s}, size {d:.2} GiB, free {d:.2} GiB\n", .{ ctx.name(), if (sysman) "Sysman" else "core total (no Sysman: WDDM)", @as(f64, @floatFromInt(mem.size)) / (1 << 30), @as(f64, @floatFromInt(mem.free)) / (1 << 30) }); + const before = try free(&ctx); + const b = try r.alloc(512 << 20); + _ = b; + const after = try free(&ctx); + std.debug.print("free as the lane host sees it: {d:.2} GiB, after a 512 MiB allocation {d:.2} GiB\n", .{ @as(f64, @floatFromInt(before)) / (1 << 30), @as(f64, @floatFromInt(after)) / (1 << 30) }); + if (before == 0 or after >= before) return error.NoFreeMemory; + std.debug.print("memory: ok\n", .{}); +} From 771371d177dde71cc26931d8d03d1a7ee8244ef6 Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:52:49 +0000 Subject: [PATCH 3/3] changelog: #1691 under Unreleased --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c7eed171e3..b0effd6cc6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - With `TF_DSV41_PF_4K=1`, `aot-needs` also lists the 2,048-row prefill variants the forward still runs, so a pack built with it no longer fails its first operation with `MissingTritonVariant _kv_store … RING 4096` (#1683). Thanks to @jayleaton. - DeepSeek-V4.1's prefill planner sizes the indexer's scratch before it shares and prices the workspace, so ragged prompts and the two-batch-overlap branch can no longer plan less than they use. This takes about 128 MiB more per rank on the Flash-1017 pack, and tokens are unchanged (#1681). Thanks to @jayleaton. +- On WSL2, where Intel's Sysman can't run, an Intel GPU's free memory falls back to the device's total less what TensorFold has allocated, so Nemotron with MTP can size its prompt cache there. Native Linux still asks Sysman (#1691). Thanks to @jschmied. - On Intel GPUs, the engine asks Level Zero how many drivers and devices there are before listing them and skips empty entries, so `tensorfold-xpu devices` and the device tests no longer crash on WSL2 with an Iris Xe (#1672). Thanks to @jschmied.