diff --git a/platforms/oneapi/libzml_oneapi.BUILD.bazel b/platforms/oneapi/libzml_oneapi.BUILD.bazel index eb08e652..1011e7b6 100644 --- a/platforms/oneapi/libzml_oneapi.BUILD.bazel +++ b/platforms/oneapi/libzml_oneapi.BUILD.bazel @@ -47,6 +47,12 @@ patchelf( soname = "libze_intel_gpu.so.1", ) +patchelf( + name = "libigdrcl_so", + src = "@intel-opencl-icd//:libigdrcl_so", + set_rpath = "$ORIGIN", +) + patchelf( name = "libigc_so_2", src = "@libigc2//:libigc_so_2", @@ -95,6 +101,7 @@ copy_to_directory( ":libiga64_so_2", ":libigc_so_2", ":libigdfcl_so_2", + ":libigdrcl_so", ":libsycl_so", ":libtbb_so_12", ":libze_intel_gpu_so", @@ -128,6 +135,7 @@ copy_to_directory( replace_prefixes = { "libiga64_so_2": "lib", "libigdfcl_so_2": "lib", + "libigdrcl_so": "lib", "libigc_so_2": "lib", "libdnnl_so_3": "lib", "libzml_oneapi_so": "lib", diff --git a/platforms/oneapi/oneapi.bzl b/platforms/oneapi/oneapi.bzl index a3c968d3..642793fb 100644 --- a/platforms/oneapi/oneapi.bzl +++ b/platforms/oneapi/oneapi.bzl @@ -1,8 +1,9 @@ load("@llvm//:http_bsdtar_archive.bzl", http_archive = "http_bsdtar_archive") load("//bazel:http_deb_archive.bzl", "http_deb_archive") -_PLUGIN_ONEAPI_URL = "https://mirror.zml.ai/plugins/202611132003.375.1.7d765f57a407/zml-oneapi-linux-amd64.tar.zst" -_PLUGIN_ONEAPI_SHA256 = "294722670832c543c8ac4c074c30f636f2a3b0854820f7c5a070fa82c0d6f3d5" +# Local XLA 806111081c build: Intel/FP8 fixes and eager kernel compilation. +_PLUGIN_ONEAPI_URL = "file:///home/steeve/.cache/zml/plugins/zml-oneapi-preload-592f199ad2a8.tar.zst" +_PLUGIN_ONEAPI_SHA256 = "592f199ad2a8a7ee1ac2987b00e96f976f85c10c25a2f5cc4ddbc32e1d6fed67" ONEAPI_VERSION = "2026.1" @@ -40,6 +41,11 @@ def _read_packages(mctx, labels): return ret _UBUNTU_PACKAGES = { + "intel-opencl-icd": """ +filegroup( + name = "libigdrcl_so", + srcs = ["lib/x86_64-linux-gnu/intel-opencl/libigdrcl.so"], +)""", "libnl-3-200": """ filegroup( name = "libnl_3", diff --git a/platforms/oneapi/oneapi.zig b/platforms/oneapi/oneapi.zig index c5654741..e08bbcbd 100644 --- a/platforms/oneapi/oneapi.zig +++ b/platforms/oneapi/oneapi.zig @@ -28,7 +28,7 @@ fn hasOneApiDevice(io: std.Io) bool { return false; } -fn setupOneAPIEnv(driver_path: [:0]const u8) void { +fn setupOneAPIEnv(driver_path: [:0]const u8, opencl_driver_path: [:0]const u8) void { _ = c.setenv("CCL_LOG_LEVEL", c.getenv("CCL_LOG_LEVEL") orelse "error", 1); _ = c.setenv("CCL_ATL_TRANSPORT", "ofi", 1); _ = c.setenv("FI_PROVIDER", "shm", 1); @@ -38,6 +38,8 @@ fn setupOneAPIEnv(driver_path: [:0]const u8) void { _ = c.setenv("XLA_ONECCL_COLLECTIVE_PERMUTE_BYPASS_SYCL_P2P", "1", 1); _ = c.setenv("ONEAPI_DEVICE_SELECTOR", "level_zero:*", 0); _ = c.setenv("ZE_ENABLE_ALT_DRIVERS", driver_path, 0); + // oneDNN also needs the matching OpenCL device for some Level Zero kernels. + _ = c.setenv("OCL_ICD_FILENAMES", opencl_driver_path, 0); } pub fn load(_: std.mem.Allocator, io: std.Io) !*const pjrt.Api { @@ -63,7 +65,9 @@ pub fn load(_: std.mem.Allocator, io: std.Io) !*const pjrt.Api { var driver_path_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; const driver_path = try stdx.Io.Dir.path.bufJoinZ(&driver_path_buf, &.{ sandbox_path, "lib", "libze_intel_gpu.so.1" }); - setupOneAPIEnv(driver_path); + var opencl_driver_path_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const opencl_driver_path = try stdx.Io.Dir.path.bufJoinZ(&opencl_driver_path_buf, &.{ sandbox_path, "lib", "libigdrcl.so" }); + setupOneAPIEnv(driver_path, opencl_driver_path); return blk: { var lib_path_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; diff --git a/platforms/oneapi/packages.lock.json b/platforms/oneapi/packages.lock.json index 140ce102..ec9cd263 100644 --- a/platforms/oneapi/packages.lock.json +++ b/platforms/oneapi/packages.lock.json @@ -242,6 +242,17 @@ ], "version": "26.31.39395.14-1~24.04~ppa1" }, + { + "arch": "amd64", + "dependencies": [], + "key": "intel-opencl-icd_26.31.39395.14-1_24.04_ppa1_amd64", + "name": "intel-opencl-icd", + "sha256": "6e79028431b03f98a77d0f0ba56750fcef48cdabbe2b0cd8361d9f03cafb9433", + "urls": [ + "https://snapshot.ppa.launchpadcontent.net/kobuk-team/intel-graphics/ubuntu/20260930T000000Z/pool/main/i/intel-compute-runtime/intel-opencl-icd_26.31.39395.14-1~24.04~ppa1_amd64.deb" + ], + "version": "26.31.39395.14-1~24.04~ppa1" + }, { "arch": "amd64", "dependencies": [], diff --git a/platforms/oneapi/packages.yaml b/platforms/oneapi/packages.yaml index 5a829ac5..d46880d5 100644 --- a/platforms/oneapi/packages.yaml +++ b/platforms/oneapi/packages.yaml @@ -49,5 +49,6 @@ packages: - "libigc2 (= 2.41.15-1~24.04)" - "libigdfcl2 (= 2.41.15-1~24.04)" - "libze-intel-gpu1 (= 26.31.39395.14-1~24.04~ppa1)" + - "intel-opencl-icd (= 26.31.39395.14-1~24.04~ppa1)" - "libze1" - "zlib1g" diff --git a/zml/BUILD.bazel b/zml/BUILD.bazel index f52ecdd5..afb83147 100644 --- a/zml/BUILD.bazel +++ b/zml/BUILD.bazel @@ -197,3 +197,9 @@ zig_test( name = "test", deps = ["//zml"], ) + +zig_test( + name = "mem_test", + main = "mem_test.zig", + deps = ["//zml"], +) diff --git a/zml/io.zig b/zml/io.zig index ab8d8e6e..d73de04b 100644 --- a/zml/io.zig +++ b/zml/io.zig @@ -340,7 +340,7 @@ pub const Loader = struct { const dma_allocators = try allocator.alloc(mem.DmaAllocator, pool_count); errdefer allocator.free(dma_allocators); for (platform.devices, 0..) |*device, i| { - dma_allocators[i] = .init(allocator, device); + dma_allocators[i] = .init(std.heap.page_allocator, device); } const buffer_pools = try allocator.alloc(mem.DynamicBufferPool, pool_count); diff --git a/zml/mem.zig b/zml/mem.zig index a64ae968..8df4a736 100644 --- a/zml/mem.zig +++ b/zml/mem.zig @@ -230,7 +230,6 @@ pub const DynamicBufferPool = struct { block_size: usize, max_blocks: usize, - arena_state: std.heap.ArenaAllocator.State = .{}, free_stack: std.atomic.Value(?*Node) = .init(null), in_flight: std.atomic.Value(usize) = .init(0), mutex: std.Io.Mutex = .init, @@ -243,8 +242,13 @@ pub const DynamicBufferPool = struct { }; } + /// All users must have stopped and returned their buffers before teardown. pub fn deinit(self: *DynamicBufferPool, allocator: std.mem.Allocator) void { - self.arena_state.promote(allocator).deinit(); + assert(self.in_flight.load(.acquire) == 0); + while (self.pop()) |node| { + const buffer: []align(@alignOf(Node)) u8 = @alignCast(self.nodeToSlice(node)); + allocator.free(buffer); + } } pub fn get(self: *DynamicBufferPool, allocator: std.mem.Allocator, io: std.Io) ![]u8 { @@ -263,12 +267,8 @@ pub const DynamicBufferPool = struct { self.mutex.lockUncancelable(io); defer self.mutex.unlock(io); - var arena = self.arena_state.promote(allocator); - defer self.arena_state = arena.state; - errdefer _ = self.in_flight.fetchSub(1, .release); - const buffer = try arena.allocator().alignedAlloc(u8, alignment, self.block_size); - return @alignCast(buffer); + return try allocator.alignedAlloc(u8, alignment, self.block_size); } self.mutex.lockUncancelable(io); diff --git a/zml/mem_test.zig b/zml/mem_test.zig new file mode 100644 index 00000000..24be9e38 --- /dev/null +++ b/zml/mem_test.zig @@ -0,0 +1,46 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; + +const zml = @import("zml"); +const DynamicBufferPool = zml.mem.DynamicBufferPool; + +test "DynamicBufferPool reuses and frees buffers" { + const allocator = std.testing.allocator; + const io = std.testing.io; + var pool: DynamicBufferPool = .init(2, 256); + defer pool.deinit(allocator); + + const first_ptr = blk: { + const first = try pool.get(allocator, io); + defer pool.put(io, first); + const second = try pool.get(allocator, io); + defer pool.put(io, second); + try std.testing.expectEqual(@as(usize, 256), first.len); + try std.testing.expect(first.ptr != second.ptr); + @memset(first, 0xa5); + @memset(second, 0x5a); + break :blk first.ptr; + }; + const reused = try pool.get(allocator, io); + defer pool.put(io, reused); + try std.testing.expectEqual(first_ptr, reused.ptr); +} + +test "DynamicBufferPool cleans up allocation failures" { + try std.testing.checkAllAllocationFailures(std.testing.allocator, struct { + fn run(allocator: Allocator) !void { + var pool: DynamicBufferPool = .init(2, 256); + defer pool.deinit(allocator); + const first = pool.get(allocator, std.testing.io) catch |err| { + try std.testing.expectEqual(@as(usize, 0), pool.in_flight.load(.acquire)); + return err; + }; + defer pool.put(std.testing.io, first); + const second = pool.get(allocator, std.testing.io) catch |err| { + try std.testing.expectEqual(@as(usize, 1), pool.in_flight.load(.acquire)); + return err; + }; + defer pool.put(std.testing.io, second); + } + }.run, .{}); +}