Skip to content

Commit b82de94

Browse files
josephsthodlen
authored andcommitted
nix: make xcrun visible in Nix sandbox for precompiling Metal shaders (ggml-org#6118)
* Symlink to /usr/bin/xcrun so that `xcrun` binary is usable during build (used for compiling Metal shaders) Fixes ggml-org#6117 * cmake - copy default.metallib to install directory When metal files are compiled to default.metallib, Cmake needs to add this to the install directory so that it's visible to llama-cpp Also, update package.nix to use absolute path for default.metallib (it's not finding the bundle) * add `precompileMetalShaders` flag (defaults to false) to disable precompilation of metal shader Precompilation requires Xcode to be installed and requires disable sandbox on nix-darwin
1 parent d8f64fe commit b82de94

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

.devops/nix/package.nix

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
config,
55
stdenv,
66
mkShell,
7+
runCommand,
78
cmake,
89
ninja,
910
pkg-config,
@@ -35,7 +36,8 @@
3536
# It's necessary to consistently use backendStdenv when building with CUDA support,
3637
# otherwise we get libstdc++ errors downstream.
3738
effectiveStdenv ? if useCuda then cudaPackages.backendStdenv else stdenv,
38-
enableStatic ? effectiveStdenv.hostPlatform.isStatic
39+
enableStatic ? effectiveStdenv.hostPlatform.isStatic,
40+
precompileMetalShaders ? false
3941
}@inputs:
4042

4143
let
@@ -87,6 +89,11 @@ let
8789
]
8890
);
8991

92+
xcrunHost = runCommand "xcrunHost" {} ''
93+
mkdir -p $out/bin
94+
ln -s /usr/bin/xcrun $out/bin
95+
'';
96+
9097
# apple_sdk is supposed to choose sane defaults, no need to handle isAarch64
9198
# separately
9299
darwinBuildInputs =
@@ -150,13 +157,23 @@ effectiveStdenv.mkDerivation (
150157
postPatch = ''
151158
substituteInPlace ./ggml-metal.m \
152159
--replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
160+
substituteInPlace ./ggml-metal.m \
161+
--replace '[bundle pathForResource:@"default" ofType:@"metallib"];' "@\"$out/bin/default.metallib\";"
153162
154163
# TODO: Package up each Python script or service appropriately.
155164
# If we were to migrate to buildPythonPackage and prepare the `pyproject.toml`,
156165
# we could make those *.py into setuptools' entrypoints
157166
substituteInPlace ./*.py --replace "/usr/bin/env python" "${llama-python}/bin/python"
158167
'';
159168

169+
# With PR#6015 https://github.com/ggerganov/llama.cpp/pull/6015,
170+
# `default.metallib` may be compiled with Metal compiler from XCode
171+
# and we need to escape sandbox on MacOS to access Metal compiler.
172+
# `xcrun` is used find the path of the Metal compiler, which is varible
173+
# and not on $PATH
174+
# see https://github.com/ggerganov/llama.cpp/pull/6118 for discussion
175+
__noChroot = effectiveStdenv.isDarwin && useMetalKit && precompileMetalShaders;
176+
160177
nativeBuildInputs =
161178
[
162179
cmake
@@ -173,6 +190,8 @@ effectiveStdenv.mkDerivation (
173190
]
174191
++ optionals (effectiveStdenv.hostPlatform.isGnu && enableStatic) [
175192
glibc.static
193+
] ++ optionals (effectiveStdenv.isDarwin && useMetalKit && precompileMetalShaders) [
194+
xcrunHost
176195
];
177196

178197
buildInputs =
@@ -217,7 +236,10 @@ effectiveStdenv.mkDerivation (
217236
# Should likely use `rocmPackages.clr.gpuTargets`.
218237
"-DAMDGPU_TARGETS=gfx803;gfx900;gfx906:xnack-;gfx908:xnack-;gfx90a:xnack+;gfx90a:xnack-;gfx940;gfx941;gfx942;gfx1010;gfx1012;gfx1030;gfx1100;gfx1101;gfx1102"
219238
]
220-
++ optionals useMetalKit [ (lib.cmakeFeature "CMAKE_C_FLAGS" "-D__ARM_FEATURE_DOTPROD=1") ];
239+
++ optionals useMetalKit [
240+
(lib.cmakeFeature "CMAKE_C_FLAGS" "-D__ARM_FEATURE_DOTPROD=1")
241+
(cmakeBool "LLAMA_METAL_EMBED_LIBRARY" (!precompileMetalShaders))
242+
];
221243

222244
# TODO(SomeoneSerge): It's better to add proper install targets at the CMake level,
223245
# if they haven't been added yet.

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,12 @@ if (LLAMA_METAL)
12651265
GROUP_READ
12661266
WORLD_READ
12671267
DESTINATION ${CMAKE_INSTALL_BINDIR})
1268+
if (NOT LLAMA_METAL_EMBED_LIBRARY)
1269+
install(
1270+
FILES ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/default.metallib
1271+
DESTINATION ${CMAKE_INSTALL_BINDIR}
1272+
)
1273+
endif()
12681274
endif()
12691275

12701276
#

0 commit comments

Comments
 (0)