flake.nix 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. MetalPerformanceShaders
  18. MetalPerformanceShadersGraph
  19. ]
  20. else if isAarch32 && isDarwin then
  21. with pkgs.darwin.apple_sdk.frameworks; [
  22. Accelerate
  23. CoreGraphics
  24. CoreVideo
  25. ]
  26. else
  27. with pkgs; [ openblas ]
  28. );
  29. pkgs = import nixpkgs { inherit system; };
  30. nativeBuildInputs = with pkgs; [ cmake pkgconfig ];
  31. llama-python =
  32. pkgs.python3.withPackages (ps: with ps; [ numpy sentencepiece ]);
  33. postPatch = ''
  34. substituteInPlace ./ggml-metal.m \
  35. --replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
  36. substituteInPlace ./*.py --replace '/usr/bin/env python' '${llama-python}/bin/python'
  37. '';
  38. postInstall = ''
  39. mv $out/bin/main $out/bin/llama
  40. mv $out/bin/server $out/bin/llama-server
  41. '';
  42. cmakeFlags = [ "-DLLAMA_BUILD_SERVER=ON" "-DLLAMA_MPI=ON" "-DBUILD_SHARED_LIBS=ON" "-DCMAKE_SKIP_BUILD_RPATH=ON" ];
  43. in {
  44. packages.default = pkgs.stdenv.mkDerivation {
  45. name = "llama.cpp";
  46. src = ./.;
  47. postPatch = postPatch;
  48. nativeBuildInputs = nativeBuildInputs;
  49. buildInputs = osSpecific;
  50. cmakeFlags = cmakeFlags
  51. ++ (if isAarch64 && isDarwin then [
  52. "-DCMAKE_C_FLAGS=-D__ARM_FEATURE_DOTPROD=1"
  53. "-DLLAMA_METAL=ON"
  54. ] else [
  55. "-DLLAMA_BLAS=ON"
  56. "-DLLAMA_BLAS_VENDOR=OpenBLAS"
  57. ]);
  58. postInstall = postInstall;
  59. meta.mainProgram = "llama";
  60. };
  61. packages.opencl = pkgs.stdenv.mkDerivation {
  62. name = "llama.cpp";
  63. src = ./.;
  64. postPatch = postPatch;
  65. nativeBuildInputs = nativeBuildInputs;
  66. buildInputs = with pkgs; buildInputs ++ [ clblast ];
  67. cmakeFlags = cmakeFlags ++ [
  68. "-DLLAMA_CLBLAST=ON"
  69. ];
  70. postInstall = postInstall;
  71. meta.mainProgram = "llama";
  72. };
  73. apps.llama-server = {
  74. type = "app";
  75. program = "${self.packages.${system}.default}/bin/llama-server";
  76. };
  77. apps.llama-embedding = {
  78. type = "app";
  79. program = "${self.packages.${system}.default}/bin/embedding";
  80. };
  81. apps.llama = {
  82. type = "app";
  83. program = "${self.packages.${system}.default}/bin/llama";
  84. };
  85. apps.default = self.apps.${system}.llama;
  86. devShells.default = pkgs.mkShell {
  87. packages = nativeBuildInputs ++ osSpecific;
  88. };
  89. });
  90. }