|  | 
|  | 1 | +ifndef UNAME_S | 
|  | 2 | +UNAME_S := $(shell uname -s) | 
|  | 3 | +endif | 
|  | 4 | + | 
|  | 5 | +ifndef UNAME_P | 
|  | 6 | +UNAME_P := $(shell uname -p) | 
|  | 7 | +endif | 
|  | 8 | + | 
|  | 9 | +ifndef UNAME_M | 
|  | 10 | +UNAME_M := $(shell uname -m) | 
|  | 11 | +endif | 
|  | 12 | + | 
|  | 13 | +CCV := $(shell $(CC) --version | head -n 1) | 
|  | 14 | +CXXV := $(shell $(CXX) --version | head -n 1) | 
|  | 15 | + | 
|  | 16 | +# Mac OS + Arm can report x86_64 | 
|  | 17 | +# ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789 | 
|  | 18 | +ifeq ($(UNAME_S),Darwin) | 
|  | 19 | +	ifneq ($(UNAME_P),arm) | 
|  | 20 | +		SYSCTL_M := $(shell sysctl -n hw.optional.arm64) | 
|  | 21 | +		ifeq ($(SYSCTL_M),1) | 
|  | 22 | +			# UNAME_P := arm | 
|  | 23 | +			# UNAME_M := arm64 | 
|  | 24 | +			warn := $(warning Your arch is announced as x86_64, but it seems to actually be ARM64. Not fixing that can lead to bad performance. For more info see: https://github.com/ggerganov/whisper.cpp/issues/66\#issuecomment-1282546789) | 
|  | 25 | +		endif | 
|  | 26 | +	endif | 
|  | 27 | +endif | 
|  | 28 | + | 
|  | 29 | +# | 
|  | 30 | +# Compile flags | 
|  | 31 | +# | 
|  | 32 | + | 
|  | 33 | +CFLAGS   = -I.              -O3 -DNDEBUG -std=c11   -fPIC | 
|  | 34 | +CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC | 
|  | 35 | +LDFLAGS  = | 
|  | 36 | + | 
|  | 37 | +# OS specific | 
|  | 38 | +# TODO: support Windows | 
|  | 39 | +ifeq ($(UNAME_S),Linux) | 
|  | 40 | +	CFLAGS   += -pthread | 
|  | 41 | +	CXXFLAGS += -pthread | 
|  | 42 | +endif | 
|  | 43 | +ifeq ($(UNAME_S),Darwin) | 
|  | 44 | +	CFLAGS   += -pthread | 
|  | 45 | +	CXXFLAGS += -pthread | 
|  | 46 | +endif | 
|  | 47 | +ifeq ($(UNAME_S),FreeBSD) | 
|  | 48 | +	CFLAGS   += -pthread | 
|  | 49 | +	CXXFLAGS += -pthread | 
|  | 50 | +endif | 
|  | 51 | +ifeq ($(UNAME_S),Haiku) | 
|  | 52 | +	CFLAGS   += -pthread | 
|  | 53 | +	CXXFLAGS += -pthread | 
|  | 54 | +endif | 
|  | 55 | + | 
|  | 56 | +# Architecture specific | 
|  | 57 | +# TODO: probably these flags need to be tweaked on some architectures | 
|  | 58 | +#       feel free to update the Makefile for your architecture and send a pull request or issue | 
|  | 59 | +ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686)) | 
|  | 60 | +	ifeq ($(UNAME_S),Darwin) | 
|  | 61 | +		CFLAGS += -mf16c | 
|  | 62 | +		AVX1_M := $(shell sysctl machdep.cpu.features) | 
|  | 63 | +		ifneq (,$(findstring FMA,$(AVX1_M))) | 
|  | 64 | +			CFLAGS += -mfma | 
|  | 65 | +		endif | 
|  | 66 | +		ifneq (,$(findstring AVX1.0,$(AVX1_M))) | 
|  | 67 | +			CFLAGS += -mavx | 
|  | 68 | +		endif | 
|  | 69 | +		AVX2_M := $(shell sysctl machdep.cpu.leaf7_features) | 
|  | 70 | +		ifneq (,$(findstring AVX2,$(AVX2_M))) | 
|  | 71 | +			CFLAGS += -mavx2 | 
|  | 72 | +		endif | 
|  | 73 | +	else ifeq ($(UNAME_S),Linux) | 
|  | 74 | +		AVX1_M := $(shell grep "avx " /proc/cpuinfo) | 
|  | 75 | +		ifneq (,$(findstring avx,$(AVX1_M))) | 
|  | 76 | +			CFLAGS += -mavx | 
|  | 77 | +		endif | 
|  | 78 | +		AVX2_M := $(shell grep "avx2 " /proc/cpuinfo) | 
|  | 79 | +		ifneq (,$(findstring avx2,$(AVX2_M))) | 
|  | 80 | +			CFLAGS += -mavx2 | 
|  | 81 | +		endif | 
|  | 82 | +		FMA_M := $(shell grep "fma " /proc/cpuinfo) | 
|  | 83 | +		ifneq (,$(findstring fma,$(FMA_M))) | 
|  | 84 | +			CFLAGS += -mfma | 
|  | 85 | +		endif | 
|  | 86 | +		F16C_M := $(shell grep "f16c " /proc/cpuinfo) | 
|  | 87 | +		ifneq (,$(findstring f16c,$(F16C_M))) | 
|  | 88 | +			CFLAGS += -mf16c | 
|  | 89 | +		endif | 
|  | 90 | +		SSE3_M := $(shell grep "sse3 " /proc/cpuinfo) | 
|  | 91 | +		ifneq (,$(findstring sse3,$(SSE3_M))) | 
|  | 92 | +			CFLAGS += -msse3 | 
|  | 93 | +		endif | 
|  | 94 | +	else ifeq ($(UNAME_S),Haiku) | 
|  | 95 | +		AVX1_M := $(shell sysinfo -cpu | grep "AVX ") | 
|  | 96 | +		ifneq (,$(findstring avx,$(AVX1_M))) | 
|  | 97 | +			CFLAGS += -mavx | 
|  | 98 | +		endif | 
|  | 99 | +		AVX2_M := $(shell sysinfo -cpu | grep "AVX2 ") | 
|  | 100 | +		ifneq (,$(findstring avx2,$(AVX2_M))) | 
|  | 101 | +			CFLAGS += -mavx2 | 
|  | 102 | +		endif | 
|  | 103 | +		FMA_M := $(shell sysinfo -cpu | grep "FMA ") | 
|  | 104 | +		ifneq (,$(findstring fma,$(FMA_M))) | 
|  | 105 | +			CFLAGS += -mfma | 
|  | 106 | +		endif | 
|  | 107 | +		F16C_M := $(shell sysinfo -cpu | grep "F16C ") | 
|  | 108 | +		ifneq (,$(findstring f16c,$(F16C_M))) | 
|  | 109 | +			CFLAGS += -mf16c | 
|  | 110 | +		endif | 
|  | 111 | +	else | 
|  | 112 | +		CFLAGS += -mfma -mf16c -mavx -mavx2 | 
|  | 113 | +	endif | 
|  | 114 | +endif | 
|  | 115 | +ifeq ($(UNAME_M),amd64) | 
|  | 116 | +	CFLAGS += -mavx -mavx2 -mfma -mf16c | 
|  | 117 | +endif | 
|  | 118 | +ifneq ($(filter ppc64%,$(UNAME_M)),) | 
|  | 119 | +	POWER9_M := $(shell grep "POWER9" /proc/cpuinfo) | 
|  | 120 | +	ifneq (,$(findstring POWER9,$(POWER9_M))) | 
|  | 121 | +		CFLAGS += -mpower9-vector | 
|  | 122 | +	endif | 
|  | 123 | +	# Require c++23's std::byteswap for big-endian support. | 
|  | 124 | +	ifeq ($(UNAME_M),ppc64) | 
|  | 125 | +		CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN | 
|  | 126 | +	endif | 
|  | 127 | +endif | 
|  | 128 | +ifndef WHISPER_NO_ACCELERATE | 
|  | 129 | +	# Mac M1 - include Accelerate framework | 
|  | 130 | +	ifeq ($(UNAME_S),Darwin) | 
|  | 131 | +		CFLAGS  += -DGGML_USE_ACCELERATE | 
|  | 132 | +		LDFLAGS += -framework Accelerate | 
|  | 133 | +	endif | 
|  | 134 | +endif | 
|  | 135 | +ifdef WHISPER_OPENBLAS | 
|  | 136 | +	CFLAGS  += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas | 
|  | 137 | +	LDFLAGS += -lopenblas | 
|  | 138 | +endif | 
|  | 139 | +ifdef WHISPER_GPROF | 
|  | 140 | +	CFLAGS   += -pg | 
|  | 141 | +	CXXFLAGS += -pg | 
|  | 142 | +endif | 
|  | 143 | +ifneq ($(filter aarch64%,$(UNAME_M)),) | 
|  | 144 | +	CFLAGS += -mcpu=native | 
|  | 145 | +	CXXFLAGS += -mcpu=native | 
|  | 146 | +endif | 
|  | 147 | +ifneq ($(filter armv6%,$(UNAME_M)),) | 
|  | 148 | +	# Raspberry Pi 1, 2, 3 | 
|  | 149 | +	CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access | 
|  | 150 | +endif | 
|  | 151 | +ifneq ($(filter armv7%,$(UNAME_M)),) | 
|  | 152 | +	# Raspberry Pi 4 | 
|  | 153 | +	CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations | 
|  | 154 | +endif | 
|  | 155 | +ifneq ($(filter armv8%,$(UNAME_M)),) | 
|  | 156 | +	# Raspberry Pi 4 | 
|  | 157 | +	CFLAGS += -mfp16-format=ieee -mno-unaligned-access | 
|  | 158 | +endif | 
|  | 159 | + | 
|  | 160 | +# | 
|  | 161 | +# Print build information | 
|  | 162 | +# | 
|  | 163 | + | 
|  | 164 | +$(info I llama.cpp build info: ) | 
|  | 165 | +$(info I UNAME_S:  $(UNAME_S)) | 
|  | 166 | +$(info I UNAME_P:  $(UNAME_P)) | 
|  | 167 | +$(info I UNAME_M:  $(UNAME_M)) | 
|  | 168 | +$(info I CFLAGS:   $(CFLAGS)) | 
|  | 169 | +$(info I CXXFLAGS: $(CXXFLAGS)) | 
|  | 170 | +$(info I LDFLAGS:  $(LDFLAGS)) | 
|  | 171 | +$(info I CC:       $(CCV)) | 
|  | 172 | +$(info I CXX:      $(CXXV)) | 
|  | 173 | +$(info ) | 
|  | 174 | + | 
|  | 175 | +default: main quantize | 
|  | 176 | + | 
|  | 177 | +# | 
|  | 178 | +# Build library | 
|  | 179 | +# | 
|  | 180 | + | 
|  | 181 | +ggml.o: ggml.c ggml.h | 
|  | 182 | +	$(CC)  $(CFLAGS)   -c ggml.c -o ggml.o | 
|  | 183 | + | 
|  | 184 | +utils.o: utils.cpp utils.h | 
|  | 185 | +	$(CXX) $(CXXFLAGS) -c utils.cpp -o utils.o | 
|  | 186 | + | 
|  | 187 | +clean: | 
|  | 188 | +	rm -f *.o main quantize | 
|  | 189 | + | 
|  | 190 | +main: main.cpp ggml.o utils.o | 
|  | 191 | +	$(CXX) $(CXXFLAGS) main.cpp ggml.o utils.o -o main $(LDFLAGS) | 
|  | 192 | +	./main -h | 
|  | 193 | + | 
|  | 194 | +quantize: quantize.cpp ggml.o utils.o | 
|  | 195 | +	$(CXX) $(CXXFLAGS) quantize.cpp ggml.o utils.o -o quantize $(LDFLAGS) | 
|  | 196 | + | 
|  | 197 | +# | 
|  | 198 | +# Tests | 
|  | 199 | +# | 
|  | 200 | + | 
|  | 201 | +.PHONY: tests | 
|  | 202 | +tests: | 
|  | 203 | +	bash ./tests/run-tests.sh | 
0 commit comments