package.nix 9.1 KB

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