package.nix 9.6 KB

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