flake.nix 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 isx86_32 isx86_64 isDarwin;
  10. osSpecific = with pkgs; [ openmpi ] ++
  11. (
  12. if isAarch64 && isDarwin then
  13. with pkgs.darwin.apple_sdk_11_0.frameworks; [
  14. Accelerate
  15. MetalKit
  16. MetalPerformanceShaders
  17. MetalPerformanceShadersGraph
  18. ]
  19. else if isAarch32 && isDarwin then
  20. with pkgs.darwin.apple_sdk.frameworks; [
  21. Accelerate
  22. CoreGraphics
  23. CoreVideo
  24. ]
  25. else if isx86_32 || isx86_64 then
  26. with pkgs; [ mkl ]
  27. else
  28. with pkgs; [ openblas ]
  29. );
  30. pkgs = import nixpkgs { inherit system; };
  31. llama-python =
  32. pkgs.python310.withPackages (ps: with ps; [ numpy sentencepiece ]);
  33. in {
  34. packages.default = pkgs.stdenv.mkDerivation {
  35. name = "llama.cpp";
  36. src = ./.;
  37. postPatch = ''
  38. substituteInPlace ./ggml-metal.m \
  39. --replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
  40. '';
  41. nativeBuildInputs = with pkgs; [ cmake pkgconfig ];
  42. buildInputs = osSpecific;
  43. cmakeFlags = [ "-DLLAMA_BUILD_SERVER=ON" "-DLLAMA_MPI=ON" "-DBUILD_SHARED_LIBS=ON" "-DCMAKE_SKIP_BUILD_RPATH=ON" ]
  44. ++ (if isAarch64 && isDarwin then [
  45. "-DCMAKE_C_FLAGS=-D__ARM_FEATURE_DOTPROD=1"
  46. "-DLLAMA_METAL=ON"
  47. ] else if isx86_32 || isx86_64 then [
  48. "-DLLAMA_BLAS=ON"
  49. "-DLLAMA_BLAS_VENDOR=Intel10_lp64"
  50. ] else [
  51. "-DLLAMA_BLAS=ON"
  52. "-DLLAMA_BLAS_VENDOR=OpenBLAS"
  53. ]);
  54. installPhase = ''
  55. runHook preInstall
  56. install -D bin/* -t $out/bin
  57. install -Dm644 lib*.so -t $out/lib
  58. mv $out/bin/main $out/bin/llama
  59. mv $out/bin/server $out/bin/llama-server
  60. echo "#!${llama-python}/bin/python" > $out/bin/convert.py
  61. cat ${./convert.py} >> $out/bin/convert.py
  62. chmod +x $out/bin/convert.py
  63. runHook postInstall
  64. '';
  65. meta.mainProgram = "llama";
  66. };
  67. apps.llama-server = {
  68. type = "app";
  69. program = "${self.packages.${system}.default}/bin/llama-server";
  70. };
  71. apps.llama-embedding = {
  72. type = "app";
  73. program = "${self.packages.${system}.default}/bin/embedding";
  74. };
  75. apps.llama = {
  76. type = "app";
  77. program = "${self.packages.${system}.default}/bin/llama";
  78. };
  79. apps.default = self.apps.${system}.llama;
  80. devShells.default = pkgs.mkShell {
  81. packages = with pkgs; [ cmake llama-python ] ++ osSpecific;
  82. };
  83. });
  84. }