package.nix 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. {
  2. lib,
  3. glibc,
  4. config,
  5. stdenv,
  6. runCommand,
  7. cmake,
  8. ninja,
  9. pkg-config,
  10. git,
  11. mpi,
  12. blas,
  13. cudaPackages,
  14. autoAddDriverRunpath,
  15. darwin,
  16. rocmPackages,
  17. vulkan-headers,
  18. vulkan-loader,
  19. curl,
  20. shaderc,
  21. useBlas ?
  22. builtins.all (x: !x) [
  23. useCuda
  24. useMetalKit
  25. useRocm
  26. useVulkan
  27. ]
  28. && blas.meta.available,
  29. useCuda ? config.cudaSupport,
  30. useMetalKit ? stdenv.isAarch64 && stdenv.isDarwin,
  31. # Increases the runtime closure size by ~700M
  32. useMpi ? false,
  33. useRocm ? config.rocmSupport,
  34. rocmGpuTargets ? builtins.concatStringsSep ";" rocmPackages.clr.gpuTargets,
  35. enableCurl ? true,
  36. useVulkan ? false,
  37. llamaVersion ? "0.0.0", # Arbitrary version, substituted by the flake
  38. # It's necessary to consistently use backendStdenv when building with CUDA support,
  39. # otherwise we get libstdc++ errors downstream.
  40. effectiveStdenv ? if useCuda then cudaPackages.backendStdenv else stdenv,
  41. enableStatic ? effectiveStdenv.hostPlatform.isStatic,
  42. precompileMetalShaders ? false,
  43. }:
  44. let
  45. inherit (lib)
  46. cmakeBool
  47. cmakeFeature
  48. optionals
  49. strings
  50. ;
  51. stdenv = throw "Use effectiveStdenv instead";
  52. suffices =
  53. lib.optionals useBlas [ "BLAS" ]
  54. ++ lib.optionals useCuda [ "CUDA" ]
  55. ++ lib.optionals useMetalKit [ "MetalKit" ]
  56. ++ lib.optionals useMpi [ "MPI" ]
  57. ++ lib.optionals useRocm [ "ROCm" ]
  58. ++ lib.optionals useVulkan [ "Vulkan" ];
  59. pnameSuffix =
  60. strings.optionalString (suffices != [ ])
  61. "-${strings.concatMapStringsSep "-" strings.toLower suffices}";
  62. descriptionSuffix = strings.optionalString (
  63. suffices != [ ]
  64. ) ", accelerated with ${strings.concatStringsSep ", " suffices}";
  65. xcrunHost = runCommand "xcrunHost" { } ''
  66. mkdir -p $out/bin
  67. ln -s /usr/bin/xcrun $out/bin
  68. '';
  69. # apple_sdk is supposed to choose sane defaults, no need to handle isAarch64
  70. # separately
  71. darwinBuildInputs =
  72. with darwin.apple_sdk.frameworks;
  73. [
  74. Accelerate
  75. CoreVideo
  76. CoreGraphics
  77. ]
  78. ++ optionals useMetalKit [ MetalKit ];
  79. cudaBuildInputs = with cudaPackages; [
  80. cuda_cudart
  81. cuda_cccl # <nv/target>
  82. libcublas
  83. ];
  84. rocmBuildInputs = with rocmPackages; [
  85. clr
  86. hipblas
  87. rocblas
  88. ];
  89. vulkanBuildInputs = [
  90. vulkan-headers
  91. vulkan-loader
  92. shaderc
  93. ];
  94. in
  95. effectiveStdenv.mkDerivation (finalAttrs: {
  96. pname = "llama-cpp${pnameSuffix}";
  97. version = llamaVersion;
  98. # Note: none of the files discarded here are visible in the sandbox or
  99. # affect the output hash. This also means they can be modified without
  100. # triggering a rebuild.
  101. src = lib.cleanSourceWith {
  102. filter =
  103. name: type:
  104. let
  105. noneOf = builtins.all (x: !x);
  106. baseName = baseNameOf name;
  107. in
  108. noneOf [
  109. (lib.hasSuffix ".nix" name) # Ignore *.nix files when computing outPaths
  110. (lib.hasSuffix ".md" name) # Ignore *.md changes whe computing outPaths
  111. (lib.hasPrefix "." baseName) # Skip hidden files and directories
  112. (baseName == "flake.lock")
  113. ];
  114. src = lib.cleanSource ../../.;
  115. };
  116. postPatch = ''
  117. substituteInPlace ./ggml/src/ggml-metal/ggml-metal.m \
  118. --replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
  119. substituteInPlace ./ggml/src/ggml-metal/ggml-metal.m \
  120. --replace '[bundle pathForResource:@"default" ofType:@"metallib"];' "@\"$out/bin/default.metallib\";"
  121. '';
  122. # With PR#6015 https://github.com/ggml-org/llama.cpp/pull/6015,
  123. # `default.metallib` may be compiled with Metal compiler from XCode
  124. # and we need to escape sandbox on MacOS to access Metal compiler.
  125. # `xcrun` is used find the path of the Metal compiler, which is varible
  126. # and not on $PATH
  127. # see https://github.com/ggml-org/llama.cpp/pull/6118 for discussion
  128. __noChroot = effectiveStdenv.isDarwin && useMetalKit && precompileMetalShaders;
  129. nativeBuildInputs =
  130. [
  131. cmake
  132. ninja
  133. pkg-config
  134. git
  135. ]
  136. ++ optionals useCuda [
  137. cudaPackages.cuda_nvcc
  138. autoAddDriverRunpath
  139. ]
  140. ++ optionals (effectiveStdenv.hostPlatform.isGnu && enableStatic) [ glibc.static ]
  141. ++ optionals (effectiveStdenv.isDarwin && useMetalKit && precompileMetalShaders) [ xcrunHost ];
  142. buildInputs =
  143. optionals effectiveStdenv.isDarwin darwinBuildInputs
  144. ++ optionals useCuda cudaBuildInputs
  145. ++ optionals useMpi [ mpi ]
  146. ++ optionals useRocm rocmBuildInputs
  147. ++ optionals useBlas [ blas ]
  148. ++ optionals useVulkan vulkanBuildInputs
  149. ++ optionals enableCurl [ curl ];
  150. cmakeFlags =
  151. [
  152. (cmakeBool "LLAMA_BUILD_SERVER" true)
  153. (cmakeBool "BUILD_SHARED_LIBS" (!enableStatic))
  154. (cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
  155. (cmakeBool "LLAMA_CURL" enableCurl)
  156. (cmakeBool "GGML_NATIVE" false)
  157. (cmakeBool "GGML_BLAS" useBlas)
  158. (cmakeBool "GGML_CUDA" useCuda)
  159. (cmakeBool "GGML_HIP" useRocm)
  160. (cmakeBool "GGML_METAL" useMetalKit)
  161. (cmakeBool "GGML_VULKAN" useVulkan)
  162. (cmakeBool "GGML_STATIC" enableStatic)
  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_HIP_COMPILER" "${rocmPackages.llvm.clang}/bin/clang")
  174. (cmakeFeature "CMAKE_HIP_ARCHITECTURES" rocmGpuTargets)
  175. ]
  176. ++ optionals useMetalKit [
  177. (lib.cmakeFeature "CMAKE_C_FLAGS" "-D__ARM_FEATURE_DOTPROD=1")
  178. (cmakeBool "GGML_METAL_EMBED_LIBRARY" (!precompileMetalShaders))
  179. ];
  180. # Environment variables needed for ROCm
  181. env = optionals useRocm {
  182. ROCM_PATH = "${rocmPackages.clr}";
  183. HIP_DEVICE_LIB_PATH = "${rocmPackages.rocm-device-libs}/amdgcn/bitcode";
  184. };
  185. # TODO(SomeoneSerge): It's better to add proper install targets at the CMake level,
  186. # if they haven't been added yet.
  187. postInstall = ''
  188. mkdir -p $out/include
  189. cp $src/include/llama.h $out/include/
  190. '';
  191. meta = {
  192. # Configurations we don't want even the CI to evaluate. Results in the
  193. # "unsupported platform" messages. This is mostly a no-op, because
  194. # cudaPackages would've refused to evaluate anyway.
  195. badPlatforms = optionals useCuda lib.platforms.darwin;
  196. # Configurations that are known to result in build failures. Can be
  197. # overridden by importing Nixpkgs with `allowBroken = true`.
  198. broken = (useMetalKit && !effectiveStdenv.isDarwin);
  199. description = "Inference of LLaMA model in pure C/C++${descriptionSuffix}";
  200. homepage = "https://github.com/ggml-org/llama.cpp/";
  201. license = lib.licenses.mit;
  202. # Accommodates `nix run` and `lib.getExe`
  203. mainProgram = "llama-cli";
  204. # These people might respond, on the best effort basis, if you ping them
  205. # in case of Nix-specific regressions or for reviewing Nix-specific PRs.
  206. # Consider adding yourself to this list if you want to ensure this flake
  207. # stays maintained and you're willing to invest your time. Do not add
  208. # other people without their consent. Consider removing people after
  209. # they've been unreachable for long periods of time.
  210. # Note that lib.maintainers is defined in Nixpkgs, but you may just add
  211. # an attrset following the same format as in
  212. # https://github.com/NixOS/nixpkgs/blob/f36a80e54da29775c78d7eff0e628c2b4e34d1d7/maintainers/maintainer-list.nix
  213. maintainers = with lib.maintainers; [
  214. philiptaron
  215. SomeoneSerge
  216. ];
  217. # Extend `badPlatforms` instead
  218. platforms = lib.platforms.all;
  219. };
  220. })