flake.nix 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 pkg-config ];
  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. mkdir -p $out/include
  49. cp ${src}/llama.h $out/include/
  50. '';
  51. cmakeFlags = [ "-DLLAMA_BUILD_SERVER=ON" "-DLLAMA_MPI=ON" "-DBUILD_SHARED_LIBS=ON" "-DCMAKE_SKIP_BUILD_RPATH=ON" ];
  52. in
  53. {
  54. packages.default = pkgs.stdenv.mkDerivation {
  55. inherit name src meta postPatch nativeBuildInputs buildInputs postInstall;
  56. cmakeFlags = cmakeFlags
  57. ++ (if isAarch64 && isDarwin then [
  58. "-DCMAKE_C_FLAGS=-D__ARM_FEATURE_DOTPROD=1"
  59. "-DLLAMA_METAL=ON"
  60. ] else [
  61. "-DLLAMA_BLAS=ON"
  62. "-DLLAMA_BLAS_VENDOR=OpenBLAS"
  63. ]);
  64. };
  65. packages.opencl = pkgs.stdenv.mkDerivation {
  66. inherit name src meta postPatch nativeBuildInputs postInstall;
  67. buildInputs = with pkgs; buildInputs ++ [ clblast ];
  68. cmakeFlags = cmakeFlags ++ [
  69. "-DLLAMA_CLBLAST=ON"
  70. ];
  71. };
  72. packages.rocm = pkgs.stdenv.mkDerivation {
  73. inherit name src meta postPatch nativeBuildInputs postInstall;
  74. buildInputs = with pkgs; buildInputs ++ [ hip hipblas rocblas ];
  75. cmakeFlags = cmakeFlags ++ [
  76. "-DLLAMA_HIPBLAS=1"
  77. "-DCMAKE_C_COMPILER=hipcc"
  78. "-DCMAKE_CXX_COMPILER=hipcc"
  79. "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
  80. ];
  81. };
  82. apps.llama-server = {
  83. type = "app";
  84. program = "${self.packages.${system}.default}/bin/llama-server";
  85. };
  86. apps.llama-embedding = {
  87. type = "app";
  88. program = "${self.packages.${system}.default}/bin/embedding";
  89. };
  90. apps.llama = {
  91. type = "app";
  92. program = "${self.packages.${system}.default}/bin/llama";
  93. };
  94. apps.quantize = {
  95. type = "app";
  96. program = "${self.packages.${system}.default}/bin/quantize";
  97. };
  98. apps.train-text-from-scratch = {
  99. type = "app";
  100. program = "${self.packages.${system}.default}/bin/train-text-from-scratch";
  101. };
  102. apps.default = self.apps.${system}.llama;
  103. devShells.default = pkgs.mkShell {
  104. buildInputs = [ llama-python ];
  105. packages = nativeBuildInputs ++ osSpecific;
  106. };
  107. });
  108. }