ollama: reformat with nixfmt-rfc-style

This commit is contained in:
Pol Dellaiera 2024-07-23 10:29:48 +02:00
parent a22838c965
commit bdfde18037
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA

View File

@ -1,32 +1,33 @@
{ lib
, buildGoModule
, fetchFromGitHub
, fetchpatch
, buildEnv
, linkFarm
, overrideCC
, makeWrapper
, stdenv
, addDriverRunpath
{
lib,
buildGoModule,
fetchFromGitHub,
fetchpatch,
buildEnv,
linkFarm,
overrideCC,
makeWrapper,
stdenv,
addDriverRunpath,
, cmake
, gcc12
, clblast
, libdrm
, rocmPackages
, cudaPackages
, darwin
, autoAddDriverRunpath
cmake,
gcc12,
clblast,
libdrm,
rocmPackages,
cudaPackages,
darwin,
autoAddDriverRunpath,
, nixosTests
, testers
, ollama
, ollama-rocm
, ollama-cuda
nixosTests,
testers,
ollama,
ollama-rocm,
ollama-cuda,
, config
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 {
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 [
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);
]) (!(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,54 +122,56 @@ let
appleFrameworks.MetalPerformanceShaders
];
wrapperOptions = [
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 [
]
++ 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 {
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;
})
// (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) [
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;
]
++ lib.optionals stdenv.isDarwin metalFrameworks;
buildInputs = lib.optionals enableRocm
(rocmLibs ++ [ libdrm ])
buildInputs =
lib.optionals enableRocm (rocmLibs ++ [ libdrm ])
++ lib.optionals enableCuda [
cudaPackages.cuda_cudart
cudaPackages.cuda_cccl
cudaPackages.libcublas
] ++ lib.optionals stdenv.isDarwin
metalFrameworks;
]
++ lib.optionals stdenv.isDarwin metalFrameworks;
patches = [
# disable uses of `git` in the `go generate` script
@ -190,10 +193,12 @@ goBuild ((lib.optionalAttrs enableRocm {
# build llama.cpp libraries for ollama
go generate ./...
'';
postFixup = ''
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) ''
''
+ lib.optionalString (enableRocm || enableCuda) ''
# expose runtime libraries necessary to use the gpu
wrapProgram "$out/bin/ollama" ${wrapperArgs}
'';
@ -205,30 +210,37 @@ goBuild ((lib.optionalAttrs enableRocm {
"-X=github.com/ollama/ollama/server.mode=release"
];
passthru.tests = {
passthru.tests =
{
inherit ollama;
service = nixosTests.ollama;
version = testers.testVersion {
inherit version;
package = ollama;
};
} // lib.optionalAttrs stdenv.isLinux {
}
// 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"
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;
platforms = if (rocmRequested || cudaRequested) then platforms.linux else platforms.unix;
mainProgram = "ollama";
maintainers = with maintainers; [ abysssol dit7ya elohmeier roydubnium ];
maintainers = with maintainers; [
abysssol
dit7ya
elohmeier
roydubnium
];
};
})
}
)