2023-03-17 22:03:48 +00: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 18:19:44 +00:00
|
|
|
name = "llama.cpp";
|
|
|
|
src = ./.;
|
|
|
|
meta.mainProgram = "llama";
|
2023-07-21 10:26:34 +00:00
|
|
|
inherit (pkgs.stdenv) isAarch32 isAarch64 isDarwin;
|
2023-07-23 11:57:02 +00:00
|
|
|
buildInputs = with pkgs; [ openmpi ];
|
|
|
|
osSpecific = with pkgs; buildInputs ++
|
2023-07-19 07:01:55 +00: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 13:25:39 +00:00
|
|
|
else if isDarwin then
|
|
|
|
with pkgs.darwin.apple_sdk.frameworks; [
|
|
|
|
Accelerate
|
|
|
|
CoreGraphics
|
|
|
|
CoreVideo
|
|
|
|
]
|
2023-07-19 07:01:55 +00:00
|
|
|
else
|
|
|
|
with pkgs; [ openblas ]
|
|
|
|
);
|
2023-06-24 11:07:08 +00:00
|
|
|
pkgs = import nixpkgs { inherit system; };
|
2023-08-26 18:19:44 +00:00
|
|
|
nativeBuildInputs = with pkgs; [ cmake ninja pkgconfig ];
|
2023-06-24 11:07:08 +00:00
|
|
|
llama-python =
|
2023-07-21 10:26:34 +00:00
|
|
|
pkgs.python3.withPackages (ps: with ps; [ numpy sentencepiece ]);
|
2023-07-23 11:57:02 +00: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
|
|
|
|
'';
|
|
|
|
cmakeFlags = [ "-DLLAMA_BUILD_SERVER=ON" "-DLLAMA_MPI=ON" "-DBUILD_SHARED_LIBS=ON" "-DCMAKE_SKIP_BUILD_RPATH=ON" ];
|
2023-08-26 18:19:44 +00:00
|
|
|
in
|
|
|
|
{
|
2023-03-17 22:03:48 +00:00
|
|
|
packages.default = pkgs.stdenv.mkDerivation {
|
2023-08-26 18:19:44 +00:00
|
|
|
inherit name src meta postPatch nativeBuildInputs buildInputs postInstall;
|
2023-07-23 11:57:02 +00:00
|
|
|
cmakeFlags = cmakeFlags
|
2023-07-19 07:01:55 +00:00
|
|
|
++ (if isAarch64 && isDarwin then [
|
2023-08-26 18:19:44 +00:00
|
|
|
"-DCMAKE_C_FLAGS=-D__ARM_FEATURE_DOTPROD=1"
|
|
|
|
"-DLLAMA_METAL=ON"
|
|
|
|
] else [
|
|
|
|
"-DLLAMA_BLAS=ON"
|
|
|
|
"-DLLAMA_BLAS_VENDOR=OpenBLAS"
|
2023-06-07 04:15:31 +00:00
|
|
|
]);
|
2023-07-23 11:57:02 +00:00
|
|
|
};
|
|
|
|
packages.opencl = pkgs.stdenv.mkDerivation {
|
2023-08-26 18:19:44 +00:00
|
|
|
inherit name src meta postPatch nativeBuildInputs postInstall;
|
2023-07-23 11:57:02 +00:00
|
|
|
buildInputs = with pkgs; buildInputs ++ [ clblast ];
|
|
|
|
cmakeFlags = cmakeFlags ++ [
|
|
|
|
"-DLLAMA_CLBLAST=ON"
|
|
|
|
];
|
2023-08-26 18:19:44 +00:00
|
|
|
};
|
|
|
|
packages.rocm = pkgs.stdenv.mkDerivation {
|
|
|
|
inherit name src meta postPatch nativeBuildInputs postInstall;
|
|
|
|
buildInputs = with pkgs; buildInputs ++ [ hip hipblas rocblas ];
|
|
|
|
cmakeFlags = cmakeFlags ++ [
|
|
|
|
"-DLLAMA_HIPBLAS=1"
|
|
|
|
"-DCMAKE_C_COMPILER=hipcc"
|
|
|
|
"-DCMAKE_CXX_COMPILER=hipcc"
|
|
|
|
"-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
|
|
|
|
];
|
2023-03-17 22:03:48 +00:00
|
|
|
};
|
2023-06-17 12:13:05 +00: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 13:25:39 +00:00
|
|
|
apps.quantize = {
|
|
|
|
type = "app";
|
|
|
|
program = "${self.packages.${system}.default}/bin/quantize";
|
|
|
|
};
|
2023-09-08 16:06:26 +00:00
|
|
|
apps.train-text-from-scratch = {
|
|
|
|
type = "app";
|
|
|
|
program = "${self.packages.${system}.default}/bin/train-text-from-scratch";
|
|
|
|
};
|
2023-06-17 12:13:05 +00:00
|
|
|
apps.default = self.apps.${system}.llama;
|
2023-03-17 22:03:48 +00:00
|
|
|
devShells.default = pkgs.mkShell {
|
2023-08-26 13:25:39 +00:00
|
|
|
buildInputs = [ llama-python ];
|
2023-07-21 10:26:34 +00:00
|
|
|
packages = nativeBuildInputs ++ osSpecific;
|
2023-03-17 22:03:48 +00:00
|
|
|
};
|
2023-06-24 11:07:08 +00:00
|
|
|
});
|
2023-03-17 22:03:48 +00:00
|
|
|
}
|