# bitsandbytes
The bitsandbytes is a lightweight wrapper around CUDA custom functions, in particular 8-bit optimizers, matrix multiplication (LLM.int8()), and quantization functions.
Resources:
- [8-bit Optimizer Paper](https://arxiv.org/abs/2110.02861) -- [Video](https://www.youtube.com/watch?v=IxrlHAJtqKE) -- [Docs](https://bitsandbytes.readthedocs.io/en/latest/)
- [LLM.int8() Paper](https://arxiv.org/abs/2208.07339) -- [LLM.int8() Software Blog Post](https://huggingface.co/blog/hf-bitsandbytes-integration) -- [LLM.int8() Emergent Features Blog Post](https://timdettmers.com/2022/08/17/llm-int8-and-emergent-features/)
## TL;DR
**Requirements**
Python >=3.8. Linux distribution (Ubuntu, MacOS, etc.) + CUDA > 10.0. LLM.int8() requires Turing or Ampere GPUs.
**Installation**:
``pip install bitsandbytes``
**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``.
```python
# 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 (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"](https://pytorch.org/get-started/locally/) 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:
```python
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](https://github.com/huggingface/transformers).
### 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:
```python
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](howto_config_override.md) 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](CHANGELOG.md).
## Errors
1. RuntimeError: CUDA error: no kernel image is available for execution on the device. [Solution](errors_and_solutions.md#No-kernel-image-available)
2. __fatbinwrap_.. [Solution](errors_and_solutions.md#fatbinwrap_)
## Compile from source
To compile from source, please follow the [compile_from_source.md](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](https://github.com/fabiocannizzo/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:
```bibtex
@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:
```bibtex
@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}
}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Keith-Hon/bitsandbytes-windows",
"name": "bitsandbytes-windows",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "gpu optimizers optimization 8-bit quantization compression",
"author": "Tim Dettmers",
"author_email": "dettmers@cs.washington.edu",
"download_url": "https://files.pythonhosted.org/packages/b1/d5/83037ca55b449de228a03ca4147642005b34f1c1894d14d7de27a0e50b20/bitsandbytes-windows-0.37.5.tar.gz",
"platform": null,
"description": "# bitsandbytes\r\n\r\nThe bitsandbytes is a lightweight wrapper around CUDA custom functions, in particular 8-bit optimizers, matrix multiplication (LLM.int8()), and quantization functions.\r\n\r\n\r\n\r\nResources:\r\n- [8-bit Optimizer Paper](https://arxiv.org/abs/2110.02861) -- [Video](https://www.youtube.com/watch?v=IxrlHAJtqKE) -- [Docs](https://bitsandbytes.readthedocs.io/en/latest/)\r\n\r\n- [LLM.int8() Paper](https://arxiv.org/abs/2208.07339) -- [LLM.int8() Software Blog Post](https://huggingface.co/blog/hf-bitsandbytes-integration) -- [LLM.int8() Emergent Features Blog Post](https://timdettmers.com/2022/08/17/llm-int8-and-emergent-features/)\r\n\r\n## TL;DR\r\n**Requirements**\r\nPython >=3.8. Linux distribution (Ubuntu, MacOS, etc.) + CUDA > 10.0. LLM.int8() requires Turing or Ampere GPUs.\r\n\r\n**Installation**:\r\n``pip install bitsandbytes``\r\n\r\n**Using 8-bit optimizer**:\r\n1. Comment out optimizer: ``#torch.optim.Adam(....)``\r\n2. Add 8-bit optimizer of your choice ``bnb.optim.Adam8bit(....)`` (arguments stay the same)\r\n3. Replace embedding layer if necessary: ``torch.nn.Embedding(..) -> bnb.nn.Embedding(..)``\r\n\r\n\r\n**Using 8-bit Inference**:\r\n1. Comment out torch.nn.Linear: ``#linear = torch.nn.Linear(...)``\r\n2. Add bnb 8-bit linear light module: ``linear = bnb.nn.Linear8bitLt(...)`` (base arguments stay the same)\r\n3. There are two modes:\r\n - Mixed 8-bit training with 16-bit main weights. Pass the argument ``has_fp16_weights=True`` (default)\r\n - Int8 inference. Pass the argument ``has_fp16_weights=False``\r\n4. To use the full LLM.int8() method, use the ``threshold=k`` argument. We recommend ``k=6.0``.\r\n```python\r\n# LLM.int8()\r\nlinear = bnb.nn.Linear8bitLt(dim1, dim2, bias=True, has_fp16_weights=False, threshold=6.0)\r\n# inputs need to be fp16\r\nout = linear(x.to(torch.float16))\r\n```\r\n\r\n\r\n## Features\r\n- 8-bit Matrix multiplication with mixed precision decomposition\r\n- LLM.int8() inference\r\n- 8-bit Optimizers: Adam, AdamW, RMSProp, LARS, LAMB (saves 75% memory)\r\n- Stable Embedding Layer: Improved stability through better initialization, and normalization\r\n- 8-bit quantization: Quantile, Linear, and Dynamic quantization\r\n- Fast quantile estimation: Up to 100x faster than other algorithms\r\n\r\n## Requirements & Installation\r\n\r\nRequirements: anaconda, cudatoolkit, pytorch\r\n\r\nHardware requirements:\r\n - LLM.int8(): NVIDIA Turing (RTX 20xx; T4) or Ampere GPU (RTX 30xx; A4-A100); (a GPU from 2018 or older).\r\n - 8-bit optimizers and quantization: NVIDIA Kepler GPU or newer (>=GTX 78X).\r\n\r\nSupported CUDA versions: 10.2 - 12.0\r\n\r\nThe bitsandbytes library is currently only supported on Linux distributions. Windows is not supported at the moment.\r\n\r\nThe requirements can best be fulfilled by installing pytorch via anaconda. You can install PyTorch by following the [\"Get Started\"](https://pytorch.org/get-started/locally/) instructions on the official website.\r\n\r\nTo install run:\r\n\r\n``pip install bitsandbytes``\r\n\r\n## Using bitsandbytes\r\n\r\n### Using Int8 Matrix Multiplication\r\n\r\nFor straight Int8 matrix multiplication with mixed precision decomposition you can use ``bnb.matmul(...)``. To enable mixed precision decomposition, use the threshold parameter:\r\n```python\r\nbnb.matmul(..., threshold=6.0)\r\n```\r\n\r\nFor 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](https://github.com/huggingface/transformers).\r\n\r\n### Using the 8-bit Optimizers\r\n\r\nWith 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:\r\n```python\r\nimport bitsandbytes as bnb\r\n\r\n# adam = torch.optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.995)) # comment out old optimizer\r\nadam = bnb.optim.Adam8bit(model.parameters(), lr=0.001, betas=(0.9, 0.995)) # add bnb optimizer\r\nadam = bnb.optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.995), optim_bits=8) # equivalent\r\n\r\n\r\ntorch.nn.Embedding(...) -> bnb.nn.StableEmbedding(...) # recommended for NLP models\r\n```\r\n\r\nNote 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:\r\n```\r\n# parameter tensors with less than 16384 values are optimized in 32-bit\r\n# it is recommended to use multiplies of 4096\r\nadam = bnb.optim.Adam8bit(model.parameters(), min_8bit_size=16384)\r\n```\r\n\r\n### Change Bits and other Hyperparameters for Individual Parameters\r\n\r\nIf 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](howto_config_override.md) for more details\r\n\r\n### Fairseq Users\r\n\r\nTo 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.).\r\n\r\n## Release and Feature History\r\n\r\nFor upcoming features and changes and full history see [Patch Notes](CHANGELOG.md).\r\n\r\n## Errors\r\n\r\n1. RuntimeError: CUDA error: no kernel image is available for execution on the device. [Solution](errors_and_solutions.md#No-kernel-image-available)\r\n2. __fatbinwrap_.. [Solution](errors_and_solutions.md#fatbinwrap_)\r\n\r\n## Compile from source\r\n\r\nTo compile from source, please follow the [compile_from_source.md](compile_from_source.md) instructions.\r\n\r\n## License\r\n\r\nThe 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.\r\n\r\nWe thank Fabio Cannizzo for his work on [FastBinarySearch](https://github.com/fabiocannizzo/FastBinarySearch) which we use for CPU quantization.\r\n\r\n## How to cite us\r\nIf you found this library and found LLM.int8() useful, please consider citing our work:\r\n\r\n```bibtex\r\n@article{dettmers2022llmint8,\r\n title={LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale},\r\n author={Dettmers, Tim and Lewis, Mike and Belkada, Younes and Zettlemoyer, Luke},\r\n journal={arXiv preprint arXiv:2208.07339},\r\n year={2022}\r\n}\r\n```\r\n\r\nFor 8-bit optimizers or quantization routines, please consider citing the following work:\r\n\r\n```bibtex\r\n@article{dettmers2022optimizers,\r\n title={8-bit Optimizers via Block-wise Quantization},\r\n author={Dettmers, Tim and Lewis, Mike and Shleifer, Sam and Zettlemoyer, Luke},\r\n journal={9th International Conference on Learning Representations, ICLR},\r\n year={2022}\r\n}\r\n```\r\n\r\n\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "8-bit optimizers and matrix multiplication routines.",
"version": "0.37.5",
"split_keywords": [
"gpu",
"optimizers",
"optimization",
"8-bit",
"quantization",
"compression"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3605b3e0f551d30be40b9843d96eaf4ad53794fcb2bdee6c1052a38616a187e2",
"md5": "5b2d2584e693e98da51b73a874e85f0f",
"sha256": "97547004651610a7f2d53ba36b88021f972105a1b4a46bd3cb135f43b5f5b058"
},
"downloads": -1,
"filename": "bitsandbytes_windows-0.37.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5b2d2584e693e98da51b73a874e85f0f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 1976931,
"upload_time": "2023-03-31T06:52:02",
"upload_time_iso_8601": "2023-03-31T06:52:02.031860Z",
"url": "https://files.pythonhosted.org/packages/36/05/b3e0f551d30be40b9843d96eaf4ad53794fcb2bdee6c1052a38616a187e2/bitsandbytes_windows-0.37.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b1d583037ca55b449de228a03ca4147642005b34f1c1894d14d7de27a0e50b20",
"md5": "aaa139043928bdf0df01ccfaf2ff13b9",
"sha256": "753643d525fd9bf9b22fffc3b872e2ef4f92b3c21db55fdeb85f4cf2e90e561f"
},
"downloads": -1,
"filename": "bitsandbytes-windows-0.37.5.tar.gz",
"has_sig": false,
"md5_digest": "aaa139043928bdf0df01ccfaf2ff13b9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 1948391,
"upload_time": "2023-03-31T06:52:04",
"upload_time_iso_8601": "2023-03-31T06:52:04.050273Z",
"url": "https://files.pythonhosted.org/packages/b1/d5/83037ca55b449de228a03ca4147642005b34f1c1894d14d7de27a0e50b20/bitsandbytes-windows-0.37.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-31 06:52:04",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "Keith-Hon",
"github_project": "bitsandbytes-windows",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "bitsandbytes-windows"
}