2023-08-18 17:11:27 +00:00
|
|
|
const std = @import("std");
|
2025-07-15 15:26:03 +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-08-18 17:11:27 +00:00
|
|
|
const bazel_builtin = @import("bazel_builtin");
|
2024-01-16 14:13:45 +00:00
|
|
|
const c = @import("c");
|
|
|
|
|
const pjrt = @import("pjrt");
|
|
|
|
|
const runfiles = @import("runfiles");
|
2025-05-19 17:35:33 +00:00
|
|
|
const stdx = @import("stdx");
|
|
|
|
|
|
|
|
|
|
const log = std.log.scoped(.@"zml/runtime/neuron");
|
2023-08-18 17:11:27 +00:00
|
|
|
|
|
|
|
|
pub fn isEnabled() bool {
|
|
|
|
|
return @hasDecl(c, "ZML_RUNTIME_NEURON");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hasNeuronDevice() bool {
|
2025-08-29 11:03:59 +00:00
|
|
|
async.File.access("/dev/neuron0", .{ .mode = .read_only }) catch return false;
|
2023-08-18 17:11:27 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn isRunningOnEC2() !bool {
|
|
|
|
|
const AmazonEC2 = "Amazon EC2";
|
|
|
|
|
|
2025-08-29 11:03:59 +00:00
|
|
|
var f = try async.File.open("/sys/devices/virtual/dmi/id/sys_vendor", .{ .mode = .read_only });
|
2023-08-18 17:11:27 +00:00
|
|
|
defer f.close() catch {};
|
|
|
|
|
|
2025-08-07 15:09:27 +00:00
|
|
|
var content: [AmazonEC2.len]u8 = undefined;
|
|
|
|
|
const n_read = try f.pread(&content, 0);
|
2023-08-18 17:11:27 +00:00
|
|
|
|
2025-08-07 15:09:27 +00:00
|
|
|
return std.mem.eql(u8, content[0..n_read], AmazonEC2);
|
2023-08-18 17:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load() !*const pjrt.Api {
|
|
|
|
|
if (comptime !isEnabled()) {
|
|
|
|
|
return error.Unavailable;
|
|
|
|
|
}
|
|
|
|
|
if (comptime builtin.os.tag != .linux) {
|
|
|
|
|
return error.Unavailable;
|
|
|
|
|
}
|
|
|
|
|
if (!(isRunningOnEC2() catch false)) {
|
|
|
|
|
return error.Unavailable;
|
|
|
|
|
}
|
|
|
|
|
if (!hasNeuronDevice()) {
|
|
|
|
|
return error.Unavailable;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-07 15:09:27 +00:00
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
|
2025-05-19 17:35:33 +00:00
|
|
|
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);
|
|
|
|
|
|
2025-07-15 15:26:03 +00:00
|
|
|
var sandbox_path_buf: [std.fs.max_path_bytes]u8 = undefined;
|
|
|
|
|
const sandbox_path = try r.rlocation("libpjrt_neuron/sandbox", &sandbox_path_buf) orelse {
|
2025-05-19 17:35:33 +00:00
|
|
|
log.err("Failed to find sandbox path for NEURON runtime", .{});
|
|
|
|
|
return error.FileNotFound;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return blk: {
|
|
|
|
|
var lib_path_buf: [std.fs.max_path_bytes]u8 = undefined;
|
|
|
|
|
const path = try stdx.fs.path.bufJoinZ(&lib_path_buf, &.{ sandbox_path, "lib", "libpjrt_neuron.so" });
|
2025-08-29 11:03:59 +00:00
|
|
|
break :blk async.callBlocking(pjrt.Api.loadFrom, .{path});
|
2025-05-19 17:35:33 +00:00
|
|
|
};
|
2023-08-18 17:11:27 +00:00
|
|
|
}
|