diff --git a/CHANGELOG.md b/CHANGELOG.md index 7939cb44c5b..664945c42ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel ## Unreleased +- XPU: Gen12LP GPUs (Iris Xe, DG1) build Nemotron's kernels, with a plain bf16 fallback for its two DPAS products and decode attention through the partial kernels; DPAS builds are unchanged (#1673). Thanks to @jschmied. + - Lane rounds count the time each stream spends in grammar mask fills, per round and per reply, reported with the round metrics; nothing is read when no stream is constrained, and behaviour is unchanged (part of #1455). - CUDA tensor parallel: a rank that cannot bind its liveness port (`NCCL_COMM_ID`'s port + 1, often held by an earlier server still exiting) says so, as do a rank that never arrives and a failed accept, instead of reporting a generic "glue kernels" error (fixes #1761). diff --git a/zig/build/xpu.zig b/zig/build/xpu.zig index c4d6706e5a5..e39bea9b48f 100644 --- a/zig/build/xpu.zig +++ b/zig/build/xpu.zig @@ -11,6 +11,7 @@ fn zeModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.built pub fn steps(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, build_options: *std.Build.Step.Options) void { const ocloc = b.option([]const u8, "ocloc", "Intel offline compiler") orelse "ocloc"; const device = b.option([]const u8, "xpu-device", "ocloc device for the XPU kernels") orelse "bmg"; + const dpas = b.option(bool, "xpu-dpas", "The device has DPAS (XMX); off: plain fallbacks (default off for Gen12LP: tgllp, rkl, adl-*, dg1)") orelse hasDpas(device); const ngen_include = b.option([]const u8, "ngen-include", "nGEN header directory (oneDNN)") orelse "ngen"; const files = b.addWriteFiles(); const opts = "-cl-std=CL3.0 -cl-intel-256-GRF-per-thread"; @@ -136,7 +137,7 @@ pub fn steps(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.buil const host = b.graph.host.result; if (target.result.cpu.arch == host.cpu.arch and target.result.os.tag == host.os.tag) tests_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = qwen27 })).step); const lanes = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = target, .optimize = optimize, .link_libc = true }); - const nem = nemotronModules(b, target, optimize, ocloc, device, ze_mod, lanes); + const nem = nemotronModules(b, target, optimize, ocloc, device, dpas, ze_mod, lanes); const api = b.createModule(.{ .root_source_file = b.path("zig/src/core/engine_api.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = lanes }} }); const gemma4 = b.createModule(.{ .root_source_file = b.path("zig/src/families/gemma4/xpu_engine.zig"), .target = target, .optimize = optimize, .link_libc = true }); gemma4.addImport("xpu_gemma4_kernels", embedded_g4); @@ -184,9 +185,10 @@ const kernels = [_]Kernel{ }; /// The runtime module; `with_kernels` false builds it host-only (empty images). -fn runtime(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, images: []const std.Build.LazyPath, ze: *std.Build.Module) *std.Build.Module { +fn runtime(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, images: []const std.Build.LazyPath, ze: *std.Build.Module, dpas: bool) *std.Build.Module { const options = b.addOptions(); options.addOption(bool, "with_kernels", images.len == kernels.len); + options.addOption(bool, "dpas", dpas); const xpu = b.createModule(.{ .root_source_file = b.path("zig/src/xpu/root.zig"), .target = target, .optimize = optimize, .link_libc = true }); xpu.addOptions("kernel_options", options); xpu.addImport("ze", ze); @@ -207,10 +209,10 @@ fn family(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin } /// Nemotron's SPIR-V images (built only when a step needs them), its runtime and its family module. -fn nemotronModules(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, ocloc: []const u8, device: []const u8, ze: *std.Build.Module, lanes: *std.Build.Module) Modules { +fn nemotronModules(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, ocloc: []const u8, device: []const u8, dpas: bool, ze: *std.Build.Module, lanes: *std.Build.Module) Modules { var images: [kernels.len]std.Build.LazyPath = undefined; - for (kernels, &images) |k, *image| image.* = spirv(b, ocloc, device, k); - var m = family(b, target, optimize, runtime(b, target, optimize, &images, ze), lanes); + for (kernels, &images) |k, *image| image.* = spirv(b, ocloc, device, dpas, k); + var m = family(b, target, optimize, runtime(b, target, optimize, &images, ze, dpas), lanes); m.images = images; return m; } @@ -224,10 +226,11 @@ fn cliModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.buil } /// ocloc -spv_only on one kernel source: `name`.spv in its own output directory. -fn spirv(b: *std.Build, ocloc: []const u8, device: []const u8, k: Kernel) std.Build.LazyPath { +fn spirv(b: *std.Build, ocloc: []const u8, device: []const u8, dpas: bool, k: Kernel) std.Build.LazyPath { const run = b.addSystemCommand(&.{ ocloc, "compile", "-device", device, "-spv_only", "-output", k.name, "-output_no_suffix", "-q" }); // ocloc builds from a copy of the source, so the header directory is named (the Run step's cwd is the build root) - if (k.options.len > 0) run.addArgs(&.{ "-options", if (k.include) b.fmt("{s} -I zig/kernels/xpu", .{k.options}) else k.options }); + const options = if (dpas) k.options else b.fmt("{s} -DTF_XPU_NO_DPAS", .{k.options}); // Gen12LP: the kernels' plain fallbacks + if (options.len > 0) run.addArgs(&.{ "-options", if (k.include) b.fmt("{s} -I zig/kernels/xpu", .{options}) else options }); _ = k.include; run.addArg("-file"); run.addFileArg(b.path(b.fmt("zig/kernels/xpu/{s}.cl", .{k.src orelse k.name}))); @@ -236,6 +239,12 @@ fn spirv(b: *std.Build, ocloc: []const u8, device: []const u8, k: Kernel) std.Bu return out.path(b, b.fmt("{s}.spv", .{k.name})); } +/// Whether ocloc's `device` has DPAS (XMX): not Gen12LP (Tiger Lake, Rocket Lake, Alder Lake, DG1). +fn hasDpas(device: []const u8) bool { + for ([_][]const u8{ "tgllp", "rkl", "adl", "dg1", "gen12lp" }) |p| if (std.mem.startsWith(u8, device, p)) return false; + return true; +} + /// 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", "memory", @@ -267,7 +276,7 @@ fn nemotronSteps(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std. /// Host unit tests of the Intel GPU runtime, the Nemotron family on it and its CLI (no GPU, no ocloc), on any host. pub fn hostTests(b: *std.Build, step: *std.Build.Step) void { const host = b.graph.host; - const xpu = runtime(b, host, .debug, &.{}, zeModule(b, host, .debug)); + const xpu = runtime(b, host, .debug, &.{}, zeModule(b, host, .debug), true); const lanes = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = host, .optimize = .debug, .link_libc = true }); const mods = family(b, host, .debug, xpu, lanes); const ze = xpu.import_table.get("ze").?; diff --git a/zig/kernels/xpu/nem_attn_dec.cl b/zig/kernels/xpu/nem_attn_dec.cl index 29a6b4a4f08..3b53f42a21c 100644 --- a/zig/kernels/xpu/nem_attn_dec.cl +++ b/zig/kernels/xpu/nem_attn_dec.cl @@ -1,4 +1,6 @@ +#ifndef TF_XPU_NO_DPAS #pragma OPENCL EXTENSION cl_intel_subgroup_matrix_multiply_accumulate : enable +#endif #pragma OPENCL EXTENSION cl_intel_subgroups_short : enable // Nemotron-H decode attention: split-K partials of 512 keys on DPAS, merged by attn_merge / attn_merge_r. #define HD 128 @@ -13,6 +15,21 @@ inline ushort to_bf(float f) { } inline float bfr(float f) { return bf(to_bf(f)); } +#ifdef TF_XPU_NO_DPAS +// Gen12LP (Iris Xe, DG1) has no DPAS: the same 8x16 by 16x16 bf16 product, A's (m, k) from lane k, fp32 FMAs. +inline float8 mad_k16(short8 a, int8 b, float8 acc) { +#pragma unroll + for (int k = 0; k < 16; k++) { + const uint pair = (uint)b[k >> 1]; + const float bk = bf((ushort)((k & 1) ? (pair >> 16) : (pair & 0xffffu))); +#pragma unroll + for (int m = 0; m < 8; m++) acc[m] = fma(bf((ushort)intel_sub_group_shuffle(a[m], (uint)k)), bk, acc[m]); + } + return acc; +} +#define intel_sub_group_bf16_bf16_matrix_mad_k16 mad_k16 +#endif + // Grid (NKV * 2 head halves, chunks, rows z), 128 items; q [row][32][128] bf16, kc/vc [pos][2][128]; len0 + z keys. __attribute__((reqd_work_group_size(128, 1, 1))) __attribute__((intel_reqd_sub_group_size(16))) __kernel void nem_attn_dec_partial(__global const ushort *q, __global const ushort *kc, __global const ushort *vc, __global float *po, __global float *pm, __global float *pl, diff --git a/zig/src/families/nemotron/xpu_model.zig b/zig/src/families/nemotron/xpu_model.zig index fe01969a091..2331b037110 100644 --- a/zig/src/families/nemotron/xpu_model.zig +++ b/zig/src/families/nemotron/xpu_model.zig @@ -9,7 +9,8 @@ const spv_basic = @import("xpu").kernels.basic; const spv_mamba = @import("xpu").kernels.mamba; const spv_moe = @import("xpu").kernels.moe; const spv_attn = @import("xpu").kernels.attn; -const spv_attn_dec = @import("xpu").kernels.nem_attn_dec; +const kernels_mod = @import("xpu").kernels; +const spv_attn_dec = kernels_mod.nem_attn_dec; const spv_glue = @import("xpu").kernels.glue; const Buf = rt.Buffer; @@ -144,6 +145,7 @@ pub const Model = struct { var em = try r.module(spv_moe); var am = try r.module(spv_attn); var gm = try r.module(spv_glue); + const dec_shape = cfg.head_dim == 128 and cfg.num_attention_heads == 32 and cfg.num_key_value_heads == 2; // nem_attn_dec const k: Kernels = .{ .embed = try bm.kernel("embed4", .{ 64, 1, 1 }), .embed_tok = try bm.kernel("embed4_tok", .{ 64, 1, 1 }), @@ -167,11 +169,12 @@ pub const Model = struct { .a_qkv = try am.kernel("qkv4_bf", .{ 64, 1, 1 }), .a_part = try am.kernel("attn_partial", .{ 256, 1, 1 }), .a_merge = try am.kernel("attn_merge", .{ 16, 1, 1 }), - .a_dec = if (cfg.head_dim == 128 and cfg.num_attention_heads == 32 and cfg.num_key_value_heads == 2) blk: { + // without DPAS attn_partial beats nem_attn_dec's fallback (12.0 against 57.3 ms at 131k keys, Iris Xe) + .a_dec = if (dec_shape and kernels_mod.dpas) blk: { var dm = try r.moduleWith(spv_attn_dec, "-cl-intel-256-GRF-per-thread"); break :blk try dm.kernel("nem_attn_dec_partial", .{ 128, 1, 1 }); } else null, - .a_dmerge = if (cfg.head_dim == 128 and cfg.num_attention_heads == 32 and cfg.num_key_value_heads == 2) blk: { + .a_dmerge = if (dec_shape and kernels_mod.dpas) blk: { var dm = try r.moduleWith(spv_attn_dec, "-cl-intel-256-GRF-per-thread"); break :blk try dm.kernel("nem_attn_dec_merge", .{ 256, 1, 1 }); } else null, diff --git a/zig/src/xpu/kernels.zig b/zig/src/xpu/kernels.zig index 346221e2153..14b3df70176 100644 --- a/zig/src/xpu/kernels.zig +++ b/zig/src/xpu/kernels.zig @@ -10,6 +10,8 @@ fn Blob(comptime import_name: []const u8) type { /// False in host-only builds (no -Dxpu); every image is then empty. pub const available = options.with_kernels; +/// The device has DPAS (-Dxpu-dpas); without it DPAS kernels build from fallbacks and models route around them. +pub const dpas = options.dpas; pub const basic: []const u8 = if (available) &Blob("spv_basic").bytes else &.{}; pub const qmv4: []const u8 = if (available) &Blob("spv_qmv4").bytes else &.{};