flake.nix 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. name = "llama.cpp";
  10. src = ./.;
  11. meta.mainProgram = "llama";
  12. inherit (pkgs.stdenv) isAarch32 isAarch64 isDarwin;
  13. buildInputs = with pkgs; [ openmpi ];
  14. osSpecific = with pkgs; buildInputs ++
  15. (
  16. if isAarch64 && isDarwin then
  17. with pkgs.darwin.apple_sdk_11_0.frameworks; [
  18. Accelerate
  19. MetalKit
  20. ]
  21. else if isAarch32 && isDarwin then
  22. with pkgs.darwin.apple_sdk.frameworks; [
  23. Accelerate
  24. CoreGraphics
  25. CoreVideo
  26. ]
  27. else if isDarwin then
  28. with pkgs.darwin.apple_sdk.frameworks; [
  29. Accelerate
  30. CoreGraphics
  31. CoreVideo
  32. ]
  33. else
  34. with pkgs; [ openblas ]
  35. );
  36. pkgs = import nixpkgs { inherit system; };
  37. nativeBuildInputs = with pkgs; [ cmake ninja pkgconfig ];
  38. llama-python =
  39. pkgs.python3.withPackages (ps: with ps; [ numpy sentencepiece ]);
  40. postPatch = ''
  41. substituteInPlace ./ggml-metal.m \
  42. --replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
  43. substituteInPlace ./*.py --replace '/usr/bin/env python' '${llama-python}/bin/python'
  44. '';
  45. postInstall = ''
  46. mv $out/bin/main $out/bin/llama
  47. mv $out/bin/server $out/bin/llama-server
  48. '';
  49. cmakeFlags = [ "-DLLAMA_BUILD_SERVER=ON" "-DLLAMA_MPI=ON" "-DBUILD_SHARED_LIBS=ON" "-DCMAKE_SKIP_BUILD_RPATH=ON" ];
  50. in
  51. {
  52. packages.default = pkgs.stdenv.mkDerivation {
  53. inherit name src meta postPatch nativeBuildInputs buildInputs postInstall;
  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. };
  63. packages.opencl = pkgs.stdenv.mkDerivation {
  64. inherit name src meta postPatch nativeBuildInputs postInstall;
  65. buildInputs = with pkgs; buildInputs ++ [ clblast ];
  66. cmakeFlags = cmakeFlags ++ [
  67. "-DLLAMA_CLBLAST=ON"
  68. ];
  69. };
  70. packages.rocm = pkgs.stdenv.mkDerivation {
  71. inherit name src meta postPatch nativeBuildInputs postInstall;
  72. buildInputs = with pkgs; buildInputs ++ [ hip hipblas rocblas ];
  73. cmakeFlags = cmakeFlags ++ [
  74. "-DLLAMA_HIPBLAS=1"
  75. "-DCMAKE_C_COMPILER=hipcc"
  76. "-DCMAKE_CXX_COMPILER=hipcc"
  77. "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
  78. ];
  79. };
  80. apps.llama-server = {
  81. type = "app";
  82. program = "${self.packages.${system}.default}/bin/llama-server";
  83. };
  84. apps.llama-embedding = {
  85. type = "app";
  86. program = "${self.packages.${system}.default}/bin/embedding";
  87. };
  88. apps.llama = {
  89. type = "app";
  90. program = "${self.packages.${system}.default}/bin/llama";
  91. };
  92. apps.quantize = {
  93. type = "app";
  94. program = "${self.packages.${system}.default}/bin/quantize";
  95. };
  96. apps.train-text-from-scratch = {
  97. type = "app";
  98. program = "${self.packages.${system}.default}/bin/train-text-from-scratch";
  99. };
  100. apps.default = self.apps.${system}.llama;
  101. devShells.default = pkgs.mkShell {
  102. buildInputs = [ llama-python ];
  103. packages = nativeBuildInputs ++ osSpecific;
  104. };
  105. });
  106. }