Skip to content

Commit ff6b05d

Browse files
committed
ggml : disable fast-math for Metal (cmake build only)
ggml-ci
1 parent 39d8bc7 commit ff6b05d

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ option(LLAMA_HIP_UMA "llama: use HIP unified memory arch
9595
option(LLAMA_CLBLAST "llama: use CLBlast" OFF)
9696
option(LLAMA_METAL "llama: use Metal" ${LLAMA_METAL_DEFAULT})
9797
option(LLAMA_METAL_NDEBUG "llama: disable Metal debugging" OFF)
98+
option(LLAMA_METAL_NO_FAST_MATH "llama: compile Metal with -fno-fast-math" OFF)
9899
option(LLAMA_MPI "llama: use MPI" OFF)
99100
option(LLAMA_QKK_64 "llama: use super-block size of 64 for k-quants" OFF)
100101

@@ -173,6 +174,27 @@ if (LLAMA_METAL)
173174
# copy ggml-metal.metal to bin directory
174175
configure_file(ggml-metal.metal ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ggml-metal.metal COPYONLY)
175176

177+
if (LLAMA_METAL_NO_FAST_MATH)
178+
# custom command to do the following:
179+
# xcrun -sdk macosx metal -fno-fast-math -c ggml-metal.metal -o ggml-metal.air
180+
# xcrun -sdk macosx metallib ggml-metal.air -o ggml.metallib
181+
#
182+
# note: this is the only way I found to disable fast-math in Metal. it's ugly, but at least it works
183+
# disabling fast math is needed in order to pass tests/test-backend-ops
184+
add_custom_command(
185+
OUTPUT ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ggml.metallib
186+
COMMAND xcrun -sdk macosx metal -fno-fast-math -c ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ggml-metal.metal -o ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ggml-metal.air
187+
COMMAND xcrun -sdk macosx metallib ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ggml-metal.air -o ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ggml.metallib
188+
DEPENDS ggml-metal.metal
189+
COMMENT "Compiling Metal kernels"
190+
)
191+
192+
add_custom_target(
193+
ggml-metal ALL
194+
DEPENDS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ggml.metallib
195+
)
196+
endif()
197+
176198
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS}
177199
${FOUNDATION_LIBRARY}
178200
${METAL_FRAMEWORK}

ci/run.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ sd=`dirname $0`
3030
cd $sd/../
3131
SRC=`pwd`
3232

33+
CMAKE_EXTRA=""
34+
35+
if [ ! -z ${GG_BUILD_METAL} ]; then
36+
CMAKE_EXTRA="${CMAKE_EXTRA} -DLLAMA_METAL_NO_FAST_MATH=ON"
37+
fi
38+
3339
## helpers
3440

3541
# download a file if it does not exist or if it is outdated
@@ -81,8 +87,8 @@ function gg_run_ctest_debug {
8187

8288
set -e
8389

84-
(time cmake -DCMAKE_BUILD_TYPE=Debug .. ) 2>&1 | tee -a $OUT/${ci}-cmake.log
85-
(time make -j ) 2>&1 | tee -a $OUT/${ci}-make.log
90+
(time cmake -DCMAKE_BUILD_TYPE=Debug ${CMAKE_EXTRA} .. ) 2>&1 | tee -a $OUT/${ci}-cmake.log
91+
(time make -j ) 2>&1 | tee -a $OUT/${ci}-make.log
8692

8793
(time ctest --output-on-failure -E test-opt ) 2>&1 | tee -a $OUT/${ci}-ctest.log
8894

@@ -109,8 +115,8 @@ function gg_run_ctest_release {
109115

110116
set -e
111117

112-
(time cmake -DCMAKE_BUILD_TYPE=Release .. ) 2>&1 | tee -a $OUT/${ci}-cmake.log
113-
(time make -j ) 2>&1 | tee -a $OUT/${ci}-make.log
118+
(time cmake -DCMAKE_BUILD_TYPE=Release .. ${CMAKE_EXTRA} ) 2>&1 | tee -a $OUT/${ci}-cmake.log
119+
(time make -j ) 2>&1 | tee -a $OUT/${ci}-make.log
114120

115121
if [ -z ${GG_BUILD_LOW_PERF} ]; then
116122
(time ctest --output-on-failure ) 2>&1 | tee -a $OUT/${ci}-ctest.log

ggml-metal.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,14 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
257257
bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
258258
#endif
259259
NSError * error = nil;
260-
NSString * libPath = [bundle pathForResource:@"default" ofType:@"metallib"];
260+
NSString * libPath = [bundle pathForResource:@"ggml" ofType:@"metallib"];
261261
if (libPath != nil) {
262+
// pre-compiled library found
262263
NSURL * libURL = [NSURL fileURLWithPath:libPath];
263264
GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [libPath UTF8String]);
264265
ctx->library = [ctx->device newLibraryWithURL:libURL error:&error];
265266
} else {
266-
GGML_METAL_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);
267+
GGML_METAL_LOG_INFO("%s: ggml.metallib not found, loading from source\n", __func__);
267268

268269
NSString * sourcePath;
269270
NSString * ggmlMetalPathResources = [[NSProcessInfo processInfo].environment objectForKey:@"GGML_METAL_PATH_RESOURCES"];
@@ -291,6 +292,13 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
291292
options = [MTLCompileOptions new];
292293
options.preprocessorMacros = @{ @"QK_K" : @(64) };
293294
#endif
295+
// try to disable fast-math
296+
// NOTE: this seems to have no effect whatsoever
297+
// instead, in order to disable fast-math, we have to build ggml.metallib from the command line
298+
// using xcrun -sdk macosx metal -fno-fast-math -c ggml-metal.metal -o ggml-metal.air
299+
// and go through the "pre-compiled library found" path above
300+
//[options setFastMathEnabled:false];
301+
294302
ctx->library = [ctx->device newLibraryWithSource:src options:options error:&error];
295303
}
296304

0 commit comments

Comments
 (0)