flake.nix 3.4 KB

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