From 8e426df698927ca85b1431a68046ef8477157a38 Mon Sep 17 00:00:00 2001 From: hacker1024 Date: Fri, 18 Apr 2025 13:52:44 +1000 Subject: [PATCH 1/9] build(nix): Use a split derivation --- .devops/nix/package.nix | 2 ++ .gitignore | 1 + 2 files changed, 3 insertions(+) diff --git a/.devops/nix/package.nix b/.devops/nix/package.nix index 6e8050a499635..2553c83b7cb4c 100644 --- a/.devops/nix/package.nix +++ b/.devops/nix/package.nix @@ -107,6 +107,8 @@ effectiveStdenv.mkDerivation (finalAttrs: { pname = "llama-cpp${pnameSuffix}"; version = llamaVersion; + outputs = [ "out" "lib" "dev" ]; + # Note: none of the files discarded here are visible in the sandbox or # affect the output hash. This also means they can be modified without # triggering a rebuild. diff --git a/.gitignore b/.gitignore index 2c67ad7f7c609..1ae37525fbd1c 100644 --- a/.gitignore +++ b/.gitignore @@ -121,6 +121,7 @@ poetry.toml # Nix /result +/result-* # Test binaries /tests/test-backend-ops From b507bb4bf19a280801bcb2abdc064e75b6f6cdd9 Mon Sep 17 00:00:00 2001 From: hacker1024 Date: Fri, 18 Apr 2025 14:20:27 +1000 Subject: [PATCH 2/9] build(nix): Move shaderc to nativeBuildInputs --- .devops/nix/package.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.devops/nix/package.nix b/.devops/nix/package.nix index 2553c83b7cb4c..200fd9e72a0f3 100644 --- a/.devops/nix/package.nix +++ b/.devops/nix/package.nix @@ -96,10 +96,13 @@ let rocblas ]; + vulkanNativeBuildInputs = [ + shaderc + ]; + vulkanBuildInputs = [ vulkan-headers vulkan-loader - shaderc ]; in @@ -155,6 +158,7 @@ effectiveStdenv.mkDerivation (finalAttrs: { autoAddDriverRunpath ] + ++ optionals useVulkan vulkanNativeBuildInputs ++ optionals (effectiveStdenv.hostPlatform.isGnu && enableStatic) [ glibc.static ] ++ optionals (effectiveStdenv.isDarwin && useMetalKit && precompileMetalShaders) [ xcrunHost ]; From bf1f1b31becde74ee512bc77302607b58759b85d Mon Sep 17 00:00:00 2001 From: hacker1024 Date: Fri, 18 Apr 2025 14:42:45 +1000 Subject: [PATCH 3/9] build(nix): Propagate BLAS appropriately --- .devops/nix/package.nix | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.devops/nix/package.nix b/.devops/nix/package.nix index 200fd9e72a0f3..4554756911bff 100644 --- a/.devops/nix/package.nix +++ b/.devops/nix/package.nix @@ -150,7 +150,6 @@ effectiveStdenv.mkDerivation (finalAttrs: { [ cmake ninja - pkg-config git ] ++ optionals useCuda [ @@ -171,6 +170,11 @@ effectiveStdenv.mkDerivation (finalAttrs: { ++ optionals useVulkan vulkanBuildInputs ++ optionals enableCurl [ curl ]; + propagatedNativeBuildInputs = [ pkg-config ]; + + propagatedBuildInputs = + optionals useBlas [ blas ]; + cmakeFlags = [ (cmakeBool "LLAMA_BUILD_SERVER" true) @@ -184,6 +188,7 @@ effectiveStdenv.mkDerivation (finalAttrs: { (cmakeBool "GGML_METAL" useMetalKit) (cmakeBool "GGML_VULKAN" useVulkan) (cmakeBool "GGML_STATIC" enableStatic) + (cmakeBool "BLA_PREFER_PKGCONFIG" true) ] ++ optionals useCuda [ ( From b1f01c71265677874c6c92e2055d55e76ac0cd75 Mon Sep 17 00:00:00 2001 From: hacker1024 Date: Fri, 18 Apr 2025 14:43:16 +1000 Subject: [PATCH 4/9] build(nix): Allow using OpenMP --- .devops/nix/package.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.devops/nix/package.nix b/.devops/nix/package.nix index 4554756911bff..c84e526a6e75b 100644 --- a/.devops/nix/package.nix +++ b/.devops/nix/package.nix @@ -13,6 +13,7 @@ cudaPackages, autoAddDriverRunpath, darwin, + llvmPackages, rocmPackages, vulkan-headers, vulkan-loader, @@ -34,6 +35,7 @@ rocmGpuTargets ? builtins.concatStringsSep ";" rocmPackages.clr.gpuTargets, enableCurl ? true, useVulkan ? false, + useOpenMP ? false, llamaVersion ? "0.0.0", # Arbitrary version, substituted by the flake # It's necessary to consistently use backendStdenv when building with CUDA support, @@ -173,7 +175,8 @@ effectiveStdenv.mkDerivation (finalAttrs: { propagatedNativeBuildInputs = [ pkg-config ]; propagatedBuildInputs = - optionals useBlas [ blas ]; + optionals useBlas [ blas ] + ++ optionals useOpenMP [ llvmPackages.openmp ]; cmakeFlags = [ @@ -187,6 +190,7 @@ effectiveStdenv.mkDerivation (finalAttrs: { (cmakeBool "GGML_HIP" useRocm) (cmakeBool "GGML_METAL" useMetalKit) (cmakeBool "GGML_VULKAN" useVulkan) + (cmakeBool "GGML_OPENMP" useOpenMP) (cmakeBool "GGML_STATIC" enableStatic) (cmakeBool "BLA_PREFER_PKGCONFIG" true) ] From 60092b405aab2f99a295f0787c6090b68ea9d640 Mon Sep 17 00:00:00 2001 From: hacker1024 Date: Fri, 18 Apr 2025 15:10:41 +1000 Subject: [PATCH 5/9] cmake: Only link stdc++fs on GCC --- ggml/src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ggml/src/CMakeLists.txt b/ggml/src/CMakeLists.txt index f00700da71fcd..a1343169f18ee 100644 --- a/ggml/src/CMakeLists.txt +++ b/ggml/src/CMakeLists.txt @@ -213,7 +213,7 @@ add_library(ggml target_link_libraries(ggml PUBLIC ggml-base) -if (CMAKE_SYSTEM_NAME MATCHES "Linux") +if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") target_link_libraries(ggml PRIVATE dl stdc++fs) endif() From 70a689e577e967eb4dcb37fc9c2304f0b4ec2804 Mon Sep 17 00:00:00 2001 From: hacker1024 Date: Fri, 18 Apr 2025 15:13:20 +1000 Subject: [PATCH 6/9] build(nix): Add hacker1024 as a Nix package maintainer --- .devops/nix/package.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/.devops/nix/package.nix b/.devops/nix/package.nix index c84e526a6e75b..96117ce7f5ffb 100644 --- a/.devops/nix/package.nix +++ b/.devops/nix/package.nix @@ -254,6 +254,7 @@ effectiveStdenv.mkDerivation (finalAttrs: { maintainers = with lib.maintainers; [ philiptaron SomeoneSerge + hacker1024 ]; # Extend `badPlatforms` instead From 4bffb15be6b46b410a5b450bbadd9ea3a7fe810c Mon Sep 17 00:00:00 2001 From: hacker1024 Date: Fri, 18 Apr 2025 14:59:50 +1000 Subject: [PATCH 7/9] build(nix): Allow disabling common, tests, examples, server --- .devops/nix/package.nix | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/.devops/nix/package.nix b/.devops/nix/package.nix index 96117ce7f5ffb..cdc9c9a09e3d4 100644 --- a/.devops/nix/package.nix +++ b/.devops/nix/package.nix @@ -36,6 +36,9 @@ enableCurl ? true, useVulkan ? false, useOpenMP ? false, + buildCommon ? true, + buildExamples ? buildCommon, + buildServer ? buildExamples, llamaVersion ? "0.0.0", # Arbitrary version, substituted by the flake # It's necessary to consistently use backendStdenv when building with CUDA support, @@ -51,6 +54,7 @@ let cmakeFeature optionals strings + assertMsg ; stdenv = throw "Use effectiveStdenv instead"; @@ -108,6 +112,9 @@ let ]; in +assert assertMsg (buildExamples -> buildCommon) "buildCommon must be true when buildExamples is true"; +assert assertMsg (buildServer -> buildExamples) "buildExamples must be true when buildServer is true"; + effectiveStdenv.mkDerivation (finalAttrs: { pname = "llama-cpp${pnameSuffix}"; version = llamaVersion; @@ -180,7 +187,6 @@ effectiveStdenv.mkDerivation (finalAttrs: { cmakeFlags = [ - (cmakeBool "LLAMA_BUILD_SERVER" true) (cmakeBool "BUILD_SHARED_LIBS" (!enableStatic)) (cmakeBool "CMAKE_SKIP_BUILD_RPATH" true) (cmakeBool "LLAMA_CURL" enableCurl) @@ -192,6 +198,10 @@ effectiveStdenv.mkDerivation (finalAttrs: { (cmakeBool "GGML_VULKAN" useVulkan) (cmakeBool "GGML_OPENMP" useOpenMP) (cmakeBool "GGML_STATIC" enableStatic) + (cmakeBool "LLAMA_BUILD_COMMON" (buildCommon || finalAttrs.doCheck or false)) + (cmakeBool "LLAMA_BUILD_TESTS" finalAttrs.doCheck or false) + (cmakeBool "LLAMA_BUILD_EXAMPLES" buildExamples) + (cmakeBool "LLAMA_BUILD_SERVER" buildServer) (cmakeBool "BLA_PREFER_PKGCONFIG" true) ] ++ optionals useCuda [ From ff1ac5c4d0107ff5561992ac33951be25546bd03 Mon Sep 17 00:00:00 2001 From: hacker1024 Date: Fri, 18 Apr 2025 15:50:33 +1000 Subject: [PATCH 8/9] build(nix): Don't set CMAKE_SKIP_BUILD_RPATH This no longer appears to be needed, and it breaks test execution. --- .devops/nix/package.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/.devops/nix/package.nix b/.devops/nix/package.nix index cdc9c9a09e3d4..5521a71ad0a41 100644 --- a/.devops/nix/package.nix +++ b/.devops/nix/package.nix @@ -188,7 +188,6 @@ effectiveStdenv.mkDerivation (finalAttrs: { cmakeFlags = [ (cmakeBool "BUILD_SHARED_LIBS" (!enableStatic)) - (cmakeBool "CMAKE_SKIP_BUILD_RPATH" true) (cmakeBool "LLAMA_CURL" enableCurl) (cmakeBool "GGML_NATIVE" false) (cmakeBool "GGML_BLAS" useBlas) From 4a4a7536c985ddd5285a4fc5019abf8770e7dd53 Mon Sep 17 00:00:00 2001 From: hacker1024 Date: Fri, 18 Apr 2025 15:50:44 +1000 Subject: [PATCH 9/9] build(nix): Enable tests --- .devops/nix/package.nix | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.devops/nix/package.nix b/.devops/nix/package.nix index 5521a71ad0a41..0b50f547fc629 100644 --- a/.devops/nix/package.nix +++ b/.devops/nix/package.nix @@ -202,6 +202,7 @@ effectiveStdenv.mkDerivation (finalAttrs: { (cmakeBool "LLAMA_BUILD_EXAMPLES" buildExamples) (cmakeBool "LLAMA_BUILD_SERVER" buildServer) (cmakeBool "BLA_PREFER_PKGCONFIG" true) + (cmakeFeature "CMAKE_CTEST_ARGUMENTS" "--exclude-regex;test-eval-callback") # Uses Internet ] ++ optionals useCuda [ ( @@ -233,6 +234,8 @@ effectiveStdenv.mkDerivation (finalAttrs: { cp $src/include/llama.h $out/include/ ''; + doCheck = true; + meta = { # Configurations we don't want even the CI to evaluate. Results in the # "unsupported platform" messages. This is mostly a no-op, because