2023-05-15 09:36:41 +00:00
|
|
|
const builtin = @import("builtin");
|
2024-01-16 14:13:45 +00:00
|
|
|
|
2025-08-29 11:03:59 +00:00
|
|
|
const async = @import("async");
|
2023-05-15 09:36:41 +00:00
|
|
|
const c = @import("c");
|
2024-01-16 14:13:45 +00:00
|
|
|
const pjrt = @import("pjrt");
|
2025-04-03 11:57:46 +00:00
|
|
|
const bazel_builtin = @import("bazel_builtin");
|
|
|
|
|
const std = @import("std");
|
|
|
|
|
const stdx = @import("stdx");
|
|
|
|
|
const runfiles = @import("runfiles");
|
|
|
|
|
|
|
|
|
|
const log = std.log.scoped(.@"zml/runtime/cpu");
|
2023-05-15 09:36:41 +00:00
|
|
|
|
|
|
|
|
pub fn isEnabled() bool {
|
|
|
|
|
return @hasDecl(c, "ZML_RUNTIME_CPU");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load() !*const pjrt.Api {
|
|
|
|
|
if (comptime !isEnabled()) {
|
|
|
|
|
return error.Unavailable;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 11:57:46 +00:00
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
|
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
|
|
var r_ = try runfiles.Runfiles.create(.{ .allocator = arena.allocator() }) orelse {
|
|
|
|
|
stdx.debug.panic("Unable to find runfiles", .{});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const source_repo = bazel_builtin.current_repository;
|
|
|
|
|
const r = r_.withSourceRepo(source_repo);
|
|
|
|
|
|
|
|
|
|
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
|
|
|
|
|
const sandbox_path = try r.rlocation("zml/runtimes/cpu/sandbox/lib", &path_buf) orelse {
|
|
|
|
|
log.err("Failed to find sandbox path for CPU runtime", .{});
|
|
|
|
|
return error.FileNotFound;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return blk: {
|
|
|
|
|
const ext = switch (builtin.os.tag) {
|
|
|
|
|
.windows => ".dll",
|
|
|
|
|
.macos, .ios, .watchos => ".dylib",
|
|
|
|
|
else => ".so",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var lib_path_buf: [std.fs.max_path_bytes]u8 = undefined;
|
|
|
|
|
const path = try stdx.fs.path.bufJoinZ(&lib_path_buf, &.{ sandbox_path, "libpjrt_cpu" ++ ext });
|
2025-08-29 11:03:59 +00:00
|
|
|
break :blk async.callBlocking(pjrt.Api.loadFrom, .{path});
|
2023-05-15 09:36:41 +00:00
|
|
|
};
|
|
|
|
|
}
|