2023-04-06 20:41:21 +00:00
|
|
|
{
|
|
|
|
lib,
|
2024-09-16 22:07:38 +00:00
|
|
|
torch,
|
|
|
|
symlinkJoin,
|
2023-04-06 20:41:21 +00:00
|
|
|
buildPythonPackage,
|
|
|
|
fetchFromGitHub,
|
2024-09-16 22:07:38 +00:00
|
|
|
cmake,
|
2023-04-06 20:41:21 +00:00
|
|
|
setuptools,
|
|
|
|
scipy,
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
pname = "bitsandbytes";
|
2024-09-16 22:07:38 +00:00
|
|
|
version = "0.44.1";
|
|
|
|
|
|
|
|
inherit (torch) cudaPackages cudaSupport;
|
|
|
|
inherit (cudaPackages) cudaVersion;
|
2023-04-06 20:41:21 +00:00
|
|
|
|
2024-09-16 22:07:38 +00:00
|
|
|
cudaVersionString = lib.replaceStrings [ "." ] [ "" ] (lib.versions.majorMinor cudaVersion);
|
2023-04-06 20:41:21 +00:00
|
|
|
|
|
|
|
# NOTE: torchvision doesn't use cudnn; torch does!
|
|
|
|
# For this reason it is not included.
|
|
|
|
cuda-common-redist = with cudaPackages; [
|
2024-09-16 22:07:38 +00:00
|
|
|
(lib.getDev cuda_cccl) # <thrust/*>
|
|
|
|
(lib.getDev libcublas) # cublas_v2.h
|
|
|
|
(lib.getLib libcublas)
|
2023-04-06 20:41:21 +00:00
|
|
|
libcurand
|
|
|
|
libcusolver # cusolverDn.h
|
2024-09-16 22:07:38 +00:00
|
|
|
(lib.getDev libcusparse) # cusparse.h
|
|
|
|
(lib.getLib libcusparse) # cusparse.h
|
|
|
|
(lib.getDev cuda_cudart) # cuda_runtime.h cuda_runtime_api.h
|
2023-04-06 20:41:21 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
cuda-native-redist = symlinkJoin {
|
|
|
|
name = "cuda-native-redist-${cudaVersion}";
|
|
|
|
paths =
|
|
|
|
with cudaPackages;
|
|
|
|
[
|
2024-09-16 22:07:38 +00:00
|
|
|
(lib.getDev cuda_cudart) # cuda_runtime.h cuda_runtime_api.h
|
|
|
|
(lib.getLib cuda_cudart)
|
|
|
|
(lib.getStatic cuda_cudart)
|
2023-04-06 20:41:21 +00:00
|
|
|
cuda_nvcc
|
|
|
|
]
|
|
|
|
++ cuda-common-redist;
|
|
|
|
};
|
|
|
|
|
|
|
|
cuda-redist = symlinkJoin {
|
|
|
|
name = "cuda-redist-${cudaVersion}";
|
|
|
|
paths = cuda-common-redist;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
buildPythonPackage {
|
|
|
|
inherit pname version;
|
2024-02-19 13:44:13 +00:00
|
|
|
pyproject = true;
|
2023-04-06 20:41:21 +00:00
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "TimDettmers";
|
2024-02-19 13:44:13 +00:00
|
|
|
repo = "bitsandbytes";
|
2023-04-06 20:41:21 +00:00
|
|
|
rev = "refs/tags/${version}";
|
2024-09-16 22:07:38 +00:00
|
|
|
hash = "sha256-yvxD5ymMK5p4Xg7Csx/90mPV3yxUC6QUuF/8BKO2p0k=";
|
2023-04-06 20:41:21 +00:00
|
|
|
};
|
|
|
|
|
2024-09-16 22:07:38 +00:00
|
|
|
# By default, which library is loaded depends on the result of `torch.cuda.is_available()`.
|
|
|
|
# When `cudaSupport` is enabled, bypass this check and load the cuda library unconditionnally.
|
|
|
|
# Indeed, in this case, only `libbitsandbytes_cuda124.so` is built. `libbitsandbytes_cpu.so` is not.
|
|
|
|
# Also, hardcode the path to the previously built library instead of relying on
|
|
|
|
# `get_cuda_bnb_library_path(cuda_specs)` which relies on `torch.cuda` too.
|
|
|
|
#
|
|
|
|
# WARNING: The cuda library is currently named `libbitsandbytes_cudaxxy` for cuda version `xx.y`.
|
|
|
|
# This upstream convention could change at some point and thus break the following patch.
|
|
|
|
postPatch = lib.optionalString cudaSupport ''
|
|
|
|
substituteInPlace bitsandbytes/cextension.py \
|
|
|
|
--replace-fail "if cuda_specs:" "if True:" \
|
|
|
|
--replace-fail \
|
|
|
|
"cuda_binary_path = get_cuda_bnb_library_path(cuda_specs)" \
|
|
|
|
"cuda_binary_path = PACKAGE_DIR / 'libbitsandbytes_cuda${cudaVersionString}.so'"
|
|
|
|
'';
|
2023-04-06 20:41:21 +00:00
|
|
|
|
2024-02-19 13:44:13 +00:00
|
|
|
nativeBuildInputs = [
|
2024-09-16 22:07:38 +00:00
|
|
|
cmake
|
|
|
|
cudaPackages.cuda_nvcc
|
|
|
|
];
|
|
|
|
|
|
|
|
build-system = [
|
2024-02-19 13:44:13 +00:00
|
|
|
setuptools
|
2024-09-16 22:07:38 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
buildInputs = lib.optionals cudaSupport [ cuda-redist ];
|
|
|
|
|
|
|
|
cmakeFlags = [
|
|
|
|
(lib.cmakeFeature "COMPUTE_BACKEND" (if cudaSupport then "cuda" else "cpu"))
|
|
|
|
];
|
|
|
|
CUDA_HOME = "${cuda-native-redist}";
|
|
|
|
NVCC_PREPEND_FLAGS = lib.optionals cudaSupport [
|
|
|
|
"-I${cuda-native-redist}/include"
|
|
|
|
"-L${cuda-native-redist}/lib"
|
|
|
|
];
|
2024-02-19 13:44:13 +00:00
|
|
|
|
2024-09-16 22:07:38 +00:00
|
|
|
preBuild = ''
|
|
|
|
make -j $NIX_BUILD_CORES
|
|
|
|
cd .. # leave /build/source/build
|
|
|
|
'';
|
2023-04-06 20:41:21 +00:00
|
|
|
|
2024-09-16 22:07:38 +00:00
|
|
|
dependencies = [
|
2023-08-09 13:43:09 +00:00
|
|
|
scipy
|
2023-04-06 20:41:21 +00:00
|
|
|
torch
|
|
|
|
];
|
|
|
|
|
|
|
|
doCheck = false; # tests require CUDA and also GPU access
|
|
|
|
|
|
|
|
pythonImportsCheck = [ "bitsandbytes" ];
|
|
|
|
|
2024-09-16 22:07:38 +00:00
|
|
|
meta = {
|
2023-04-06 20:41:21 +00:00
|
|
|
description = "8-bit CUDA functions for PyTorch";
|
2024-02-19 13:44:13 +00:00
|
|
|
homepage = "https://github.com/TimDettmers/bitsandbytes";
|
|
|
|
changelog = "https://github.com/TimDettmers/bitsandbytes/releases/tag/${version}";
|
2024-09-16 22:07:38 +00:00
|
|
|
license = lib.licenses.mit;
|
|
|
|
maintainers = with lib.maintainers; [ bcdarwin ];
|
2023-04-06 20:41:21 +00:00
|
|
|
};
|
|
|
|
}
|