package.nix 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. {
  2. lib,
  3. config,
  4. stdenv,
  5. mkShell,
  6. cmake,
  7. ninja,
  8. pkg-config,
  9. git,
  10. python3,
  11. mpi,
  12. openblas, # TODO: Use the generic `blas` so users could switch between alternative implementations
  13. cudaPackages,
  14. darwin,
  15. rocmPackages,
  16. clblast,
  17. useBlas ? builtins.all (x: !x) [
  18. useCuda
  19. useMetalKit
  20. useOpenCL
  21. useRocm
  22. ],
  23. useCuda ? config.cudaSupport,
  24. useMetalKit ? stdenv.isAarch64 && stdenv.isDarwin && !useOpenCL,
  25. useMpi ? false, # Increases the runtime closure size by ~700M
  26. useOpenCL ? false,
  27. useRocm ? config.rocmSupport,
  28. llamaVersion ? "0.0.0", # Arbitrary version, substituted by the flake
  29. }@inputs:
  30. let
  31. inherit (lib)
  32. cmakeBool
  33. cmakeFeature
  34. optionals
  35. strings
  36. versionOlder
  37. ;
  38. # It's necessary to consistently use backendStdenv when building with CUDA support,
  39. # otherwise we get libstdc++ errors downstream.
  40. stdenv = throw "Use effectiveStdenv instead";
  41. effectiveStdenv = if useCuda then cudaPackages.backendStdenv else inputs.stdenv;
  42. suffices =
  43. lib.optionals useBlas [ "BLAS" ]
  44. ++ lib.optionals useCuda [ "CUDA" ]
  45. ++ lib.optionals useMetalKit [ "MetalKit" ]
  46. ++ lib.optionals useMpi [ "MPI" ]
  47. ++ lib.optionals useOpenCL [ "OpenCL" ]
  48. ++ lib.optionals useRocm [ "ROCm" ];
  49. pnameSuffix =
  50. strings.optionalString (suffices != [ ])
  51. "-${strings.concatMapStringsSep "-" strings.toLower suffices}";
  52. descriptionSuffix =
  53. strings.optionalString (suffices != [ ])
  54. ", accelerated with ${strings.concatStringsSep ", " suffices}";
  55. # TODO: package the Python in this repository in a Nix-like way.
  56. # It'd be nice to migrate to buildPythonPackage, as well as ensure this repo
  57. # is PEP 517-compatible, and ensure the correct .dist-info is generated.
  58. # https://peps.python.org/pep-0517/
  59. llama-python = python3.withPackages (
  60. ps: [
  61. ps.numpy
  62. ps.sentencepiece
  63. ]
  64. );
  65. # TODO(Green-Sky): find a better way to opt-into the heavy ml python runtime
  66. llama-python-extra = python3.withPackages (
  67. ps: [
  68. ps.numpy
  69. ps.sentencepiece
  70. ps.tiktoken
  71. ps.torchWithoutCuda
  72. ps.transformers
  73. ]
  74. );
  75. # apple_sdk is supposed to choose sane defaults, no need to handle isAarch64
  76. # separately
  77. darwinBuildInputs =
  78. with darwin.apple_sdk.frameworks;
  79. [
  80. Accelerate
  81. CoreVideo
  82. CoreGraphics
  83. ]
  84. ++ optionals useMetalKit [ MetalKit ];
  85. cudaBuildInputs = with cudaPackages; [
  86. cuda_cccl.dev # <nv/target>
  87. # A temporary hack for reducing the closure size, remove once cudaPackages
  88. # have stopped using lndir: https://github.com/NixOS/nixpkgs/issues/271792
  89. cuda_cudart.dev
  90. cuda_cudart.lib
  91. cuda_cudart.static
  92. libcublas.dev
  93. libcublas.lib
  94. libcublas.static
  95. ];
  96. rocmBuildInputs = with rocmPackages; [
  97. clr
  98. hipblas
  99. rocblas
  100. ];
  101. in
  102. effectiveStdenv.mkDerivation (
  103. finalAttrs: {
  104. pname = "llama-cpp${pnameSuffix}";
  105. version = llamaVersion;
  106. # Note: none of the files discarded here are visible in the sandbox or
  107. # affect the output hash. This also means they can be modified without
  108. # triggering a rebuild.
  109. src = lib.cleanSourceWith {
  110. filter =
  111. name: type:
  112. let
  113. noneOf = builtins.all (x: !x);
  114. baseName = baseNameOf name;
  115. in
  116. noneOf [
  117. (lib.hasSuffix ".nix" name) # Ignore *.nix files when computing outPaths
  118. (lib.hasSuffix ".md" name) # Ignore *.md changes whe computing outPaths
  119. (lib.hasPrefix "." baseName) # Skip hidden files and directories
  120. (baseName == "flake.lock")
  121. ];
  122. src = lib.cleanSource ../../.;
  123. };
  124. postPatch = ''
  125. substituteInPlace ./ggml-metal.m \
  126. --replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
  127. # TODO: Package up each Python script or service appropriately.
  128. # If we were to migrate to buildPythonPackage and prepare the `pyproject.toml`,
  129. # we could make those *.py into setuptools' entrypoints
  130. substituteInPlace ./*.py --replace "/usr/bin/env python" "${llama-python}/bin/python"
  131. '';
  132. nativeBuildInputs =
  133. [
  134. cmake
  135. ninja
  136. pkg-config
  137. git
  138. ]
  139. ++ optionals useCuda [
  140. cudaPackages.cuda_nvcc
  141. # TODO: Replace with autoAddDriverRunpath
  142. # once https://github.com/NixOS/nixpkgs/pull/275241 has been merged
  143. cudaPackages.autoAddOpenGLRunpathHook
  144. ];
  145. buildInputs =
  146. optionals effectiveStdenv.isDarwin darwinBuildInputs
  147. ++ optionals useCuda cudaBuildInputs
  148. ++ optionals useMpi [ mpi ]
  149. ++ optionals useOpenCL [ clblast ]
  150. ++ optionals useRocm rocmBuildInputs;
  151. cmakeFlags =
  152. [
  153. (cmakeBool "LLAMA_NATIVE" false)
  154. (cmakeBool "LLAMA_BUILD_SERVER" true)
  155. (cmakeBool "BUILD_SHARED_LIBS" true)
  156. (cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
  157. (cmakeBool "LLAMA_BLAS" useBlas)
  158. (cmakeBool "LLAMA_CLBLAST" useOpenCL)
  159. (cmakeBool "LLAMA_CUBLAS" useCuda)
  160. (cmakeBool "LLAMA_HIPBLAS" useRocm)
  161. (cmakeBool "LLAMA_METAL" useMetalKit)
  162. (cmakeBool "LLAMA_MPI" useMpi)
  163. ]
  164. ++ optionals useCuda [
  165. (
  166. with cudaPackages.flags;
  167. cmakeFeature "CMAKE_CUDA_ARCHITECTURES" (
  168. builtins.concatStringsSep ";" (map dropDot cudaCapabilities)
  169. )
  170. )
  171. ]
  172. ++ optionals useRocm [
  173. (cmakeFeature "CMAKE_C_COMPILER" "hipcc")
  174. (cmakeFeature "CMAKE_CXX_COMPILER" "hipcc")
  175. # Build all targets supported by rocBLAS. When updating search for TARGET_LIST_ROCM
  176. # in https://github.com/ROCmSoftwarePlatform/rocBLAS/blob/develop/CMakeLists.txt
  177. # and select the line that matches the current nixpkgs version of rocBLAS.
  178. # Should likely use `rocmPackages.clr.gpuTargets`.
  179. "-DAMDGPU_TARGETS=gfx803;gfx900;gfx906:xnack-;gfx908:xnack-;gfx90a:xnack+;gfx90a:xnack-;gfx940;gfx941;gfx942;gfx1010;gfx1012;gfx1030;gfx1100;gfx1101;gfx1102"
  180. ]
  181. ++ optionals useMetalKit [ (lib.cmakeFeature "CMAKE_C_FLAGS" "-D__ARM_FEATURE_DOTPROD=1") ]
  182. ++ optionals useBlas [ (lib.cmakeFeature "LLAMA_BLAS_VENDOR" "OpenBLAS") ];
  183. # TODO(SomeoneSerge): It's better to add proper install targets at the CMake level,
  184. # if they haven't been added yet.
  185. postInstall = ''
  186. mv $out/bin/main $out/bin/llama
  187. mv $out/bin/server $out/bin/llama-server
  188. mkdir -p $out/include
  189. cp $src/llama.h $out/include/
  190. '';
  191. # Define the shells here, but don't add in the inputsFrom to avoid recursion.
  192. passthru = {
  193. inherit
  194. useBlas
  195. useCuda
  196. useMetalKit
  197. useMpi
  198. useOpenCL
  199. useRocm
  200. ;
  201. shell = mkShell {
  202. name = "shell-${finalAttrs.finalPackage.name}";
  203. description = "contains numpy and sentencepiece";
  204. buildInputs = [ llama-python ];
  205. inputsFrom = [ finalAttrs.finalPackage ];
  206. };
  207. shell-extra = mkShell {
  208. name = "shell-extra-${finalAttrs.finalPackage.name}";
  209. description = "contains numpy, sentencepiece, torchWithoutCuda, and transformers";
  210. buildInputs = [ llama-python-extra ];
  211. inputsFrom = [ finalAttrs.finalPackage ];
  212. };
  213. };
  214. meta = {
  215. # Configurations we don't want even the CI to evaluate. Results in the
  216. # "unsupported platform" messages. This is mostly a no-op, because
  217. # cudaPackages would've refused to evaluate anyway.
  218. badPlatforms = optionals (useCuda || useOpenCL) lib.platforms.darwin;
  219. # Configurations that are known to result in build failures. Can be
  220. # overridden by importing Nixpkgs with `allowBroken = true`.
  221. broken = (useMetalKit && !effectiveStdenv.isDarwin);
  222. description = "Inference of LLaMA model in pure C/C++${descriptionSuffix}";
  223. homepage = "https://github.com/ggerganov/llama.cpp/";
  224. license = lib.licenses.mit;
  225. # Accommodates `nix run` and `lib.getExe`
  226. mainProgram = "llama";
  227. # These people might respond, on the best effort basis, if you ping them
  228. # in case of Nix-specific regressions or for reviewing Nix-specific PRs.
  229. # Consider adding yourself to this list if you want to ensure this flake
  230. # stays maintained and you're willing to invest your time. Do not add
  231. # other people without their consent. Consider removing people after
  232. # they've been unreachable for long periods of time.
  233. # Note that lib.maintainers is defined in Nixpkgs, but you may just add
  234. # an attrset following the same format as in
  235. # https://github.com/NixOS/nixpkgs/blob/f36a80e54da29775c78d7eff0e628c2b4e34d1d7/maintainers/maintainer-list.nix
  236. maintainers = with lib.maintainers; [
  237. philiptaron
  238. SomeoneSerge
  239. ];
  240. # Extend `badPlatforms` instead
  241. platforms = lib.platforms.all;
  242. };
  243. }
  244. )