nixpkgs/pkgs/by-name/ol/ollama/package.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

215 lines
6.4 KiB
Nix
Raw Normal View History

2023-07-30 07:46:09 +01:00
{ lib
, buildGo122Module
2023-07-30 07:46:09 +01:00
, fetchFromGitHub
, fetchpatch
, buildEnv
, linkFarm
, overrideCC
, makeWrapper
, stdenv
, nixosTests
, pkgs
, cmake
, gcc12
, clblast
, libdrm
, rocmPackages
, cudaPackages
, linuxPackages
, darwin
, testers
, ollama
2024-04-12 07:47:22 +01:00
, config
# one of `[ null false "rocm" "cuda" ]`
, acceleration ? null
2023-07-30 07:46:09 +01:00
}:
let
2023-07-30 07:46:09 +01:00
pname = "ollama";
2024-03-31 20:16:03 +01:00
# don't forget to invalidate all hashes each update
2024-05-14 10:42:14 +01:00
version = "0.1.38";
2024-03-31 20:16:03 +01:00
2024-02-26 21:43:22 +00:00
src = fetchFromGitHub {
owner = "jmorganca";
repo = "ollama";
rev = "v${version}";
2024-05-14 10:42:14 +01:00
hash = "sha256-9HHR48gqETYVJgIaDH8s/yHTrDPEmHm80shpDNS+6hY=";
2024-02-26 21:43:22 +00:00
fetchSubmodules = true;
};
2024-05-14 10:42:14 +01:00
vendorHash = "sha256-zOQGhNcGNlQppTqZdPfx+y4fUrxH0NOUl38FN8J6ffE=";
2024-03-31 20:16:03 +01:00
# ollama's patches of llama.cpp's example server
# `ollama/llm/generate/gen_common.sh` -> "apply temporary patches until fix is upstream"
# each update, these patches should be synchronized with the contents of `ollama/llm/patches/`
llamacppPatches = [
2024-05-14 10:42:14 +01:00
(preparePatch "02-clip-log.diff" "sha256-rMWbl3QgrPlhisTeHwD7EnGRJyOhLB4UeS7rqa0tdXM=")
2024-03-31 20:16:03 +01:00
(preparePatch "03-load_exception.diff" "sha256-1DfNahFYYxqlx4E4pwMKQpL+XR0bibYnDFGt6dCL4TM=")
2024-05-14 10:42:14 +01:00
(preparePatch "04-metal.diff" "sha256-Ne8J9R8NndUosSK0qoMvFfKNwqV5xhhce1nSoYrZo7Y=")
(preparePatch "05-clip-fix.diff" "sha256-rCc3xNuJR11OkyiXuau8y46hb+KYk40ZqH1Llq+lqWc=")
2024-03-31 20:16:03 +01:00
];
preparePatch = patch: hash: fetchpatch {
url = "file://${src}/llm/patches/${patch}";
inherit hash;
stripLen = 1;
extraPrefix = "llm/llama.cpp/";
};
2023-07-30 07:46:09 +01:00
2024-04-12 07:47:22 +01:00
accelIsValid = builtins.elem acceleration [ null false "rocm" "cuda" ];
validateFallback = lib.warnIf (config.rocmSupport && config.cudaSupport)
(lib.concatStrings [
"both `nixpkgs.config.rocmSupport` and `nixpkgs.config.cudaSupport` are enabled, "
"but they are mutually exclusive; falling back to cpu"
])
(!(config.rocmSupport && config.cudaSupport));
validateLinux = api: (lib.warnIfNot stdenv.isLinux
2024-02-21 23:41:45 +00:00
"building ollama with `${api}` is only supported on linux; falling back to cpu"
stdenv.isLinux);
2024-04-12 07:47:22 +01:00
shouldEnable = assert accelIsValid;
mode: fallback:
((acceleration == mode)
|| (fallback && acceleration == null && validateFallback))
&& (validateLinux mode);
enableRocm = shouldEnable "rocm" config.rocmSupport;
enableCuda = shouldEnable "cuda" config.cudaSupport;
2024-03-31 20:16:03 +01:00
2024-05-14 10:42:14 +01:00
rocmLibs = [
rocmPackages.clr
rocmPackages.hipblas
(rocmPackages.rocblas.override { tensileSepArch = true; tensileLazyLib = true; })
rocmPackages.rocsolver
rocmPackages.rocsparse
rocmPackages.rocm-device-libs
rocmPackages.rocm-smi
];
rocmClang = linkFarm "rocm-clang" {
llvm = rocmPackages.llvm.clang;
};
rocmPath = buildEnv {
name = "rocm-path";
2024-05-14 10:42:14 +01:00
paths = rocmLibs ++ [ rocmClang ];
};
cudaToolkit = buildEnv {
name = "cuda-toolkit";
ignoreCollisions = true; # FIXME: find a cleaner way to do this without ignoring collisions
paths = [
cudaPackages.cudatoolkit
cudaPackages.cuda_cudart
cudaPackages.cuda_cudart.static
];
};
2024-02-21 23:41:45 +00:00
runtimeLibs = lib.optionals enableRocm [
2024-05-14 10:42:14 +01:00
rocmPath
2024-02-21 23:41:45 +00:00
] ++ lib.optionals enableCuda [
linuxPackages.nvidia_x11
];
2024-02-21 23:41:45 +00:00
appleFrameworks = darwin.apple_sdk_11_0.frameworks;
metalFrameworks = [
appleFrameworks.Accelerate
appleFrameworks.Metal
appleFrameworks.MetalKit
appleFrameworks.MetalPerformanceShaders
];
goBuild =
2024-02-21 23:41:45 +00:00
if enableCuda then
buildGo122Module.override { stdenv = overrideCC stdenv gcc12; }
else
buildGo122Module;
2024-02-21 23:41:45 +00:00
inherit (lib) licenses platforms maintainers;
in
2024-02-21 23:41:45 +00:00
goBuild ((lib.optionalAttrs enableRocm {
ROCM_PATH = rocmPath;
CLBlast_DIR = "${clblast}/lib/cmake/CLBlast";
}) // (lib.optionalAttrs enableCuda {
CUDA_LIB_DIR = "${cudaToolkit}/lib";
CUDACXX = "${cudaToolkit}/bin/nvcc";
CUDAToolkit_ROOT = cudaToolkit;
}) // {
2024-03-31 20:16:03 +01:00
inherit pname version src vendorHash;
2024-02-21 23:41:45 +00:00
nativeBuildInputs = [
cmake
] ++ lib.optionals enableRocm [
rocmPackages.llvm.bintools
2024-02-21 23:41:45 +00:00
] ++ lib.optionals (enableRocm || enableCuda) [
makeWrapper
] ++ lib.optionals stdenv.isDarwin
metalFrameworks;
2024-05-14 10:42:14 +01:00
buildInputs = lib.optionals enableRocm
(rocmLibs ++ [ libdrm ])
++ lib.optionals enableCuda [
2024-02-21 23:41:45 +00:00
cudaPackages.cuda_cudart
] ++ lib.optionals stdenv.isDarwin
metalFrameworks;
patches = [
2024-03-31 20:16:03 +01:00
# disable uses of `git` in the `go generate` script
# ollama's build script assumes the source is a git repo, but nix removes the git directory
# this also disables necessary patches contained in `ollama/llm/patches/`
# those patches are added to `llamacppPatches`, and reapplied here in the patch phase
./disable-git.patch
2024-05-14 10:42:14 +01:00
# disable a check that unnecessarily exits compilation during rocm builds
# since `rocmPath` is in `LD_LIBRARY_PATH`, ollama uses rocm correctly
./disable-lib-check.patch
2024-03-31 20:16:03 +01:00
] ++ llamacppPatches;
2024-02-21 23:41:45 +00:00
postPatch = ''
# replace inaccurate version number with actual release version
substituteInPlace version/version.go --replace-fail 0.0.0 '${version}'
'';
preBuild = ''
2024-03-31 20:16:03 +01:00
# disable uses of `git`, since nix removes the git directory
2024-02-21 23:41:45 +00:00
export OLLAMA_SKIP_PATCHING=true
# build llama.cpp libraries for ollama
go generate ./...
'';
postFixup = ''
# the app doesn't appear functional at the moment, so hide it
mv "$out/bin/app" "$out/bin/.ollama-app"
'' + lib.optionalString (enableRocm || enableCuda) ''
# expose runtime libraries necessary to use the gpu
mv "$out/bin/ollama" "$out/bin/.ollama-unwrapped"
2024-03-31 20:16:03 +01:00
makeWrapper "$out/bin/.ollama-unwrapped" "$out/bin/ollama" ${
lib.optionalString enableRocm
''--set-default HIP_PATH '${rocmPath}' ''} \
--suffix LD_LIBRARY_PATH : '/run/opengl-driver/lib:${lib.makeLibraryPath runtimeLibs}'
2024-02-21 23:41:45 +00:00
'';
ldflags = [
"-s"
"-w"
"-X=github.com/jmorganca/ollama/version.Version=${version}"
"-X=github.com/jmorganca/ollama/server.mode=release"
];
passthru.tests = {
service = nixosTests.ollama;
rocm = pkgs.ollama.override { acceleration = "rocm"; };
cuda = pkgs.ollama.override { acceleration = "cuda"; };
version = testers.testVersion {
inherit version;
package = ollama;
};
};
2024-02-21 23:41:45 +00:00
meta = {
description = "Get up and running with large language models locally";
2024-03-31 20:16:03 +01:00
homepage = "https://github.com/ollama/ollama";
changelog = "https://github.com/ollama/ollama/releases/tag/v${version}";
2024-02-21 23:41:45 +00:00
license = licenses.mit;
platforms = platforms.unix;
mainProgram = "ollama";
maintainers = with maintainers; [ abysssol dit7ya elohmeier ];
};
})