2023-03-17 23:03:48 +01:00
|
|
|
{
|
|
|
|
inputs = {
|
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
|
|
|
flake-utils.url = "github:numtide/flake-utils";
|
|
|
|
};
|
|
|
|
outputs = { self, nixpkgs, flake-utils }:
|
|
|
|
flake-utils.lib.eachDefaultSystem (system:
|
|
|
|
let
|
2023-08-26 20:19:44 +02:00
|
|
|
name = "llama.cpp";
|
|
|
|
src = ./.;
|
|
|
|
meta.mainProgram = "llama";
|
2023-07-21 12:26:34 +02:00
|
|
|
inherit (pkgs.stdenv) isAarch32 isAarch64 isDarwin;
|
2023-07-23 13:57:02 +02:00
|
|
|
buildInputs = with pkgs; [ openmpi ];
|
2023-10-31 18:24:03 +01:00
|
|
|
osSpecific = with pkgs; buildInputs ++ (
|
2023-07-19 09:01:55 +02:00
|
|
|
if isAarch64 && isDarwin then
|
|
|
|
with pkgs.darwin.apple_sdk_11_0.frameworks; [
|
|
|
|
Accelerate
|
|
|
|
MetalKit
|
|
|
|
]
|
|
|
|
else if isAarch32 && isDarwin then
|
|
|
|
with pkgs.darwin.apple_sdk.frameworks; [
|
|
|
|
Accelerate
|
|
|
|
CoreGraphics
|
|
|
|
CoreVideo
|
|
|
|
]
|
2023-08-26 15:25:39 +02:00
|
|
|
else if isDarwin then
|
|
|
|
with pkgs.darwin.apple_sdk.frameworks; [
|
|
|
|
Accelerate
|
|
|
|
CoreGraphics
|
|
|
|
CoreVideo
|
|
|
|
]
|
2023-07-19 09:01:55 +02:00
|
|
|
else
|
|
|
|
with pkgs; [ openblas ]
|
|
|
|
);
|
2023-06-24 13:07:08 +02:00
|
|
|
pkgs = import nixpkgs { inherit system; };
|
2023-09-15 10:10:22 +02:00
|
|
|
nativeBuildInputs = with pkgs; [ cmake ninja pkg-config ];
|
2023-09-25 13:48:30 +02:00
|
|
|
cudatoolkit_joined = with pkgs; symlinkJoin {
|
|
|
|
# HACK(Green-Sky): nix currently has issues with cmake findcudatoolkit
|
|
|
|
# see https://github.com/NixOS/nixpkgs/issues/224291
|
|
|
|
# copied from jaxlib
|
|
|
|
name = "${cudaPackages.cudatoolkit.name}-merged";
|
|
|
|
paths = [
|
|
|
|
cudaPackages.cudatoolkit.lib
|
|
|
|
cudaPackages.cudatoolkit.out
|
|
|
|
] ++ lib.optionals (lib.versionOlder cudaPackages.cudatoolkit.version "11") [
|
|
|
|
# for some reason some of the required libs are in the targets/x86_64-linux
|
|
|
|
# directory; not sure why but this works around it
|
|
|
|
"${cudaPackages.cudatoolkit}/targets/${system}"
|
|
|
|
];
|
|
|
|
};
|
2023-06-24 13:07:08 +02:00
|
|
|
llama-python =
|
2023-07-21 12:26:34 +02:00
|
|
|
pkgs.python3.withPackages (ps: with ps; [ numpy sentencepiece ]);
|
2023-10-28 16:41:07 +02:00
|
|
|
# TODO(Green-Sky): find a better way to opt-into the heavy ml python runtime
|
|
|
|
llama-python-extra =
|
|
|
|
pkgs.python3.withPackages (ps: with ps; [ numpy sentencepiece torchWithoutCuda transformers ]);
|
2023-07-23 13:57:02 +02:00
|
|
|
postPatch = ''
|
|
|
|
substituteInPlace ./ggml-metal.m \
|
|
|
|
--replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
|
|
|
|
substituteInPlace ./*.py --replace '/usr/bin/env python' '${llama-python}/bin/python'
|
|
|
|
'';
|
|
|
|
postInstall = ''
|
|
|
|
mv $out/bin/main $out/bin/llama
|
|
|
|
mv $out/bin/server $out/bin/llama-server
|
2023-09-14 20:54:47 +02:00
|
|
|
mkdir -p $out/include
|
2023-09-14 19:25:00 +02:00
|
|
|
cp ${src}/llama.h $out/include/
|
2023-07-23 13:57:02 +02:00
|
|
|
'';
|
2023-10-03 18:53:15 +02:00
|
|
|
cmakeFlags = [ "-DLLAMA_NATIVE=OFF" "-DLLAMA_BUILD_SERVER=ON" "-DBUILD_SHARED_LIBS=ON" "-DCMAKE_SKIP_BUILD_RPATH=ON" ];
|
2023-08-26 20:19:44 +02:00
|
|
|
in
|
|
|
|
{
|
2023-03-17 23:03:48 +01:00
|
|
|
packages.default = pkgs.stdenv.mkDerivation {
|
2023-09-20 15:48:22 +02:00
|
|
|
inherit name src meta postPatch nativeBuildInputs postInstall;
|
|
|
|
buildInputs = osSpecific;
|
2023-07-23 13:57:02 +02:00
|
|
|
cmakeFlags = cmakeFlags
|
2023-07-19 09:01:55 +02:00
|
|
|
++ (if isAarch64 && isDarwin then [
|
2023-08-26 20:19:44 +02:00
|
|
|
"-DCMAKE_C_FLAGS=-D__ARM_FEATURE_DOTPROD=1"
|
|
|
|
"-DLLAMA_METAL=ON"
|
|
|
|
] else [
|
|
|
|
"-DLLAMA_BLAS=ON"
|
|
|
|
"-DLLAMA_BLAS_VENDOR=OpenBLAS"
|
2023-06-07 06:15:31 +02:00
|
|
|
]);
|
2023-07-23 13:57:02 +02:00
|
|
|
};
|
|
|
|
packages.opencl = pkgs.stdenv.mkDerivation {
|
2023-08-26 20:19:44 +02:00
|
|
|
inherit name src meta postPatch nativeBuildInputs postInstall;
|
2023-07-23 13:57:02 +02:00
|
|
|
buildInputs = with pkgs; buildInputs ++ [ clblast ];
|
|
|
|
cmakeFlags = cmakeFlags ++ [
|
|
|
|
"-DLLAMA_CLBLAST=ON"
|
|
|
|
];
|
2023-08-26 20:19:44 +02:00
|
|
|
};
|
2023-09-25 13:48:30 +02:00
|
|
|
packages.cuda = pkgs.stdenv.mkDerivation {
|
|
|
|
inherit name src meta postPatch nativeBuildInputs postInstall;
|
|
|
|
buildInputs = with pkgs; buildInputs ++ [ cudatoolkit_joined ];
|
|
|
|
cmakeFlags = cmakeFlags ++ [
|
|
|
|
"-DLLAMA_CUBLAS=ON"
|
|
|
|
];
|
|
|
|
};
|
2023-08-26 20:19:44 +02:00
|
|
|
packages.rocm = pkgs.stdenv.mkDerivation {
|
|
|
|
inherit name src meta postPatch nativeBuildInputs postInstall;
|
2023-10-31 18:24:03 +01:00
|
|
|
buildInputs = with pkgs.rocmPackages; buildInputs ++ [ clr hipblas rocblas ];
|
2023-08-26 20:19:44 +02:00
|
|
|
cmakeFlags = cmakeFlags ++ [
|
|
|
|
"-DLLAMA_HIPBLAS=1"
|
|
|
|
"-DCMAKE_C_COMPILER=hipcc"
|
|
|
|
"-DCMAKE_CXX_COMPILER=hipcc"
|
2023-10-31 18:24:03 +01:00
|
|
|
# Build all targets supported by rocBLAS. When updating search for TARGET_LIST_ROCM
|
|
|
|
# in github.com/ROCmSoftwarePlatform/rocBLAS/blob/develop/CMakeLists.txt
|
|
|
|
# and select the line that matches the current nixpkgs version of rocBLAS.
|
|
|
|
"-DAMDGPU_TARGETS=gfx803;gfx900;gfx906:xnack-;gfx908:xnack-;gfx90a:xnack+;gfx90a:xnack-;gfx940;gfx941;gfx942;gfx1010;gfx1012;gfx1030;gfx1100;gfx1101;gfx1102"
|
2023-08-26 20:19:44 +02:00
|
|
|
];
|
2023-03-17 23:03:48 +01:00
|
|
|
};
|
2023-06-17 14:13:05 +02:00
|
|
|
apps.llama-server = {
|
|
|
|
type = "app";
|
|
|
|
program = "${self.packages.${system}.default}/bin/llama-server";
|
|
|
|
};
|
|
|
|
apps.llama-embedding = {
|
|
|
|
type = "app";
|
|
|
|
program = "${self.packages.${system}.default}/bin/embedding";
|
|
|
|
};
|
|
|
|
apps.llama = {
|
|
|
|
type = "app";
|
|
|
|
program = "${self.packages.${system}.default}/bin/llama";
|
|
|
|
};
|
2023-08-26 15:25:39 +02:00
|
|
|
apps.quantize = {
|
|
|
|
type = "app";
|
|
|
|
program = "${self.packages.${system}.default}/bin/quantize";
|
|
|
|
};
|
2023-09-08 18:06:26 +02:00
|
|
|
apps.train-text-from-scratch = {
|
|
|
|
type = "app";
|
|
|
|
program = "${self.packages.${system}.default}/bin/train-text-from-scratch";
|
|
|
|
};
|
2023-06-17 14:13:05 +02:00
|
|
|
apps.default = self.apps.${system}.llama;
|
2023-03-17 23:03:48 +01:00
|
|
|
devShells.default = pkgs.mkShell {
|
2023-08-26 15:25:39 +02:00
|
|
|
buildInputs = [ llama-python ];
|
2023-07-21 12:26:34 +02:00
|
|
|
packages = nativeBuildInputs ++ osSpecific;
|
2023-03-17 23:03:48 +01:00
|
|
|
};
|
2023-10-28 16:41:07 +02:00
|
|
|
devShells.extra = pkgs.mkShell {
|
|
|
|
buildInputs = [ llama-python-extra ];
|
|
|
|
packages = nativeBuildInputs ++ osSpecific;
|
|
|
|
};
|
2023-06-24 13:07:08 +02:00
|
|
|
});
|
2023-03-17 23:03:48 +01:00
|
|
|
}
|