package.nix 9.7 KB

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