mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-11-04 09:32:00 +00:00 
			
		
		
		
	build : on Mac OS enable Metal by default
This commit is contained in:
		@@ -36,6 +36,12 @@ endif()
 | 
				
			|||||||
# Option list
 | 
					# Option list
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if (APPLE)
 | 
				
			||||||
 | 
					    set(LLAMA_METAL_DEFAULT ON)
 | 
				
			||||||
 | 
					else()
 | 
				
			||||||
 | 
					    set(LLAMA_METAL_DEFAULT OFF)
 | 
				
			||||||
 | 
					endif()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# general
 | 
					# general
 | 
				
			||||||
option(LLAMA_STATIC                     "llama: static link libraries"                          OFF)
 | 
					option(LLAMA_STATIC                     "llama: static link libraries"                          OFF)
 | 
				
			||||||
option(LLAMA_NATIVE                     "llama: enable -march=native flag"                      OFF)
 | 
					option(LLAMA_NATIVE                     "llama: enable -march=native flag"                      OFF)
 | 
				
			||||||
@@ -76,7 +82,7 @@ option(LLAMA_CUDA_F16                        "llama: use 16 bit floats for some
 | 
				
			|||||||
set(LLAMA_CUDA_KQUANTS_ITER "2" CACHE STRING "llama: iters./thread per block for Q2_K/Q6_K")
 | 
					set(LLAMA_CUDA_KQUANTS_ITER "2" CACHE STRING "llama: iters./thread per block for Q2_K/Q6_K")
 | 
				
			||||||
option(LLAMA_HIPBLAS                         "llama: use hipBLAS"                               OFF)
 | 
					option(LLAMA_HIPBLAS                         "llama: use hipBLAS"                               OFF)
 | 
				
			||||||
option(LLAMA_CLBLAST                         "llama: use CLBlast"                               OFF)
 | 
					option(LLAMA_CLBLAST                         "llama: use CLBlast"                               OFF)
 | 
				
			||||||
option(LLAMA_METAL                           "llama: use Metal"                                 OFF)
 | 
					option(LLAMA_METAL                           "llama: use Metal"                                 ${LLAMA_METAL_DEFAULT})
 | 
				
			||||||
option(LLAMA_MPI                             "llama: use MPI"                                   OFF)
 | 
					option(LLAMA_MPI                             "llama: use MPI"                                   OFF)
 | 
				
			||||||
option(LLAMA_K_QUANTS                        "llama: use k-quants"                              ON)
 | 
					option(LLAMA_K_QUANTS                        "llama: use k-quants"                              ON)
 | 
				
			||||||
option(LLAMA_QKK_64                          "llama: use super-block size of 64 for k-quants"   OFF)
 | 
					option(LLAMA_QKK_64                          "llama: use super-block size of 64 for k-quants"   OFF)
 | 
				
			||||||
@@ -158,6 +164,31 @@ if (APPLE AND LLAMA_ACCELERATE)
 | 
				
			|||||||
    endif()
 | 
					    endif()
 | 
				
			||||||
endif()
 | 
					endif()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if (LLAMA_METAL)
 | 
				
			||||||
 | 
					    find_library(FOUNDATION_LIBRARY         Foundation              REQUIRED)
 | 
				
			||||||
 | 
					    find_library(METAL_FRAMEWORK            Metal                   REQUIRED)
 | 
				
			||||||
 | 
					    find_library(METALKIT_FRAMEWORK         MetalKit                REQUIRED)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    message(STATUS "Metal framework found")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    set(GGML_SOURCES_METAL ggml-metal.m ggml-metal.h)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    add_compile_definitions(GGML_USE_METAL)
 | 
				
			||||||
 | 
					    #add_compile_definitions(GGML_METAL_NDEBUG)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # get full path to the file
 | 
				
			||||||
 | 
					    #add_compile_definitions(GGML_METAL_DIR_KERNELS="${CMAKE_CURRENT_SOURCE_DIR}/")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # copy ggml-metal.metal to bin directory
 | 
				
			||||||
 | 
					    configure_file(ggml-metal.metal bin/ggml-metal.metal COPYONLY)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS}
 | 
				
			||||||
 | 
					        ${FOUNDATION_LIBRARY}
 | 
				
			||||||
 | 
					        ${METAL_FRAMEWORK}
 | 
				
			||||||
 | 
					        ${METALKIT_FRAMEWORK}
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					endif()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (LLAMA_BLAS)
 | 
					if (LLAMA_BLAS)
 | 
				
			||||||
    if (LLAMA_STATIC)
 | 
					    if (LLAMA_STATIC)
 | 
				
			||||||
        set(BLA_STATIC ON)
 | 
					        set(BLA_STATIC ON)
 | 
				
			||||||
@@ -293,29 +324,6 @@ if (LLAMA_CUBLAS)
 | 
				
			|||||||
    endif()
 | 
					    endif()
 | 
				
			||||||
endif()
 | 
					endif()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (LLAMA_METAL)
 | 
					 | 
				
			||||||
    find_library(FOUNDATION_LIBRARY         Foundation              REQUIRED)
 | 
					 | 
				
			||||||
    find_library(METAL_FRAMEWORK            Metal                   REQUIRED)
 | 
					 | 
				
			||||||
    find_library(METALKIT_FRAMEWORK         MetalKit                REQUIRED)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    set(GGML_SOURCES_METAL ggml-metal.m ggml-metal.h)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    add_compile_definitions(GGML_USE_METAL)
 | 
					 | 
				
			||||||
    #add_compile_definitions(GGML_METAL_NDEBUG)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # get full path to the file
 | 
					 | 
				
			||||||
    #add_compile_definitions(GGML_METAL_DIR_KERNELS="${CMAKE_CURRENT_SOURCE_DIR}/")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # copy ggml-metal.metal to bin directory
 | 
					 | 
				
			||||||
    configure_file(ggml-metal.metal bin/ggml-metal.metal COPYONLY)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS}
 | 
					 | 
				
			||||||
        ${FOUNDATION_LIBRARY}
 | 
					 | 
				
			||||||
        ${METAL_FRAMEWORK}
 | 
					 | 
				
			||||||
        ${METALKIT_FRAMEWORK}
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
endif()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if (LLAMA_MPI)
 | 
					if (LLAMA_MPI)
 | 
				
			||||||
    cmake_minimum_required(VERSION 3.10)
 | 
					    cmake_minimum_required(VERSION 3.10)
 | 
				
			||||||
    find_package(MPI)
 | 
					    find_package(MPI)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										35
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								Makefile
									
									
									
									
									
								
							@@ -1,6 +1,10 @@
 | 
				
			|||||||
# Define the default target now so that it is always the first target
 | 
					# Define the default target now so that it is always the first target
 | 
				
			||||||
BUILD_TARGETS = main quantize quantize-stats perplexity embedding vdot train-text-from-scratch convert-llama2c-to-ggml simple save-load-state server embd-input-test gguf llama-bench baby-llama beam-search tests/test-c.o
 | 
					BUILD_TARGETS = main quantize quantize-stats perplexity embedding vdot train-text-from-scratch convert-llama2c-to-ggml simple save-load-state server embd-input-test gguf llama-bench baby-llama beam-search tests/test-c.o
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ifndef LLAMA_NO_METAL
 | 
				
			||||||
 | 
					BUILD_TARGETS += metal
 | 
				
			||||||
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Binaries only useful for tests
 | 
					# Binaries only useful for tests
 | 
				
			||||||
TEST_TARGETS = tests/test-llama-grammar tests/test-grammar-parser tests/test-double-float tests/test-grad0 tests/test-opt tests/test-quantize-fns tests/test-quantize-perf tests/test-sampling tests/test-tokenizer-0-llama tests/test-tokenizer-0-falcon tests/test-tokenizer-1
 | 
					TEST_TARGETS = tests/test-llama-grammar tests/test-grammar-parser tests/test-double-float tests/test-grad0 tests/test-opt tests/test-quantize-fns tests/test-quantize-perf tests/test-sampling tests/test-tokenizer-0-llama tests/test-tokenizer-0-falcon tests/test-tokenizer-1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -235,14 +239,24 @@ endif
 | 
				
			|||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifndef LLAMA_NO_ACCELERATE
 | 
					ifndef LLAMA_NO_ACCELERATE
 | 
				
			||||||
	# Mac M1 - include Accelerate framework.
 | 
						# Mac OS - include Accelerate framework.
 | 
				
			||||||
	# `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
 | 
						# `-framework Accelerate` works both with Apple Silicon and Mac Intel
 | 
				
			||||||
	ifeq ($(UNAME_S),Darwin)
 | 
						ifeq ($(UNAME_S),Darwin)
 | 
				
			||||||
		CFLAGS  += -DGGML_USE_ACCELERATE
 | 
							CFLAGS  += -DGGML_USE_ACCELERATE
 | 
				
			||||||
		LDFLAGS += -framework Accelerate
 | 
							LDFLAGS += -framework Accelerate
 | 
				
			||||||
	endif
 | 
						endif
 | 
				
			||||||
endif # LLAMA_NO_ACCELERATE
 | 
					endif # LLAMA_NO_ACCELERATE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ifndef LLAMA_NO_METAL
 | 
				
			||||||
 | 
						# By default - use GPU acceleration on Mac OS
 | 
				
			||||||
 | 
						ifeq ($(UNAME_S),Darwin)
 | 
				
			||||||
 | 
							CFLAGS   += -DGGML_USE_METAL #-DGGML_METAL_NDEBUG
 | 
				
			||||||
 | 
							CXXFLAGS += -DGGML_USE_METAL
 | 
				
			||||||
 | 
							LDFLAGS  += -framework Foundation -framework Metal -framework MetalKit
 | 
				
			||||||
 | 
							OBJS     += ggml-metal.o
 | 
				
			||||||
 | 
						endif
 | 
				
			||||||
 | 
					endif # LLAMA_NO_METAL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifdef LLAMA_MPI
 | 
					ifdef LLAMA_MPI
 | 
				
			||||||
	CFLAGS += -DGGML_USE_MPI -Wno-cast-qual
 | 
						CFLAGS += -DGGML_USE_MPI -Wno-cast-qual
 | 
				
			||||||
	CXXFLAGS += -DGGML_USE_MPI -Wno-cast-qual
 | 
						CXXFLAGS += -DGGML_USE_MPI -Wno-cast-qual
 | 
				
			||||||
@@ -352,17 +366,10 @@ ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
 | 
				
			|||||||
	$(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
 | 
						$(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
 | 
				
			||||||
endif # LLAMA_HIPBLAS
 | 
					endif # LLAMA_HIPBLAS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifdef LLAMA_METAL
 | 
					ifndef LLAMA_NO_METAL
 | 
				
			||||||
	CFLAGS   += -DGGML_USE_METAL #-DGGML_METAL_NDEBUG
 | 
					 | 
				
			||||||
	CXXFLAGS += -DGGML_USE_METAL
 | 
					 | 
				
			||||||
	LDFLAGS  += -framework Foundation -framework Metal -framework MetalKit
 | 
					 | 
				
			||||||
	OBJS     += ggml-metal.o
 | 
					 | 
				
			||||||
endif # LLAMA_METAL
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
ifdef LLAMA_METAL
 | 
					 | 
				
			||||||
ggml-metal.o: ggml-metal.m ggml-metal.h
 | 
					ggml-metal.o: ggml-metal.m ggml-metal.h
 | 
				
			||||||
	$(CC) $(CFLAGS) -c $< -o $@
 | 
						$(CC) $(CFLAGS) -c $< -o $@
 | 
				
			||||||
endif # LLAMA_METAL
 | 
					endif # LLAMA_NO_METAL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifdef LLAMA_MPI
 | 
					ifdef LLAMA_MPI
 | 
				
			||||||
ggml-mpi.o: ggml-mpi.c ggml-mpi.h
 | 
					ggml-mpi.o: ggml-mpi.c ggml-mpi.h
 | 
				
			||||||
@@ -475,11 +482,7 @@ baby-llama: examples/baby-llama/baby-llama.cpp ggml.o llama.o common.o $(OBJS)
 | 
				
			|||||||
beam-search: examples/beam-search/beam-search.cpp build-info.h ggml.o llama.o common.o $(OBJS)
 | 
					beam-search: examples/beam-search/beam-search.cpp build-info.h ggml.o llama.o common.o $(OBJS)
 | 
				
			||||||
	$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
 | 
						$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifneq '' '$(or $(filter clean,$(MAKECMDGOALS)),$(LLAMA_METAL))'
 | 
					ifndef LLAMA_NO_METAL
 | 
				
			||||||
BUILD_TARGETS += metal
 | 
					 | 
				
			||||||
endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
ifdef LLAMA_METAL
 | 
					 | 
				
			||||||
metal: examples/metal/metal.cpp ggml.o $(OBJS)
 | 
					metal: examples/metal/metal.cpp ggml.o $(OBJS)
 | 
				
			||||||
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
 | 
						$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user