Skip to content

Commit 89e3b82

Browse files
committed
Added more detailed cuda setup debug and debugging instructions.
1 parent 4cd63de commit 89e3b82

File tree

6 files changed

+140
-35
lines changed

6 files changed

+140
-35
lines changed

README.md

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,40 @@ Resources:
1111

1212
## TL;DR
1313
**Requirements**
14-
Python >=3.8. Linux distribution (Ubuntu, MacOS, etc.) + CUDA > 10.0. LLM.int8() requires Turing or Ampere GPUs.
14+
Python >=3.8. Linux distribution (Ubuntu, MacOS, etc.) + CUDA > 10.0.
15+
16+
(Deprecated: CUDA 10.0 is deprecated and only CUDA >= 11.0) will be supported with release 0.39.0)
1517

1618
**Installation**:
1719

1820
``pip install bitsandbytes``
1921

20-
In some cases it can happen that you need to compile from source. In that case, you can install CUDA with the install script in the repository. No sudo is required for this install.
22+
In some cases it can happen that you need to compile from source. If this happens please consider submitting a bug report with `python -m bitsandbytes` information. What now follows is some short instructions which might work out of the box if `nvcc` is installed. If these do not work see further below.
2123

24+
Compilation quickstart:
2225
```bash
23-
wget https://raw.githubusercontent.com/TimDettmers/bitsandbytes/main/cuda_install.sh
24-
# Syntax cuda_install CUDA_VERSION INSTALL_PREFIX EXPORT_TO_BASH
25-
# CUDA_VERSION in {110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121}
26-
# EXPORT_TO_BASH in {0, 1} with 0=False and 1=True
27-
28-
# For example, the following installs CUDA 11.8 to ~/local/cuda-11.8 and exports the path to your .bashrc
29-
bash cuda install 118 ~/local 1
26+
git clone https://github.com/timdettmers/bitsandbytes.git
27+
cd bitsandbytes
28+
29+
# CUDA_VERSIONS in {110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 120}
30+
# make argument in {cuda110, cuda11x, cuda12x}
31+
# if you do not know what CUDA you have, try looking at the output of: python -m bitsandbytes
32+
CUDA_VERSION=117 make cuda11x
33+
python setup.py install
3034
```
3135

32-
To use a specific CUDA version just for a single compile run, you can set the variable `CUDA_HOME`, for example the following command compiles `libbitsandbytes_cuda117.so` using compiler flags for cuda11x with the cuda version at `~/local/cuda-11.7`:
36+
**Using Int8 inference with HuggingFace Transformers**
3337

34-
``CUDA_HOME=~/local/cuda-11.7 CUDA_VERSION=117 make cuda11x``
38+
```python
39+
from transformers import AutoModelForCausalLM
40+
model = AutoModelForCausalLM.from_pretrained(
41+
'decapoda-research/llama-7b-hf,
42+
device_map='auto',
43+
load_in_8bit=True,
44+
max_memory=f'{int(torch.cuda.mem_get_info()[0]/1024**3)-2}GB')
45+
```
46+
47+
A more detailed example, can be found in [examples/int8_inference_huggingface.py](examples/int8_inference_huggingface.py).
3548

3649
**Using 8-bit optimizer**:
3750
1. Comment out optimizer: ``#torch.optim.Adam(....)``
@@ -130,8 +143,23 @@ For upcoming features and changes and full history see [Patch Notes](CHANGELOG.m
130143
2. __fatbinwrap_.. [Solution](errors_and_solutions.md#fatbinwrap_)
131144

132145
## Compile from source
146+
To compile from source, you need an installation of CUDA. If `nvcc` is not installed, you can install the CUDA Toolkit with nvcc through the following commands.
147+
148+
```bash
149+
wget https://raw.githubusercontent.com/TimDettmers/bitsandbytes/main/cuda_install.sh
150+
# Syntax cuda_install CUDA_VERSION INSTALL_PREFIX EXPORT_TO_BASH
151+
# CUDA_VERSION in {110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121}
152+
# EXPORT_TO_BASH in {0, 1} with 0=False and 1=True
153+
154+
# For example, the following installs CUDA 11.8 to ~/local/cuda-11.8 and exports the path to your .bashrc
155+
bash cuda install 118 ~/local 1
156+
```
157+
158+
To use a specific CUDA version just for a single compile run, you can set the variable `CUDA_HOME`, for example the following command compiles `libbitsandbytes_cuda117.so` using compiler flags for cuda11x with the cuda version at `~/local/cuda-11.7`:
159+
160+
``CUDA_HOME=~/local/cuda-11.7 CUDA_VERSION=117 make cuda11x``
133161

134-
To compile from source, please follow the [compile_from_source.md](compile_from_source.md) instructions.
162+
For more detailed instruction, please follow the [compile_from_source.md](compile_from_source.md) instructions.
135163

136164
## License
137165

bitsandbytes/__main__.py

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,82 @@
11
import os
22
import sys
3+
import shlex
4+
import subprocess
5+
36
from warnings import warn
7+
from typing import Tuple
8+
from os.path import isdir
49

510
import torch
611

712
HEADER_WIDTH = 60
813

14+
def execute_and_return(command_string: str) -> Tuple[str, str]:
15+
def _decode(subprocess_err_out_tuple):
16+
return tuple(
17+
to_decode.decode("UTF-8").strip()
18+
for to_decode in subprocess_err_out_tuple
19+
)
20+
21+
def execute_and_return_decoded_std_streams(command_string):
22+
return _decode(
23+
subprocess.Popen(
24+
shlex.split(command_string),
25+
stdout=subprocess.PIPE,
26+
stderr=subprocess.PIPE,
27+
).communicate()
28+
)
29+
30+
std_out, std_err = execute_and_return_decoded_std_streams(command_string)
31+
return std_out, std_err
32+
33+
def find_file_recursive(folder, filename):
34+
cmd = f'find {folder} -name {filename}'
35+
out, err = execute_and_return(cmd)
36+
if len(err) > 0:
37+
raise RuntimeError('Something when wrong when trying to find file. Maybe you do not have a linux system?')
38+
39+
return out
40+
41+
42+
def generate_bug_report_information():
43+
print_header("")
44+
print_header("BUG REPORT INFORMATION")
45+
print_header("")
46+
print('')
47+
48+
if 'CONDA_PREFIX' in os.environ:
49+
paths = find_file_recursive(os.environ['CONDA_PREFIX'], '*cuda*so')
50+
print_header("ANACONDA CUDA PATHS")
51+
print(paths)
52+
print('')
53+
if isdir('/usr/local/'):
54+
paths = find_file_recursive('/usr/local', '*cuda*so')
55+
print_header("/usr/local CUDA PATHS")
56+
print(paths)
57+
print('')
58+
59+
if isdir(os.getcwd()):
60+
paths = find_file_recursive(os.getcwd(), '*cuda*so')
61+
print_header("WORKING DIRECTORY CUDA PATHS")
62+
print(paths)
63+
print('')
64+
65+
print_header("LD_LIBRARY CUDA PATHS")
66+
lib_path = os.environ['LD_LIBRARY_PATH'].strip()
67+
for path in set(lib_path.split(':')):
68+
try:
69+
if isdir(path):
70+
print_header(f"{path} CUDA PATHS")
71+
paths = find_file_recursive(path, '*cuda*so')
72+
print(paths)
73+
except:
74+
print(f'Could not read LD_LIBRARY_PATH: {path}')
75+
print('')
76+
77+
78+
79+
980

1081
def print_header(
1182
txt: str, width: int = HEADER_WIDTH, filler: str = "+"
@@ -21,25 +92,13 @@ def print_debug_info() -> None:
2192
)
2293

2394

24-
print_header("")
25-
print_header("DEBUG INFORMATION")
26-
print_header("")
27-
print()
95+
generate_bug_report_information()
2896

2997

3098
from . import COMPILED_WITH_CUDA, PACKAGE_GITHUB_URL
3199
from .cuda_setup.env_vars import to_be_ignored
32100
from .cuda_setup.main import get_compute_capabilities, get_cuda_lib_handle
33101

34-
print_header("POTENTIALLY LIBRARY-PATH-LIKE ENV VARS")
35-
for k, v in os.environ.items():
36-
if "/" in v and not to_be_ignored(k, v):
37-
print(f"'{k}': '{v}'")
38-
print_header("")
39-
40-
print(
41-
"\nWARNING: Please be sure to sanitize sensible info from any such env vars!\n"
42-
)
43102

44103
print_header("OTHER")
45104
print(f"COMPILED_WITH_CUDA = {COMPILED_WITH_CUDA}")
@@ -55,6 +114,7 @@ def print_debug_info() -> None:
55114
+ CUDA function is callable
56115
"""
57116
)
117+
print("\nWARNING: Please be sure to sanitize sensible info from any such env vars!\n")
58118

59119
try:
60120
from bitsandbytes.optim import Adam
@@ -91,3 +151,4 @@ def print_debug_info() -> None:
91151
print(e)
92152
print_debug_info()
93153
sys.exit(1)
154+

bitsandbytes/cuda_setup/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ def evaluate_cuda_setup():
373373
if 'BITSANDBYTES_NOWELCOME' not in os.environ or str(os.environ['BITSANDBYTES_NOWELCOME']) == '0':
374374
print('')
375375
print('='*35 + 'BUG REPORT' + '='*35)
376-
print('Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues')
376+
print(('Welcome to bitsandbytes. For bug reports, please run\n\npython -m bitsandbytes\n\n'),
377+
('and submit this information together with your error trace to: https://github.com/TimDettmers/bitsandbytes/issues'))
377378
print('='*80)
378379
if not torch.cuda.is_available(): return 'libbitsandbytes_cpu.so', None, None, None, None
379380

compile_from_source.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
# Compiling from source
22

33
Basic steps.
4-
1. `make [target]` where `[target]` is among `cuda92, cuda10x, cuda110, cuda11x, cuda12x, cpuonly`
5-
2. `CUDA_VERSION=XXX python setup.py install`
4+
1. `CUDA_VERSION=XXX make [target]` where `[target]` is among `cuda92, cuda10x, cuda110, cuda11x, cuda12x, cpuonly`
5+
2. `python setup.py install`
66

77
To run these steps you will need to have the nvcc compiler installed that comes with a CUDA installation. If you use anaconda (recommended) then you can figure out which version of CUDA you are using with PyTorch via the command `conda list | grep cudatoolkit`. Then you can install the nvcc compiler by downloading and installing the same CUDA version from the [CUDA toolkit archive](https://developer.nvidia.com/cuda-toolkit-archive).
88

9-
For your convenience, there is an installation script in the root directory that installs CUDA 11.1 locally and configures it automatically. After installing you should add the `bin` sub-directory to the `$PATH` variable to make the compiler visible to your system. To do this you can add this to your `.bashrc` by executing these commands:
9+
You can install CUDA locally without sudo by following the following steps:
10+
1011
```bash
11-
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/" >> ~/.bashrc
12-
echo "export PATH=$PATH:/usr/local/cuda/bin/" >> ~/.bashrc
13-
source ~/.bashrc
12+
wget https://raw.githubusercontent.com/TimDettmers/bitsandbytes/main/cuda_install.sh
13+
# Syntax cuda_install CUDA_VERSION INSTALL_PREFIX EXPORT_TO_BASH
14+
# CUDA_VERSION in {110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121}
15+
# EXPORT_TO_BASH in {0, 1} with 0=False and 1=True
16+
17+
# For example, the following installs CUDA 11.7 to ~/local/cuda-11.7 and exports the path to your .bashrc
18+
bash cuda install 117 ~/local 1
1419
```
1520

1621
By default, the Makefile will look at your `CUDA_HOME` environmental variable to find your CUDA version for compiling the library. If this path is not set it is inferred from the path of your `nvcc` compiler.
1722

1823
Either `nvcc` needs to be in path for the `CUDA_HOME` variable needs to be set to the CUDA directory root (e.g. `/usr/local/cuda`) in order for compilation to succeed
1924

25+
If you type `nvcc` and it cannot be found, you might need to add to your path or set the CUDA_HOME variable. You can run `python -m bitsandbytes` to find the path to CUDA. For example if `python -m bitsandbytes` shows you the following:
26+
```
27+
++++++++++++++++++ /usr/local CUDA PATHS +++++++++++++++++++
28+
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudart.so
29+
```
30+
You can set `CUDA_HOME` to `/usr/local/cuda-11.7`. For example, you might be able to compile like this.
31+
32+
``CUDA_HOME=~/local/cuda-11.7 CUDA_VERSION=117 make cuda11x``
33+
34+
2035
If you have problems compiling the library with these instructions from source, please open an issue.

cuda_install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ FILE=$(basename $URL)
7777
if [[ -n "$CUDA_VERSION" ]]; then
7878
echo $URL
7979
echo $FILE
80-
#wget $URL
81-
#bash $FILE --no-drm --no-man-page --override --toolkitpath=$BASE_PATH/$FOLDER/ --toolkit --silent
80+
wget $URL
81+
bash $FILE --no-drm --no-man-page --override --toolkitpath=$BASE_PATH/$FOLDER/ --toolkit --silent
8282
if [ "$EXPORT_BASHRC" -eq "1" ]; then
8383
echo "export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:$BASE_PATH/$FOLDER/lib64" >> ~/.bashrc
8484
echo "export PATH=\$PATH:$BASE_PATH/$FOLDER/bin" >> ~/.bashrc

errors_and_solutions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# No kernel image available
22

3-
This problem arises with the cuda version loaded by bitsandbytes is not supported by your GPU, or if you pytorch CUDA version mismatches. So solve this problem you need to debug ``$LD_LIBRARY_PATH``, ``$CUDA_HOME``, ``$PATH``. You can print these via ``echo $PATH``. You should look for multiple paths to different CUDA versions. This can include versions in your anaconda path, for example ``$HOME/anaconda3/lib``. You can check those versions via ``ls -l $HOME/anaconda3/lib/*cuda*`` or equivalent paths. Look at the CUDA versions of files in these paths. Does it match with ``nvidia-smi``?
3+
This problem arises with the cuda version loaded by bitsandbytes is not supported by your GPU, or if you pytorch CUDA version mismatches. To solve this problem you need to debug ``$LD_LIBRARY_PATH``, ``$CUDA_HOME``, ``$PATH``. You can print these via ``echo $PATH``. You should look for multiple paths to different CUDA versions. This can include versions in your anaconda path, for example ``$HOME/anaconda3/lib``. You can check those versions via ``ls -l $HOME/anaconda3/lib/*cuda*`` or equivalent paths. Look at the CUDA versions of files in these paths. Does it match with ``nvidia-smi``?
44

55
If you are feeling lucky, you can also try to compile the library from source. This can be still problematic if your PATH variables have multiple cuda versions. As such, it is recommended to figure out path conflicts before you proceed with compilation.
66

0 commit comments

Comments
 (0)