From 26a309c6d6d7b62af1fa7a3ba88f5af0e854b2da Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 23 Jul 2024 10:15:42 +0200 Subject: [PATCH 1/4] ollama: 0.2.7 -> 0.2.8 changelog: https://github.com/ollama/ollama/releases/tag/v0.2.8 diff: https://github.com/ollama/ollama/compare/v0.2.7..v0.2.8 --- pkgs/by-name/ol/ollama/package.nix | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkgs/by-name/ol/ollama/package.nix b/pkgs/by-name/ol/ollama/package.nix index 6dd421ec6176cf3..05b33ae57241ad7 100644 --- a/pkgs/by-name/ol/ollama/package.nix +++ b/pkgs/by-name/ol/ollama/package.nix @@ -32,13 +32,13 @@ let pname = "ollama"; # don't forget to invalidate all hashes each update - version = "0.2.7"; + version = "0.2.8"; src = fetchFromGitHub { owner = "ollama"; repo = "ollama"; rev = "v${version}"; - hash = "sha256-YHBGS615dxz6pZR+8awMinp+ZRf3J8Re5BVeDEIyt2E="; + hash = "sha256-uir/GoFs0nhhHpznyYkwVjAUWxwjWmGZ7liU2htyQ04="; fetchSubmodules = true; }; @@ -52,11 +52,11 @@ let (preparePatch "02-clip-log.diff" "sha256-rMWbl3QgrPlhisTeHwD7EnGRJyOhLB4UeS7rqa0tdXM=") (preparePatch "03-load_exception.diff" "sha256-NJkT/k8Mf8HcEMb0XkaLmyUNKV3T+384JRPnmwDI/sk=") (preparePatch "04-metal.diff" "sha256-bPBCfoT3EjZPjWKfCzh0pnCUbM/fGTj37yOaQr+QxQ4=") - (preparePatch "05-default-pretokenizer.diff" "sha256-50+mzQBQZmYEhYvARHw/dliH0M/gDOYm2uy/yJupDF4=") - (preparePatch "06-qwen2.diff" "sha256-FdDqEIblPy47z3yavKUnaV93Yk+3oboEzj4vHq+R66M=") - (preparePatch "07-embeddings.diff" "sha256-lqg2SI0OapD9LCoAG6MJW6HIHXEmCTv7P75rE9yq/Mo=") - (preparePatch "08-clip-unicode.diff" "sha256-1qMJoXhDewxsqPbmi+/7xILQfGaybZDyXc5eH0winL8=") - (preparePatch "09-pooling.diff" "sha256-7meKWbr06lbVrtxau0AU9BwJ88Z9svwtDXhmHI+hYBk=") + (preparePatch "05-default-pretokenizer.diff" "sha256-Mgx+xi59rz3d5yEXp90QPQMiUr9InlA0Wo1mOSuRcec=") + (preparePatch "06-embeddings.diff" "sha256-lqg2SI0OapD9LCoAG6MJW6HIHXEmCTv7P75rE9yq/Mo=") + (preparePatch "07-clip-unicode.diff" "sha256-1qMJoXhDewxsqPbmi+/7xILQfGaybZDyXc5eH0winL8=") + (preparePatch "08-pooling.diff" "sha256-7meKWbr06lbVrtxau0AU9BwJ88Z9svwtDXhmHI+hYBk=") + (preparePatch "09-lora.diff" "sha256-HVDYiqNkuWO9K7aIiT73iiMj5lxMsJC1oqIG4madAPk=") ]; preparePatch = patch: hash: fetchpatch { From ef6a3aec105ecdf21fb3d0d92f1e22f80419d82a Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 23 Jul 2024 10:29:48 +0200 Subject: [PATCH 2/4] ollama: reformat with `nixfmt-rfc-style` --- .git-blame-ignore-revs | 3 + pkgs/by-name/ol/ollama/package.nix | 320 +++++++++++++++-------------- 2 files changed, 169 insertions(+), 154 deletions(-) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 012889bb2e8247f..ae22633837b6fe5 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -141,3 +141,6 @@ acd0e3898feb321cb9a71a0fd376f1157d0f4553 # poptracker: format with nixfmt-rfc-style (#326697) ff5c8f6cc3d1f2e017e86d50965c14b71f00567b + +# ollama: reformat with nixfmt-rfc-style (#329353) +8f91c4628a6b2e3f486c150c15b7407214d9b9de diff --git a/pkgs/by-name/ol/ollama/package.nix b/pkgs/by-name/ol/ollama/package.nix index 05b33ae57241ad7..7b1f2c88e92e591 100644 --- a/pkgs/by-name/ol/ollama/package.nix +++ b/pkgs/by-name/ol/ollama/package.nix @@ -1,32 +1,33 @@ -{ lib -, buildGoModule -, fetchFromGitHub -, fetchpatch -, buildEnv -, linkFarm -, overrideCC -, makeWrapper -, stdenv -, addDriverRunpath - -, cmake -, gcc12 -, clblast -, libdrm -, rocmPackages -, cudaPackages -, darwin -, autoAddDriverRunpath - -, nixosTests -, testers -, ollama -, ollama-rocm -, ollama-cuda - -, config +{ + lib, + buildGoModule, + fetchFromGitHub, + fetchpatch, + buildEnv, + linkFarm, + overrideCC, + makeWrapper, + stdenv, + addDriverRunpath, + + cmake, + gcc12, + clblast, + libdrm, + rocmPackages, + cudaPackages, + darwin, + autoAddDriverRunpath, + + nixosTests, + testers, + ollama, + ollama-rocm, + ollama-cuda, + + config, # one of `[ null false "rocm" "cuda" ]` -, acceleration ? null + acceleration ? null, }: let @@ -59,25 +60,28 @@ let (preparePatch "09-lora.diff" "sha256-HVDYiqNkuWO9K7aIiT73iiMj5lxMsJC1oqIG4madAPk=") ]; - preparePatch = patch: hash: fetchpatch { - url = "file://${src}/llm/patches/${patch}"; - inherit hash; - stripLen = 1; - extraPrefix = "llm/llama.cpp/"; - }; - + preparePatch = + patch: hash: + fetchpatch { + url = "file://${src}/llm/patches/${patch}"; + inherit hash; + stripLen = 1; + extraPrefix = "llm/llama.cpp/"; + }; - 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)); - shouldEnable = assert accelIsValid; - mode: fallback: - (acceleration == mode) - || (fallback && acceleration == null && validateFallback); + 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)); + shouldEnable = + assert accelIsValid; + mode: fallback: (acceleration == mode) || (fallback && acceleration == null && validateFallback); rocmRequested = shouldEnable "rocm" config.rocmSupport; cudaRequested = shouldEnable "cuda" config.cudaSupport; @@ -85,7 +89,6 @@ let enableRocm = rocmRequested && stdenv.isLinux; enableCuda = cudaRequested && stdenv.isLinux; - rocmLibs = [ rocmPackages.clr rocmPackages.hipblas @@ -95,9 +98,7 @@ let rocmPackages.rocm-device-libs rocmPackages.rocm-smi ]; - rocmClang = linkFarm "rocm-clang" { - llvm = rocmPackages.llvm.clang; - }; + rocmClang = linkFarm "rocm-clang" { llvm = rocmPackages.llvm.clang; }; rocmPath = buildEnv { name = "rocm-path"; paths = rocmLibs ++ [ rocmClang ]; @@ -121,114 +122,125 @@ let appleFrameworks.MetalPerformanceShaders ]; - wrapperOptions = [ - # ollama embeds llama-cpp binaries which actually run the ai models - # these llama-cpp binaries are unaffected by the ollama binary's DT_RUNPATH - # LD_LIBRARY_PATH is temporarily required to use the gpu - # until these llama-cpp binaries can have their runpath patched - "--suffix LD_LIBRARY_PATH : '${addDriverRunpath.driverLink}/lib'" - ] ++ lib.optionals enableRocm [ - "--suffix LD_LIBRARY_PATH : '${rocmPath}/lib'" - "--set-default HIP_PATH '${rocmPath}'" - ]; + wrapperOptions = + [ + # ollama embeds llama-cpp binaries which actually run the ai models + # these llama-cpp binaries are unaffected by the ollama binary's DT_RUNPATH + # LD_LIBRARY_PATH is temporarily required to use the gpu + # until these llama-cpp binaries can have their runpath patched + "--suffix LD_LIBRARY_PATH : '${addDriverRunpath.driverLink}/lib'" + ] + ++ lib.optionals enableRocm [ + "--suffix LD_LIBRARY_PATH : '${rocmPath}/lib'" + "--set-default HIP_PATH '${rocmPath}'" + ]; wrapperArgs = builtins.concatStringsSep " " wrapperOptions; - goBuild = - if enableCuda then - buildGoModule.override { stdenv = overrideCC stdenv gcc12; } - else - buildGoModule; + if enableCuda then buildGoModule.override { stdenv = overrideCC stdenv gcc12; } else buildGoModule; inherit (lib) licenses platforms maintainers; in -goBuild ((lib.optionalAttrs enableRocm { - ROCM_PATH = rocmPath; - CLBlast_DIR = "${clblast}/lib/cmake/CLBlast"; -}) // (lib.optionalAttrs enableCuda { - CUDA_LIB_DIR = "${cudaToolkit}/lib"; -}) // { - inherit pname version src vendorHash; - - nativeBuildInputs = [ - cmake - ] ++ lib.optionals enableRocm [ - rocmPackages.llvm.bintools - ] ++ lib.optionals enableCuda [ - cudaPackages.cuda_nvcc - ] ++ lib.optionals (enableRocm || enableCuda) [ - makeWrapper - autoAddDriverRunpath - ] ++ lib.optionals stdenv.isDarwin - metalFrameworks; - - buildInputs = lib.optionals enableRocm - (rocmLibs ++ [ libdrm ]) - ++ lib.optionals enableCuda [ - cudaPackages.cuda_cudart - cudaPackages.cuda_cccl - cudaPackages.libcublas - ] ++ lib.optionals stdenv.isDarwin - metalFrameworks; - - patches = [ - # 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 - # 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 - ] ++ llamacppPatches; - postPatch = '' - # replace inaccurate version number with actual release version - substituteInPlace version/version.go --replace-fail 0.0.0 '${version}' - ''; - preBuild = '' - # disable uses of `git`, since nix removes the git directory - 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 - wrapProgram "$out/bin/ollama" ${wrapperArgs} - ''; - - ldflags = [ - "-s" - "-w" - "-X=github.com/ollama/ollama/version.Version=${version}" - "-X=github.com/ollama/ollama/server.mode=release" - ]; +goBuild ( + (lib.optionalAttrs enableRocm { + ROCM_PATH = rocmPath; + CLBlast_DIR = "${clblast}/lib/cmake/CLBlast"; + }) + // (lib.optionalAttrs enableCuda { CUDA_LIB_DIR = "${cudaToolkit}/lib"; }) + // { + inherit + pname + version + src + vendorHash + ; + + nativeBuildInputs = + [ cmake ] + ++ lib.optionals enableRocm [ rocmPackages.llvm.bintools ] + ++ lib.optionals enableCuda [ cudaPackages.cuda_nvcc ] + ++ lib.optionals (enableRocm || enableCuda) [ + makeWrapper + autoAddDriverRunpath + ] + ++ lib.optionals stdenv.isDarwin metalFrameworks; + + buildInputs = + lib.optionals enableRocm (rocmLibs ++ [ libdrm ]) + ++ lib.optionals enableCuda [ + cudaPackages.cuda_cudart + cudaPackages.cuda_cccl + cudaPackages.libcublas + ] + ++ lib.optionals stdenv.isDarwin metalFrameworks; + + patches = [ + # 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 + # 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 + ] ++ llamacppPatches; + postPatch = '' + # replace inaccurate version number with actual release version + substituteInPlace version/version.go --replace-fail 0.0.0 '${version}' + ''; + preBuild = '' + # disable uses of `git`, since nix removes the git directory + 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 + wrapProgram "$out/bin/ollama" ${wrapperArgs} + ''; + + ldflags = [ + "-s" + "-w" + "-X=github.com/ollama/ollama/version.Version=${version}" + "-X=github.com/ollama/ollama/server.mode=release" + ]; - passthru.tests = { - inherit ollama; - service = nixosTests.ollama; - version = testers.testVersion { - inherit version; - package = ollama; + passthru.tests = + { + inherit ollama; + service = nixosTests.ollama; + version = testers.testVersion { + inherit version; + package = ollama; + }; + } + // lib.optionalAttrs stdenv.isLinux { + inherit ollama-rocm ollama-cuda; + service-cuda = nixosTests.ollama-cuda; + service-rocm = nixosTests.ollama-rocm; + }; + + meta = { + description = + "Get up and running with large language models locally" + + lib.optionalString rocmRequested ", using ROCm for AMD GPU acceleration" + + lib.optionalString cudaRequested ", using CUDA for NVIDIA GPU acceleration"; + homepage = "https://github.com/ollama/ollama"; + changelog = "https://github.com/ollama/ollama/releases/tag/v${version}"; + license = licenses.mit; + platforms = if (rocmRequested || cudaRequested) then platforms.linux else platforms.unix; + mainProgram = "ollama"; + maintainers = with maintainers; [ + abysssol + dit7ya + elohmeier + roydubnium + ]; }; - } // lib.optionalAttrs stdenv.isLinux { - inherit ollama-rocm ollama-cuda; - service-cuda = nixosTests.ollama-cuda; - service-rocm = nixosTests.ollama-rocm; - }; - - meta = { - description = "Get up and running with large language models locally" - + lib.optionalString rocmRequested ", using ROCm for AMD GPU acceleration" - + lib.optionalString cudaRequested ", using CUDA for NVIDIA GPU acceleration"; - homepage = "https://github.com/ollama/ollama"; - changelog = "https://github.com/ollama/ollama/releases/tag/v${version}"; - license = licenses.mit; - platforms = - if (rocmRequested || cudaRequested) then platforms.linux - else platforms.unix; - mainProgram = "ollama"; - maintainers = with maintainers; [ abysssol dit7ya elohmeier roydubnium ]; - }; -}) + } +) From d86d9340c5314a10d0ba3e7ef498d108def03538 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 23 Jul 2024 11:08:55 +0200 Subject: [PATCH 3/4] ollama: move the assert after the top level parameters --- pkgs/by-name/ol/ollama/package.nix | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkgs/by-name/ol/ollama/package.nix b/pkgs/by-name/ol/ollama/package.nix index 7b1f2c88e92e591..c325192a583bbe7 100644 --- a/pkgs/by-name/ol/ollama/package.nix +++ b/pkgs/by-name/ol/ollama/package.nix @@ -30,6 +30,13 @@ acceleration ? null, }: +assert builtins.elem acceleration [ + null + false + "rocm" + "cuda" +]; + let pname = "ollama"; # don't forget to invalidate all hashes each update @@ -69,18 +76,11 @@ let extraPrefix = "llm/llama.cpp/"; }; - 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)); shouldEnable = - assert accelIsValid; mode: fallback: (acceleration == mode) || (fallback && acceleration == null && validateFallback); rocmRequested = shouldEnable "rocm" config.rocmSupport; From 1b3b0049526c02ef652b2bc6bdca5042c6a59f31 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 23 Jul 2024 11:51:43 +0200 Subject: [PATCH 4/4] ollama: replace variable with `with` keyword --- pkgs/by-name/ol/ollama/package.nix | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pkgs/by-name/ol/ollama/package.nix b/pkgs/by-name/ol/ollama/package.nix index c325192a583bbe7..a1f4dcc2062923f 100644 --- a/pkgs/by-name/ol/ollama/package.nix +++ b/pkgs/by-name/ol/ollama/package.nix @@ -114,12 +114,11 @@ let ]; }; - appleFrameworks = darwin.apple_sdk_11_0.frameworks; - metalFrameworks = [ - appleFrameworks.Accelerate - appleFrameworks.Metal - appleFrameworks.MetalKit - appleFrameworks.MetalPerformanceShaders + metalFrameworks = with darwin.apple_sdk_11_0.frameworks; [ + Accelerate + Metal + MetalKit + MetalPerformanceShaders ]; wrapperOptions =