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. 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/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..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,27 +287,40 @@ 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 = 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; @@ -302,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", .{}); +}