Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion zig/build/xpu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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-<name>-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.
Expand Down
3 changes: 2 additions & 1 deletion zig/src/native/xpu_nem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
52 changes: 48 additions & 4 deletions zig/src/xpu/ze.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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));
Expand Down
29 changes: 29 additions & 0 deletions zig/tests/xpu/memory.zig
Original file line number Diff line number Diff line change
@@ -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", .{});
}
Loading