package.nix 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. {
  2. lib,
  3. glibc,
  4. config,
  5. stdenv,
  6. mkShell,
  7. runCommand,
  8. cmake,
  9. ninja,
  10. pkg-config,
  11. git,
  12. python3,
  13. mpi,
  14. blas,
  15. cudaPackages,
  16. darwin,
  17. rocmPackages,
  18. vulkan-headers,
  19. vulkan-loader,
  20. curl,
  21. shaderc,
  22. useBlas ? builtins.all (x: !x) [
  23. useCuda
  24. useMetalKit
  25. useRocm
  26. useVulkan
  27. ] && blas.meta.available,
  28. useCuda ? config.cudaSupport,
  29. useMetalKit ? stdenv.isAarch64 && stdenv.isDarwin,
  30. useMpi ? false, # Increases the runtime closure size by ~700M
  31. useRocm ? config.rocmSupport,
  32. enableCurl ? true,
  33. useVulkan ? false,
  34. llamaVersion ? "0.0.0", # Arbitrary version, substituted by the flake
  35. # It's necessary to consistently use backendStdenv when building with CUDA support,
  36. # otherwise we get libstdc++ errors downstream.
  37. effectiveStdenv ? if useCuda then cudaPackages.backendStdenv else stdenv,
  38. enableStatic ? effectiveStdenv.hostPlatform.isStatic,
  39. precompileMetalShaders ? false
  40. }@inputs:
  41. let
  42. inherit (lib)
  43. cmakeBool
  44. cmakeFeature
  45. optionals
  46. strings
  47. versionOlder
  48. ;
  49. stdenv = throw "Use effectiveStdenv instead";
  50. suffices =
  51. lib.optionals useBlas [ "BLAS" ]
  52. ++ lib.optionals useCuda [ "CUDA" ]
  53. ++ lib.optionals useMetalKit [ "MetalKit" ]
  54. ++ lib.optionals useMpi [ "MPI" ]
  55. ++ lib.optionals useRocm [ "ROCm" ]
  56. ++ lib.optionals useVulkan [ "Vulkan" ];
  57. pnameSuffix =
  58. strings.optionalString (suffices != [ ])
  59. "-${strings.concatMapStringsSep "-" strings.toLower suffices}";
  60. descriptionSuffix =
  61. strings.optionalString (suffices != [ ])
  62. ", accelerated with ${strings.concatStringsSep ", " suffices}";
  63. executableSuffix = effectiveStdenv.hostPlatform.extensions.executable;
  64. # TODO: package the Python in this repository in a Nix-like way.
  65. # It'd be nice to migrate to buildPythonPackage, as well as ensure this repo
  66. # is PEP 517-compatible, and ensure the correct .dist-info is generated.
  67. # https://peps.python.org/pep-0517/
  68. #
  69. # TODO: Package up each Python script or service appropriately, by making
  70. # them into "entrypoints"
  71. llama-python = python3.withPackages (
  72. ps: [
  73. ps.numpy
  74. ps.sentencepiece
  75. ]
  76. );
  77. # TODO(Green-Sky): find a better way to opt-into the heavy ml python runtime
  78. llama-python-extra = python3.withPackages (
  79. ps: [
  80. ps.numpy
  81. ps.sentencepiece
  82. ps.tiktoken
  83. ps.torchWithoutCuda
  84. ps.transformers
  85. # server bench
  86. ps.matplotlib
  87. # server tests
  88. ps.openai
  89. ps.behave
  90. ps.prometheus-client
  91. # for examples/pydantic-models-to-grammar-examples.py
  92. ps.docstring-parser
  93. ps.pydantic
  94. # for scripts/compare-llama-bench.py
  95. ps.gitpython
  96. ps.tabulate
  97. ]
  98. );
  99. xcrunHost = runCommand "xcrunHost" {} ''
  100. mkdir -p $out/bin
  101. ln -s /usr/bin/xcrun $out/bin
  102. '';
  103. # apple_sdk is supposed to choose sane defaults, no need to handle isAarch64
  104. # separately
  105. darwinBuildInputs =
  106. with darwin.apple_sdk.frameworks;
  107. [
  108. Accelerate
  109. CoreVideo
  110. CoreGraphics
  111. ]
  112. ++ optionals useMetalKit [ MetalKit ];
  113. cudaBuildInputs = with cudaPackages; [
  114. cuda_cudart
  115. cuda_cccl # <nv/target>
  116. libcublas
  117. ];
  118. rocmBuildInputs = with rocmPackages; [
  119. clr
  120. hipblas
  121. rocblas
  122. ];
  123. vulkanBuildInputs = [
  124. vulkan-headers
  125. vulkan-loader
  126. shaderc
  127. ];
  128. in
  129. effectiveStdenv.mkDerivation (
  130. finalAttrs: {
  131. pname = "llama-cpp${pnameSuffix}";
  132. version = llamaVersion;
  133. # Note: none of the files discarded here are visible in the sandbox or
  134. # affect the output hash. This also means they can be modified without
  135. # triggering a rebuild.
  136. src = lib.cleanSourceWith {
  137. filter =
  138. name: type:
  139. let
  140. noneOf = builtins.all (x: !x);
  141. baseName = baseNameOf name;
  142. in
  143. noneOf [
  144. (lib.hasSuffix ".nix" name) # Ignore *.nix files when computing outPaths
  145. (lib.hasSuffix ".md" name) # Ignore *.md changes whe computing outPaths
  146. (lib.hasPrefix "." baseName) # Skip hidden files and directories
  147. (baseName == "flake.lock")
  148. ];
  149. src = lib.cleanSource ../../.;
  150. };
  151. postPatch = ''
  152. substituteInPlace ./ggml/src/ggml-metal.m \
  153. --replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
  154. substituteInPlace ./ggml/src/ggml-metal.m \
  155. --replace '[bundle pathForResource:@"default" ofType:@"metallib"];' "@\"$out/bin/default.metallib\";"
  156. '';
  157. # With PR#6015 https://github.com/ggerganov/llama.cpp/pull/6015,
  158. # `default.metallib` may be compiled with Metal compiler from XCode
  159. # and we need to escape sandbox on MacOS to access Metal compiler.
  160. # `xcrun` is used find the path of the Metal compiler, which is varible
  161. # and not on $PATH
  162. # see https://github.com/ggerganov/llama.cpp/pull/6118 for discussion
  163. __noChroot = effectiveStdenv.isDarwin && useMetalKit && precompileMetalShaders;
  164. nativeBuildInputs =
  165. [
  166. cmake
  167. ninja
  168. pkg-config
  169. git
  170. ]
  171. ++ optionals useCuda [
  172. cudaPackages.cuda_nvcc
  173. # TODO: Replace with autoAddDriverRunpath
  174. # once https://github.com/NixOS/nixpkgs/pull/275241 has been merged
  175. cudaPackages.autoAddOpenGLRunpathHook
  176. ]
  177. ++ optionals (effectiveStdenv.hostPlatform.isGnu && enableStatic) [
  178. glibc.static
  179. ] ++ optionals (effectiveStdenv.isDarwin && useMetalKit && precompileMetalShaders) [
  180. xcrunHost
  181. ];
  182. buildInputs =
  183. optionals effectiveStdenv.isDarwin darwinBuildInputs
  184. ++ optionals useCuda cudaBuildInputs
  185. ++ optionals useMpi [ mpi ]
  186. ++ optionals useRocm rocmBuildInputs
  187. ++ optionals useBlas [ blas ]
  188. ++ optionals useVulkan vulkanBuildInputs
  189. ++ optionals enableCurl [ curl ];
  190. cmakeFlags =
  191. [
  192. (cmakeBool "LLAMA_BUILD_SERVER" true)
  193. (cmakeBool "BUILD_SHARED_LIBS" (!enableStatic))
  194. (cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
  195. (cmakeBool "LLAMA_CURL" enableCurl)
  196. (cmakeBool "GGML_NATIVE" false)
  197. (cmakeBool "GGML_BLAS" useBlas)
  198. (cmakeBool "GGML_CUDA" useCuda)
  199. (cmakeBool "GGML_HIPBLAS" useRocm)
  200. (cmakeBool "GGML_METAL" useMetalKit)
  201. (cmakeBool "GGML_VULKAN" useVulkan)
  202. (cmakeBool "GGML_STATIC" enableStatic)
  203. ]
  204. ++ optionals useCuda [
  205. (
  206. with cudaPackages.flags;
  207. cmakeFeature "CMAKE_CUDA_ARCHITECTURES" (
  208. builtins.concatStringsSep ";" (map dropDot cudaCapabilities)
  209. )
  210. )
  211. ]
  212. ++ optionals useRocm [
  213. (cmakeFeature "CMAKE_HIP_COMPILER" "${rocmPackages.llvm.clang}/bin/clang")
  214. (cmakeFeature "CMAKE_HIP_ARCHITECTURES" (builtins.concatStringsSep ";" rocmPackages.clr.gpuTargets))
  215. ]
  216. ++ optionals useMetalKit [
  217. (lib.cmakeFeature "CMAKE_C_FLAGS" "-D__ARM_FEATURE_DOTPROD=1")
  218. (cmakeBool "GGML_METAL_EMBED_LIBRARY" (!precompileMetalShaders))
  219. ];
  220. # Environment variables needed for ROCm
  221. env = optionals useRocm {
  222. ROCM_PATH = "${rocmPackages.clr}";
  223. HIP_DEVICE_LIB_PATH = "${rocmPackages.rocm-device-libs}/amdgcn/bitcode";
  224. };
  225. # TODO(SomeoneSerge): It's better to add proper install targets at the CMake level,
  226. # if they haven't been added yet.
  227. postInstall = ''
  228. mkdir -p $out/include
  229. cp $src/include/llama.h $out/include/
  230. '';
  231. # Define the shells here, but don't add in the inputsFrom to avoid recursion.
  232. passthru = {
  233. inherit
  234. useBlas
  235. useCuda
  236. useMetalKit
  237. useMpi
  238. useRocm
  239. useVulkan
  240. ;
  241. shell = mkShell {
  242. name = "shell-${finalAttrs.finalPackage.name}";
  243. description = "contains numpy and sentencepiece";
  244. buildInputs = [ llama-python ];
  245. inputsFrom = [ finalAttrs.finalPackage ];
  246. shellHook = ''
  247. addToSearchPath "LD_LIBRARY_PATH" "${lib.getLib effectiveStdenv.cc.cc}/lib"
  248. '';
  249. };
  250. shell-extra = mkShell {
  251. name = "shell-extra-${finalAttrs.finalPackage.name}";
  252. description = "contains numpy, sentencepiece, torchWithoutCuda, and transformers";
  253. buildInputs = [ llama-python-extra ];
  254. inputsFrom = [ finalAttrs.finalPackage ];
  255. };
  256. };
  257. meta = {
  258. # Configurations we don't want even the CI to evaluate. Results in the
  259. # "unsupported platform" messages. This is mostly a no-op, because
  260. # cudaPackages would've refused to evaluate anyway.
  261. badPlatforms = optionals useCuda lib.platforms.darwin;
  262. # Configurations that are known to result in build failures. Can be
  263. # overridden by importing Nixpkgs with `allowBroken = true`.
  264. broken = (useMetalKit && !effectiveStdenv.isDarwin);
  265. description = "Inference of LLaMA model in pure C/C++${descriptionSuffix}";
  266. homepage = "https://github.com/ggerganov/llama.cpp/";
  267. license = lib.licenses.mit;
  268. # Accommodates `nix run` and `lib.getExe`
  269. mainProgram = "llama-cli";
  270. # These people might respond, on the best effort basis, if you ping them
  271. # in case of Nix-specific regressions or for reviewing Nix-specific PRs.
  272. # Consider adding yourself to this list if you want to ensure this flake
  273. # stays maintained and you're willing to invest your time. Do not add
  274. # other people without their consent. Consider removing people after
  275. # they've been unreachable for long periods of time.
  276. # Note that lib.maintainers is defined in Nixpkgs, but you may just add
  277. # an attrset following the same format as in
  278. # https://github.com/NixOS/nixpkgs/blob/f36a80e54da29775c78d7eff0e628c2b4e34d1d7/maintainers/maintainer-list.nix
  279. maintainers = with lib.maintainers; [
  280. philiptaron
  281. SomeoneSerge
  282. ];
  283. # Extend `badPlatforms` instead
  284. platforms = lib.platforms.all;
  285. };
  286. }
  287. )