about summary refs log tree commit diff
path: root/pkgs/development/python-modules/bitsandbytes/default.nix
blob: 9bfc4a89784fe3df0756769e3781ef7277ff19fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{
  lib,
  buildPythonPackage,
  fetchFromGitHub,
  python,
  pythonOlder,
  setuptools,
  wheel,
  torch,
  scipy,
  symlinkJoin,
}:

let
  pname = "bitsandbytes";
  version = "0.43.1";

  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}";
    paths =
      with cudaPackages;
      [
        cuda_cudart # cuda_runtime.h cuda_runtime_api.h
        cuda_nvcc
      ]
      ++ cuda-common-redist;
  };

  cuda-redist = symlinkJoin {
    name = "cuda-redist-${cudaVersion}";
    paths = cuda-common-redist;
  };
in
buildPythonPackage {
  inherit pname version;
  pyproject = true;

  disabled = pythonOlder "3.7";

  src = fetchFromGitHub {
    owner = "TimDettmers";
    repo = "bitsandbytes";
    rev = "refs/tags/${version}";
    hash = "sha256-GFbFKPdV96DXPA+PZO4h0zdBclN670fb0PGv4QPHWHU=";
  };

  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"
    '';

  CUDA_HOME = "${cuda-native-redist}";

  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'';

  nativeBuildInputs = [
    setuptools
    wheel
  ] ++ lib.optionals torch.cudaSupport [ cuda-native-redist ];

  buildInputs = lib.optionals torch.cudaSupport [ cuda-redist ];

  propagatedBuildInputs = [
    scipy
    torch
  ];

  doCheck = false; # tests require CUDA and also GPU access

  pythonImportsCheck = [ "bitsandbytes" ];

  meta = with lib; {
    description = "8-bit CUDA functions for PyTorch";
    homepage = "https://github.com/TimDettmers/bitsandbytes";
    changelog = "https://github.com/TimDettmers/bitsandbytes/releases/tag/${version}";
    license = licenses.mit;
    maintainers = with maintainers; [ bcdarwin ];
  };
}