llama-cpp: Use runtime instruction dispatch for massive speedups (#518204)

This commit is contained in:
misuzu
2026-05-25 07:17:21 +00:00
committed by GitHub
+15 -2
View File
@@ -15,6 +15,8 @@
rocmPackages ? { },
rocmGpuTargets ? rocmPackages.clr.localGpuTargets or rocmPackages.clr.gpuTargets,
cpuArchDynamicDispatch ? true,
openclSupport ? false,
clblast,
@@ -141,8 +143,7 @@ effectiveStdenv.mkDerivation (finalAttrs: {
'';
cmakeFlags = [
# -march=native is non-deterministic; override with platform-specific flags if needed
(cmakeBool "GGML_NATIVE" false)
(cmakeBool "GGML_NATIVE" false) # -march=native would make builds non-deterministic
(cmakeBool "LLAMA_BUILD_EXAMPLES" false)
(cmakeBool "LLAMA_BUILD_SERVER" true)
(cmakeBool "LLAMA_BUILD_TESTS" (finalAttrs.finalPackage.doCheck or false))
@@ -157,6 +158,18 @@ effectiveStdenv.mkDerivation (finalAttrs: {
(cmakeBool "GGML_VULKAN" vulkanSupport)
(cmakeFeature "LLAMA_BUILD_NUMBER" finalAttrs.version)
]
++ optionals cpuArchDynamicDispatch [
# Build all CPU backend variants for runtime dynamic dispatch.
# This avoids illegal instructions on older CPUs and gives optimal performance
# on newer ones without needing separate builds.
# Enabling AVX2 can make CPU inference 13x faster compared to NixOS's x86_64 defaults.
# Note it is not a bug that the CPU variant .so files are placed in `bin/`
# (as opposed to `lib/`) alongside the executables by upstream's `CMakeLists.txt` design:
# * https://github.com/ggml-org/llama.cpp/blob/b46812de78f8fbcb6cf0154947e8633ebc78d9ac/ggml/src/CMakeLists.txt#L249-L252
# * https://github.com/ggml-org/llama.cpp/blob/b46812de78f8fbcb6cf0154947e8633ebc78d9ac/ggml/src/ggml-backend-reg.cpp#L480-L486
(cmakeBool "GGML_CPU_ALL_VARIANTS" true)
(cmakeBool "GGML_BACKEND_DL" true)
]
++ optionals cudaSupport [
(cmakeFeature "CMAKE_CUDA_ARCHITECTURES" cudaPackages.flags.cmakeCudaArchitecturesString)
]