flake.nix 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. {
  2. inputs = {
  3. nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  4. flake-utils.url = "github:numtide/flake-utils";
  5. };
  6. outputs = { self, nixpkgs, flake-utils }:
  7. flake-utils.lib.eachDefaultSystem (system:
  8. let
  9. inherit (pkgs.stdenv) isAarch32 isAarch64 isDarwin;
  10. buildInputs = with pkgs; [ openmpi ];
  11. osSpecific = with pkgs; buildInputs ++
  12. (
  13. if isAarch64 && isDarwin then
  14. with pkgs.darwin.apple_sdk_11_0.frameworks; [
  15. Accelerate
  16. MetalKit
  17. ]
  18. else if isAarch32 && isDarwin then
  19. with pkgs.darwin.apple_sdk.frameworks; [
  20. Accelerate
  21. CoreGraphics
  22. CoreVideo
  23. ]
  24. else
  25. with pkgs; [ openblas ]
  26. );
  27. pkgs = import nixpkgs { inherit system; };
  28. nativeBuildInputs = with pkgs; [ cmake pkgconfig ];
  29. llama-python =
  30. pkgs.python3.withPackages (ps: with ps; [ numpy sentencepiece ]);
  31. postPatch = ''
  32. substituteInPlace ./ggml-metal.m \
  33. --replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
  34. substituteInPlace ./*.py --replace '/usr/bin/env python' '${llama-python}/bin/python'
  35. '';
  36. postInstall = ''
  37. mv $out/bin/main $out/bin/llama
  38. mv $out/bin/server $out/bin/llama-server
  39. '';
  40. cmakeFlags = [ "-DLLAMA_BUILD_SERVER=ON" "-DLLAMA_MPI=ON" "-DBUILD_SHARED_LIBS=ON" "-DCMAKE_SKIP_BUILD_RPATH=ON" ];
  41. in {
  42. packages.default = pkgs.stdenv.mkDerivation {
  43. name = "llama.cpp";
  44. src = ./.;
  45. postPatch = postPatch;
  46. nativeBuildInputs = nativeBuildInputs;
  47. buildInputs = osSpecific;
  48. cmakeFlags = cmakeFlags
  49. ++ (if isAarch64 && isDarwin then [
  50. "-DCMAKE_C_FLAGS=-D__ARM_FEATURE_DOTPROD=1"
  51. "-DLLAMA_METAL=ON"
  52. ] else [
  53. "-DLLAMA_BLAS=ON"
  54. "-DLLAMA_BLAS_VENDOR=OpenBLAS"
  55. ]);
  56. postInstall = postInstall;
  57. meta.mainProgram = "llama";
  58. };
  59. packages.opencl = pkgs.stdenv.mkDerivation {
  60. name = "llama.cpp";
  61. src = ./.;
  62. postPatch = postPatch;
  63. nativeBuildInputs = nativeBuildInputs;
  64. buildInputs = with pkgs; buildInputs ++ [ clblast ];
  65. cmakeFlags = cmakeFlags ++ [
  66. "-DLLAMA_CLBLAST=ON"
  67. ];
  68. postInstall = postInstall;
  69. meta.mainProgram = "llama";
  70. };
  71. apps.llama-server = {
  72. type = "app";
  73. program = "${self.packages.${system}.default}/bin/llama-server";
  74. };
  75. apps.llama-embedding = {
  76. type = "app";
  77. program = "${self.packages.${system}.default}/bin/embedding";
  78. };
  79. apps.llama = {
  80. type = "app";
  81. program = "${self.packages.${system}.default}/bin/llama";
  82. };
  83. apps.default = self.apps.${system}.llama;
  84. devShells.default = pkgs.mkShell {
  85. packages = nativeBuildInputs ++ osSpecific;
  86. };
  87. });
  88. }