2024-06-05 15:53:02 +00:00
|
|
|
{
|
|
|
|
lib,
|
|
|
|
buildPythonPackage,
|
|
|
|
fetchFromGitHub,
|
|
|
|
python,
|
|
|
|
pythonOlder,
|
|
|
|
setuptools,
|
|
|
|
wheel,
|
|
|
|
torch,
|
|
|
|
scipy,
|
|
|
|
symlinkJoin,
|
2023-04-29 16:46:19 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
pname = "bitsandbytes";
|
2024-07-27 06:49:29 +00:00
|
|
|
version = "0.43.1";
|
2023-04-29 16:46:19 +00:00
|
|
|
|
|
|
|
inherit (torch) cudaCapabilities cudaPackages cudaSupport;
|
|
|
|
inherit (cudaPackages) backendStdenv cudaVersion;
|
|
|
|
|
|
|
|
# NOTE: torchvision doesn't use cudnn; torch does!
|
|
|
|
# For this reason it is not included.
|
|
|
|
cuda-common-redist = with cudaPackages; [
|
|
|
|
cuda_cccl # <thrust/*>
|
|
|
|
libcublas # cublas_v2.h
|
|
|
|
libcurand
|
|
|
|
libcusolver # cusolverDn.h
|
|
|
|
libcusparse # cusparse.h
|
|
|
|
];
|
|
|
|
|
|
|
|
cuda-native-redist = symlinkJoin {
|
|
|
|
name = "cuda-native-redist-${cudaVersion}";
|
2024-06-05 15:53:02 +00:00
|
|
|
paths =
|
|
|
|
with cudaPackages;
|
|
|
|
[
|
|
|
|
cuda_cudart # cuda_runtime.h cuda_runtime_api.h
|
|
|
|
cuda_nvcc
|
|
|
|
]
|
|
|
|
++ cuda-common-redist;
|
2023-04-29 16:46:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
cuda-redist = symlinkJoin {
|
|
|
|
name = "cuda-redist-${cudaVersion}";
|
|
|
|
paths = cuda-common-redist;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
buildPythonPackage {
|
|
|
|
inherit pname version;
|
2024-02-29 20:09:43 +00:00
|
|
|
pyproject = true;
|
2023-04-29 16:46:19 +00:00
|
|
|
|
|
|
|
disabled = pythonOlder "3.7";
|
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "TimDettmers";
|
2024-02-29 20:09:43 +00:00
|
|
|
repo = "bitsandbytes";
|
2023-04-29 16:46:19 +00:00
|
|
|
rev = "refs/tags/${version}";
|
2024-07-27 06:49:29 +00:00
|
|
|
hash = "sha256-GFbFKPdV96DXPA+PZO4h0zdBclN670fb0PGv4QPHWHU=";
|
2023-04-29 16:46:19 +00:00
|
|
|
};
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
postPatch =
|
|
|
|
''
|
|
|
|
substituteInPlace Makefile --replace "/usr/bin/g++" "g++" --replace "lib64" "lib"
|
|
|
|
substituteInPlace bitsandbytes/cuda_setup/main.py \
|
|
|
|
--replace "binary_path = package_dir / self.binary_name" \
|
|
|
|
"binary_path = Path('$out/${python.sitePackages}/${pname}')/self.binary_name"
|
|
|
|
''
|
|
|
|
+ lib.optionalString torch.cudaSupport ''
|
|
|
|
substituteInPlace bitsandbytes/cuda_setup/main.py \
|
|
|
|
--replace "/usr/local/cuda/lib64" "${cuda-native-redist}/lib"
|
|
|
|
'';
|
2023-04-29 16:46:19 +00:00
|
|
|
|
|
|
|
CUDA_HOME = "${cuda-native-redist}";
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
preBuild =
|
|
|
|
if torch.cudaSupport then
|
|
|
|
with torch.cudaPackages;
|
|
|
|
let
|
|
|
|
cudaVersion = lib.concatStrings (lib.splitVersion torch.cudaPackages.cudaMajorMinorVersion);
|
|
|
|
in
|
|
|
|
''make CUDA_VERSION=${cudaVersion} cuda${cudaMajorVersion}x''
|
|
|
|
else
|
|
|
|
''make CUDA_VERSION=CPU cpuonly'';
|
2023-04-29 16:46:19 +00:00
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
nativeBuildInputs = [
|
|
|
|
setuptools
|
|
|
|
wheel
|
2024-06-05 15:53:02 +00:00
|
|
|
] ++ lib.optionals torch.cudaSupport [ cuda-native-redist ];
|
2024-02-29 20:09:43 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
buildInputs = lib.optionals torch.cudaSupport [ cuda-redist ];
|
2023-04-29 16:46:19 +00:00
|
|
|
|
|
|
|
propagatedBuildInputs = [
|
2023-08-10 07:59:29 +00:00
|
|
|
scipy
|
2023-04-29 16:46:19 +00:00
|
|
|
torch
|
|
|
|
];
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
doCheck = false; # tests require CUDA and also GPU access
|
2023-04-29 16:46:19 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
pythonImportsCheck = [ "bitsandbytes" ];
|
2023-04-29 16:46:19 +00:00
|
|
|
|
|
|
|
meta = with lib; {
|
|
|
|
description = "8-bit CUDA functions for PyTorch";
|
2024-02-29 20:09:43 +00:00
|
|
|
homepage = "https://github.com/TimDettmers/bitsandbytes";
|
|
|
|
changelog = "https://github.com/TimDettmers/bitsandbytes/releases/tag/${version}";
|
2023-04-29 16:46:19 +00:00
|
|
|
license = licenses.mit;
|
|
|
|
maintainers = with maintainers; [ bcdarwin ];
|
|
|
|
};
|
|
|
|
}
|