Skip to content

Commit b8dc996

Browse files
committed
update flake to support metal on m1/m2
1 parent 2d7bf11 commit b8dc996

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

flake.lock

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
outputs = { self, nixpkgs, flake-utils }:
77
flake-utils.lib.eachDefaultSystem (system:
88
let
9+
inherit (pkgs.stdenv) isAarch64 isDarwin;
10+
inherit (pkgs.lib) optionals;
11+
isM1 = isAarch64 && isDarwin;
12+
osSpecific = with pkgs.darwin.apple_sdk_11_0.frameworks; (optionals isDarwin [ Accelerate ]) ++ (optionals isM1 [ MetalKit MetalPerformanceShaders MetalPerformanceShadersGraph ]);
913
pkgs = import nixpkgs {
1014
inherit system;
1115
};
@@ -18,17 +22,22 @@
1822
packages.default = pkgs.stdenv.mkDerivation {
1923
name = "llama.cpp";
2024
src = ./.;
25+
postPatch =
26+
if isM1 then ''
27+
substituteInPlace ./ggml-metal.m \
28+
--replace '[[NSBundle mainBundle] pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/ggml-metal.metal\";"
29+
'' else "";
2130
nativeBuildInputs = with pkgs; [ cmake ];
22-
buildInputs = with pkgs; lib.optionals stdenv.isDarwin [
23-
darwin.apple_sdk.frameworks.Accelerate
24-
];
25-
cmakeFlags = with pkgs; lib.optionals (system == "aarch64-darwin") [
31+
buildInputs = osSpecific;
32+
cmakeFlags = [ "-DLLAMA_BUILD_SERVER=ON" ] ++ (optionals isM1 [
2633
"-DCMAKE_C_FLAGS=-D__ARM_FEATURE_DOTPROD=1"
27-
];
34+
"-DLLAMA_METAL=ON"
35+
]);
2836
installPhase = ''
2937
mkdir -p $out/bin
3038
mv bin/* $out/bin/
3139
mv $out/bin/main $out/bin/llama
40+
mv $out/bin/server $out/bin/llama-server
3241
3342
echo "#!${llama-python}/bin/python" > $out/bin/convert.py
3443
cat ${./convert.py} >> $out/bin/convert.py
@@ -40,9 +49,7 @@
4049
packages = with pkgs; [
4150
cmake
4251
llama-python
43-
] ++ lib.optionals stdenv.isDarwin [
44-
darwin.apple_sdk.frameworks.Accelerate
45-
];
52+
] ++ osSpecific;
4653
};
4754
}
4855
);

0 commit comments

Comments
 (0)