Files
llama.cpp/.devops/nix/package.nix
T

247 lines
7.2 KiB
Nix
Raw Normal View History

2023-12-29 06:42:26 -08:00
{
lib,
2024-03-05 02:33:08 +01:00
glibc,
2023-12-29 06:42:26 -08:00
config,
stdenv,
runCommand,
2023-12-29 06:42:26 -08:00
cmake,
ninja,
pkg-config,
git,
mpi,
2024-03-25 18:52:45 +01:00
blas,
2023-12-29 06:42:26 -08:00
cudaPackages,
autoAddDriverRunpath,
2023-12-29 06:42:26 -08:00
darwin,
rocmPackages,
2024-01-28 12:59:43 +01:00
vulkan-headers,
vulkan-loader,
2024-07-01 07:47:04 -04:00
curl,
2024-07-13 13:12:39 -03:00
shaderc,
2024-09-02 16:51:01 +05:30
useBlas ?
builtins.all (x: !x) [
useCuda
useMetalKit
useRocm
useVulkan
]
&& blas.meta.available,
2023-12-29 06:42:26 -08:00
useCuda ? config.cudaSupport,
2024-07-01 14:46:18 +03:00
useMetalKit ? stdenv.isAarch64 && stdenv.isDarwin,
2024-09-02 16:51:01 +05:30
# Increases the runtime closure size by ~700M
useMpi ? false,
2023-12-29 06:42:26 -08:00
useRocm ? config.rocmSupport,
rocmGpuTargets ? builtins.concatStringsSep ";" rocmPackages.clr.gpuTargets,
2024-07-01 07:47:04 -04:00
enableCurl ? true,
2024-01-28 12:59:43 +01:00
useVulkan ? false,
useRpc ? false,
2023-12-29 06:42:26 -08:00
llamaVersion ? "0.0.0", # Arbitrary version, substituted by the flake
2024-03-05 02:33:08 +01:00
# It's necessary to consistently use backendStdenv when building with CUDA support,
# otherwise we get libstdc++ errors downstream.
effectiveStdenv ? if useCuda then cudaPackages.backendStdenv else stdenv,
enableStatic ? effectiveStdenv.hostPlatform.isStatic,
2024-09-02 16:51:01 +05:30
precompileMetalShaders ? false,
}:
2023-12-29 06:42:26 -08:00
let
inherit (lib)
cmakeBool
cmakeFeature
optionalAttrs
2023-12-29 06:42:26 -08:00
optionals
strings
;
2024-03-05 15:12:23 +09:00
2023-12-29 06:42:26 -08:00
stdenv = throw "Use effectiveStdenv instead";
suffices =
lib.optionals useBlas [ "BLAS" ]
++ lib.optionals useCuda [ "CUDA" ]
++ lib.optionals useMetalKit [ "MetalKit" ]
++ lib.optionals useMpi [ "MPI" ]
2024-01-28 12:59:43 +01:00
++ lib.optionals useRocm [ "ROCm" ]
++ lib.optionals useVulkan [ "Vulkan" ];
2023-12-29 06:42:26 -08:00
pnameSuffix =
strings.optionalString (suffices != [ ])
"-${strings.concatMapStringsSep "-" strings.toLower suffices}";
2024-09-02 16:51:01 +05:30
descriptionSuffix = strings.optionalString (
suffices != [ ]
) ", accelerated with ${strings.concatStringsSep ", " suffices}";
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
xcrunHost = runCommand "xcrunHost" { } ''
mkdir -p $out/bin
ln -s /usr/bin/xcrun $out/bin
'';
2023-12-29 06:42:26 -08:00
# apple_sdk is supposed to choose sane defaults, no need to handle isAarch64
# separately
darwinBuildInputs =
with darwin.apple_sdk.frameworks;
[
Accelerate
CoreVideo
CoreGraphics
]
++ optionals useMetalKit [ MetalKit ];
cudaBuildInputs = with cudaPackages; [
cuda_cudart
cuda_cccl # <nv/target>
libcublas
2023-12-29 06:42:26 -08:00
];
rocmBuildInputs = with rocmPackages; [
clr
hipblas
rocblas
];
2024-01-28 12:59:43 +01:00
vulkanBuildInputs = [
vulkan-headers
vulkan-loader
2024-07-13 13:12:39 -03:00
shaderc
2024-01-28 12:59:43 +01:00
];
2023-12-29 06:42:26 -08:00
in
2024-09-02 16:51:01 +05:30
effectiveStdenv.mkDerivation (finalAttrs: {
pname = "llama-cpp${pnameSuffix}";
version = llamaVersion;
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
# 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.
src = lib.cleanSourceWith {
filter =
name: type:
let
noneOf = builtins.all (x: !x);
baseName = baseNameOf name;
in
noneOf [
(lib.hasSuffix ".nix" name) # Ignore *.nix files when computing outPaths
(lib.hasSuffix ".md" name) # Ignore *.md changes whe computing outPaths
(lib.hasPrefix "." baseName) # Skip hidden files and directories
(baseName == "flake.lock")
2023-12-29 06:42:26 -08:00
];
2024-09-02 16:51:01 +05:30
src = lib.cleanSource ../../.;
};
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
postPatch = ''
'';
2023-12-29 06:42:26 -08:00
2025-02-15 16:40:57 +02:00
# With PR#6015 https://github.com/ggml-org/llama.cpp/pull/6015,
2024-09-02 16:51:01 +05:30
# `default.metallib` may be compiled with Metal compiler from XCode
# and we need to escape sandbox on MacOS to access Metal compiler.
# `xcrun` is used find the path of the Metal compiler, which is varible
# and not on $PATH
2025-02-15 16:40:57 +02:00
# see https://github.com/ggml-org/llama.cpp/pull/6118 for discussion
2024-09-02 16:51:01 +05:30
__noChroot = effectiveStdenv.isDarwin && useMetalKit && precompileMetalShaders;
nativeBuildInputs =
[
cmake
ninja
pkg-config
git
]
++ optionals useCuda [
cudaPackages.cuda_nvcc
autoAddDriverRunpath
]
++ optionals (effectiveStdenv.hostPlatform.isGnu && enableStatic) [ glibc.static ]
++ optionals (effectiveStdenv.isDarwin && useMetalKit && precompileMetalShaders) [ xcrunHost ];
buildInputs =
optionals effectiveStdenv.isDarwin darwinBuildInputs
++ optionals useCuda cudaBuildInputs
++ optionals useMpi [ mpi ]
++ optionals useRocm rocmBuildInputs
++ optionals useBlas [ blas ]
++ optionals useVulkan vulkanBuildInputs
++ optionals enableCurl [ curl ];
cmakeFlags =
[
(cmakeBool "LLAMA_BUILD_SERVER" true)
(cmakeBool "BUILD_SHARED_LIBS" (!enableStatic))
(cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
(cmakeBool "LLAMA_CURL" enableCurl)
(cmakeBool "GGML_NATIVE" false)
(cmakeBool "GGML_BLAS" useBlas)
(cmakeBool "GGML_CUDA" useCuda)
2024-11-14 18:04:35 +01:00
(cmakeBool "GGML_HIP" useRocm)
2024-09-02 16:51:01 +05:30
(cmakeBool "GGML_METAL" useMetalKit)
(cmakeBool "GGML_VULKAN" useVulkan)
(cmakeBool "GGML_STATIC" enableStatic)
(cmakeBool "GGML_RPC" useRpc)
2024-09-02 16:51:01 +05:30
]
++ optionals useCuda [
(
with cudaPackages.flags;
cmakeFeature "CMAKE_CUDA_ARCHITECTURES" (
builtins.concatStringsSep ";" (map dropDot cudaCapabilities)
2023-12-29 06:42:26 -08:00
)
2024-09-02 16:51:01 +05:30
)
]
++ optionals useRocm [
(cmakeFeature "CMAKE_HIP_COMPILER" "${rocmPackages.llvm.clang}/bin/clang")
(cmakeFeature "CMAKE_HIP_ARCHITECTURES" rocmGpuTargets)
2024-09-02 16:51:01 +05:30
]
++ optionals useMetalKit [
(lib.cmakeFeature "CMAKE_C_FLAGS" "-D__ARM_FEATURE_DOTPROD=1")
(cmakeBool "GGML_METAL_EMBED_LIBRARY" (!precompileMetalShaders))
];
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
# Environment variables needed for ROCm
env = optionalAttrs useRocm {
2024-09-02 16:51:01 +05:30
ROCM_PATH = "${rocmPackages.clr}";
HIP_DEVICE_LIB_PATH = "${rocmPackages.rocm-device-libs}/amdgcn/bitcode";
};
2024-05-17 11:03:03 -04:00
2024-09-02 16:51:01 +05:30
# TODO(SomeoneSerge): It's better to add proper install targets at the CMake level,
# if they haven't been added yet.
postInstall = ''
mkdir -p $out/include
cp $src/include/llama.h $out/include/
'';
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
meta = {
# Configurations we don't want even the CI to evaluate. Results in the
# "unsupported platform" messages. This is mostly a no-op, because
# cudaPackages would've refused to evaluate anyway.
badPlatforms = optionals useCuda lib.platforms.darwin;
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
# Configurations that are known to result in build failures. Can be
# overridden by importing Nixpkgs with `allowBroken = true`.
broken = (useMetalKit && !effectiveStdenv.isDarwin);
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
description = "Inference of LLaMA model in pure C/C++${descriptionSuffix}";
2025-02-15 16:40:57 +02:00
homepage = "https://github.com/ggml-org/llama.cpp/";
2024-09-02 16:51:01 +05:30
license = lib.licenses.mit;
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
# Accommodates `nix run` and `lib.getExe`
mainProgram = "llama-cli";
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
# These people might respond, on the best effort basis, if you ping them
# in case of Nix-specific regressions or for reviewing Nix-specific PRs.
# Consider adding yourself to this list if you want to ensure this flake
# stays maintained and you're willing to invest your time. Do not add
# other people without their consent. Consider removing people after
# they've been unreachable for long periods of time.
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
# Note that lib.maintainers is defined in Nixpkgs, but you may just add
# an attrset following the same format as in
# https://github.com/NixOS/nixpkgs/blob/f36a80e54da29775c78d7eff0e628c2b4e34d1d7/maintainers/maintainer-list.nix
maintainers = with lib.maintainers; [
philiptaron
SomeoneSerge
];
2023-12-29 06:42:26 -08:00
2024-09-02 16:51:01 +05:30
# Extend `badPlatforms` instead
platforms = lib.platforms.all;
};
})