Skip to content

Commit c48e2a5

Browse files
committed
Add native CPU build toggle
1 parent 93affc0 commit c48e2a5

7 files changed

Lines changed: 76 additions & 7 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(ENGINE_DEFAULT_ENABLE_VULKAN OFF)
1919
set(ENGINE_DEFAULT_ENABLE_METAL OFF)
2020
set(ENGINE_DEFAULT_ENABLE_LLAMAFILE ON)
2121
set(ENGINE_DEFAULT_ENABLE_CUDA_GRAPHS ON)
22+
set(ENGINE_DEFAULT_ENABLE_NATIVE_CPU ON)
2223
if (APPLE)
2324
set(ENGINE_DEFAULT_ENABLE_METAL ON)
2425
endif()
@@ -43,6 +44,7 @@ option(ENGINE_ENABLE_VULKAN "Build ggml with Vulkan backend support" ${ENGINE_DE
4344
option(ENGINE_ENABLE_METAL "Build ggml with Metal backend support" ${ENGINE_DEFAULT_ENABLE_METAL})
4445
option(ENGINE_ENABLE_LLAMAFILE "Build ggml with llamafile SGEMM support" ${ENGINE_DEFAULT_ENABLE_LLAMAFILE})
4546
option(ENGINE_ENABLE_CUDA_GRAPHS "Enable ggml CUDA graphs support" ${ENGINE_DEFAULT_ENABLE_CUDA_GRAPHS})
47+
option(ENGINE_ENABLE_NATIVE_CPU "Build ggml CPU kernels with native host ISA flags" ${ENGINE_DEFAULT_ENABLE_NATIVE_CPU})
4648
option(ENGINE_ENABLE_OPENMP "Build host code with OpenMP support" ON)
4749
option(ENGINE_BUILD_EXAMPLES "Build example binaries" OFF)
4850
option(ENGINE_BUILD_TESTS "Build framework unit tests" OFF)
@@ -66,6 +68,7 @@ set(GGML_VULKAN ${ENGINE_ENABLE_VULKAN} CACHE BOOL "Build ggml with Vulkan backe
6668
set(GGML_METAL ${ENGINE_ENABLE_METAL} CACHE BOOL "Build ggml with Metal backend support" FORCE)
6769
set(GGML_LLAMAFILE ${ENGINE_ENABLE_LLAMAFILE} CACHE BOOL "Build ggml with llamafile SGEMM support" FORCE)
6870
set(GGML_CUDA_GRAPHS ${ENGINE_ENABLE_CUDA_GRAPHS} CACHE BOOL "Enable ggml CUDA graphs support" FORCE)
71+
set(GGML_NATIVE ${ENGINE_ENABLE_NATIVE_CPU} CACHE BOOL "Build ggml CPU kernels with native host ISA flags" FORCE)
6972

7073
set(SPM_ENABLE_SHARED OFF CACHE BOOL "Build sentencepiece shared libs" FORCE)
7174
set(SPM_BUILD_TEST OFF CACHE BOOL "Do not build sentencepiece tests" FORCE)

‎Makefile.windows‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ PRESET ?= windows-cuda-release
77
TARGET ?= audiocpp_cli
88
JOBS ?=
99
VS_INSTALL ?=
10+
NATIVE_CPU ?=
1011

1112
.PHONY: all build configure cpu cpu-release cuda cuda-debug cuda-release clean presets help
1213

1314
all: cuda
1415

1516
build:
16-
$(POWERSHELL) -NoProfile -ExecutionPolicy Bypass -File "$(BUILD_SCRIPT)" -Preset "$(PRESET)" $(if $(TARGET),-Target "$(TARGET)",) $(if $(JOBS),-Jobs "$(JOBS)",) $(if $(VS_INSTALL),-VsInstall "$(VS_INSTALL)",)
17+
$(POWERSHELL) -NoProfile -ExecutionPolicy Bypass -File "$(BUILD_SCRIPT)" -Preset "$(PRESET)" $(if $(TARGET),-Target "$(TARGET)",) $(if $(JOBS),-Jobs "$(JOBS)",) $(if $(NATIVE_CPU),-NativeCpu "$(NATIVE_CPU)",) $(if $(VS_INSTALL),-VsInstall "$(VS_INSTALL)",)
1718

1819
configure:
19-
$(POWERSHELL) -NoProfile -ExecutionPolicy Bypass -File "$(BUILD_SCRIPT)" -Preset "$(PRESET)" -ConfigureOnly $(if $(VS_INSTALL),-VsInstall "$(VS_INSTALL)",)
20+
$(POWERSHELL) -NoProfile -ExecutionPolicy Bypass -File "$(BUILD_SCRIPT)" -Preset "$(PRESET)" -ConfigureOnly $(if $(NATIVE_CPU),-NativeCpu "$(NATIVE_CPU)",) $(if $(VS_INSTALL),-VsInstall "$(VS_INSTALL)",)
2021

2122
cpu:
2223
$(MAKE) -f "$(THIS_MAKEFILE)" build PRESET=windows-cpu-release
@@ -52,5 +53,6 @@ help:
5253
@echo Examples:
5354
@echo make -f Makefile.windows cpu JOBS=8
5455
@echo make -f Makefile.windows cuda JOBS=8
56+
@echo make -f Makefile.windows cuda NATIVE_CPU=OFF JOBS=8
5557
@echo make -f Makefile.windows cuda-debug TARGET=audiocpp_cli JOBS=8
5658
@echo make -f Makefile.windows cuda VS_INSTALL="C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools"

‎README.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ That default configure is a CPU build unless you enable an accelerator backend e
9393

9494
Use GCC 13 or newer for Linux builds.
9595

96+
Native ggml CPU optimization is enabled by default for local performance. If your compiler or assembler rejects a generated CPU instruction such as `vpdpbusd`, reconfigure with `-DENGINE_ENABLE_NATIVE_CPU=OFF` to build portable CPU kernels.
97+
9698
Common Linux configure examples:
9799

98100
CPU-only:
@@ -113,6 +115,12 @@ Vulkan:
113115
cmake -S . -B build -DENGINE_ENABLE_VULKAN=ON
114116
```
115117

118+
Portable CPU-kernel fallback:
119+
120+
```bash
121+
cmake -S . -B build -DENGINE_ENABLE_NATIVE_CPU=OFF
122+
```
123+
116124
Build the CLI and server from the configured tree:
117125

118126
```bash
@@ -133,6 +141,7 @@ Examples:
133141
scripts/build_linux.sh --backend cuda --target audiocpp_cli --target audiocpp_server
134142
scripts/build_linux.sh --backend vulkan --target audiocpp_cli --target audiocpp_server
135143
scripts/build_linux.sh --backend cpu --target audiocpp_cli --target audiocpp_server
144+
scripts/build_linux.sh --backend cuda --native-cpu OFF --target audiocpp_cli --target audiocpp_server
136145
```
137146

138147
Use `--build-dir <dir>` only when you intentionally want a custom output directory.
@@ -170,16 +179,18 @@ If GNU Make is available on Windows:
170179
```bash
171180
make -f Makefile.windows cpu JOBS=16
172181
make -f Makefile.windows cuda JOBS=16
182+
make -f Makefile.windows cuda NATIVE_CPU=OFF JOBS=16
173183
```
174184

175-
The Windows script configures `build/windows-cuda-release` by default and builds `audiocpp_cli`. CUDA presets enable CUDA, CUDA graphs, OpenMP, Ninja, `/utf-8`, `/EHsc`, MSVC OpenMP SIMD support with `/openmp:experimental`, and the same portable CPU optimization baseline used for the Windows CUDA path. The CPU preset uses the same MSVC/Ninja/OpenMP setup without requiring CUDA. CUDA presets auto-detect the local GPU CUDA architecture when `nvidia-smi` is available.
185+
The Windows script configures `build/windows-cuda-release` by default and builds `audiocpp_cli`. CUDA presets enable CUDA, CUDA graphs, OpenMP, Ninja, `/utf-8`, `/EHsc`, MSVC OpenMP SIMD support with `/openmp:experimental`, and native CPU optimization by default. The CPU preset uses the same MSVC/Ninja/OpenMP setup without requiring CUDA. CUDA presets auto-detect the local GPU CUDA architecture when `nvidia-smi` is available. Pass `-NativeCpu OFF` or `NATIVE_CPU=OFF` to use portable CPU kernels.
176186

177187
Useful variants:
178188

179189
```powershell
180190
.\scripts\build_windows.ps1 -Target audiocpp_server -Jobs 16
181191
.\scripts\build_windows.ps1 -Preset windows-cpu-release -Target audiocpp_cli
182192
.\scripts\build_windows.ps1 -Preset windows-cuda-debug -Target audiocpp_cli
193+
.\scripts\build_windows.ps1 -NativeCpu OFF -Target audiocpp_cli
183194
.\scripts\build_windows.ps1 -ConfigureOnly
184195
.\scripts\build_windows.ps1 -CudaArchitectures 120a-real
185196
```
@@ -214,6 +225,7 @@ scripts/build_metal.sh --target audiocpp_server
214225
scripts/build_metal.sh --build-type Release --archs arm64 --target audiocpp_cli
215226
scripts/build_metal.sh --with-tests --target audio_dsp_test
216227
scripts/build_metal.sh --openmp auto --target audiocpp_cli
228+
scripts/build_metal.sh --native-cpu OFF --target audiocpp_cli
217229
```
218230

219231
The built CLI is written to:
@@ -231,6 +243,7 @@ Build options:
231243
| `ENGINE_ENABLE_METAL` | Enable the ggml Metal backend. Required for `--backend metal`. | `OFF` on most platforms, `ON` on Apple |
232244
| `ENGINE_ENABLE_LLAMAFILE` | Enable llamafile SGEMM support in ggml CPU builds. | `ON` |
233245
| `ENGINE_ENABLE_CUDA_GRAPHS` | Enable ggml CUDA graphs support when CUDA is enabled. | `ON` |
246+
| `ENGINE_ENABLE_NATIVE_CPU` | Build ggml CPU kernels with native host ISA flags such as `-march=native`. Disable this for portable CPU kernels or toolchains that reject generated CPU instructions. | `ON` |
234247
| `ENGINE_ENABLE_OPENMP` | Enable OpenMP for host-side parallel work. | `ON` |
235248
| `ENGINE_BUILD_EXAMPLES` | Build example binaries. | `OFF` |
236249
| `ENGINE_BUILD_TESTS` | Build framework unit tests. | `OFF` |

‎scripts/build_linux.sh‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ VULKAN_MODE="off"
1010
WITH_TESTS="OFF"
1111
WITH_EXAMPLES="OFF"
1212
WITH_WARMBENCH="OFF"
13+
NATIVE_CPU="ON"
1314
TARGETS=()
1415
JOBS=""
1516

@@ -63,6 +64,21 @@ while [[ $# -gt 0 ]]; do
6364
WITH_WARMBENCH="ON"
6465
shift
6566
;;
67+
--native-cpu)
68+
case "$2" in
69+
ON|on|On|1|true|TRUE|yes|YES)
70+
NATIVE_CPU="ON"
71+
;;
72+
OFF|off|Off|0|false|FALSE|no|NO)
73+
NATIVE_CPU="OFF"
74+
;;
75+
*)
76+
echo "--native-cpu must be ON or OFF" >&2
77+
exit 1
78+
;;
79+
esac
80+
shift 2
81+
;;
6682
--target)
6783
TARGETS+=("$2")
6884
shift 2
@@ -161,6 +177,7 @@ echo "Using generator: $GENERATOR"
161177
echo "Using build dir: $BUILD_DIR"
162178
echo "Including CUDA backend: $ENGINE_ENABLE_CUDA"
163179
echo "Including Vulkan backend: $ENGINE_ENABLE_VULKAN"
180+
echo "Native CPU optimization: $NATIVE_CPU"
164181
echo "Building examples: $WITH_EXAMPLES"
165182
echo "Building tests: $WITH_TESTS"
166183
echo "Building warmbench: $WITH_WARMBENCH"
@@ -172,6 +189,7 @@ echo "Building warmbench: $WITH_WARMBENCH"
172189
-DCMAKE_BUILD_TYPE="$BUILD_TYPE" \
173190
-DENGINE_ENABLE_CUDA="$ENGINE_ENABLE_CUDA" \
174191
-DENGINE_ENABLE_VULKAN="$ENGINE_ENABLE_VULKAN" \
192+
-DENGINE_ENABLE_NATIVE_CPU="$NATIVE_CPU" \
175193
-DENGINE_BUILD_EXAMPLES="$WITH_EXAMPLES" \
176194
-DENGINE_BUILD_TESTS="$WITH_TESTS" \
177195
-DENGINE_BUILD_WARMBENCH="$WITH_WARMBENCH"

‎scripts/build_metal.sh‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ WITH_EXAMPLES="OFF"
1212
WITH_WARMBENCH="OFF"
1313
OPENMP_MODE="off"
1414
LLAMAFILE="ON"
15+
NATIVE_CPU="ON"
1516
METAL_EMBED_LIBRARY="ON"
1617
DEPLOYMENT_TARGET=""
1718
ARCHS=""
@@ -36,6 +37,8 @@ Options:
3637
Default: OFF
3738
--llamafile ON|OFF Enable ggml llamafile SGEMM.
3839
Default: ON
40+
--native-cpu ON|OFF Build ggml CPU kernels with native host ISA flags.
41+
Default: ON
3942
--embed-metal ON|OFF Embed ggml Metal shader library.
4043
Default: ON
4144
--with-tests Build framework unit tests.
@@ -95,6 +98,10 @@ while [[ $# -gt 0 ]]; do
9598
LLAMAFILE="$(normalize_on_off --llamafile "$2")"
9699
shift 2
97100
;;
101+
--native-cpu)
102+
NATIVE_CPU="$(normalize_on_off --native-cpu "$2")"
103+
shift 2
104+
;;
98105
--embed-metal)
99106
METAL_EMBED_LIBRARY="$(normalize_on_off --embed-metal "$2")"
100107
shift 2
@@ -194,6 +201,7 @@ CMAKE_CMD=(
194201
-DENGINE_ENABLE_METAL=ON
195202
-DENGINE_ENABLE_OPENMP="$ENGINE_ENABLE_OPENMP"
196203
-DENGINE_ENABLE_LLAMAFILE="$LLAMAFILE"
204+
-DENGINE_ENABLE_NATIVE_CPU="$NATIVE_CPU"
197205
-DGGML_OPENMP="$ENGINE_ENABLE_OPENMP"
198206
-DGGML_METAL_EMBED_LIBRARY="$METAL_EMBED_LIBRARY"
199207
-DENGINE_BUILD_TESTS="$WITH_TESTS"
@@ -225,6 +233,7 @@ echo "Including Metal backend: ON"
225233
echo "Embedding Metal library: $METAL_EMBED_LIBRARY"
226234
echo "Including OpenMP: $ENGINE_ENABLE_OPENMP"
227235
echo "Including llamafile: $LLAMAFILE"
236+
echo "Native CPU optimization: $NATIVE_CPU"
228237
echo "Building examples: $WITH_EXAMPLES"
229238
echo "Building tests: $WITH_TESTS"
230239
echo "Building warmbench: $WITH_WARMBENCH"

‎scripts/build_windows.ps1‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ param(
66
[switch]$ConfigureOnly,
77
[switch]$Clean,
88
[string]$CudaArchitectures = "auto",
9+
[ValidateSet("ON", "OFF")]
10+
[string]$NativeCpu = $null,
911
[string]$VsInstall = ""
1012
)
1113

@@ -258,7 +260,7 @@ function Get-PresetSettings {
258260
return @{
259261
BuildType = "Release"
260262
BuildTests = "OFF"
261-
Native = "OFF"
263+
Native = "ON"
262264
EnableCuda = "OFF"
263265
EnableCudaGraphs = "OFF"
264266
CFlagsDebug = ""
@@ -269,7 +271,7 @@ function Get-PresetSettings {
269271
return @{
270272
BuildType = "Debug"
271273
BuildTests = "ON"
272-
Native = "OFF"
274+
Native = "ON"
273275
EnableCuda = "ON"
274276
EnableCudaGraphs = "ON"
275277
CFlagsDebug = "/O2 /Zi"
@@ -280,7 +282,7 @@ function Get-PresetSettings {
280282
return @{
281283
BuildType = "Release"
282284
BuildTests = "OFF"
283-
Native = "OFF"
285+
Native = "ON"
284286
EnableCuda = "ON"
285287
EnableCudaGraphs = "ON"
286288
CFlagsDebug = ""
@@ -305,6 +307,9 @@ function Get-PresetSettings {
305307
}
306308

307309
$settings = Get-PresetSettings $Preset
310+
if ($null -ne $NativeCpu) {
311+
$settings.Native = $NativeCpu
312+
}
308313
$isCudaPreset = $settings.EnableCuda -eq "ON"
309314

310315
if ($isCudaPreset) {
@@ -350,6 +355,7 @@ Write-Host "Windows SDK: $(Split-Path $mt -Parent)"
350355
if ($arch -ne "") {
351356
Write-Host "CUDA architectures: $arch"
352357
}
358+
Write-Host "Native CPU optimization: $($settings.Native)"
353359

354360
if ($Clean) {
355361
$buildDirForClean = Join-Path (Join-Path (Split-Path $PSScriptRoot -Parent) "build") $Preset
@@ -380,7 +386,7 @@ $configureArgs = @(
380386
"-DENGINE_ENABLE_VULKAN=OFF",
381387
"-DENGINE_ENABLE_METAL=OFF",
382388
"-DGGML_OPENMP=ON",
383-
"-DGGML_NATIVE=$($settings.Native)",
389+
"-DENGINE_ENABLE_NATIVE_CPU=$($settings.Native)",
384390
"-DENGINE_BUILD_TESTS=$($settings.BuildTests)"
385391
)
386392
if ($settings.CFlagsDebug -ne "") {

‎scripts/build_xcframework.sh‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ DEPLOYMENT_TARGET="13.3"
1313
JOBS="$(sysctl -n hw.logicalcpu 2>/dev/null || echo 8)"
1414
CLEAN="OFF"
1515
LLAMAFILE="ON"
16+
NATIVE_CPU="ON"
1617

1718
usage() {
1819
cat <<'EOF'
@@ -36,6 +37,8 @@ Options:
3637
--clean Remove intermediate/output directories first.
3738
--llamafile ON|OFF Enable ggml llamafile SGEMM.
3839
Default: ON
40+
--native-cpu ON|OFF Build ggml CPU kernels with native host ISA flags.
41+
Default: ON
3942
-j, --jobs <n> Parallel build jobs.
4043
-h, --help Show this help.
4144
@@ -80,6 +83,10 @@ while [[ $# -gt 0 ]]; do
8083
LLAMAFILE="$2"
8184
shift 2
8285
;;
86+
--native-cpu)
87+
NATIVE_CPU="$2"
88+
shift 2
89+
;;
8390
-j|--jobs)
8491
JOBS="$2"
8592
shift 2
@@ -118,6 +125,16 @@ case "$LLAMAFILE" in
118125
;;
119126
esac
120127

128+
case "$NATIVE_CPU" in
129+
ON|OFF) ;;
130+
on) NATIVE_CPU="ON" ;;
131+
off) NATIVE_CPU="OFF" ;;
132+
*)
133+
echo "--native-cpu must be ON or OFF" >&2
134+
exit 1
135+
;;
136+
esac
137+
121138
ARCHS="${ARCHS//,/ }"
122139
read -r -a ARCH_LIST <<< "$ARCHS"
123140
if [[ ${#ARCH_LIST[@]} -eq 0 ]]; then
@@ -165,6 +182,7 @@ archive_for_arch() {
165182
-DENGINE_ENABLE_VULKAN=OFF \
166183
-DENGINE_ENABLE_METAL=ON \
167184
-DENGINE_ENABLE_LLAMAFILE="$LLAMAFILE" \
185+
-DENGINE_ENABLE_NATIVE_CPU="$NATIVE_CPU" \
168186
-DGGML_METAL_EMBED_LIBRARY=ON \
169187
-DENGINE_BUILD_TESTS=OFF \
170188
-DENGINE_BUILD_EXAMPLES=OFF

0 commit comments

Comments
 (0)