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