2024-09-10 09:14:28 +00:00
|
|
|
const std = @import("std");
|
2023-01-02 14:28:25 +00:00
|
|
|
const builtin = @import("builtin");
|
2024-09-10 09:14:28 +00:00
|
|
|
|
2023-05-12 11:40:23 +00:00
|
|
|
const c = @import("c");
|
2023-06-21 14:45:14 +00:00
|
|
|
const mlir = @import("mlir");
|
2023-05-12 11:40:23 +00:00
|
|
|
const runfiles = @import("runfiles");
|
2023-05-15 09:36:41 +00:00
|
|
|
const runtimes = @import("runtimes");
|
2023-06-21 14:45:14 +00:00
|
|
|
const stdx = @import("stdx");
|
2023-01-02 14:28:25 +00:00
|
|
|
|
2024-09-10 09:14:28 +00:00
|
|
|
const pjrt = @import("pjrtx.zig");
|
2023-06-21 14:45:14 +00:00
|
|
|
|
2025-08-20 10:27:54 +00:00
|
|
|
const zml = struct {
|
|
|
|
|
pub const callback = @import("callback.zig");
|
|
|
|
|
pub const HostBuffer = @import("hostbuffer.zig").HostBuffer;
|
|
|
|
|
pub const Platform = @import("platform.zig").Platform;
|
|
|
|
|
pub const platform = @import("platform.zig");
|
|
|
|
|
pub const Shape = @import("shape.zig").Shape;
|
|
|
|
|
pub const Target = @import("platform.zig").Target;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const PjrtApiMap = std.EnumArray(zml.Target, ?*const pjrt.Api);
|
|
|
|
|
const PlatformsMap = std.EnumArray(zml.Target, ?zml.Platform);
|
2023-06-21 14:45:14 +00:00
|
|
|
const log = std.log.scoped(.@"zml/context");
|
2023-01-02 14:28:25 +00:00
|
|
|
|
2023-11-06 11:25:57 +00:00
|
|
|
test {
|
|
|
|
|
std.testing.refAllDecls(Context);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 14:28:25 +00:00
|
|
|
/// Every program using ZML must start with a `zml.Context.init(.{});`
|
|
|
|
|
/// The ZML context contains global state to interact with the different
|
|
|
|
|
/// devices available on your system.
|
|
|
|
|
/// Note that the runtimes available depends on how the program was compiled.
|
|
|
|
|
/// For example you need to compile your program with `--//runtimes:cuda=true`
|
|
|
|
|
/// to have the CUDA runtime available.
|
|
|
|
|
pub const Context = struct {
|
|
|
|
|
var apis = PjrtApiMap.initFill(null);
|
|
|
|
|
var apis_once = std.once(struct {
|
|
|
|
|
fn call() void {
|
2023-05-15 09:36:41 +00:00
|
|
|
inline for (comptime std.enums.values(runtimes.Platform)) |t| {
|
|
|
|
|
if (runtimes.load(t)) |api| {
|
|
|
|
|
Context.apis.set(t, api);
|
|
|
|
|
} else |_| {}
|
2023-01-02 14:28:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}.call);
|
|
|
|
|
|
|
|
|
|
var mlir_once = std.once(struct {
|
|
|
|
|
fn call() void {
|
|
|
|
|
mlir.registerPasses("Transforms");
|
|
|
|
|
}
|
|
|
|
|
}.call);
|
|
|
|
|
|
2023-05-12 11:40:23 +00:00
|
|
|
var runfiles_once = std.once(struct {
|
|
|
|
|
fn call_() !void {
|
|
|
|
|
if (std.process.hasEnvVarConstant("RUNFILES_MANIFEST_FILE") or std.process.hasEnvVarConstant("RUNFILES_DIR")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
|
|
|
|
|
const allocator = arena.allocator();
|
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
|
|
var envMap = std.process.EnvMap.init(allocator);
|
|
|
|
|
var r = (try runfiles.Runfiles.create(.{ .allocator = allocator })) orelse return;
|
|
|
|
|
try r.environment(&envMap);
|
|
|
|
|
|
|
|
|
|
var it = envMap.iterator();
|
|
|
|
|
while (it.next()) |entry| {
|
|
|
|
|
const keyZ = try allocator.dupeZ(u8, entry.key_ptr.*);
|
|
|
|
|
const valueZ = try allocator.dupeZ(u8, entry.value_ptr.*);
|
|
|
|
|
_ = c.setenv(keyZ.ptr, valueZ.ptr, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn call() void {
|
|
|
|
|
call_() catch @panic("Unable to init runfiles env");
|
|
|
|
|
}
|
|
|
|
|
}.call);
|
|
|
|
|
|
2023-01-02 14:28:25 +00:00
|
|
|
platforms: PlatformsMap,
|
|
|
|
|
|
|
|
|
|
/// Creates a ZML Context and returns it.
|
|
|
|
|
pub fn init() !Context {
|
2023-05-12 11:40:23 +00:00
|
|
|
Context.runfiles_once.call();
|
2023-01-02 14:28:25 +00:00
|
|
|
Context.apis_once.call();
|
|
|
|
|
Context.mlir_once.call();
|
|
|
|
|
|
2023-02-14 13:52:49 +00:00
|
|
|
var num_platforms: u8 = 0;
|
2023-11-13 12:45:17 +00:00
|
|
|
for (Context.apis.values) |api| {
|
|
|
|
|
if (api != null) num_platforms += 1;
|
2023-01-02 14:28:25 +00:00
|
|
|
}
|
2023-09-04 13:34:37 +00:00
|
|
|
if (num_platforms == 0) {
|
|
|
|
|
log.err("No platform available", .{});
|
|
|
|
|
return error.NoPlatformAvailable;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 12:45:17 +00:00
|
|
|
return .{ .platforms = PlatformsMap.initFill(null) };
|
2023-01-02 14:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-20 10:27:54 +00:00
|
|
|
fn platformToLibrary(comptime target: zml.Target) []const u8 {
|
2023-01-02 14:28:25 +00:00
|
|
|
const ext = switch (builtin.os.tag) {
|
|
|
|
|
.windows => ".dll",
|
|
|
|
|
.macos, .ios, .watchos => ".dylib",
|
|
|
|
|
else => ".so",
|
|
|
|
|
};
|
|
|
|
|
return switch (target) {
|
|
|
|
|
inline else => "libpjrt_" ++ @tagName(target) ++ ext,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 10:27:54 +00:00
|
|
|
pub fn pjrtApi(target: zml.Target) *const pjrt.Api {
|
2023-01-02 14:28:25 +00:00
|
|
|
return Context.apis.get(target).?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn deinit(self: *Context) void {
|
|
|
|
|
var iterator = self.platforms.iterator();
|
|
|
|
|
while (iterator.next()) |entry| {
|
|
|
|
|
if (entry.value.*) |*p| {
|
|
|
|
|
p.deinit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.* = undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 10:27:54 +00:00
|
|
|
const prefered_targets = [_]zml.Target{ .tpu, .neuron, .cuda, .rocm, .cpu };
|
2023-11-13 12:45:17 +00:00
|
|
|
|
2023-01-02 14:28:25 +00:00
|
|
|
/// Automatically selects the best Platform loaded in the current Context.
|
|
|
|
|
///
|
|
|
|
|
/// For example, if supported, this will select a platform corresponding to an accelerator (GPU, TPU, ...).
|
2025-08-20 10:27:54 +00:00
|
|
|
pub fn autoPlatform(self: *Context, opts: zml.Platform.CreateOptions) zml.Platform {
|
2023-11-13 12:45:17 +00:00
|
|
|
stdx.debug.assert(prefered_targets.len == apis.values.len, "New target need to be inserted inside `zml.Context.preferred_targets`", .{});
|
|
|
|
|
|
|
|
|
|
return self.platformByPreferences(opts, &prefered_targets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Given a list of preferred targets to select the best Platform
|
|
|
|
|
///
|
|
|
|
|
/// For example, if supported, this will select a platform corresponding to an accelerator (GPU, TPU, ...).
|
2025-08-20 10:27:54 +00:00
|
|
|
pub fn platformByPreferences(self: *Context, opts: zml.Platform.CreateOptions, prefered: []const zml.Target) zml.Platform {
|
2023-11-13 12:45:17 +00:00
|
|
|
// Try prefered targets.
|
|
|
|
|
for (prefered) |target| {
|
|
|
|
|
if (apis.get(target) == null) continue;
|
|
|
|
|
return self.platform(target, opts) catch |err| {
|
|
|
|
|
log.err("Failed to load platform .{s}: {}", .{ @tagName(target), err });
|
|
|
|
|
continue;
|
|
|
|
|
};
|
2023-01-02 14:28:25 +00:00
|
|
|
}
|
2023-11-13 12:45:17 +00:00
|
|
|
|
|
|
|
|
// Try unlisted targets
|
|
|
|
|
var it = Context.apis.iterator();
|
|
|
|
|
while (it.next()) |entry| {
|
|
|
|
|
const target = entry.key;
|
|
|
|
|
// CPU should only be use as fallback.
|
|
|
|
|
if (target == .cpu) continue;
|
|
|
|
|
if (entry.value.* == null) continue;
|
2025-08-20 10:27:54 +00:00
|
|
|
if (std.mem.indexOfScalar(zml.Target, prefered, target) != null) continue;
|
2023-11-13 12:45:17 +00:00
|
|
|
return self.platform(target, opts) catch |err| {
|
|
|
|
|
log.err("Failed to load platform .{s}: {}", .{ @tagName(target), err });
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Finally fallback to cpu.
|
|
|
|
|
return self.platform(.cpu, opts) catch {
|
|
|
|
|
log.err("No platform available", .{});
|
|
|
|
|
@panic("No platform available !");
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 10:27:54 +00:00
|
|
|
pub fn platform(self: *Context, target: zml.Target, opts: zml.Platform.CreateOptions) !zml.Platform {
|
2023-11-13 12:45:17 +00:00
|
|
|
if (self.platforms.get(target)) |p| {
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
const api = Context.apis.get(target);
|
|
|
|
|
if (api == null) return error.PlatformNotCompiled;
|
2025-08-20 10:27:54 +00:00
|
|
|
const p = try zml.Platform.init(target, api.?, opts);
|
2023-11-13 12:45:17 +00:00
|
|
|
if (p.getDevices().len == 0) {
|
|
|
|
|
log.err("No device found for platform {} !", .{target});
|
|
|
|
|
return error.NoDevicesFound;
|
|
|
|
|
}
|
2024-09-10 09:14:28 +00:00
|
|
|
|
2023-11-13 12:45:17 +00:00
|
|
|
self.platforms.set(target, p);
|
2025-08-20 10:27:54 +00:00
|
|
|
try zml.callback.registerInternalCallbacks(p);
|
|
|
|
|
|
2023-11-13 12:45:17 +00:00
|
|
|
return p;
|
2023-01-02 14:28:25 +00:00
|
|
|
}
|
2023-06-05 13:42:45 +00:00
|
|
|
|
2025-08-20 10:27:54 +00:00
|
|
|
pub fn printAvailablePlatforms(self: Context, selected: zml.Platform) void {
|
2023-06-21 14:45:14 +00:00
|
|
|
// List available targets
|
|
|
|
|
log.info("Available Platforms:", .{});
|
|
|
|
|
const selected_prefix = "✅";
|
|
|
|
|
const not_selected_prefix = "• ";
|
|
|
|
|
const selected_postfix = "(AUTO-SELECTED)";
|
|
|
|
|
const not_selected_postfix = "";
|
|
|
|
|
|
2025-08-20 10:27:54 +00:00
|
|
|
for (zml.platform.available_targets) |target| {
|
2023-06-21 14:45:14 +00:00
|
|
|
log.info(" {s} {s} {s}", .{
|
|
|
|
|
if (target == selected.target) selected_prefix else not_selected_prefix,
|
|
|
|
|
@tagName(target),
|
|
|
|
|
if (target == selected.target) selected_postfix else not_selected_postfix,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// now the platform's devices
|
|
|
|
|
if (self.platforms.get(target)) |pfm| {
|
|
|
|
|
for (pfm.getDevices(), 0..) |device, index| {
|
|
|
|
|
const deviceKind = device.getDescription(pfm.pjrt_api).getKind(pfm.pjrt_api);
|
|
|
|
|
log.info(" ◦ #{d}: {s}", .{
|
|
|
|
|
index,
|
|
|
|
|
deviceKind,
|
|
|
|
|
});
|
|
|
|
|
// we only list 1 CPU device
|
|
|
|
|
if (target == .cpu) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-28 13:54:28 +00:00
|
|
|
};
|