Skip to content

Commit 5b57a5b

Browse files
authored
flake : update to support metal on m1/m2 (#1724)
1 parent 4dc62c5 commit 5b57a5b

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-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: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
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 =
13+
if isM1 then with pkgs.darwin.apple_sdk_11_0.frameworks; [ Accelerate MetalKit MetalPerformanceShaders MetalPerformanceShadersGraph ]
14+
else if isDarwin then with pkgs.darwin.apple_sdk.frameworks; [ Accelerate CoreGraphics CoreVideo ]
15+
else [ ];
916
pkgs = import nixpkgs {
1017
inherit system;
1118
};
@@ -18,17 +25,22 @@
1825
packages.default = pkgs.stdenv.mkDerivation {
1926
name = "llama.cpp";
2027
src = ./.;
28+
postPatch =
29+
if isM1 then ''
30+
substituteInPlace ./ggml-metal.m \
31+
--replace '[[NSBundle mainBundle] pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/ggml-metal.metal\";"
32+
'' else "";
2133
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") [
34+
buildInputs = osSpecific;
35+
cmakeFlags = [ "-DLLAMA_BUILD_SERVER=ON" ] ++ (optionals isM1 [
2636
"-DCMAKE_C_FLAGS=-D__ARM_FEATURE_DOTPROD=1"
27-
];
37+
"-DLLAMA_METAL=ON"
38+
]);
2839
installPhase = ''
2940
mkdir -p $out/bin
3041
mv bin/* $out/bin/
3142
mv $out/bin/main $out/bin/llama
43+
mv $out/bin/server $out/bin/llama-server
3244
3345
echo "#!${llama-python}/bin/python" > $out/bin/convert.py
3446
cat ${./convert.py} >> $out/bin/convert.py
@@ -40,9 +52,7 @@
4052
packages = with pkgs; [
4153
cmake
4254
llama-python
43-
] ++ lib.optionals stdenv.isDarwin [
44-
darwin.apple_sdk.frameworks.Accelerate
45-
];
55+
] ++ osSpecific;
4656
};
4757
}
4858
);

0 commit comments

Comments
 (0)