Skip to content

Commit

Permalink
add support for metal
Browse files Browse the repository at this point in the history
  • Loading branch information
cztomsik committed Aug 2, 2023
1 parent 2840d18 commit 256c726
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
18 changes: 16 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ pub fn build(b: *std.Build) !void {
lib.linker_allow_shlib_undefined = true;

// ggml
// lib.linkFramework("Accelerate");
// lib.defineCMacroRaw("GGML_USE_ACCELERATE=1");
lib.addIncludePath(.{ .path = "deps/ggml/include/ggml" });
lib.addIncludePath(.{ .path = "deps/ggml/src" });
lib.addCSourceFile(.{ .file = .{ .path = "deps/ggml/src/ggml.c" }, .flags = &.{ "-std=c11", "-pthread" } });

// Use Metal on macOS
if (target.getOsTag() == .macos) {
lib.defineCMacroRaw("GGML_USE_METAL");
lib.defineCMacroRaw("GGML_METAL_NDEBUG");
lib.addCSourceFiles(&.{"deps/ggml/src/ggml-metal.m"}, &.{"-std=c11"});
lib.linkFramework("Foundation");
lib.linkFramework("Metal");
lib.linkFramework("MetalKit");
lib.linkFramework("MetalPerformanceShaders");

// copy the *.metal file so that it can be loaded at runtime
const copy_metal_step = b.addInstallLibFile(.{ .path = "deps/ggml/src/ggml-metal.metal" }, "ggml-metal.metal");
b.getInstallStep().dependOn(&copy_metal_step.step);
}

const napigen = b.createModule(.{ .source_file = .{ .path = "deps/napigen/napigen.zig" } });
lib.addModule("napigen", napigen);

Expand Down
5 changes: 5 additions & 0 deletions src/ggml.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const std = @import("std");
const builtin = @import("builtin");

const ggml = @cImport({
// NEON is supported, this is just for the @cImport
@cUndef("__ARM_NEON");
@cDefine("GGML_ASSERT(x)", "(x || abort())");
@cInclude("ggml.h");

if (builtin.os.tag == .macos) {
@cInclude("ggml-metal.h");
}
});

// re-export everything
Expand Down
5 changes: 5 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");
const napigen = @import("napigen");
const ggml = @import("ggml.zig");
const safetensors = @import("safetensors.zig");
Expand All @@ -12,6 +13,10 @@ comptime {
}

fn initModule(js: *napigen.JsContext, exports: napigen.napi_value) napigen.Error!napigen.napi_value {
if (builtin.os.tag == .macos) {
_ = ggml.ggml_metal_init(1);
}

@setEvalBranchQuota(100_000);
inline for (comptime std.meta.declarations(ggml)) |d| {
if (comptime !std.ascii.startsWithIgnoreCase(d.name, "ggml_") or
Expand Down

0 comments on commit 256c726

Please sign in to comment.