Skip to content

Commit c7f95b7

Browse files
authored
build : detect AVX512 in Makefile, add AVX512 option in CMake (ggml-org#2043)
* make : add AVX512 detection to Makefile and CMakeLists.txt * make : autodetect more AVX512 instruction subsets * cmake : do not default to AVX512, must be enabled explicitly * cmake : enable a set of AVX512 subsets, when AVX512 is turned on * make : consolidate AVX512 subsets, add AVX512 VBMI * cmake : revert to NO AVX512 setting, add settings for AVX512 VNNI and VBMI * make : re-introduce AVX512VNNI back * cmake : remove superfluous comment line
1 parent 5c554c0 commit c7f95b7

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

CMakeLists.txt

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ option(WHISPER_BUILD_EXAMPLES "whisper: build examples" ${WHISPER_STANDA
5959

6060
option(WHISPER_SDL2 "whisper: support for libSDL2" OFF)
6161

62-
option(WHISPER_NO_AVX "whisper: disable AVX" OFF)
63-
option(WHISPER_NO_AVX2 "whisper: disable AVX2" OFF)
64-
option(WHISPER_NO_FMA "whisper: disable FMA" OFF)
65-
option(WHISPER_NO_F16C "whisper: disable F16c" OFF)
62+
option(WHISPER_NO_AVX "whisper: disable AVX" OFF)
63+
option(WHISPER_NO_AVX2 "whisper: disable AVX2" OFF)
64+
option(WHISPER_NO_AVX512 "whisper: disable AVX512" ON)
65+
option(WHISPER_NO_AVX512_VBMI "whisper: disable AVX512-VBMI" ON)
66+
option(WHISPER_NO_AVX512_VNNI "whisper: disable AVX512-VNNI" ON)
67+
option(WHISPER_NO_FMA "whisper: disable FMA" OFF)
68+
option(WHISPER_NO_F16C "whisper: disable F16c" OFF)
6669

6770
option(WHISPER_OPENVINO "whisper: support for OpenVINO" OFF)
6871

@@ -464,16 +467,30 @@ else()
464467
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
465468
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /utf-8")
466469
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /utf-8")
467-
if(NOT WHISPER_NO_AVX2)
470+
if(NOT WHISPER_NO_AVX512)
471+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX512")
472+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX512")
473+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX512")
474+
# MSVC has no compile-time flags enabling specific
475+
# AVX512 extensions, neither it defines the
476+
# macros corresponding to the extensions.
477+
# Do it manually.
478+
if (NOT WHISPER_NO_AVX512_VBMI)
479+
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VBMI__>)
480+
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VBMI__>)
481+
endif()
482+
if (NOT WHISPER_NO_AVX512_VNNI)
483+
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VNNI__>)
484+
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
485+
endif()
486+
elseif(NOT WHISPER_NO_AVX2)
468487
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2")
469488
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX2")
470489
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX2")
471-
else()
472-
if(NOT WHISPER_NO_AVX)
473-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX")
474-
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX")
475-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX")
476-
endif()
490+
elseif(NOT WHISPER_NO_AVX)
491+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX")
492+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX")
493+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX")
477494
endif()
478495
else()
479496
if (EMSCRIPTEN)
@@ -486,6 +503,15 @@ else()
486503
if(NOT WHISPER_NO_AVX2)
487504
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2")
488505
endif()
506+
if(NOT WHISPER_NO_AVX512)
507+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512f -mavx512cd -mavx512vl -mavx512dq -mavx512bw")
508+
endif()
509+
if(NOT WHISPER_NO_AVX512_VBMI)
510+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512vbmi")
511+
endif()
512+
if(NOT WHISPER_NO_AVX512_VNNI)
513+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512vnni")
514+
endif()
489515
if(NOT WHISPER_NO_FMA)
490516
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfma")
491517
endif()

Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
144144
CXXFLAGS += -mavx2
145145
endif
146146

147+
AVX512F_M := $(shell $(CPUINFO_CMD) | grep -iw 'AVX512F')
148+
ifneq (,$(AVX512F_M))
149+
CFLAGS += -mavx512f -mavx512cd -mavx512vl -mavx512dq -mavx512bw
150+
CXXFLAGS += -mavx512f -mavx512cd -mavx512vl -mavx512dq -mavx512bw
151+
endif
152+
153+
AVX512VNNI_M := $(shell $(CPUINFO_CMD) | grep -iwE 'AVX512_VNNI|AVX512VNNI')
154+
ifneq (,$(AVX512VNNI_M))
155+
CFLAGS += -mavx512vnni
156+
CXXFLAGS += -mavx512vnni
157+
endif
158+
159+
AVX512VBMI_M := $(shell $(CPUINFO_CMD) | grep -iw 'AVX512VBMI')
160+
ifneq (,$(AVX512VBMI_M))
161+
CFLAGS += -mavx512vbmi
162+
CXXFLAGS += -mavx512vbmi
163+
endif
164+
147165
FMA_M := $(shell $(CPUINFO_CMD) | grep -iw 'FMA')
148166
ifneq (,$(FMA_M))
149167
CFLAGS += -mfma

0 commit comments

Comments
 (0)