Go to file
mrq c88f97a9c8 drop support for gfx903 because depending on hipblaslt gums up too many things 2023-10-12 19:16:14 +07:00
benchmarking/switchback Refactored simulated fp8 modules into research.nn. 2023-04-12 11:24:44 +07:00
bitsandbytes Revert get_cuda_version ROCM version change 2023-08-08 21:31:20 +07:00
csrc drop support for gfx903 because depending on hipblaslt gums up too many things 2023-10-12 19:16:14 +07:00
examples Added missing example folder. 2023-04-12 11:27:31 +07:00
include Algo-Direct2.h: fix hipcc issue 2023-08-05 02:12:14 +07:00
tests Fixed two bugs in dynamic data type creation. 2023-08-03 19:47:15 +07:00
.buckconfig Initial commit 2021-10-05 19:16:20 +07:00
.gitignore Initial commit 2021-10-05 19:16:20 +07:00
CHANGELOG.md Version 0.41.1. 2023-08-03 20:01:10 +07:00
CODE_OF_CONDUCT.md Initial commit 2021-10-05 19:16:20 +07:00
CONTRIBUTING.md Remove trailing whitespace & ensure newline at EOF 2022-10-27 13:11:29 +07:00
LICENSE Initial commit 2021-10-05 19:16:20 +07:00
Makefile Use workaround for ROCm wave32 recognition 2023-08-08 18:50:26 +07:00
NOTICE.md Initial commit 2021-10-05 19:16:20 +07:00
README.md Update README rocm quickstart 2023-08-08 21:28:37 +07:00
check_bnb_install.py Fixed issue where the CUDA SETUP was not printed. 2023-01-04 03:50:53 +07:00
compile_from_source.md Release 0.39.1. 2023-06-19 19:40:41 +07:00
cuda_install.sh Fixed typo in cuda_install.sh. 2023-07-10 06:40:19 +07:00
deploy.sh Added H100 support for CUDA 11.8 precompiled binaries. 2023-07-13 21:16:23 +07:00
environment.yml allow hiding of the welcome message 2022-10-27 16:04:49 +07:00
errors_and_solutions.md Added more detailed cuda setup debug and debugging instructions. 2023-04-11 13:47:10 +07:00
how_to_use_nonpytorch_cuda.md Changed CUDA_INSTALL variable to BNB_CUDA_INSTALL. 2023-07-14 18:16:45 +07:00
howto_config_override.md Remove trailing whitespace & ensure newline at EOF 2022-10-27 13:11:29 +07:00
pyproject.toml Initial commit 2021-10-05 19:16:20 +07:00
requirements.txt Added missing scipy requirement. Addressing #544 2023-07-13 21:25:07 +07:00
setup.py Version 0.41.1. 2023-08-03 20:01:10 +07:00

README.md

bitsandbytes

The bitsandbytes is a lightweight wrapper around CUDA custom functions, in particular 8-bit optimizers, matrix multiplication (LLM.int8()), and quantization functions.

Resources:

Quickstart Rocm

Works well with these docker images:

For installation then do:

git clone https://github.com/arlo-phoenix/bitsandbytes-rocm-5.6.git bitsandbytes
cd bitsandbytes
make hip ROCM_TARGET=gfx...
pip install .

see https://www.llvm.org/docs/AMDGPUUsage.html#processors for finding ROCM_TARGET (e.g. gfx1030 for 6800XT,6900XT)

Info about this port / Credits

Instead of just using the hipified output, I went through all the different variables/functions and used defines to make the Cuda code use the HIP equivalents. That idea is taken from the llama.cpp rocblas port.

The python/makefile/compatibility changes are just copied from this clean older rocm port by @agrocylo. Thanks for that, was easy to look through.

I very much recommend using docker if you want to run this. As this just redefines some Cuda variables/functions, I also had to include all the needed dependency headers. Including hipBLASlt, which is still in it's infancy and not supported by most architectures, the header works though. That's also why some of the newer functions won't work and will just log that they are not functioning. The optimizers like AdamW8bit should work though and this fork will be a lot easier to keep up to date when the Cuda source files change.

TL;DR

Requirements Python >=3.8. Linux distribution (Ubuntu, MacOS, etc.) + CUDA > 10.0.

(Deprecated: CUDA 10.0 is deprecated and only CUDA >= 11.0) will be supported with release 0.39.0)

Installation:

pip install bitsandbytes

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.

Compilation quickstart:

git clone https://github.com/timdettmers/bitsandbytes.git
cd bitsandbytes

# CUDA_VERSIONS in {110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 120}
# make argument in {cuda110, cuda11x, cuda12x}
# if you do not know what CUDA you have, try looking at the output of: python -m bitsandbytes
CUDA_VERSION=117 make cuda11x
python setup.py install

Using Int8 inference with HuggingFace Transformers

from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
  'decapoda-research/llama-7b-hf,
  device_map='auto',
  load_in_8bit=True,
  max_memory=f'{int(torch.cuda.mem_get_info()[0]/1024**3)-2}GB')

A more detailed example, can be found in examples/int8_inference_huggingface.py.

Using 8-bit optimizer:

  1. Comment out optimizer: #torch.optim.Adam(....)
  2. Add 8-bit optimizer of your choice bnb.optim.Adam8bit(....) (arguments stay the same)
  3. Replace embedding layer if necessary: torch.nn.Embedding(..) -> bnb.nn.Embedding(..)

Using 8-bit Inference:

  1. Comment out torch.nn.Linear: #linear = torch.nn.Linear(...)
  2. Add bnb 8-bit linear light module: linear = bnb.nn.Linear8bitLt(...) (base arguments stay the same)
  3. There are two modes:
    • Mixed 8-bit training with 16-bit main weights. Pass the argument has_fp16_weights=True (default)
    • Int8 inference. Pass the argument has_fp16_weights=False
  4. To use the full LLM.int8() method, use the threshold=k argument. We recommend k=6.0.
# LLM.int8()
linear = bnb.nn.Linear8bitLt(dim1, dim2, bias=True, has_fp16_weights=False, threshold=6.0)
# inputs need to be fp16
out = linear(x.to(torch.float16))

Features

  • 8-bit Matrix multiplication with mixed precision decomposition
  • LLM.int8() inference
  • 8-bit Optimizers: Adam, AdamW, RMSProp, LARS, LAMB, Lion (saves 75% memory)
  • Stable Embedding Layer: Improved stability through better initialization, and normalization
  • 8-bit quantization: Quantile, Linear, and Dynamic quantization
  • Fast quantile estimation: Up to 100x faster than other algorithms

Requirements & Installation

Requirements: anaconda, cudatoolkit, pytorch

Hardware requirements:

  • LLM.int8(): NVIDIA Turing (RTX 20xx; T4) or Ampere GPU (RTX 30xx; A4-A100); (a GPU from 2018 or older).
  • 8-bit optimizers and quantization: NVIDIA Kepler GPU or newer (>=GTX 78X).

Supported CUDA versions: 10.2 - 12.0

The bitsandbytes library is currently only supported on Linux distributions. Windows is not supported at the moment.

The requirements can best be fulfilled by installing pytorch via anaconda. You can install PyTorch by following the "Get Started" instructions on the official website.

To install run:

pip install bitsandbytes

Using bitsandbytes

Using Int8 Matrix Multiplication

For straight Int8 matrix multiplication with mixed precision decomposition you can use bnb.matmul(...). To enable mixed precision decomposition, use the threshold parameter:

bnb.matmul(..., threshold=6.0)

For instructions how to use LLM.int8() inference layers in your own code, see the TL;DR above or for extended instruction see this blog post.

Using the 8-bit Optimizers

With bitsandbytes 8-bit optimizers can be used by changing a single line of code in your codebase. For NLP models we recommend also to use the StableEmbedding layers (see below) which improves results and helps with stable 8-bit optimization. To get started with 8-bit optimizers, it is sufficient to replace your old optimizer with the 8-bit optimizer in the following way:

import bitsandbytes as bnb

# adam = torch.optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.995)) # comment out old optimizer
adam = bnb.optim.Adam8bit(model.parameters(), lr=0.001, betas=(0.9, 0.995)) # add bnb optimizer
adam = bnb.optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.995), optim_bits=8) # equivalent


torch.nn.Embedding(...) ->  bnb.nn.StableEmbedding(...) # recommended for NLP models

Note that by default all parameter tensors with less than 4096 elements are kept at 32-bit even if you initialize those parameters with 8-bit optimizers. This is done since such small tensors do not save much memory and often contain highly variable parameters (biases) or parameters that require high precision (batch norm, layer norm). You can change this behavior like so:

# parameter tensors with less than 16384 values are optimized in 32-bit
# it is recommended to use multiplies of 4096
adam = bnb.optim.Adam8bit(model.parameters(), min_8bit_size=16384)

Change Bits and other Hyperparameters for Individual Parameters

If you want to optimize some unstable parameters with 32-bit Adam and others with 8-bit Adam, you can use the GlobalOptimManager. With this, we can also configure specific hyperparameters for particular layers, such as embedding layers. To do that, we need two things: (1) register the parameter while they are still on the CPU, (2) override the config with the new desired hyperparameters (anytime, anywhere). See our guide for more details

Fairseq Users

To use the Stable Embedding Layer, override the respective build_embedding(...) function of your model. Make sure to also use the --no-scale-embedding flag to disable scaling of the word embedding layer (nor replaced with layer norm). You can use the optimizers by replacing the optimizer in the respective file (adam.py etc.).

Release and Feature History

For upcoming features and changes and full history see Patch Notes.

Errors

  1. RuntimeError: CUDA error: no kernel image is available for execution on the device. Solution
  2. _fatbinwrap.. Solution

Compile from source

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.

wget https://raw.githubusercontent.com/TimDettmers/bitsandbytes/main/cuda_install.sh
# Syntax cuda_install CUDA_VERSION INSTALL_PREFIX EXPORT_TO_BASH
#   CUDA_VERSION in {110, 111, 112, 113, 114, 115, 116, 117, 118, 120, 121}
#   EXPORT_TO_BASH in {0, 1} with 0=False and 1=True 

# For example, the following installs CUDA 11.8 to ~/local/cuda-11.8 and exports the path to your .bashrc
bash cuda install 118 ~/local 1 

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:

CUDA_HOME=~/local/cuda-11.7 CUDA_VERSION=117 make cuda11x

For more detailed instruction, please follow the compile_from_source.md instructions.

License

The majority of bitsandbytes is licensed under MIT, however portions of the project are available under separate license terms: Pytorch is licensed under the BSD license.

We thank Fabio Cannizzo for his work on FastBinarySearch which we use for CPU quantization.

How to cite us

If you found this library and found LLM.int8() useful, please consider citing our work:

@article{dettmers2022llmint8,
  title={LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale},
  author={Dettmers, Tim and Lewis, Mike and Belkada, Younes and Zettlemoyer, Luke},
  journal={arXiv preprint arXiv:2208.07339},
  year={2022}
}

For 8-bit optimizers or quantization routines, please consider citing the following work:

@article{dettmers2022optimizers,
  title={8-bit Optimizers via Block-wise Quantization},
  author={Dettmers, Tim and Lewis, Mike and Shleifer, Sam and Zettlemoyer, Luke},
  journal={9th International Conference on Learning Representations, ICLR},
  year={2022}
}