2023-12-29 14:42:26 +00:00
|
|
|
{
|
|
|
|
lib,
|
2024-03-05 01:33:08 +00:00
|
|
|
glibc,
|
2023-12-29 14:42:26 +00:00
|
|
|
config,
|
|
|
|
stdenv,
|
2024-03-26 00:51:46 +00:00
|
|
|
runCommand,
|
2023-12-29 14:42:26 +00:00
|
|
|
cmake,
|
|
|
|
ninja,
|
|
|
|
pkg-config,
|
|
|
|
git,
|
|
|
|
mpi,
|
2024-03-25 17:52:45 +00:00
|
|
|
blas,
|
2023-12-29 14:42:26 +00:00
|
|
|
cudaPackages,
|
2024-08-22 21:19:14 +00:00
|
|
|
autoAddDriverRunpath,
|
2023-12-29 14:42:26 +00:00
|
|
|
darwin,
|
|
|
|
rocmPackages,
|
2024-01-28 11:59:43 +00:00
|
|
|
vulkan-headers,
|
|
|
|
vulkan-loader,
|
2024-07-01 11:47:04 +00:00
|
|
|
curl,
|
2024-07-13 16:12:39 +00:00
|
|
|
shaderc,
|
2024-09-02 11:21:01 +00:00
|
|
|
useBlas ?
|
|
|
|
builtins.all (x: !x) [
|
|
|
|
useCuda
|
|
|
|
useMetalKit
|
|
|
|
useRocm
|
|
|
|
useVulkan
|
|
|
|
]
|
|
|
|
&& blas.meta.available,
|
2023-12-29 14:42:26 +00:00
|
|
|
useCuda ? config.cudaSupport,
|
2024-07-01 11:46:18 +00:00
|
|
|
useMetalKit ? stdenv.isAarch64 && stdenv.isDarwin,
|
2024-09-02 11:21:01 +00:00
|
|
|
# Increases the runtime closure size by ~700M
|
|
|
|
useMpi ? false,
|
2023-12-29 14:42:26 +00:00
|
|
|
useRocm ? config.rocmSupport,
|
2024-12-14 18:17:36 +00:00
|
|
|
rocmGpuTargets ? builtins.concatStringsSep ";" rocmPackages.clr.gpuTargets,
|
2024-07-01 11:47:04 +00:00
|
|
|
enableCurl ? true,
|
2024-01-28 11:59:43 +00:00
|
|
|
useVulkan ? false,
|
2023-12-29 14:42:26 +00:00
|
|
|
llamaVersion ? "0.0.0", # Arbitrary version, substituted by the flake
|
2024-03-05 01:33:08 +00: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,
|
2024-03-26 00:51:46 +00:00
|
|
|
enableStatic ? effectiveStdenv.hostPlatform.isStatic,
|
2024-09-02 11:21:01 +00:00
|
|
|
precompileMetalShaders ? false,
|
|
|
|
}:
|
2023-12-29 14:42:26 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
inherit (lib)
|
|
|
|
cmakeBool
|
|
|
|
cmakeFeature
|
|
|
|
optionals
|
|
|
|
strings
|
|
|
|
;
|
2024-03-05 06:12:23 +00:00
|
|
|
|
2023-12-29 14:42:26 +00: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 11:59:43 +00:00
|
|
|
++ lib.optionals useRocm [ "ROCm" ]
|
|
|
|
++ lib.optionals useVulkan [ "Vulkan" ];
|
2023-12-29 14:42:26 +00:00
|
|
|
|
|
|
|
pnameSuffix =
|
|
|
|
strings.optionalString (suffices != [ ])
|
|
|
|
"-${strings.concatMapStringsSep "-" strings.toLower suffices}";
|
2024-09-02 11:21:01 +00:00
|
|
|
descriptionSuffix = strings.optionalString (
|
|
|
|
suffices != [ ]
|
|
|
|
) ", accelerated with ${strings.concatStringsSep ", " suffices}";
|
2023-12-29 14:42:26 +00:00
|
|
|
|
2024-09-02 11:21:01 +00:00
|
|
|
xcrunHost = runCommand "xcrunHost" { } ''
|
2024-03-26 00:51:46 +00:00
|
|
|
mkdir -p $out/bin
|
|
|
|
ln -s /usr/bin/xcrun $out/bin
|
|
|
|
'';
|
|
|
|
|
2023-12-29 14:42:26 +00: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; [
|
2024-07-30 20:35:30 +00:00
|
|
|
cuda_cudart
|
|
|
|
cuda_cccl # <nv/target>
|
|
|
|
libcublas
|
2023-12-29 14:42:26 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
rocmBuildInputs = with rocmPackages; [
|
|
|
|
clr
|
|
|
|
hipblas
|
|
|
|
rocblas
|
|
|
|
];
|
2024-01-28 11:59:43 +00:00
|
|
|
|
|
|
|
vulkanBuildInputs = [
|
|
|
|
vulkan-headers
|
|
|
|
vulkan-loader
|
2024-07-13 16:12:39 +00:00
|
|
|
shaderc
|
2024-01-28 11:59:43 +00:00
|
|
|
];
|
2023-12-29 14:42:26 +00:00
|
|
|
in
|
|
|
|
|
2024-09-02 11:21:01 +00:00
|
|
|
effectiveStdenv.mkDerivation (finalAttrs: {
|
|
|
|
pname = "llama-cpp${pnameSuffix}";
|
|
|
|
version = llamaVersion;
|
|
|
|
|
|
|
|
# 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")
|
2024-03-26 00:51:46 +00:00
|
|
|
];
|
2024-09-02 11:21:01 +00:00
|
|
|
src = lib.cleanSource ../../.;
|
|
|
|
};
|
|
|
|
|
|
|
|
postPatch = ''
|
2024-11-14 17:04:35 +00:00
|
|
|
substituteInPlace ./ggml/src/ggml-metal/ggml-metal.m \
|
2024-09-02 11:21:01 +00:00
|
|
|
--replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
|
2024-11-14 17:04:35 +00:00
|
|
|
substituteInPlace ./ggml/src/ggml-metal/ggml-metal.m \
|
2024-09-02 11:21:01 +00:00
|
|
|
--replace '[bundle pathForResource:@"default" ofType:@"metallib"];' "@\"$out/bin/default.metallib\";"
|
|
|
|
'';
|
2023-12-29 14:42:26 +00:00
|
|
|
|
2024-09-02 11:21:01 +00:00
|
|
|
# With PR#6015 https://github.com/ggerganov/llama.cpp/pull/6015,
|
|
|
|
# `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
|
|
|
|
# see https://github.com/ggerganov/llama.cpp/pull/6118 for discussion
|
|
|
|
__noChroot = effectiveStdenv.isDarwin && useMetalKit && precompileMetalShaders;
|
2023-12-29 14:42:26 +00:00
|
|
|
|
2024-09-02 11:21:01 +00:00
|
|
|
nativeBuildInputs =
|
|
|
|
[
|
|
|
|
cmake
|
|
|
|
ninja
|
|
|
|
pkg-config
|
|
|
|
git
|
|
|
|
]
|
|
|
|
++ optionals useCuda [
|
|
|
|
cudaPackages.cuda_nvcc
|
2023-12-29 14:42:26 +00:00
|
|
|
|
2024-09-02 11:21:01 +00:00
|
|
|
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 17:04:35 +00:00
|
|
|
(cmakeBool "GGML_HIP" useRocm)
|
2024-09-02 11:21:01 +00:00
|
|
|
(cmakeBool "GGML_METAL" useMetalKit)
|
|
|
|
(cmakeBool "GGML_VULKAN" useVulkan)
|
|
|
|
(cmakeBool "GGML_STATIC" enableStatic)
|
|
|
|
]
|
|
|
|
++ optionals useCuda [
|
|
|
|
(
|
|
|
|
with cudaPackages.flags;
|
|
|
|
cmakeFeature "CMAKE_CUDA_ARCHITECTURES" (
|
|
|
|
builtins.concatStringsSep ";" (map dropDot cudaCapabilities)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
++ optionals useRocm [
|
|
|
|
(cmakeFeature "CMAKE_HIP_COMPILER" "${rocmPackages.llvm.clang}/bin/clang")
|
2024-12-14 18:17:36 +00:00
|
|
|
(cmakeFeature "CMAKE_HIP_ARCHITECTURES" rocmGpuTargets)
|
2024-09-02 11:21:01 +00:00
|
|
|
]
|
|
|
|
++ optionals useMetalKit [
|
|
|
|
(lib.cmakeFeature "CMAKE_C_FLAGS" "-D__ARM_FEATURE_DOTPROD=1")
|
|
|
|
(cmakeBool "GGML_METAL_EMBED_LIBRARY" (!precompileMetalShaders))
|
|
|
|
];
|
|
|
|
|
|
|
|
# Environment variables needed for ROCm
|
|
|
|
env = optionals useRocm {
|
|
|
|
ROCM_PATH = "${rocmPackages.clr}";
|
|
|
|
HIP_DEVICE_LIB_PATH = "${rocmPackages.rocm-device-libs}/amdgcn/bitcode";
|
|
|
|
};
|
|
|
|
|
|
|
|
# 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 14:42:26 +00:00
|
|
|
|
2024-09-02 11:21:01 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
# Configurations that are known to result in build failures. Can be
|
|
|
|
# overridden by importing Nixpkgs with `allowBroken = true`.
|
|
|
|
broken = (useMetalKit && !effectiveStdenv.isDarwin);
|
|
|
|
|
|
|
|
description = "Inference of LLaMA model in pure C/C++${descriptionSuffix}";
|
|
|
|
homepage = "https://github.com/ggerganov/llama.cpp/";
|
|
|
|
license = lib.licenses.mit;
|
|
|
|
|
|
|
|
# Accommodates `nix run` and `lib.getExe`
|
|
|
|
mainProgram = "llama-cli";
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
# 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
|
|
|
|
];
|
|
|
|
|
|
|
|
# Extend `badPlatforms` instead
|
|
|
|
platforms = lib.platforms.all;
|
|
|
|
};
|
|
|
|
})
|